author | sr |
Wed, 18 Oct 2017 11:28:54 +0200 | |
changeset 1875 | bc702eeb2964 |
parent 1835 | e23ee59eb10c |
child 1882 | b4bb993596b6 |
permissions | -rw-r--r-- |
1835 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1447 | 3 |
"{ Package: 'stx:goodies/regression' }" |
310 | 4 |
|
5 |
"{ NameSpace: RegressionTests }" |
|
6 |
||
7 |
TestCase subclass:#SmallIntegerTest |
|
8 |
instanceVariableNames:'' |
|
9 |
classVariableNames:'' |
|
10 |
poolDictionaries:'' |
|
1074 | 11 |
category:'tests-Regression-Numbers' |
310 | 12 |
! |
13 |
||
14 |
||
619
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
15 |
!SmallIntegerTest class methodsFor:'others'! |
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
16 |
|
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
17 |
version_CVS |
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
18 |
^ '$Header$' |
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
19 |
! ! |
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
20 |
|
310 | 21 |
!SmallIntegerTest methodsFor:'tests-arithmetic'! |
22 |
||
1347 | 23 |
testBitShift |
1447 | 24 |
#( |
25 |
(1 1 2) |
|
26 |
(1 2 4) |
|
27 |
(1 3 8) |
|
28 |
(1 7 16r080) |
|
29 |
(1 8 16r100) |
|
30 |
(1 15 16r08000) |
|
31 |
(1 16 16r10000) |
|
32 |
(1 17 16r20000) |
|
33 |
(1 30 16r040000000) |
|
34 |
(1 31 16r080000000) |
|
35 |
(1 32 16r100000000) |
|
36 |
(1 33 16r200000000) |
|
1347 | 37 |
|
1447 | 38 |
(1 62 16r04000000000000000) |
39 |
(1 63 16r08000000000000000) |
|
40 |
(1 64 16r10000000000000000) |
|
41 |
(1 65 16r20000000000000000) |
|
1347 | 42 |
|
1447 | 43 |
(1 126 16r040000000000000000000000000000000) |
44 |
(1 127 16r080000000000000000000000000000000) |
|
45 |
(1 128 16r100000000000000000000000000000000) |
|
46 |
(1 129 16r200000000000000000000000000000000) |
|
1347 | 47 |
|
1447 | 48 |
(16r10 1 16r20) |
49 |
(16r10 2 16r40) |
|
50 |
(16r10 3 16r80) |
|
51 |
(16r10 7 16r0800) |
|
52 |
(16r10 8 16r1000) |
|
53 |
(16r10 15 16r080000) |
|
54 |
(16r10 16 16r100000) |
|
55 |
(16r10 17 16r200000) |
|
56 |
(16r10 30 16r0400000000) |
|
57 |
(16r10 31 16r0800000000) |
|
58 |
(16r10 32 16r1000000000) |
|
59 |
(16r10 33 16r2000000000) |
|
1347 | 60 |
|
1447 | 61 |
(16r10 62 16r040000000000000000) |
62 |
(16r10 63 16r080000000000000000) |
|
63 |
(16r10 64 16r100000000000000000) |
|
64 |
(16r10 65 16r200000000000000000) |
|
1347 | 65 |
|
1447 | 66 |
(16r10 126 16r0400000000000000000000000000000000) |
67 |
(16r10 127 16r0800000000000000000000000000000000) |
|
68 |
(16r10 128 16r1000000000000000000000000000000000) |
|
69 |
(16r10 129 16r2000000000000000000000000000000000) |
|
1347 | 70 |
) triplesDo:[:val :cnt :expected | |
1447 | 71 |
|rslt1 rslt2| |
72 |
||
73 |
rslt1 := val bitShift:cnt. |
|
74 |
self assert:(rslt1 = expected). |
|
75 |
expected class == SmallInteger ifTrue:[ |
|
76 |
self assert:(rslt1 == expected) |
|
77 |
]. |
|
78 |
rslt2 := rslt1 bitShift:cnt negated. |
|
79 |
self assert:(rslt2 = val). |
|
80 |
val class == SmallInteger ifTrue:[ |
|
81 |
self assert:(rslt2 == val) |
|
82 |
]. |
|
1347 | 83 |
]. |
84 |
! |
|
85 |
||
86 |
testBitShiftNegative |
|
1447 | 87 |
#( |
1709 | 88 |
(-1 1 -2) |
89 |
(-1 2 -4) |
|
90 |
(-1 3 -8) |
|
91 |
(-1 7 16r-080) |
|
92 |
(-1 8 16r-100) |
|
93 |
(-1 15 16r-08000) |
|
94 |
(-1 16 16r-10000) |
|
95 |
(-1 17 16r-20000) |
|
96 |
(-1 30 16r-040000000) |
|
97 |
(-1 31 16r-080000000) |
|
98 |
(-1 32 16r-100000000) |
|
99 |
(-1 33 16r-200000000) |
|
1347 | 100 |
|
1709 | 101 |
(-1 62 16r-04000000000000000) |
102 |
(-1 63 16r-08000000000000000) |
|
103 |
(-1 64 16r-10000000000000000) |
|
104 |
(-1 65 16r-20000000000000000) |
|
1347 | 105 |
|
1709 | 106 |
(-1 126 16r-040000000000000000000000000000000) |
107 |
(-1 127 16r-080000000000000000000000000000000) |
|
108 |
(-1 128 16r-100000000000000000000000000000000) |
|
109 |
(-1 129 16r-200000000000000000000000000000000) |
|
1347 | 110 |
|
1709 | 111 |
(16r-10 1 16r-20) |
112 |
(16r-10 2 16r-40) |
|
113 |
(16r-10 3 16r-80) |
|
114 |
(16r-10 7 16r-0800) |
|
115 |
(16r-10 8 16r-1000) |
|
116 |
(16r-10 15 16r-080000) |
|
117 |
(16r-10 16 16r-100000) |
|
118 |
(16r-10 17 16r-200000) |
|
119 |
(16r-10 30 16r-0400000000) |
|
120 |
(16r-10 31 16r-0800000000) |
|
121 |
(16r-10 32 16r-1000000000) |
|
122 |
(16r-10 33 16r-2000000000) |
|
1347 | 123 |
|
1709 | 124 |
(16r-10 62 16r-040000000000000000) |
125 |
(16r-10 63 16r-080000000000000000) |
|
126 |
(16r-10 64 16r-100000000000000000) |
|
127 |
(16r-10 65 16r-200000000000000000) |
|
1347 | 128 |
|
1709 | 129 |
(16r-10 126 16r-0400000000000000000000000000000000) |
130 |
(16r-10 127 16r-0800000000000000000000000000000000) |
|
131 |
(16r-10 128 16r-1000000000000000000000000000000000) |
|
132 |
(16r-10 129 16r-2000000000000000000000000000000000) |
|
1347 | 133 |
) triplesDo:[:val :cnt :expected | |
1709 | 134 |
|rslt1 rslt2| |
1447 | 135 |
|
1709 | 136 |
rslt1 := val bitShift:cnt. |
137 |
self assert:(rslt1 = expected). |
|
138 |
expected class == SmallInteger ifTrue:[ |
|
139 |
self assert:(rslt1 == expected) |
|
140 |
]. |
|
141 |
rslt2 := rslt1 bitShift:cnt negated. |
|
142 |
self assert:(rslt2 = val). |
|
143 |
val class == SmallInteger ifTrue:[ |
|
144 |
self assert:(rslt2 == val) |
|
145 |
]. |
|
1347 | 146 |
]. |
1709 | 147 |
|
148 |
"Modified (format): / 19-09-2017 / 16:29:36 / stefan" |
|
1347 | 149 |
! |
150 |
||
310 | 151 |
testDivide |
1835 | 152 |
|zero us1 us2 us3 us4 usm2 usm4| |
619
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
153 |
|
1835 | 154 |
"circumvent compiler error about division by zero" |
155 |
zero := 0. |
|
619
f918fa22ad81
Circumvent compiler error about div by zero
Stefan Vogel <sv@exept.de>
parents:
364
diff
changeset
|
156 |
|
1835 | 157 |
Transcript showCR:(2 / 1) printString. |
158 |
self assert: 2 / 1 = 2. |
|
159 |
self assert: (3 / 2) isFraction. |
|
160 |
self assert: 4 / 2 = 2. |
|
161 |
self assert: 4 / -2 = -2. |
|
162 |
self assert: -4 / 2 = -2. |
|
163 |
self assert: -4 / -2 = 2. |
|
164 |
self should: [ 1 / zero ] raise: ZeroDivide. |
|
1223
708a62eac280
class: RegressionTests::SmallIntegerTest
Claus Gittinger <cg@exept.de>
parents:
1074
diff
changeset
|
165 |
|
1835 | 166 |
us1 := 1. |
167 |
us2 := 2. |
|
168 |
us3 := 3. |
|
169 |
us4 := 4. |
|
170 |
usm2 := -2. |
|
171 |
usm4 := -4. |
|
1223
708a62eac280
class: RegressionTests::SmallIntegerTest
Claus Gittinger <cg@exept.de>
parents:
1074
diff
changeset
|
172 |
|
1835 | 173 |
self assert: us2 / us1 = us2. |
174 |
self assert: (us3 / us2) isFraction. |
|
175 |
self assert: us4 / us2 = 2. |
|
176 |
self assert: us4 / usm2 = -2. |
|
177 |
self assert: usm4 / us2 = -2. |
|
178 |
self assert: usm4 / usm2 = 2. |
|
364 | 179 |
! |
180 |
||
181 |
testMultiply |
|
182 |
|i ii| |
|
183 |
||
1447 | 184 |
#( |
185 |
16r3fff |
|
186 |
16r7fff |
|
187 |
16rffff |
|
188 |
16r3fffffff |
|
189 |
16r7fffffff |
|
190 |
16rffffffff |
|
191 |
16r20000000 |
|
192 |
16r40000000 |
|
193 |
16r80000000 |
|
194 |
16r100000000 |
|
195 |
16r20000001 |
|
196 |
16r40000001 |
|
197 |
16r80000001 |
|
198 |
16r100000001 |
|
1223
708a62eac280
class: RegressionTests::SmallIntegerTest
Claus Gittinger <cg@exept.de>
parents:
1074
diff
changeset
|
199 |
|
1447 | 200 |
"/ for 64 bit machines: |
201 |
16r3fffffffffffffff |
|
202 |
16r7fffffffffffffff |
|
203 |
16rffffffffffffffff |
|
204 |
16r2000000000000000 |
|
205 |
16r4000000000000000 |
|
206 |
16r8000000000000000 |
|
207 |
16r10000000000000000 |
|
208 |
16r2000000000000001 |
|
209 |
16r4000000000000001 |
|
210 |
16r8000000000000001 |
|
211 |
16r10000000000000001 |
|
364 | 212 |
) do:[:x | |
1447 | 213 |
i := x. |
214 |
ii := i * i. |
|
215 |
self assert:((ii / i) = i). |
|
216 |
i class == SmallInteger ifTrue:[ |
|
217 |
self assert:((ii / i) == i). |
|
218 |
]. |
|
364 | 219 |
|
1447 | 220 |
i := x negated. |
221 |
ii := i * i. |
|
222 |
self assert:((ii / i) = i). |
|
223 |
i class == SmallInteger ifTrue:[ |
|
224 |
self assert:((ii / i) == i). |
|
225 |
]. |
|
364 | 226 |
]. |
227 |
! |
|
228 |
||
229 |
testNegation |
|
1596 | 230 |
|i iN iNN iNNN| |
364 | 231 |
|
1447 | 232 |
#( |
1596 | 233 |
16r1fffffff |
234 |
16r3fffffff |
|
235 |
16r7fffffff |
|
236 |
16rffffffff |
|
237 |
16r20000000 |
|
238 |
16r40000000 |
|
239 |
16r80000000 |
|
240 |
16r100000000 |
|
241 |
16r20000001 |
|
242 |
16r40000001 |
|
243 |
16r80000001 |
|
244 |
16r100000001 |
|
1223
708a62eac280
class: RegressionTests::SmallIntegerTest
Claus Gittinger <cg@exept.de>
parents:
1074
diff
changeset
|
245 |
|
1596 | 246 |
"/ for 64bit machines: |
247 |
16r3fffffffffffffff |
|
248 |
16r7fffffffffffffff |
|
249 |
16rffffffffffffffff |
|
250 |
16r2000000000000000 |
|
251 |
16r4000000000000000 |
|
252 |
16r8000000000000000 |
|
253 |
16r10000000000000000 |
|
254 |
16r2000000000000001 |
|
255 |
16r4000000000000001 |
|
256 |
16r8000000000000001 |
|
257 |
16r10000000000000001 |
|
364 | 258 |
) do:[:x | |
1596 | 259 |
i := x. |
260 |
iN := i negated. |
|
261 |
iNN := iN negated. |
|
262 |
iNNN := iNN negated. |
|
263 |
self assert:(i = iNN). |
|
264 |
i class == SmallInteger ifTrue:[ |
|
265 |
self assert:(i == iNN). |
|
266 |
]. |
|
267 |
self assert:(iN = iNNN). |
|
268 |
iN class == SmallInteger ifTrue:[ |
|
269 |
self assert:(iN == iNNN). |
|
270 |
]. |
|
364 | 271 |
]. |
1306 | 272 |
self assert:(SmallInteger maxVal negated class == SmallInteger). |
273 |
self assert:(SmallInteger maxVal negated negated == SmallInteger maxVal). |
|
1447 | 274 |
|
1306 | 275 |
self assert:(SmallInteger maxVal negated -1 == SmallInteger minVal). |
276 |
self assert:(SmallInteger minVal negated negated class == SmallInteger). |
|
1596 | 277 |
|
278 |
"Modified: / 28-02-2017 / 16:42:04 / cg" |
|
310 | 279 |
! ! |
280 |
||
281 |
!SmallIntegerTest methodsFor:'tests-class protocol'! |
|
282 |
||
283 |
testBasicNew |
|
1447 | 284 |
self should: [SmallInteger basicNew] raise: TestResult error. |
310 | 285 |
! |
286 |
||
287 |
testMaxVal |
|
288 |
"/ the original code did not check for pointer-size; |
|
289 |
||
290 |
Smalltalk isSmalltalkX ifTrue:[ |
|
1447 | 291 |
SmallInteger maxBytes == 4 ifTrue:[ |
292 |
self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
|
293 |
self should: [SmallInteger maxVal == 16r3FFFFFFF]. |
|
294 |
] ifFalse:[ |
|
295 |
self should: [SmallInteger maxVal = 16r3FFFFFFFFFFFFFFF]. |
|
296 |
self should: [SmallInteger maxVal == 16r3FFFFFFFFFFFFFFF]. |
|
297 |
]. |
|
310 | 298 |
] ifFalse:[ |
1447 | 299 |
self should: [SmallInteger maxVal = 16r3FFFFFFF]. |
310 | 300 |
] |
301 |
! |
|
302 |
||
1347 | 303 |
testMaxValPointerSizeConsistency |
304 |
SmallInteger maxBytes == 4 ifTrue:[ |
|
1447 | 305 |
self assert: ((SmallInteger maxBits == 31) or:[SmallInteger maxBits == 32]). |
306 |
self assert: (ExternalAddress pointerSize == 4). |
|
1347 | 307 |
] ifFalse:[ |
1447 | 308 |
self assert: ((SmallInteger maxBits == 63) or:[SmallInteger maxBits == 64]). |
309 |
self assert: (ExternalAddress pointerSize == 8). |
|
1347 | 310 |
] |
311 |
! |
|
312 |
||
310 | 313 |
testMinVal |
314 |
"/ the original code did not check for pointer-size; |
|
315 |
||
316 |
Smalltalk isSmalltalkX ifTrue:[ |
|
1709 | 317 |
SmallInteger maxBytes == 4 ifTrue:[ |
318 |
self should: [SmallInteger minVal = 16r-40000000]. |
|
319 |
self should: [SmallInteger minVal == 16r-40000000]. |
|
320 |
] ifFalse:[ |
|
321 |
self should: [SmallInteger minVal = 16r-4000000000000000]. |
|
322 |
self should: [SmallInteger minVal == 16r-4000000000000000]. |
|
323 |
]. |
|
310 | 324 |
] ifFalse:[ |
1709 | 325 |
self should: [SmallInteger minVal = 16r-40000000]. |
310 | 326 |
] |
1709 | 327 |
|
328 |
"Modified (format): / 19-09-2017 / 16:29:45 / stefan" |
|
310 | 329 |
! |
330 |
||
331 |
testNew |
|
1447 | 332 |
self should: [SmallInteger new] raise: TestResult error. |
310 | 333 |
! ! |
334 |
||
335 |
!SmallIntegerTest class methodsFor:'documentation'! |
|
336 |
||
337 |
version |
|
338 |
^ '$Header$' |
|
339 |
! ! |
|
1074 | 340 |