1 UnitTests define: #Numeric &parents: {TestCase} &slots: {
2 "100 factorial as determined by bc(1)"
3 #bigInt -> 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000.
5 #complexZero -> (Complex zero).
6 #complexOne -> (1 plusImaginary: 1).
7 #complexNegativeOne -> (-1 plusImaginary: -1).
8 #complexFraction -> ((1 / 2) plusImaginary: (1 / 3)).
9 #complexFloat -> (2.5 plusImaginary: 0.5).
10 #complexTriangle -> (4 plusImaginary: 3).
13 "Tests involving Floats use numbers which can be represented as binary
14 fractions to avoid precision issues. This isn't a problem since here we're
15 testing coercions, not arithmetic."
17 t@(UnitTests Numeric traits) testIsZero
21 t assert: (0 / 1) isZero.
24 t deny: t bigInt isZero.
25 t deny: (1 / 2) isZero.
27 t assert: t complexZero isZero.
28 t deny: t complexOne isZero.
29 t deny: t complexNegativeOne isZero.
30 t deny: t complexTriangle isZero.
33 t@(UnitTests Numeric traits) testIsPositive
35 t assert: t smallInt isPositive.
36 t assert: t bigInt isPositive.
37 t assert: (5 / 10) isPositive.
38 t assert: (-5 / -10) isPositive.
39 t assert: 0.1 isPositive.
41 t deny: t smallInt negated isPositive.
42 t deny: t bigInt negated isPositive.
43 t deny: -0.1 isPositive.
44 t deny: (-5 / 10) isPositive.
45 t deny: (5 / -10) isPositive.
47 "Is #isPositive meaningful for complex numbers?"
50 t@(UnitTests Numeric traits) testIsNegative
52 t deny: t smallInt isNegative.
53 t deny: t bigInt isNegative.
54 t deny: (5 / 10) isNegative.
55 t deny: (-5 / -10) isNegative.
56 t deny: 0.1 isNegative.
58 t assert: -0.1 isNegative.
59 t assert: t smallInt negated isNegative.
60 t assert: t bigInt negated isNegative.
61 t assert: (-5 / 10) isNegative.
63 "Is #isNegative meaningful for complex numbers?"
66 "Integer arithmetic tests."
68 t@(UnitTests Numeric traits) testIntegerAddition
69 "Test addition of Numbers to Integers"
72 t assert: 2 + (1 / 2) = (5 / 2).
73 t assert: 2 + 2.1 = 4.1.
75 t assert: 2 + t complexZero = 2.
76 t assert: 2 + t complexOne = (3 plusImaginary: 1).
77 t assert: 2 + t complexFraction = ((5 / 2) plusImaginary: (1 / 3)).
78 t assert: 2 + t complexFloat = (4.5 plusImaginary: 0.5).
81 t@(UnitTests Numeric traits) testIntegerSubtraction
82 "Test subtraction of Numbers from Integers"
85 t assert: 2 - (1 / 2) = (3 / 2).
86 t assert: 2 - 2.5 = -0.5.
88 t assert: 2 - t complexZero = (2 plusImaginary: 0).
89 t assert: 2 - t complexOne = (1 plusImaginary: -1).
90 t assert: 2 - t complexFraction = ((3 / 2) plusImaginary: (-1 / 3)).
91 t assert: 2 - t complexFloat = (-0.5 plusImaginary: -0.5).
94 t@(UnitTests Numeric traits) testIntegerMultiplication
97 t assert: 2 * (3 / 4) = (3 / 2).
98 t assert: 2 * 2.5 = 5.0.
99 t assert: 2 * -2 = -4.
100 t assert: 2 * (-3 / 4) = (-3 / 2).
101 t assert: 2 * -2.5 = -5.0.
102 t assert: -2 * 2 = -4.
103 t assert: -2 * (3 / 4) = (-3 / 2).
104 t assert: -2 * 2.5 = -5.0.
105 t assert: -2 * -2 = 4.
106 t assert: -2 * (-3 / 4) = (3 / 2).
107 t assert: -2 * -2.5 = 5.0.
108 t assert: 2 * 0.0 = 0.0.
109 t assert: -2 * 0.0 = 0.0.
111 "TODO add complex numbers"
114 t@(UnitTests Numeric traits) testIntegerDivision
117 t assert: 4 / (1 / 2) = 8.
118 t assert: 4 / 0.5 = 8.0.
119 t assert: 4 / t complexOne = (2 plusImaginary: -2).
120 t assert: 4 / t complexNegativeOne = (-2 plusImaginary: 2).
121 t assert: 4 / t complexFraction = ((72 / 13) plusImaginary: (-48 / 13)).
122 t assert: 4 / t complexFloat ~= 0.
123 t assert: 4 / t complexTriangle = ((16 / 25) plusImaginary: (-12 / 25)).
126 t@(UnitTests Numeric traits) testIntegerEquality
130 t assert: 2 = (4 / 2).
134 "Fraction arithmetic tests."
136 t@(UnitTests Numeric traits) testFractionAddition
137 "Test addition of Numbers to Fractions"
139 t assert: (1 / 2) + 2 = (5 / 2) .
140 t assert: (1 / 2) + (1 / 4) = (3 / 4).
141 t assert: (1 / 2) + 2.1 = 2.6.
144 t@(UnitTests Numeric traits) testFractionSubtraction
145 "Test subtraction of Numbers from Fractions"
147 t assert: (1 / 2) - 2 = (-3 / 2) .
148 t assert: (1 / 2) - (1 / 4) = (1 / 4).
149 t assert: (1 / 4) - 2.5 = -2.25.
152 t@(UnitTests Numeric traits) testFractionMultiplication
154 t assert: (3 / 4) * 3 = (9 / 4).
155 t assert: (3 / 4) * -3 = (-9 / 4).
156 t assert: (-3 / 4) * 3 = (-9 / 4).
157 t assert: (3 / -4) * -3 = (9 / 4).
158 t assert: (-3 / -4) * -3 = (-9 / 4).
160 t assert: (3 / 4) * (1 / 2) = (3 / 8).
161 t assert: (-3 / 4) * (1 / 2) = (-3 / 8).
162 t assert: (3 / 4) * (-1 / 2) = (-3 / 8).
163 t assert: (-3 / 4) * (-1 / 2) = (3 / 8).
164 t assert: (-3 / -4) * (-1 / 2) = (-3 / 8).
166 t assert: (1 / 2) * 3.5 = 1.75.
167 t assert: (-1 / 2) * 3.5 = -1.75.
168 t assert: (1 / 2) * -3.5 = -1.75.
169 t assert: (-1 / 2) * -3.5 = 1.75.
170 t assert: (-1 / -2) * -3.5 = -1.75.
172 t assert: (3 / 4) * 0 = 0.
173 t assert: (3 / 4) * 0.0 = 0.0.
176 t@(UnitTests Numeric traits) testFractionDivision
178 t assert: (3 / 4) / 3 = (1 / 4).
179 t assert: (3 / 4) / (1 / 2) = (6 / 4).
180 t assert: (3 / 4) / 0.5 = 1.5.
181 t assert: (3 / 4) / t complexTriangle = ((3 / 25) plusImaginary: (-9 / 100)).
184 "Float arithmetic tests."
186 t@(UnitTests Numeric traits) testFloatAddition
187 "Test addition of Numbers to Floats"
189 t assert: 2.1 + 2 = 4.1.
190 t assert: 2.1 + (1 / 2) = 2.6.
191 t assert: 2.1 + 2.1 = 4.2.
194 t@(UnitTests Numeric traits) testFloatSubtraction
195 "Test subtraction of Numbers from Floats"
197 t assert: 2.5 - 2 = 0.5.
198 t assert: 2.5 - (3 / 4) = 1.75.
199 t assert: 2.5 - 1.75 = 0.75.
200 t assert: 2.5 - 2.5 = 0.0.
203 t@(UnitTests Numeric traits) testFloatMultiplication
205 t assert: 3.5 * 2 = 7.0.
206 t assert: -3.5 * 2 = -7.0.
207 t assert: 3.5 * -2 = -7.0.
208 t assert: -3.5 * -2 = 7.0.
210 t assert: 3.5 * (1 / 2) = 1.75.
211 t assert: -3.5 * (1 / 2) = -1.75.
212 t assert: 3.5 * (-1 / 2) = -1.75.
213 t assert: -3.5 * (-1 / 2) = 1.75.
215 t assert: 3.5 * 1.5 = 5.25.
216 t assert: -3.5 * 1.5 = -5.25.
217 t assert: 3.5 * -1.5 = -5.25.
218 t assert: -3.5 * -1.5 = 5.25.
220 t assert: 3.5 * 0 = 0.
221 t assert: 3.5 * 0.0 = 0.0.
224 t@(UnitTests Numeric traits) testFloatDivision
226 t assert: 3.5 / 2 = 1.75.
227 t assert: -3.5 / 2 = -1.75.
228 t assert: 3.5 / -2 = -1.75.
229 t assert: -3.5 / -2 = 1.75.
231 t assert: 3.5 / (1 / 2) = 7.0.
232 t assert: -3.5 / (1 / 2) = -7.0.
233 t assert: 3.5 / (-1 / 2) = -7.0.
234 t assert: -3.5 / (-1 / 2) = 7.0.
236 t assert: 3.5 / 1.75 = 2.0.
237 t assert: -3.5 / 1.75 = -2.0.
238 t assert: 3.5 / -1.75 = -2.0.
239 t assert: -3.5 / -1.75 = 2.0.
241 t assert: 0 / 3.5 = 0.
242 t assert: 0.0 / 3.5 = 0.0.
245 t@(UnitTests Numeric traits) testIsCloseTo
247 t deny: (0.1 isCloseTo: 0.0).
248 t deny: (0.0 isCloseTo: 0.1).
249 t assert: (1e-12 isCloseTo: 0.0).
250 t deny: (3.1 isCloseTo: 3).
251 t deny: (3 isCloseTo: 4).
252 t assert: (7 + 1e-12 isCloseTo: 7).
253 t deny: ((1 plusImaginary: -5) isCloseTo: 1).
254 t assert: ((1 plusImaginary: -5) isCloseTo: (1 plusImaginary: -5 + 1e-12)).
255 t assert: (3 isCloseTo: (3 plusImaginary: 0)).
259 "Complex number arithmetic tests."
261 t@(UnitTests Numeric traits) testComplexAddition
263 c1 ::= 2.5 plusImaginary: -3.5.
264 c2 ::= -35 plusImaginary: 4.75.
266 t assert: (c1 + c2 isCloseTo: (-32.5 plusImaginary: 1.25)).
267 t assert: (c2 + c1 isCloseTo: (-32.5 plusImaginary: 1.25)).
268 t assert: (c1 + 5.3 isCloseTo: (7.8 plusImaginary: -3.5)).
269 t assert: (c2 + 3 isCloseTo: (-32 plusImaginary: 4.75)).
270 t assert: (3 + c2 isCloseTo: (-32 plusImaginary: 4.75)).
273 t@(UnitTests Numeric traits) testComplexSubtraction
275 c1 ::= 2.5 plusImaginary: -3.5.
276 c2 ::= -35 plusImaginary: 4.75.
278 t assert: (c1 - c2 isCloseTo: (37.5 plusImaginary: -8.25)).
279 t assert: (c2 - c1 isCloseTo: (-37.5 plusImaginary: 8.25)).
280 t assert: (c1 - 5.3 isCloseTo: (-2.8 plusImaginary: -3.5)).
281 t assert: (c2 - 3 isCloseTo: (-38 plusImaginary: 4.75)).
282 t assert: (3 - c2 isCloseTo: (38 plusImaginary: -4.75)).
285 t@(UnitTests Numeric traits) testComplexMultiplication
287 c1 ::= 2.5 plusImaginary: -3.5.
288 c2 ::= -35 plusImaginary: 4.75.
290 t assert: (c1 * c2 isCloseTo: (-70.875 plusImaginary: 134.375)).
291 t assert: (c2 * c1 isCloseTo: (-70.875 plusImaginary: 134.375)).
292 t assert: (c1 * 5.3 isCloseTo: (13.25 plusImaginary: -18.55)).
293 t assert: (c2 * 3 isCloseTo: (-105 plusImaginary: 14.25)).
294 t assert: (3 * c2 isCloseTo: (-105 plusImaginary: 14.25)).
297 t@(UnitTests Numeric traits) testComplexDivision
299 c1 ::= 2.5 plusImaginary: -3.5.
300 c2 ::= -35 plusImaginary: 4.75.
302 t assert: (c1 / c2 isCloseTo: (-0.0834627 plusImaginary: 0.0886729)).
303 t assert: (c2 / c1 isCloseTo: (-5.628378 plusImaginary: -5.979729)).
304 t assert: (c1 / 5.3 isCloseTo: (0.471698 plusImaginary: -0.660377)).
305 t assert: (c2 / 3 isCloseTo: (-11.666667 plusImaginary: 1.583333)).
306 t assert: (3 / c2 isCloseTo: (-0.0841641 plusImaginary: -0.0114222)).
314 t@(UnitTests Numeric traits) suite
315 [t suiteForSelectors: {
319 #testIntegerAddition.
320 #testIntegerSubtraction.
321 #testIntegerMultiplication.
322 #testIntegerDivision.
323 #testIntegerEquality.
324 #testFractionAddition.
325 #testFractionSubtraction.
326 #testFractionMultiplication.
327 #testFractionDivision.
329 #testFloatSubtraction.
330 #testFloatMultiplication.
333 #testComplexAddition.
334 #testComplexSubtraction.
335 #testComplexMultiplication.
336 #testComplexDivision.