17 ! ! |
17 ! ! |
18 |
18 |
19 !SmallIntegerTest methodsFor:'tests-arithmetic'! |
19 !SmallIntegerTest methodsFor:'tests-arithmetic'! |
20 |
20 |
21 testBitShift |
21 testBitShift |
22 #( |
22 #( |
23 (1 1 2) |
23 (1 1 2) |
24 (1 2 4) |
24 (1 2 4) |
25 (1 3 8) |
25 (1 3 8) |
26 (1 7 16r080) |
26 (1 7 16r080) |
27 (1 8 16r100) |
27 (1 8 16r100) |
28 (1 15 16r08000) |
28 (1 15 16r08000) |
29 (1 16 16r10000) |
29 (1 16 16r10000) |
30 (1 17 16r20000) |
30 (1 17 16r20000) |
31 (1 30 16r040000000) |
31 (1 30 16r040000000) |
32 (1 31 16r080000000) |
32 (1 31 16r080000000) |
33 (1 32 16r100000000) |
33 (1 32 16r100000000) |
34 (1 33 16r200000000) |
34 (1 33 16r200000000) |
35 |
35 |
36 (1 62 16r04000000000000000) |
36 (1 62 16r04000000000000000) |
37 (1 63 16r08000000000000000) |
37 (1 63 16r08000000000000000) |
38 (1 64 16r10000000000000000) |
38 (1 64 16r10000000000000000) |
39 (1 65 16r20000000000000000) |
39 (1 65 16r20000000000000000) |
40 |
40 |
41 (1 126 16r040000000000000000000000000000000) |
41 (1 126 16r040000000000000000000000000000000) |
42 (1 127 16r080000000000000000000000000000000) |
42 (1 127 16r080000000000000000000000000000000) |
43 (1 128 16r100000000000000000000000000000000) |
43 (1 128 16r100000000000000000000000000000000) |
44 (1 129 16r200000000000000000000000000000000) |
44 (1 129 16r200000000000000000000000000000000) |
45 |
45 |
46 (16r10 1 16r20) |
46 (16r10 1 16r20) |
47 (16r10 2 16r40) |
47 (16r10 2 16r40) |
48 (16r10 3 16r80) |
48 (16r10 3 16r80) |
49 (16r10 7 16r0800) |
49 (16r10 7 16r0800) |
50 (16r10 8 16r1000) |
50 (16r10 8 16r1000) |
51 (16r10 15 16r080000) |
51 (16r10 15 16r080000) |
52 (16r10 16 16r100000) |
52 (16r10 16 16r100000) |
53 (16r10 17 16r200000) |
53 (16r10 17 16r200000) |
54 (16r10 30 16r0400000000) |
54 (16r10 30 16r0400000000) |
55 (16r10 31 16r0800000000) |
55 (16r10 31 16r0800000000) |
56 (16r10 32 16r1000000000) |
56 (16r10 32 16r1000000000) |
57 (16r10 33 16r2000000000) |
57 (16r10 33 16r2000000000) |
58 |
58 |
59 (16r10 62 16r040000000000000000) |
59 (16r10 62 16r040000000000000000) |
60 (16r10 63 16r080000000000000000) |
60 (16r10 63 16r080000000000000000) |
61 (16r10 64 16r100000000000000000) |
61 (16r10 64 16r100000000000000000) |
62 (16r10 65 16r200000000000000000) |
62 (16r10 65 16r200000000000000000) |
63 |
63 |
64 (16r10 126 16r0400000000000000000000000000000000) |
64 (16r10 126 16r0400000000000000000000000000000000) |
65 (16r10 127 16r0800000000000000000000000000000000) |
65 (16r10 127 16r0800000000000000000000000000000000) |
66 (16r10 128 16r1000000000000000000000000000000000) |
66 (16r10 128 16r1000000000000000000000000000000000) |
67 (16r10 129 16r2000000000000000000000000000000000) |
67 (16r10 129 16r2000000000000000000000000000000000) |
68 ) triplesDo:[:val :cnt :expected | |
68 ) triplesDo:[:val :cnt :expected | |
69 |rslt1 rslt2| |
69 |rslt1 rslt2| |
70 |
70 |
71 rslt1 := val bitShift:cnt. |
71 rslt1 := val bitShift:cnt. |
72 self assert:(rslt1 = expected). |
72 self assert:(rslt1 = expected). |
73 expected class == SmallInteger ifTrue:[ |
73 expected class == SmallInteger ifTrue:[ |
74 self assert:(rslt1 == expected) |
74 self assert:(rslt1 == expected) |
75 ]. |
75 ]. |
76 rslt2 := rslt1 bitShift:cnt negated. |
76 rslt2 := rslt1 bitShift:cnt negated. |
77 self assert:(rslt2 = val). |
77 self assert:(rslt2 = val). |
78 val class == SmallInteger ifTrue:[ |
78 val class == SmallInteger ifTrue:[ |
79 self assert:(rslt2 == val) |
79 self assert:(rslt2 == val) |
80 ]. |
80 ]. |
81 ]. |
81 ]. |
82 ! |
82 ! |
83 |
83 |
84 testBitShiftNegative |
84 testBitShiftNegative |
85 #( |
85 #( |
86 (-1 1 -2) |
86 (-1 1 -2) |
87 (-1 2 -4) |
87 (-1 2 -4) |
88 (-1 3 -8) |
88 (-1 3 -8) |
89 (-1 7 -16r080) |
89 (-1 7 -16r080) |
90 (-1 8 -16r100) |
90 (-1 8 -16r100) |
91 (-1 15 -16r08000) |
91 (-1 15 -16r08000) |
92 (-1 16 -16r10000) |
92 (-1 16 -16r10000) |
93 (-1 17 -16r20000) |
93 (-1 17 -16r20000) |
94 (-1 30 -16r040000000) |
94 (-1 30 -16r040000000) |
95 (-1 31 -16r080000000) |
95 (-1 31 -16r080000000) |
96 (-1 32 -16r100000000) |
96 (-1 32 -16r100000000) |
97 (-1 33 -16r200000000) |
97 (-1 33 -16r200000000) |
98 |
98 |
99 (-1 62 -16r04000000000000000) |
99 (-1 62 -16r04000000000000000) |
100 (-1 63 -16r08000000000000000) |
100 (-1 63 -16r08000000000000000) |
101 (-1 64 -16r10000000000000000) |
101 (-1 64 -16r10000000000000000) |
102 (-1 65 -16r20000000000000000) |
102 (-1 65 -16r20000000000000000) |
103 |
103 |
104 (-1 126 -16r040000000000000000000000000000000) |
104 (-1 126 -16r040000000000000000000000000000000) |
105 (-1 127 -16r080000000000000000000000000000000) |
105 (-1 127 -16r080000000000000000000000000000000) |
106 (-1 128 -16r100000000000000000000000000000000) |
106 (-1 128 -16r100000000000000000000000000000000) |
107 (-1 129 -16r200000000000000000000000000000000) |
107 (-1 129 -16r200000000000000000000000000000000) |
108 |
108 |
109 (-16r10 1 -16r20) |
109 (-16r10 1 -16r20) |
110 (-16r10 2 -16r40) |
110 (-16r10 2 -16r40) |
111 (-16r10 3 -16r80) |
111 (-16r10 3 -16r80) |
112 (-16r10 7 -16r0800) |
112 (-16r10 7 -16r0800) |
113 (-16r10 8 -16r1000) |
113 (-16r10 8 -16r1000) |
114 (-16r10 15 -16r080000) |
114 (-16r10 15 -16r080000) |
115 (-16r10 16 -16r100000) |
115 (-16r10 16 -16r100000) |
116 (-16r10 17 -16r200000) |
116 (-16r10 17 -16r200000) |
117 (-16r10 30 -16r0400000000) |
117 (-16r10 30 -16r0400000000) |
118 (-16r10 31 -16r0800000000) |
118 (-16r10 31 -16r0800000000) |
119 (-16r10 32 -16r1000000000) |
119 (-16r10 32 -16r1000000000) |
120 (-16r10 33 -16r2000000000) |
120 (-16r10 33 -16r2000000000) |
121 |
121 |
122 (-16r10 62 -16r040000000000000000) |
122 (-16r10 62 -16r040000000000000000) |
123 (-16r10 63 -16r080000000000000000) |
123 (-16r10 63 -16r080000000000000000) |
124 (-16r10 64 -16r100000000000000000) |
124 (-16r10 64 -16r100000000000000000) |
125 (-16r10 65 -16r200000000000000000) |
125 (-16r10 65 -16r200000000000000000) |
126 |
126 |
127 (-16r10 126 -16r0400000000000000000000000000000000) |
127 (-16r10 126 -16r0400000000000000000000000000000000) |
128 (-16r10 127 -16r0800000000000000000000000000000000) |
128 (-16r10 127 -16r0800000000000000000000000000000000) |
129 (-16r10 128 -16r1000000000000000000000000000000000) |
129 (-16r10 128 -16r1000000000000000000000000000000000) |
130 (-16r10 129 -16r2000000000000000000000000000000000) |
130 (-16r10 129 -16r2000000000000000000000000000000000) |
131 ) triplesDo:[:val :cnt :expected | |
131 ) triplesDo:[:val :cnt :expected | |
132 |rslt1 rslt2| |
132 |rslt1 rslt2| |
133 |
133 |
134 rslt1 := val bitShift:cnt. |
134 rslt1 := val bitShift:cnt. |
135 self assert:(rslt1 = expected). |
135 self assert:(rslt1 = expected). |
136 expected class == SmallInteger ifTrue:[ |
136 expected class == SmallInteger ifTrue:[ |
137 self assert:(rslt1 == expected) |
137 self assert:(rslt1 == expected) |
138 ]. |
138 ]. |
139 rslt2 := rslt1 bitShift:cnt negated. |
139 rslt2 := rslt1 bitShift:cnt negated. |
140 self assert:(rslt2 = val). |
140 self assert:(rslt2 = val). |
141 val class == SmallInteger ifTrue:[ |
141 val class == SmallInteger ifTrue:[ |
142 self assert:(rslt2 == val) |
142 self assert:(rslt2 == val) |
143 ]. |
143 ]. |
144 ]. |
144 ]. |
145 ! |
145 ! |
146 |
146 |
147 testDivide |
147 testDivide |
148 |zero _1 _2 _3 _4 _m2 _m4| |
148 |zero _1 _2 _3 _4 _m2 _m4| |
149 |
149 |
150 "circumvent compiler error about division by zero" |
150 "circumvent compiler error about division by zero" |
151 zero := 0. |
151 zero := 0. |
152 |
152 |
153 self assert: 2 / 1 = 2. |
153 self assert: 2 / 1 = 2. |
154 self assert: (3 / 2) isFraction. |
154 self assert: (3 / 2) isFraction. |
155 self assert: 4 / 2 = 2. |
155 self assert: 4 / 2 = 2. |
156 self assert: 4 / -2 = -2. |
156 self assert: 4 / -2 = -2. |
157 self assert: -4 / 2 = -2. |
157 self assert: -4 / 2 = -2. |
158 self assert: -4 / -2 = 2. |
158 self assert: -4 / -2 = 2. |
159 self should: [ 1 / zero ] raise: ZeroDivide. |
159 self should: [ 1 / zero ] raise: ZeroDivide. |
160 |
160 |
161 _1 := 1. |
161 _1 := 1. |
162 _2 := 2. |
162 _2 := 2. |
163 _3 := 3. |
163 _3 := 3. |
164 _4 := 4. |
164 _4 := 4. |
165 _m2 := -2. |
165 _m2 := -2. |
166 _m4 := -4. |
166 _m4 := -4. |
167 |
167 |
168 self assert: _2 / _1 = _2. |
168 self assert: _2 / _1 = _2. |
169 self assert: (_3 / _2) isFraction. |
169 self assert: (_3 / _2) isFraction. |
170 self assert: _4 / _2 = 2. |
170 self assert: _4 / _2 = 2. |
171 self assert: _4 / _m2 = -2. |
171 self assert: _4 / _m2 = -2. |
172 self assert: _m4 / _2 = -2. |
172 self assert: _m4 / _2 = -2. |
173 self assert: _m4 / _m2 = 2. |
173 self assert: _m4 / _m2 = 2. |
174 ! |
174 ! |
175 |
175 |
176 testMultiply |
176 testMultiply |
177 |i ii| |
177 |i ii| |
178 |
178 |
179 #( |
179 #( |
180 16r3fff |
180 16r3fff |
181 16r7fff |
181 16r7fff |
182 16rffff |
182 16rffff |
183 16r3fffffff |
183 16r3fffffff |
184 16r7fffffff |
184 16r7fffffff |
185 16rffffffff |
185 16rffffffff |
186 16r20000000 |
186 16r20000000 |
187 16r40000000 |
187 16r40000000 |
188 16r80000000 |
188 16r80000000 |
189 16r100000000 |
189 16r100000000 |
190 16r20000001 |
190 16r20000001 |
191 16r40000001 |
191 16r40000001 |
192 16r80000001 |
192 16r80000001 |
193 16r100000001 |
193 16r100000001 |
194 |
194 |
195 "/ for 64 bit machines: |
195 "/ for 64 bit machines: |
196 16r3fffffffffffffff |
196 16r3fffffffffffffff |
197 16r7fffffffffffffff |
197 16r7fffffffffffffff |
198 16rffffffffffffffff |
198 16rffffffffffffffff |
199 16r2000000000000000 |
199 16r2000000000000000 |
200 16r4000000000000000 |
200 16r4000000000000000 |
201 16r8000000000000000 |
201 16r8000000000000000 |
202 16r10000000000000000 |
202 16r10000000000000000 |
203 16r2000000000000001 |
203 16r2000000000000001 |
204 16r4000000000000001 |
204 16r4000000000000001 |
205 16r8000000000000001 |
205 16r8000000000000001 |
206 16r10000000000000001 |
206 16r10000000000000001 |
207 ) do:[:x | |
207 ) do:[:x | |
208 i := x. |
208 i := x. |
209 ii := i * i. |
209 ii := i * i. |
210 self assert:((ii / i) = i). |
210 self assert:((ii / i) = i). |
211 i class == SmallInteger ifTrue:[ |
211 i class == SmallInteger ifTrue:[ |
212 self assert:((ii / i) == i). |
212 self assert:((ii / i) == i). |
213 ]. |
213 ]. |
214 |
214 |
215 i := x negated. |
215 i := x negated. |
216 ii := i * i. |
216 ii := i * i. |
217 self assert:((ii / i) = i). |
217 self assert:((ii / i) = i). |
218 i class == SmallInteger ifTrue:[ |
218 i class == SmallInteger ifTrue:[ |
219 self assert:((ii / i) == i). |
219 self assert:((ii / i) == i). |
220 ]. |
220 ]. |
221 ]. |
221 ]. |
222 ! |
222 ! |
223 |
223 |
224 testNegation |
224 testNegation |
225 |i iN iNN| |
225 |i iN iNN| |
226 |
226 |
227 #( |
227 #( |
228 16r3fffffff |
228 16r3fffffff |
229 16r7fffffff |
229 16r7fffffff |
230 16rffffffff |
230 16rffffffff |
231 16r20000000 |
231 16r20000000 |
232 16r40000000 |
232 16r40000000 |
233 16r80000000 |
233 16r80000000 |
234 16r100000000 |
234 16r100000000 |
235 16r20000001 |
235 16r20000001 |
236 16r40000001 |
236 16r40000001 |
237 16r80000001 |
237 16r80000001 |
238 16r100000001 |
238 16r100000001 |
239 |
239 |
240 "/ for 64bit machines: |
240 "/ for 64bit machines: |
241 16r3fffffffffffffff |
241 16r3fffffffffffffff |
242 16r7fffffffffffffff |
242 16r7fffffffffffffff |
243 16rffffffffffffffff |
243 16rffffffffffffffff |
244 16r2000000000000000 |
244 16r2000000000000000 |
245 16r4000000000000000 |
245 16r4000000000000000 |
246 16r8000000000000000 |
246 16r8000000000000000 |
247 16r10000000000000000 |
247 16r10000000000000000 |
248 16r2000000000000001 |
248 16r2000000000000001 |
249 16r4000000000000001 |
249 16r4000000000000001 |
250 16r8000000000000001 |
250 16r8000000000000001 |
251 16r10000000000000001 |
251 16r10000000000000001 |
252 ) do:[:x | |
252 ) do:[:x | |
253 i := x. |
253 i := x. |
254 iN := i negated. |
254 iN := i negated. |
255 iNN := iN negated. |
255 iNN := iN negated. |
256 self assert:(i = iNN). |
256 self assert:(i = iNN). |
257 i class == SmallInteger ifTrue:[ |
257 i class == SmallInteger ifTrue:[ |
258 self assert:(i == iNN). |
258 self assert:(i == iNN). |
259 ]. |
259 ]. |
260 ]. |
260 ]. |
261 self assert:(SmallInteger maxVal negated class == SmallInteger). |
261 self assert:(SmallInteger maxVal negated class == SmallInteger). |
262 self assert:(SmallInteger maxVal negated negated == SmallInteger maxVal). |
262 self assert:(SmallInteger maxVal negated negated == SmallInteger maxVal). |
263 |
263 |
264 self assert:(SmallInteger maxVal negated -1 == SmallInteger minVal). |
264 self assert:(SmallInteger maxVal negated -1 == SmallInteger minVal). |
265 self assert:(SmallInteger minVal negated negated class == SmallInteger). |
265 self assert:(SmallInteger minVal negated negated class == SmallInteger). |
266 ! ! |
266 ! ! |
267 |
267 |
268 !SmallIntegerTest methodsFor:'tests-class protocol'! |
268 !SmallIntegerTest methodsFor:'tests-class protocol'! |
269 |
269 |
270 testBasicNew |
270 testBasicNew |
271 self should: [SmallInteger basicNew] raise: TestResult error. |
271 self should: [SmallInteger basicNew] raise: TestResult error. |
272 ! |
272 ! |
273 |
273 |
274 testMaxVal |
274 testMaxVal |
275 "/ the original code did not check for pointer-size; |
275 "/ the original code did not check for pointer-size; |
276 |
276 |
277 Smalltalk isSmalltalkX ifTrue:[ |
277 Smalltalk isSmalltalkX ifTrue:[ |
278 SmallInteger maxBytes == 4 ifTrue:[ |
278 SmallInteger maxBytes == 4 ifTrue:[ |
279 self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
279 self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
280 self should: [SmallInteger maxVal == 16r3FFFFFFF]. |
280 self should: [SmallInteger maxVal == 16r3FFFFFFF]. |
281 ] ifFalse:[ |
281 ] ifFalse:[ |
282 self should: [SmallInteger maxVal = 16r3FFFFFFFFFFFFFFF]. |
282 self should: [SmallInteger maxVal = 16r3FFFFFFFFFFFFFFF]. |
283 self should: [SmallInteger maxVal == 16r3FFFFFFFFFFFFFFF]. |
283 self should: [SmallInteger maxVal == 16r3FFFFFFFFFFFFFFF]. |
284 ]. |
284 ]. |
285 ] ifFalse:[ |
285 ] ifFalse:[ |
286 self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
286 self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
287 ] |
287 ] |
288 ! |
288 ! |
289 |
289 |
290 testMaxValPointerSizeConsistency |
290 testMaxValPointerSizeConsistency |
291 SmallInteger maxBytes == 4 ifTrue:[ |
291 SmallInteger maxBytes == 4 ifTrue:[ |
292 self assert: ((SmallInteger maxBits == 31) or:[SmallInteger maxBits == 32]). |
292 self assert: ((SmallInteger maxBits == 31) or:[SmallInteger maxBits == 32]). |
293 self assert: (ExternalAddress pointerSize == 4). |
293 self assert: (ExternalAddress pointerSize == 4). |
294 ] ifFalse:[ |
294 ] ifFalse:[ |
295 self assert: ((SmallInteger maxBits == 63) or:[SmallInteger maxBits == 64]). |
295 self assert: ((SmallInteger maxBits == 63) or:[SmallInteger maxBits == 64]). |
296 self assert: (ExternalAddress pointerSize == 8). |
296 self assert: (ExternalAddress pointerSize == 8). |
297 ] |
297 ] |
298 ! |
298 ! |
299 |
299 |
300 testMinVal |
300 testMinVal |
301 "/ the original code did not check for pointer-size; |
301 "/ the original code did not check for pointer-size; |
302 |
302 |
303 Smalltalk isSmalltalkX ifTrue:[ |
303 Smalltalk isSmalltalkX ifTrue:[ |
304 SmallInteger maxBytes == 4 ifTrue:[ |
304 SmallInteger maxBytes == 4 ifTrue:[ |
305 self should: [SmallInteger minVal = -16r40000000]. |
305 self should: [SmallInteger minVal = -16r40000000]. |
306 self should: [SmallInteger minVal == -16r40000000]. |
306 self should: [SmallInteger minVal == -16r40000000]. |
307 ] ifFalse:[ |
307 ] ifFalse:[ |
308 self should: [SmallInteger minVal = -16r4000000000000000]. |
308 self should: [SmallInteger minVal = -16r4000000000000000]. |
309 self should: [SmallInteger minVal == -16r4000000000000000]. |
309 self should: [SmallInteger minVal == -16r4000000000000000]. |
310 ]. |
310 ]. |
311 ] ifFalse:[ |
311 ] ifFalse:[ |
312 self should: [SmallInteger minVal = -16r40000000]. |
312 self should: [SmallInteger minVal = -16r40000000]. |
313 ] |
313 ] |
314 ! |
314 ! |
315 |
315 |
316 testNew |
316 testNew |
317 self should: [SmallInteger new] raise: TestResult error. |
317 self should: [SmallInteger new] raise: TestResult error. |
318 ! ! |
318 ! ! |
319 |
319 |
320 !SmallIntegerTest class methodsFor:'documentation'! |
320 !SmallIntegerTest class methodsFor:'documentation'! |
321 |
321 |
322 version |
322 version |