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