Debian Codegraph @

gcc/testsuite/gm2/pim/fail/integer.mod

raw
1 (* Copyright (C) 2011 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with gm2; see the file COPYING. If not, write to the Free Software
16 Foundation, 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA. *)
18
19 MODULE integer ;
20
21 (*
22 Title : integer
23 Author : Gaius Mulley
24 System : GNU Modula-2
25 Date : Fri May 18 17:05:36 2012
26 Revision : $Version$
27 Description: simple test module to test the principles of catching signed and unsigned
28 integer arithmetic overflow.
29 *)
30
31 FROM SYSTEM IMPORT ADDRESS ;
32 FROM libc IMPORT printf ;
33 FROM DynamicStrings IMPORT String, InitString, string, KillString, InitString ;
34
35 CONST
36 Verbose = TRUE ;
37 SizeOfIntAndLongSame = TRUE ;
38
39
40 PROCEDURE ssub (i, j: INTEGER) ;
41 BEGIN
42 IF ((j>0) AND (i < MIN(INTEGER)+j)) OR
43 ((j<0) AND (i > MAX(INTEGER)+j))
44 THEN
45 expecting(overflow, 'signed subtraction')
46 ELSE
47 expecting(none, 'signed subtraction')
48 END
49 END ssub ;
50
51
52 PROCEDURE sadd (i, j: INTEGER) ;
53 BEGIN
54 IF ((j = MAX(INTEGER) AND (i > 0))) OR
55 ((i = MAX(INTEGER) AND (j > 0))) OR
56
57 ((j>0) AND (i > MAX(INTEGER)-j)) OR
58 ((j<0) AND (i < MIN(INTEGER)-j))
59 THEN
60 expecting(overflow, 'signed addition')
61 ELSE
62 expecting(none, 'signed addition')
63 END
64 END sadd ;
65
66
67 (*
68 smallMult -
69 *)
70
71 PROCEDURE smallMult (i, j: INTEGER) ;
72 BEGIN
73 IF i>0
74 THEN
75 IF j>0
76 THEN
77 IF i>maxInt DIV j
78 THEN
79 expecting(overflow, 'signed mult')
80 ELSE
81 expecting(none, 'signed mult')
82 END
83 ELSE
84 IF j<minInt DIV i
85 THEN
86 expecting(overflow, 'signed mult')
87 ELSE
88 expecting(none, 'signed mult')
89 END
90 END
91 ELSE
92 IF j>0
93 THEN
94 IF i<minInt DIV j
95 THEN
96 expecting(overflow, 'signed mult')
97 ELSE
98 expecting(none, 'signed mult')
99 END
100 ELSE
101 IF (i#0) AND (j<maxInt DIV i)
102 THEN
103 expecting(overflow, 'signed mult')
104 ELSE
105 expecting(none, 'signed mult')
106 END
107 END
108 END
109 END smallMult ;
110
111
112 (*
113 smult -
114 *)
115
116 PROCEDURE smult (i, j: INTEGER) ;
117 VAR
118 li, lj, lt: LONGINT ;
119 BEGIN
120 IF SizeOfIntAndLongSame OR (SIZE(LONGINT)=SIZE(INTEGER))
121 THEN
122 smallMult(i, j)
123 ELSE
124 li := i ;
125 lj := j ;
126 lt := li * lj ;
127 IF (lt<VAL(LONGINT, minInt)) OR (lt>VAL(LONGINT, maxInt))
128 THEN
129 expecting(overflow, 'signed multiply')
130 ELSE
131 expecting(none, 'signed multiply')
132 END
133 END
134 END smult ;
135
136
137 (*
138 sneg -
139 *)
140
141 PROCEDURE sneg (i: INTEGER) ;
142 BEGIN
143 IF i=minInt
144 THEN
145 expecting(overflow, 'signed negate')
146 ELSE
147 expecting(none, 'signed negate')
148 END
149 END sneg ;
150
151
152 (*
153 passed -
154 *)
155
156 PROCEDURE expecting (e: error; a: ARRAY OF CHAR) ;
157 VAR
158 s: String ;
159 t: ADDRESS ;
160 BEGIN
161 WITH test[testNo] DO
162 IF expected#e
163 THEN
164 s := InitString(a) ;
165 t := string(s) ;
166 printf("test %s (%d) has failed\n", t, testNo) ;
167 s := KillString(s)
168 ELSIF Verbose
169 THEN
170 s := InitString(a) ;
171 t := string(s) ;
172 printf("test %s (%d) has passed\n", t, testNo) ;
173 s := KillString(s)
174 END
175 END
176 END expecting ;
177
178
179 (*
180 doTest -
181 *)
182
183 PROCEDURE doTest ;
184 BEGIN
185 WITH test[testNo] DO
186 CASE op OF
187
188 iadd : sadd(l, r) |
189 isub : ssub(l, r) |
190 ineg : sneg(l) |
191 imult: smult(l, r) |
192 idiv : |
193 imod : |
194
195 END
196 END
197 END doTest ;
198
199
200 (*
201 doTests -
202 *)
203
204 PROCEDURE doTests ;
205 BEGIN
206 testNo := 0 ;
207 WHILE testNo<=maxTest DO
208 doTest ;
209 INC(testNo)
210 END
211 END doTests ;
212
213
214 CONST
215 maxTest = 25 ;
216 maxInt = MAX(INTEGER) ;
217 minInt = MIN(INTEGER) ;
218
219 TYPE
220 opcode = (iadd, isub, ineg, imult, idiv, imod) ;
221 error = (overflow, underflow, none) ;
222
223 case = RECORD
224 l, r : INTEGER ;
225 op : opcode ;
226 expected: error ;
227 END ;
228 cases = ARRAY [0..maxTest] OF case ;
229
230 VAR
231 test : cases ;
232 testNo: CARDINAL ;
233
234 BEGIN
235 test := cases{{minInt, 0, ineg, overflow},
236 (* 1 *)
237 {maxInt, 0, ineg, none},
238 {minInt DIV 2, minInt DIV 2, iadd, none},
239 {minInt DIV 2, minInt DIV 2-1, iadd, overflow},
240 {maxInt DIV 2, maxInt DIV 2, iadd, none},
241 (* 4 *)
242 {maxInt DIV 2, maxInt DIV 2+1, iadd, none},
243 {maxInt DIV 2+1, maxInt DIV 2+1, iadd, overflow},
244 {maxInt, 1, iadd, overflow},
245 {maxInt, 0, iadd, none},
246 (* 8 *)
247 {minInt, -1, iadd, overflow},
248 {minInt, 0, iadd, none},
249 {-1, maxInt, isub, none},
250 {-2, maxInt, isub, overflow},
251 (* 12 *)
252 {minInt, 1, isub, overflow},
253 {minInt, 0, isub, none},
254 {maxInt, -2, isub, overflow},
255 {maxInt, minInt, isub, overflow},
256 (* 16 *)
257 {0, maxInt, isub, none},
258 {0, minInt, isub, overflow},
259 {-1, maxInt, isub, none},
260 {-2, maxInt, isub, overflow},
261 (* 20 *)
262 {maxInt, 2, imult, overflow},
263 {maxInt DIV 2, 2, imult, none},
264 {minInt DIV 2, 2, imult, none},
265 {minInt DIV 2-1, 2, imult, overflow},
266 (* 24 *)
267 {maxInt DIV 3, 3, imult, none},
268 {minInt DIV 3, 3, imult, none}
269 } ;
270 doTests
271 END integer.