1 UnitTests addPrototype: #Numeric derivedFrom: {TestCase}.
2 "100 factorial as determined by bc(1)"
3 UnitTests Numeric addSlot: #bigInt valued: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000.
4 UnitTests Numeric addSlot: #smallInt valued: 100.
5 UnitTests Numeric addSlot: #complexZero valued: (Complex zero).
6 UnitTests Numeric addSlot: #complexOne valued: (1 plusImaginary: 1).
7 UnitTests Numeric addSlot: #complexNegativeOne valued: (-1 plusImaginary: -1).
8 UnitTests Numeric addSlot: #complexFraction valued: ((1 / 2) plusImaginary: (1 / 3)).
9 UnitTests Numeric addSlot: #complexFloat valued: (2.5 plusImaginary: 0.5).
10 UnitTests Numeric addSlot: #complexTriangle valued: (4 plusImaginary: 3).
12 "Tests involving Floats use numbers which can be represented as binary
13 fractions to avoid precision issues. This isn't a problem since here we're
14 testing coercions, not arithmetic."
16 t@(UnitTests Numeric traits) testIsZero
20 t assert: (0 / 1) isZero.
23 t deny: t bigInt isZero.
24 t deny: (1 / 2) isZero.
26 t assert: t complexZero isZero.
27 t deny: t complexOne isZero.
28 t deny: t complexNegativeOne isZero.
29 t deny: t complexTriangle isZero.
32 t@(UnitTests Numeric traits) testIsPositive
34 t assert: t smallInt isPositive.
35 t assert: t bigInt isPositive.
36 t assert: (5 / 10) isPositive.
37 t assert: (-5 / -10) isPositive.
38 t assert: 0.1 isPositive.
40 t deny: t smallInt negated isPositive.
41 t deny: t bigInt negated isPositive.
42 t deny: -0.1 isPositive.
43 t deny: (-5 / 10) isPositive.
44 t deny: (5 / -10) isPositive.
46 "Is #isPositive meaningful for complex numbers?"
49 t@(UnitTests Numeric traits) testIsNegative
51 t deny: t smallInt isNegative.
52 t deny: t bigInt isNegative.
53 t deny: (5 / 10) isNegative.
54 t deny: (-5 / -10) isNegative.
55 t deny: 0.1 isNegative.
57 t assert: -0.1 isNegative.
58 t assert: t smallInt negated isNegative.
59 t assert: t bigInt negated isNegative.
60 t assert: (-5 / 10) isNegative.
62 "Is #isNegative meaningful for complex numbers?"
65 "Integer arithmetic tests."
67 t@(UnitTests Numeric traits) testIntegerAddition
68 "Test addition of Numbers to Integers"
71 t assert: 2 + (1 / 2) = (5 / 2).
72 t assert: 2 + 2.1 = 4.1.
74 t assert: 2 + t complexZero = 2.
75 t assert: 2 + t complexOne = (3 plusImaginary: 1).
76 t assert: 2 + t complexFraction = ((5 / 2) plusImaginary: (1 / 3)).
77 t assert: 2 + t complexFloat = (4.5 plusImaginary: 0.5).
80 t@(UnitTests Numeric traits) testIntegerSubtraction
81 "Test subtraction of Numbers from Integers"
84 t assert: 2 - (1 / 2) = (3 / 2).
85 t assert: 2 - 2.5 = -0.5.
87 t assert: 2 - t complexZero = (2 plusImaginary: 0).
88 t assert: 2 - t complexOne = (1 plusImaginary: -1).
89 t assert: 2 - t complexFraction = ((3 / 2) plusImaginary: (-1 / 3)).
90 t assert: 2 - t complexFloat = (-0.5 plusImaginary: -0.5).
93 t@(UnitTests Numeric traits) testIntegerMultiplication
96 t assert: 2 * (3 / 4) = (3 / 2).
97 t assert: 2 * 2.5 = 5.0.
98 t assert: 2 * -2 = -4.
99 t assert: 2 * (-3 / 4) = (-3 / 2).
100 t assert: 2 * -2.5 = -5.0.
101 t assert: -2 * 2 = -4.
102 t assert: -2 * (3 / 4) = (-3 / 2).
103 t assert: -2 * 2.5 = -5.0.
104 t assert: -2 * -2 = 4.
105 t assert: -2 * (-3 / 4) = (3 / 2).
106 t assert: -2 * -2.5 = 5.0.
107 t assert: 2 * 0.0 = 0.0.
108 t assert: -2 * 0.0 = 0.0.
110 "TODO add complex numbers"
113 t@(UnitTests Numeric traits) testIntegerDivision
116 t assert: 4 / (1 / 2) = 8.
117 t assert: 4 / 0.5 = 8.0.
118 t assert: 4 / t complexOne = (2 plusImaginary: -2).
119 t assert: 4 / t complexNegativeOne = (-2 plusImaginary: 2).
120 t assert: 4 / t complexFraction = ((72 / 13) plusImaginary: (-48 / 13)).
121 t assert: 4 / t complexFloat ~= 0.
122 t assert: 4 / t complexTriangle = ((16 / 25) plusImaginary: (-12 / 25)).
125 t@(UnitTests Numeric traits) testIntegerEquality
129 t assert: 2 = (4 / 2).
133 "Fraction arithmetic tests."
135 t@(UnitTests Numeric traits) testFractionAddition
136 "Test addition of Numbers to Fractions"
138 t assert: (1 / 2) + 2 = (5 / 2) .
139 t assert: (1 / 2) + (1 / 4) = (3 / 4).
140 t assert: (1 / 2) + 2.1 = 2.6.
143 t@(UnitTests Numeric traits) testFractionSubtraction
144 "Test subtraction of Numbers from Fractions"
146 t assert: (1 / 2) - 2 = (-3 / 2) .
147 t assert: (1 / 2) - (1 / 4) = (1 / 4).
148 t assert: (1 / 4) - 2.5 = -2.25.
151 t@(UnitTests Numeric traits) testFractionMultiplication
153 t assert: (3 / 4) * 3 = (9 / 4).
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).
159 t assert: (3 / 4) * (1 / 2) = (3 / 8).
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).
165 t assert: (1 / 2) * 3.5 = 1.75.
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.
171 t assert: (3 / 4) * 0 = 0.
172 t assert: (3 / 4) * 0.0 = 0.0.
175 t@(UnitTests Numeric traits) testFractionDivision
177 t assert: (3 / 4) / 3 = (1 / 4).
178 t assert: (3 / 4) / (1 / 2) = (6 / 4).
179 t assert: (3 / 4) / 0.5 = 1.5.
180 t assert: (3 / 4) / t complexTriangle = ((3 / 25) plusImaginary: (-9 / 100)).
183 "Float arithmetic tests."
185 t@(UnitTests Numeric traits) testFloatAddition
186 "Test addition of Numbers to Floats"
188 t assert: 2.1 + 2 = 4.1.
189 t assert: 2.1 + (1 / 2) = 2.6.
190 t assert: 2.1 + 2.1 = 4.2.
193 t@(UnitTests Numeric traits) testFloatSubtraction
194 "Test subtraction of Numbers from Floats"
196 t assert: 2.5 - 2 = 0.5.
197 t assert: 2.5 - (3 / 4) = 1.75.
198 t assert: 2.5 - 1.75 = 0.75.
199 t assert: 2.5 - 2.5 = 0.0.
202 t@(UnitTests Numeric traits) testFloatMultiplication
204 t assert: 3.5 * 2 = 7.0.
205 t assert: -3.5 * 2 = -7.0.
206 t assert: 3.5 * -2 = -7.0.
207 t assert: -3.5 * -2 = 7.0.
209 t assert: 3.5 * (1 / 2) = 1.75.
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.
214 t assert: 3.5 * 1.5 = 5.25.
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.
219 t assert: 3.5 * 0 = 0.
220 t assert: 3.5 * 0.0 = 0.0.
223 t@(UnitTests Numeric traits) testFloatDivision
225 t assert: 3.5 / 2 = 1.75.
226 t assert: -3.5 / 2 = -1.75.
227 t assert: 3.5 / -2 = -1.75.
228 t assert: -3.5 / -2 = 1.75.
230 t assert: 3.5 / (1 / 2) = 7.0.
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.
235 t assert: 3.5 / 1.75 = 2.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.
240 t assert: 0 / 3.5 = 0.
241 t assert: 0.0 / 3.5 = 0.0.
244 t@(UnitTests Numeric traits) testIsCloseTo
246 t deny: (0.1 isCloseTo: 0.0).
247 t deny: (0.0 isCloseTo: 0.1).
248 t assert: (1e-12 isCloseTo: 0.0).
249 t deny: (3.1 isCloseTo: 3).
250 t deny: (3 isCloseTo: 4).
251 t assert: (7 + 1e-12 isCloseTo: 7).
252 t deny: ((1 plusImaginary: -5) isCloseTo: 1).
253 t assert: ((1 plusImaginary: -5) isCloseTo: (1 plusImaginary: -5 + 1e-12)).
254 t assert: (3 isCloseTo: (3 plusImaginary: 0)).
258 "Complex number arithmetic tests."
260 t@(UnitTests Numeric traits) testComplexAddition
262 c1: (2.5 plusImaginary: -3.5).
263 c2: (-35 plusImaginary: 4.75).
265 t assert: (c1 + c2 isCloseTo: (-32.5 plusImaginary: 1.25)).
266 t assert: (c2 + c1 isCloseTo: (-32.5 plusImaginary: 1.25)).
267 t assert: (c1 + 5.3 isCloseTo: (7.8 plusImaginary: -3.5)).
268 t assert: (c2 + 3 isCloseTo: (-32 plusImaginary: 4.75)).
269 t assert: (3 + c2 isCloseTo: (-32 plusImaginary: 4.75)).
272 t@(UnitTests Numeric traits) testComplexSubtraction
274 c1: (2.5 plusImaginary: -3.5).
275 c2: (-35 plusImaginary: 4.75).
277 t assert: (c1 - c2 isCloseTo: (37.5 plusImaginary: -8.25)).
278 t assert: (c2 - c1 isCloseTo: (-37.5 plusImaginary: 8.25)).
279 t assert: (c1 - 5.3 isCloseTo: (-2.8 plusImaginary: -3.5)).
280 t assert: (c2 - 3 isCloseTo: (-38 plusImaginary: 4.75)).
281 t assert: (3 - c2 isCloseTo: (38 plusImaginary: -4.75)).
284 t@(UnitTests Numeric traits) testComplexMultiplication
286 c1: (2.5 plusImaginary: -3.5).
287 c2: (-35 plusImaginary: 4.75).
289 t assert: (c1 * c2 isCloseTo: (-70.875 plusImaginary: 134.375)).
290 t assert: (c2 * c1 isCloseTo: (-70.875 plusImaginary: 134.375)).
291 t assert: (c1 * 5.3 isCloseTo: (13.25 plusImaginary: -18.55)).
292 t assert: (c2 * 3 isCloseTo: (-105 plusImaginary: 14.25)).
293 t assert: (3 * c2 isCloseTo: (-105 plusImaginary: 14.25)).
296 t@(UnitTests Numeric traits) testComplexDivision
298 c1: (2.5 plusImaginary: -3.5).
299 c2: (-35 plusImaginary: 4.75).
301 t assert: (c1 / c2 isCloseTo: (-0.0834627 plusImaginary: 0.0886729)).
302 t assert: (c2 / c1 isCloseTo: (-5.628378 plusImaginary: -5.979729)).
303 t assert: (c1 / 5.3 isCloseTo: (0.471698 plusImaginary: -0.660377)).
304 t assert: (c2 / 3 isCloseTo: (-11.666667 plusImaginary: 1.583333)).
305 t assert: (3 / c2 isCloseTo: (-0.0841641 plusImaginary: -0.0114222)).
315 t@(UnitTests Numeric traits) suite
316 [t suiteForSelectors: {
320 #testIntegerAddition.
321 #testIntegerSubtraction.
322 #testIntegerMultiplication.
323 #testIntegerDivision.
324 #testIntegerEquality.
325 #testFractionAddition.
326 #testFractionSubtraction.
327 #testFractionMultiplication.
328 #testFractionDivision.
330 #testFloatSubtraction.
331 #testFloatMultiplication.
334 #testComplexAddition.
335 #testComplexSubtraction.
336 #testComplexMultiplication.
337 #testComplexDivision.