2
|
1 |
Object subclass:#IntegerTest
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Tests - Numbers'
|
|
6 |
!
|
|
7 |
|
|
8 |
|
4
|
9 |
!IntegerTest class methodsFor:'misc'!
|
|
10 |
|
|
11 |
testPlus1_a:arg
|
|
12 |
^ arg + 1
|
|
13 |
|
|
14 |
"
|
|
15 |
(self testPlus1_a:SmallInteger maxVal) = 1073741824
|
|
16 |
"
|
|
17 |
!
|
|
18 |
|
|
19 |
testPlus1_b:arg
|
|
20 |
^ arg perform:#+ with:1
|
|
21 |
|
|
22 |
"
|
|
23 |
(self testPlus1_b:SmallInteger maxVal) == 1073741824
|
|
24 |
"
|
|
25 |
! !
|
|
26 |
|
3
|
27 |
!IntegerTest class methodsFor:'private'!
|
2
|
28 |
|
|
29 |
test:aBlock
|
3
|
30 |
aBlock value == true ifFalse:[
|
2
|
31 |
self halt:'test failed'
|
|
32 |
]
|
|
33 |
! !
|
|
34 |
|
|
35 |
!IntegerTest class methodsFor:'tests'!
|
|
36 |
|
|
37 |
test1
|
|
38 |
"general conversion & tests"
|
|
39 |
|
3
|
40 |
|minVal maxVal maxValPlus1 minValMinus1 halfMin halfMax t t1 t2|
|
|
41 |
|
|
42 |
minVal := SmallInteger perform:('minVal' asSymbol).
|
|
43 |
maxVal := SmallInteger perform:('maxVal' asSymbol).
|
|
44 |
|
|
45 |
self test:[minVal == SmallInteger minVal].
|
|
46 |
self test:[maxVal == SmallInteger maxVal].
|
2
|
47 |
|
|
48 |
self test:[10 printString = '10'].
|
|
49 |
self test:[100 printString = '100'].
|
|
50 |
self test:[1000 printString = '1000'].
|
|
51 |
self test:[10000 printString = '10000'].
|
|
52 |
self test:[100000 printString = '100000'].
|
|
53 |
self test:[1000000 printString = '1000000'].
|
|
54 |
self test:[10000000 printString = '10000000'].
|
|
55 |
self test:[100000000 printString = '100000000'].
|
|
56 |
self test:[1000000000 printString = '1000000000'].
|
|
57 |
self test:[10000000000 printString = '10000000000'].
|
3
|
58 |
self test:[100000000000 printString = '100000000000'].
|
|
59 |
self test:[1000000000000 printString = '1000000000000'].
|
|
60 |
self test:[10000000000000 printString = '10000000000000'].
|
|
61 |
self test:[100000000000000 printString = '100000000000000'].
|
|
62 |
self test:[1000000000000000 printString = '1000000000000000'].
|
|
63 |
self test:[10000000000000000 printString = '10000000000000000'].
|
|
64 |
self test:[100000000000000000 printString = '100000000000000000'].
|
2
|
65 |
|
|
66 |
SmallInteger maxBytes == 4 ifTrue:[
|
3
|
67 |
self test:[minVal hexPrintString = '-40000000'].
|
|
68 |
self test:[maxVal hexPrintString = '3FFFFFFF'].
|
|
69 |
self test:[minVal == -1073741824].
|
|
70 |
self test:[maxVal == 1073741823].
|
2
|
71 |
maxValPlus1 := 1073741824.
|
|
72 |
minValMinus1 := -1073741825.
|
|
73 |
self test:[minValMinus1 hexPrintString = '-40000001'].
|
|
74 |
self test:[maxValPlus1 hexPrintString = '40000000'].
|
3
|
75 |
halfMin := -16r20000000.
|
|
76 |
halfMax := 16r20000000.
|
2
|
77 |
].
|
|
78 |
SmallInteger maxBytes == 8 ifTrue:[
|
3
|
79 |
self test:[minVal hexPrintString = '-4000000000000000'].
|
|
80 |
self test:[maxVal hexPrintString = '3FFFFFFFFFFFFFFF'].
|
|
81 |
self test:[minVal == -4611686018427387904].
|
|
82 |
self test:[maxVal == 4611686018427387903].
|
2
|
83 |
maxValPlus1 := 4611686018427387904.
|
|
84 |
minValMinus1 := -4611686018427387905.
|
|
85 |
self test:[minValMinus1 hexPrintString = '-4000000000000001'].
|
|
86 |
self test:[maxValPlus1 hexPrintString = '4000000000000000'].
|
3
|
87 |
halfMin := -16r2000000000000000.
|
|
88 |
halfMax := 16r2000000000000000.
|
2
|
89 |
].
|
|
90 |
|
|
91 |
"arithmetic overFlow checks"
|
|
92 |
|
4
|
93 |
"/ self test:[(maxVal + 1) inspect.maxValPlus1 inspect. true].
|
3
|
94 |
self test:[maxVal + 1 = maxValPlus1].
|
|
95 |
self test:[minVal - 1 = minValMinus1].
|
|
96 |
|
|
97 |
self test:[(halfMax + halfMax) = maxValPlus1].
|
|
98 |
self test:[(halfMin + halfMin) == minVal].
|
|
99 |
self test:[(halfMax * 2) = maxValPlus1].
|
|
100 |
self test:[(halfMin * 2) == minVal].
|
|
101 |
self test:[maxValPlus1 // 2 == halfMax].
|
|
102 |
self test:[(halfMax bitShift:1) = maxValPlus1].
|
|
103 |
self test:[(halfMin bitShift:1) == minVal].
|
|
104 |
|
|
105 |
"LargeInt op -> SmallInt result"
|
|
106 |
t := maxVal + 1.
|
|
107 |
self test:[t - 1 == maxVal].
|
|
108 |
t := minVal - 1.
|
|
109 |
self test:[t + 1 == minVal].
|
|
110 |
|
|
111 |
"multiplication"
|
|
112 |
t1 := 100.
|
|
113 |
self test:[t1 * t1 == 10000].
|
|
114 |
t1 := 1000.
|
|
115 |
self test:[t1 * t1 == 1000000].
|
|
116 |
t1 := 10000.
|
|
117 |
self test:[t1 * t1 == 100000000].
|
|
118 |
t1 := 100000.
|
|
119 |
SmallInteger maxBytes == 4 ifTrue:[
|
|
120 |
self test:[t1 * t1 ~~ 10000000000].
|
|
121 |
].
|
|
122 |
SmallInteger maxBytes == 8 ifTrue:[
|
|
123 |
self test:[t1 * t1 == 10000000000].
|
|
124 |
].
|
4
|
125 |
|
|
126 |
self test:[(t1 perform:'*' asSymbol with:t1) printString = '10000000000'].
|
3
|
127 |
self test:[(t1 * t1) printString = '10000000000'].
|
|
128 |
t1 := 1000000.
|
|
129 |
self test:[(t1 * t1) printString = '1000000000000'].
|
|
130 |
t1 := 10000000.
|
|
131 |
self test:[(t1 * t1) printString = '100000000000000'].
|
|
132 |
t1 := 100000000.
|
|
133 |
self test:[(t1 * t1) printString = '10000000000000000'].
|
|
134 |
t1 := 1000000000.
|
|
135 |
self test:[(t1 * t1) printString = '1000000000000000000'].
|
2
|
136 |
|
3
|
137 |
"Large * SmallInt multiplication"
|
|
138 |
self test:[10 factorial printString = '3628800'].
|
|
139 |
self test:[20 factorial printString = '2432902008176640000'].
|
|
140 |
self test:[100 factorial printString = '93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000'].
|
|
141 |
self test:[1000 factorial printString = '402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'].
|
2
|
142 |
|
3
|
143 |
"Large * Large multiplication"
|
|
144 |
self test:[(20 factorial * 20 factorial) printString = '5919012181389927685417441689600000000'].
|
|
145 |
t := 20 factorial.
|
|
146 |
t := t // 20.
|
|
147 |
self test:[t printString = 19 factorial printString].
|
|
148 |
t := t // 19.
|
|
149 |
self test:[t printString = 18 factorial printString].
|
|
150 |
t := t // 18.
|
|
151 |
self test:[t printString = 17 factorial printString].
|
|
152 |
t := t // 17.
|
|
153 |
self test:[t printString = 16 factorial printString].
|
|
154 |
t := t // 16.
|
|
155 |
self test:[t printString = 15 factorial printString].
|
|
156 |
t := t // 15.
|
|
157 |
self test:[t printString = 14 factorial printString].
|
|
158 |
t := t // 14.
|
|
159 |
self test:[t printString = 13 factorial printString].
|
|
160 |
t := t // 13.
|
|
161 |
self test:[t printString = 12 factorial printString].
|
|
162 |
t := t // 12.
|
|
163 |
self test:[t printString = 11 factorial printString].
|
|
164 |
t := t // 11.
|
|
165 |
self test:[t printString = 10 factorial printString].
|
|
166 |
self test:[t == 10 factorial].
|
|
167 |
|
|
168 |
"Large // Large division"
|
|
169 |
self test:[(20 factorial / 19 factorial) == 20].
|
|
170 |
|
|
171 |
"Large + Large addition"
|
|
172 |
self test:[(20 factorial + 20 factorial) printString = '4865804016353280000'].
|
|
173 |
self test:[(20 factorial + 1) printString = '2432902008176640001'].
|
|
174 |
self test:[(20 factorial + 1000) printString = '2432902008176641000'].
|
|
175 |
|
|
176 |
|
|
177 |
"Large - small subtraction"
|
|
178 |
self test:[(20 factorial + 20 factorial - 1) printString = '4865804016353279999'].
|
|
179 |
self test:[(20 factorial - 10 factorial + 3628800) printString = '2432902008176640000'].
|
2
|
180 |
|
|
181 |
"
|
|
182 |
self test1
|
|
183 |
"
|
|
184 |
! !
|
|
185 |
|
|
186 |
!IntegerTest class methodsFor:'documentation'!
|
|
187 |
|
|
188 |
version
|
|
189 |
^ '$Header$'
|
|
190 |
! !
|