1743
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
1447
|
3 |
"{ Package: 'stx:goodies/regression' }"
|
192
|
4 |
|
|
5 |
"{ NameSpace: RegressionTests }"
|
|
6 |
|
|
7 |
TestCase subclass:#ComplexTest
|
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
1066
|
11 |
category:'tests-Regression-Numbers'
|
192
|
12 |
!
|
|
13 |
|
|
14 |
|
566
|
15 |
!ComplexTest class methodsFor:'accessing'!
|
|
16 |
|
|
17 |
testedClasses
|
|
18 |
"for the browser and for coverage analysis:
|
|
19 |
return a collection of classNames, which are tested by this testCase"
|
|
20 |
|
|
21 |
^ #( Complex )
|
|
22 |
! !
|
|
23 |
|
192
|
24 |
!ComplexTest methodsFor:'tests'!
|
|
25 |
|
308
|
26 |
testAbs
|
|
27 |
| c |
|
|
28 |
|
|
29 |
c := (6 - 6 i).
|
|
30 |
self assert: c abs = 72 sqrt.
|
|
31 |
|
|
32 |
"
|
|
33 |
self new testAbs
|
|
34 |
|
|
35 |
self run: #testAbs
|
|
36 |
self debug: #testAbs
|
|
37 |
"
|
|
38 |
!
|
192
|
39 |
|
308
|
40 |
testAddition
|
|
41 |
| c1 c2 c3 |
|
|
42 |
|
|
43 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
44 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
|
45 |
|
|
46 |
self should: [ (c1 + c2) = (Complex real: 4 imaginary: 6) ].
|
192
|
47 |
|
308
|
48 |
c3 := (5 - 6 i) + (-5 + 8 i). "Complex with Complex"
|
566
|
49 |
self assert: (c3 = (0 + 2 i)).
|
|
50 |
|
|
51 |
self assert: (c1 + 1) = (2 + 2 i).
|
|
52 |
self assert: (c1 + 1.0) = (2 + 2 i).
|
|
53 |
self assert: (1 + c1) = (2 + 2 i).
|
|
54 |
self assert: (1.0 + c1) = (2 + 2 i).
|
308
|
55 |
|
|
56 |
"
|
|
57 |
self run: #testAddition
|
|
58 |
self new testAddition
|
|
59 |
"
|
192
|
60 |
!
|
|
61 |
|
566
|
62 |
testAngle
|
|
63 |
| c |
|
|
64 |
|
|
65 |
c := (1 + 1 i).
|
|
66 |
self assert: (c angle radiansToDegrees rounded = 45).
|
|
67 |
self assert: (c asPoint x = 1).
|
|
68 |
self assert: (c asPoint y = 1).
|
|
69 |
|
|
70 |
c := (-1 + 1 i).
|
|
71 |
self assert: (c angle radiansToDegrees rounded = 135).
|
|
72 |
self assert: (c asPoint x = -1).
|
|
73 |
self assert: (c asPoint y = 1).
|
|
74 |
|
|
75 |
c := (-1 - 1 i).
|
|
76 |
self assert: (c angle radiansToDegrees rounded = -135).
|
|
77 |
self assert: (c asPoint x = -1).
|
|
78 |
self assert: (c asPoint y = -1).
|
|
79 |
|
|
80 |
"
|
|
81 |
self new testAngle
|
|
82 |
|
|
83 |
self run: #testAngle
|
|
84 |
self debug: #testAngle
|
|
85 |
"
|
|
86 |
!
|
|
87 |
|
|
88 |
testCompare
|
|
89 |
| c1 c2 |
|
|
90 |
|
|
91 |
c1 := Complex real: 1 imaginary: 2.
|
|
92 |
c2 := Complex real: 3 imaginary: 4.
|
|
93 |
|
|
94 |
self assert: ( c1 ~= c2 ).
|
|
95 |
self assert: ( c1 = c2 ) not.
|
|
96 |
|
|
97 |
self should: [ c1 < c2 ] raise: Exception.
|
|
98 |
self should: [ c1 <= c2 ] raise: Exception.
|
|
99 |
self should: [ c1 >= c2 ] raise: Exception.
|
|
100 |
self should: [ c1 > c2 ] raise: Exception.
|
|
101 |
|
|
102 |
c1 := Complex fromReal: 1.
|
|
103 |
|
|
104 |
self assert: ( c1 = 1 ).
|
|
105 |
self assert: ( c1 = 1.0 ).
|
|
106 |
self assert: ( c1 ~= 2 ).
|
|
107 |
self assert: ( c1 ~= 2.0 ).
|
|
108 |
|
|
109 |
self assert: ( 1 = c1 ).
|
|
110 |
self assert: ( 1.0 = c1 ).
|
|
111 |
self assert: ( 2 ~= c1 ).
|
|
112 |
self assert: ( 2.0 ~= c1 ).
|
|
113 |
!
|
|
114 |
|
|
115 |
testConjugated
|
|
116 |
| c |
|
|
117 |
|
|
118 |
c := (2 + 5 i) .
|
|
119 |
self assert: c conjugated = (2 - 5i).
|
|
120 |
|
|
121 |
"
|
|
122 |
self run: #testConjugated
|
|
123 |
self new testConjugated
|
|
124 |
"
|
|
125 |
!
|
|
126 |
|
|
127 |
testConversion
|
|
128 |
| c |
|
|
129 |
|
|
130 |
c := Complex fromReal:1.
|
|
131 |
self assert: (c asComplex = c).
|
|
132 |
self assert: (c asFloat = 1).
|
|
133 |
self assert: (c asInteger = 1).
|
|
134 |
self assert: (c asPoint = (1@0) ).
|
|
135 |
|
|
136 |
c := (1 + 1 i).
|
|
137 |
self assert: (c asComplex = c).
|
|
138 |
self should:[ c asFloat ] raise:Exception.
|
|
139 |
self should:[ c asInteger ] raise:Exception.
|
|
140 |
self assert: (c isComplex ).
|
|
141 |
self assert: (c asPoint = (1@1) ).
|
|
142 |
|
|
143 |
"
|
|
144 |
self new testConversion
|
|
145 |
|
|
146 |
self run: #testConversion
|
|
147 |
self debug: #testConversion
|
|
148 |
"
|
|
149 |
!
|
|
150 |
|
192
|
151 |
testCreation
|
308
|
152 |
| c |
|
|
153 |
|
|
154 |
false ifTrue:[
|
1447
|
155 |
"cg: I dont think this has to be known outside complex"
|
|
156 |
"i.e. should it really be allowed to create complex numbers with new ?"
|
|
157 |
self should: [ (c := Complex new) realPart = 0 ].
|
|
158 |
self should: [ c imaginaryPart = 0 ].
|
308
|
159 |
].
|
|
160 |
self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ].
|
|
161 |
self should: [ c imaginaryPart = 2 ].
|
|
162 |
|
|
163 |
self should: [ (c := Complex real: -1 imaginary: 2) realPart = -1 ].
|
|
164 |
self should: [ c imaginaryPart = 2 ].
|
|
165 |
|
|
166 |
self should: [ (c := Complex real: 1 imaginary: -2) realPart = 1 ].
|
|
167 |
self should: [ c imaginaryPart = -2 ].
|
|
168 |
|
|
169 |
self should: [ (c := Complex real: -1 imaginary: -2) realPart = -1 ].
|
|
170 |
self should: [ c imaginaryPart = -2 ].
|
|
171 |
|
1447
|
172 |
c := (1 % 2).
|
308
|
173 |
self assert: (c real = 1).
|
|
174 |
self assert: (c imaginary = 2).
|
|
175 |
|
1447
|
176 |
c := (-1 % 2).
|
308
|
177 |
self assert: (c real = -1).
|
|
178 |
self assert: (c imaginary = 2).
|
|
179 |
|
1447
|
180 |
c := (1 % -2).
|
308
|
181 |
self assert: (c real = 1).
|
|
182 |
self assert: (c imaginary = -2).
|
|
183 |
|
1447
|
184 |
c := (-1 % -2).
|
308
|
185 |
self assert: (c real = -1).
|
|
186 |
self assert: (c imaginary = -2).
|
192
|
187 |
|
1447
|
188 |
c := (1.0 % 2.0).
|
308
|
189 |
self assert: (c real = 1.0).
|
|
190 |
self assert: (c imaginary = 2.0).
|
|
191 |
|
1447
|
192 |
c := (-1.0 % 2.0).
|
308
|
193 |
self assert: (c real = -1.0).
|
|
194 |
self assert: (c imaginary = 2.0).
|
|
195 |
|
1447
|
196 |
c := (1.0 % -2.0).
|
308
|
197 |
self assert: (c real = 1.0).
|
|
198 |
self assert: (c imaginary = -2.0).
|
|
199 |
|
1447
|
200 |
c := (-1.0 % -2.0).
|
308
|
201 |
self assert: (c real = -1.0).
|
|
202 |
self assert: (c imaginary = -2.0).
|
|
203 |
|
|
204 |
c := 5 i.
|
|
205 |
self assert: (c real = 0).
|
|
206 |
self assert: (c imaginary = 5).
|
1447
|
207 |
|
308
|
208 |
c := 6 + 7 i.
|
|
209 |
self assert: (c real = 6).
|
|
210 |
self assert: ( c imaginary = 7).
|
1447
|
211 |
|
308
|
212 |
c := 5.6 - 8 i.
|
|
213 |
self assert: (c real = 5.6).
|
|
214 |
self assert: (c imaginary = -8).
|
1447
|
215 |
|
308
|
216 |
c := Complex real: 10 imaginary: 5.
|
|
217 |
self assert: (c real = 10).
|
|
218 |
self assert: (c imaginary = 5).
|
1447
|
219 |
|
308
|
220 |
c := Complex abs: 5 arg: (Float pi/2).
|
|
221 |
self assert: (c real rounded = 0).
|
|
222 |
self assert: (c imaginary = 5).
|
|
223 |
|
|
224 |
"
|
|
225 |
self run: #testCreation
|
|
226 |
self new testCreation
|
|
227 |
"
|
192
|
228 |
!
|
|
229 |
|
|
230 |
testDivision
|
|
231 |
| c1 c2 |
|
|
232 |
|
|
233 |
self shouldnt: [ c1 := Complex real: 2 imaginary: 2 ] raise: Exception.
|
|
234 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 6 ] raise: Exception.
|
|
235 |
|
|
236 |
self should: [ (c1 / c1) = (Complex real: 1 imaginary: 0) ].
|
|
237 |
self should: [ (c1 / c2) = (Complex real: 2/5 imaginary: (-2/15)) ].
|
|
238 |
self should: [ (c2 / c1) = (Complex real: 9/4 imaginary: 3/4) ].
|
|
239 |
|
|
240 |
self should: [ c2 / 3 = (Complex real: 1 imaginary: 2) ].
|
|
241 |
self should: [ c1 / 2 = (Complex real: 1 imaginary: 1) ].
|
|
242 |
!
|
|
243 |
|
|
244 |
testEquality
|
308
|
245 |
self should: [ 3 = (Complex real: 3 imaginary: 0) ].
|
|
246 |
self should: [ (Complex real: 3 imaginary: 0) = 3 ].
|
|
247 |
|
|
248 |
self should: [ 3.0 = (Complex real: 3 imaginary: 0) ].
|
|
249 |
self should: [ (Complex real: 3 imaginary: 0) = 3.0 ].
|
192
|
250 |
|
308
|
251 |
self shouldnt: [ 3 = (Complex real: 3 imaginary: 1) ].
|
|
252 |
self shouldnt: [ (Complex real: 3 imaginary: 1) = 3 ].
|
192
|
253 |
|
308
|
254 |
self shouldnt: [ 3.0 = (Complex real: 3 imaginary: 1) ].
|
|
255 |
self shouldnt: [ (Complex real: 3 imaginary: 1) = 3.0 ].
|
192
|
256 |
|
308
|
257 |
self should: [ 3 ~= (Complex real: 3 imaginary: 1) ].
|
|
258 |
self should: [ (Complex real: 3 imaginary: 1) ~= 3 ].
|
|
259 |
|
|
260 |
self should: [ 3.0 ~= (Complex real: 3 imaginary: 1) ].
|
|
261 |
self should: [ (Complex real: 3 imaginary: 1) ~= 3.0 ].
|
192
|
262 |
|
308
|
263 |
self assert: 0i = 0.
|
1743
|
264 |
self assert: (2 - 5i) = ((1 - 4 i) + (1 - 1i)).
|
308
|
265 |
self assert: 0i isZero.
|
|
266 |
self deny: (1 + 3 i) = 1.
|
|
267 |
self deny: (1 + 3 i) = (1 + 2i).
|
192
|
268 |
|
308
|
269 |
"
|
|
270 |
self run: #testEquality
|
|
271 |
self new testEquality
|
|
272 |
"
|
192
|
273 |
!
|
|
274 |
|
734
|
275 |
testFloorCeiling
|
|
276 |
| c1 c2 |
|
|
277 |
|
|
278 |
c1 := Complex real: 1 imaginary: 2.
|
|
279 |
c2 := Complex real: 3 imaginary: 4.
|
|
280 |
|
|
281 |
self should: [ c1 floor ] raise:Error.
|
|
282 |
self should: [ c1 ceiling ] raise:Error.
|
|
283 |
|
|
284 |
"Created: / 02-11-2012 / 10:04:43 / cg"
|
|
285 |
!
|
|
286 |
|
192
|
287 |
testModulus
|
|
288 |
| c1 c2 |
|
|
289 |
|
|
290 |
"Test case where |a| < |b| in complex number (a + ib)."
|
|
291 |
|
|
292 |
self shouldnt: [ c1 := Complex real: 2 imaginary: 3 ] raise: Exception.
|
|
293 |
self should: [ c1 modulus = (3 * ((1 + ((2 / 3) * (2 / 3))) sqrt)) ].
|
|
294 |
|
|
295 |
"Test case where |a| >= |b| in complex number (a + ib)."
|
|
296 |
|
|
297 |
self shouldnt: [ c2 := Complex real: 4 imaginary: -2 ] raise: Exception.
|
|
298 |
self should: [ c2 modulus = (4 * ((1 + ((-2 / 4) * (-2 / 4))) sqrt)) ].
|
|
299 |
!
|
|
300 |
|
|
301 |
testMultiplication
|
566
|
302 |
| c1 c2 |
|
192
|
303 |
|
566
|
304 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
305 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
192
|
306 |
|
566
|
307 |
self should: [ (c1 * c2) = (Complex real: -5 imaginary: 10) ].
|
|
308 |
self should: [ (c1 * Complex zero) = Complex zero ].
|
192
|
309 |
|
566
|
310 |
self should: [ c1 * 5 = (Complex real: 5 imaginary: 10) ].
|
|
311 |
self should: [ c1 * 1.1 = (Complex real: 1.1 imaginary: 2.2) ].
|
|
312 |
self should: [ c1 * (2/3) = (Complex real: 2/3 imaginary: 4/3) ].
|
|
313 |
|
|
314 |
self assert:( 1.0 * c1 = c1 ).
|
192
|
315 |
!
|
|
316 |
|
308
|
317 |
testNegated
|
|
318 |
| c |
|
|
319 |
c := (2 + 5 i) .
|
|
320 |
self assert: c negated = (-2 - 5i).
|
|
321 |
|
|
322 |
"
|
|
323 |
self run: #testNegated
|
|
324 |
self new testNegated
|
|
325 |
"
|
|
326 |
!
|
|
327 |
|
192
|
328 |
testRaisedTo
|
1447
|
329 |
| c |
|
192
|
330 |
|
1447
|
331 |
self shouldnt: [ c := Complex real: 3 imaginary: 2 ] raise: Exception.
|
|
332 |
self assert: ((c raisedTo: 2) = (c * c)).
|
|
333 |
self assert: ((c raisedTo: 3) = (c * c * c)).
|
|
334 |
self assert: ((c raisedTo: 4) = (c * c * c * c)).
|
192
|
335 |
!
|
|
336 |
|
308
|
337 |
testReciprocal
|
|
338 |
| c |
|
|
339 |
c := (2 + 5 i).
|
|
340 |
self assert: c reciprocal = ((2/29) - (5/29)i).
|
1447
|
341 |
|
308
|
342 |
"
|
|
343 |
self run: #testReciprocal
|
|
344 |
self new testReciprocal
|
|
345 |
"
|
|
346 |
!
|
|
347 |
|
|
348 |
testReciprocalError
|
|
349 |
| c |
|
|
350 |
c := (0 i).
|
1447
|
351 |
self should: [c reciprocal] raise: ZeroDivide
|
308
|
352 |
"
|
|
353 |
self run: #testReciprocalError
|
|
354 |
self new testReciprocalError
|
|
355 |
"
|
|
356 |
!
|
|
357 |
|
192
|
358 |
testSqrt
|
1447
|
359 |
| c w |
|
192
|
360 |
|
1447
|
361 |
self shouldnt: [ c := Complex real: 0 imaginary: 0 ] raise: Exception.
|
|
362 |
self should: [ c sqrt = 0 ].
|
192
|
363 |
|
1447
|
364 |
self shouldnt: [ c := Complex real: 9 imaginary: 4 ] raise: Exception.
|
|
365 |
"cg: that cannot be tested easily, due to rounding errors"
|
|
366 |
"original" false ifTrue:[
|
|
367 |
self should: [ w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt).
|
|
368 |
c sqrt = (Complex real: w imaginary: 4 / (2 * w)) ].
|
|
369 |
] ifFalse:[
|
|
370 |
self should: [ |t1 t2 epsilon|
|
|
371 |
w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt).
|
|
372 |
t1 := c sqrt.
|
|
373 |
t2 := (Complex real: w imaginary: 4 / (2 * w)).
|
|
374 |
epsilon := 0.0000001.
|
|
375 |
(t1 realPart - t2 realPart) < epsilon
|
|
376 |
and:[ (t1 imaginaryPart - t2 imaginaryPart) < epsilon ]
|
|
377 |
].
|
|
378 |
].
|
192
|
379 |
|
1447
|
380 |
self shouldnt: [ c := Complex imaginary: -2 ] raise: Exception.
|
|
381 |
"self should: [ c sqrt = (Complex real: 1 imaginary: -1) ]. Should be true, but rounding bites us..."
|
|
382 |
self should: [ (c sqrt realPart - 1) abs < 1.0e-10 ].
|
|
383 |
self should: [ (c sqrt imaginaryPart + 1) abs < 1.0e-10 ].
|
192
|
384 |
!
|
|
385 |
|
308
|
386 |
testSquared
|
|
387 |
| c c2 |
|
|
388 |
c := (6 - 6 i).
|
|
389 |
c2 := (c squared).
|
|
390 |
self assert: c2 imaginary = -72.
|
|
391 |
self assert: c2 real = 0.
|
|
392 |
|
|
393 |
"
|
|
394 |
self new testSquared
|
|
395 |
|
|
396 |
self run: #testSquared
|
|
397 |
self debug: #testSquared
|
|
398 |
"
|
|
399 |
!
|
|
400 |
|
192
|
401 |
testSubtraction
|
566
|
402 |
| c1 c2 |
|
|
403 |
|
|
404 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
405 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
192
|
406 |
|
566
|
407 |
self should: [ (c1 - c2) = (Complex real: -2 imaginary: -2) ].
|
192
|
408 |
|
566
|
409 |
self assert: ( (c1 - 1.0) = (Complex real: 0 imaginary: 2) ).
|
|
410 |
self assert: ( (1.0 - c1) = (Complex real: 0 imaginary: -2) ).
|
|
411 |
self assert: ( (1.0 - (Complex fromReal:1.0)) = 0 ).
|
308
|
412 |
!
|
|
413 |
|
|
414 |
xxtestSecureDivision1
|
|
415 |
| c1 c2 quotient |
|
|
416 |
c1 := 2.0e252 + 3.0e70 i.
|
|
417 |
c2 := c1.
|
|
418 |
quotient := c1 divideSecureBy: c2.
|
1447
|
419 |
self assert: (quotient - 1) isZero.
|
308
|
420 |
|
|
421 |
"
|
|
422 |
self run: #testSecureDivision1
|
|
423 |
self new testSecureDivision1
|
|
424 |
"
|
|
425 |
!
|
|
426 |
|
|
427 |
xxtestSecureDivision2
|
|
428 |
| c1 c2 quotient |
|
|
429 |
c1 := 2.0e252 + 3.0e70 i.
|
|
430 |
c2 := c1.
|
|
431 |
quotient := c1 divideFastAndSecureBy: c2.
|
|
432 |
self assert: (quotient - 1) isZero.
|
|
433 |
|
|
434 |
"
|
|
435 |
self run: #testSecureDivision2
|
|
436 |
self new testSecureDivision2
|
|
437 |
"
|
192
|
438 |
! !
|
|
439 |
|
|
440 |
!ComplexTest class methodsFor:'documentation'!
|
|
441 |
|
|
442 |
version
|
|
443 |
^ '$Header$'
|
566
|
444 |
!
|
|
445 |
|
|
446 |
version_CVS
|
|
447 |
^ '$Header$'
|
192
|
448 |
! !
|
1066
|
449 |
|