Debian Codegraph @

gcc/testsuite/gm2/pim/no-options/run/pass/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 printf ("i = %d, j = %d MIN(INTEGER) = %d\n",
55 i, j, MIN(INTEGER));
56 printf ("MIN(INTEGER) = %d, -j = %d\n", MIN(INTEGER), -j);
57 IF ((j = MIN(INTEGER)) AND (i < 0)) OR
58 ((i = MIN(INTEGER)) AND (j < 0)) OR
59
60 ((j>0) AND (i > MAX(INTEGER)-j)) OR
61 ((j<0) AND (i < MIN(INTEGER)-j))
62 THEN
63 expecting(overflow, 'signed addition')
64 ELSE
65 expecting(none, 'signed addition')
66 END
67 END sadd ;
68
69
70 (*
71 smallMult -
72 *)
73
74 PROCEDURE smallMult (i, j: INTEGER) ;
75 BEGIN
76 IF i>0
77 THEN
78 IF j>0
79 THEN
80 IF i>maxInt DIV j
81 THEN
82 expecting(overflow, 'signed mult')
83 ELSE
84 expecting(none, 'signed mult')
85 END
86 ELSE
87 IF j<minInt DIV i
88 THEN
89 expecting(overflow, 'signed mult')
90 ELSE
91 expecting(none, 'signed mult')
92 END
93 END
94 ELSE
95 IF j>0
96 THEN
97 IF i<minInt DIV j
98 THEN
99 expecting(overflow, 'signed mult')
100 ELSE
101 expecting(none, 'signed mult')
102 END
103 ELSE
104 IF (i#0) AND (j<maxInt DIV i)
105 THEN
106 expecting(overflow, 'signed mult')
107 ELSE
108 expecting(none, 'signed mult')
109 END
110 END
111 END
112 END smallMult ;
113
114
115 (*
116 smult -
117 *)
118
119 PROCEDURE smult (i, j: INTEGER) ;
120 VAR
121 li, lj, lt: LONGINT ;
122 BEGIN
123 IF SizeOfIntAndLongSame OR (SIZE(LONGINT)=SIZE(INTEGER))
124 THEN
125 smallMult(i, j)
126 ELSE
127 li := i ;
128 lj := j ;
129 lt := li * lj ;
130 IF (lt<VAL(LONGINT, minInt)) OR (lt>VAL(LONGINT, maxInt))
131 THEN
132 expecting(overflow, 'signed multiply')
133 ELSE
134 expecting(none, 'signed multiply')
135 END
136 END
137 END smult ;
138
139
140 (*
141 sneg -
142 *)
143
144 PROCEDURE sneg (i: INTEGER) ;
145 BEGIN
146 IF i=minInt
147 THEN
148 expecting(overflow, 'signed negate')
149 ELSE
150 expecting(none, 'signed negate')
151 END
152 END sneg ;
153
154
155 (*
156 passed -
157 *)
158
159 PROCEDURE expecting (e: error; a: ARRAY OF CHAR) ;
160 VAR
161 s: String ;
162 t: ADDRESS ;
163 BEGIN
164 WITH test[testNo] DO
165 IF expected#e
166 THEN
167 s := InitString(a) ;
168 t := string(s) ;
169 printf("test %s (%d) has failed\n", t, testNo) ;
170 s := KillString(s)
171 ELSIF Verbose
172 THEN
173 s := InitString(a) ;
174 t := string(s) ;
175 printf("test %s (%d) has passed\n", t, testNo) ;
176 s := KillString(s)
177 END
178 END
179 END expecting ;
180
181
182 (*
183 doTest -
184 *)
185
186 PROCEDURE doTest ;
187 BEGIN
188 WITH test[testNo] DO
189 CASE op OF
190
191 iadd : sadd(l, r) |
192 isub : ssub(l, r) |
193 ineg : sneg(l) |
194 imult: smult(l, r) |
195 idiv : |
196 imod : |
197
198 END
199 END
200 END doTest ;
201
202
203 (*
204 doTests -
205 *)
206
207 PROCEDURE doTests ;
208 BEGIN
209 testNo := 0 ;
210 WHILE testNo<=maxTest DO
211 doTest ;
212 INC(testNo)
213 END
214 END doTests ;
215
216
217 CONST
218 maxTest = 25 ;
219 maxInt = MAX(INTEGER) ;
220 minInt = MIN(INTEGER) ;
221
222 TYPE
223 opcode = (iadd, isub, ineg, imult, idiv, imod) ;
224 error = (overflow, underflow, none) ;
225
226 case = RECORD
227 l, r : INTEGER ;
228 op : opcode ;
229 expected: error ;
230 END ;
231 cases = ARRAY [0..maxTest] OF case ;
232
233 VAR
234 test : cases ;
235 testNo: CARDINAL ;
236
237 BEGIN
238 test := cases{{minInt, 0, ineg, overflow},
239 (* 1 *)
240 {maxInt, 0, ineg, none},
241 {minInt DIV 2, minInt DIV 2, iadd, none},
242 {minInt DIV 2, minInt DIV 2-1, iadd, overflow},
243 {maxInt DIV 2, maxInt DIV 2, iadd, none},
244 (* 4 *)
245 {maxInt DIV 2, maxInt DIV 2+1, iadd, none},
246 {maxInt DIV 2+1, maxInt DIV 2+1, iadd, overflow},
247 {maxInt, 1, iadd, overflow},
248 {maxInt, 0, iadd, none},
249 (* 8 *)
250 {minInt, -1, iadd, overflow},
251 {minInt, 0, iadd, none},
252 {-1, maxInt, isub, none},
253 {-2, maxInt, isub, overflow},
254 (* 12 *)
255 {minInt, 1, isub, overflow},
256 {minInt, 0, isub, none},
257 {maxInt, -2, isub, overflow},
258 {maxInt, minInt, isub, overflow},
259 (* 16 *)
260 {0, maxInt, isub, none},
261 {0, minInt, isub, overflow},
262 {-1, maxInt, isub, none},
263 {-2, maxInt, isub, overflow},
264 (* 20 *)
265 {maxInt, 2, imult, overflow},
266 {maxInt DIV 2, 2, imult, none},
267 {minInt DIV 2, 2, imult, none},
268 {minInt DIV 2-1, 2, imult, overflow},
269 (* 24 *)
270 {maxInt DIV 3, 3, imult, none},
271 {minInt DIV 3, 3, imult, none}
272 } ;
273 doTests
274 END integer.