192
|
1 |
"{ Package: 'exept:regression' }"
|
|
2 |
|
|
3 |
"{ NameSpace: RegressionTests }"
|
|
4 |
|
|
5 |
TestCase subclass:#ComplexTest
|
|
6 |
instanceVariableNames:''
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'tests-Regression'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!ComplexTest methodsFor:'tests'!
|
|
14 |
|
|
15 |
testAddition
|
|
16 |
| c1 c2 |
|
|
17 |
|
|
18 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
19 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
|
20 |
|
|
21 |
self should: [ (c1 + c2) = (Complex real: 4 imaginary: 6) ].
|
|
22 |
!
|
|
23 |
|
|
24 |
testCreation
|
|
25 |
| c |
|
|
26 |
|
|
27 |
false ifTrue:[
|
|
28 |
"cg: I dont think this has to be known outside complex"
|
|
29 |
"i.e. should it really be allowed to create complex numbers with new ?"
|
|
30 |
self should: [ (c := Complex new) realPart = 0 ].
|
|
31 |
self should: [ c imaginaryPart = 0 ].
|
|
32 |
].
|
|
33 |
self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ].
|
|
34 |
self should: [ c imaginaryPart = 2 ]
|
|
35 |
!
|
|
36 |
|
|
37 |
testDivision
|
|
38 |
| c1 c2 |
|
|
39 |
|
|
40 |
self shouldnt: [ c1 := Complex real: 2 imaginary: 2 ] raise: Exception.
|
|
41 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 6 ] raise: Exception.
|
|
42 |
|
|
43 |
self should: [ (c1 / c1) = (Complex real: 1 imaginary: 0) ].
|
|
44 |
self should: [ (c1 / c2) = (Complex real: 2/5 imaginary: (-2/15)) ].
|
|
45 |
self should: [ (c2 / c1) = (Complex real: 9/4 imaginary: 3/4) ].
|
|
46 |
|
|
47 |
self should: [ c2 / 3 = (Complex real: 1 imaginary: 2) ].
|
|
48 |
self should: [ c1 / 2 = (Complex real: 1 imaginary: 1) ].
|
|
49 |
!
|
|
50 |
|
|
51 |
testEquality
|
|
52 |
self should: [ 3 = (Complex real: 3 imaginary: 0) ].
|
|
53 |
self should: [ (Complex real: 3 imaginary: 0) = 3 ].
|
|
54 |
|
|
55 |
self should: [ 3.0 = (Complex real: 3 imaginary: 0) ].
|
|
56 |
self should: [ (Complex real: 3 imaginary: 0) = 3.0 ].
|
|
57 |
|
|
58 |
self shouldnt: [ 3 = (Complex real: 3 imaginary: 1) ].
|
|
59 |
self shouldnt: [ (Complex real: 3 imaginary: 1) = 3 ].
|
|
60 |
|
|
61 |
self shouldnt: [ 3.0 = (Complex real: 3 imaginary: 1) ].
|
|
62 |
self shouldnt: [ (Complex real: 3 imaginary: 1) = 3.0 ].
|
|
63 |
|
|
64 |
self should: [ 3 ~= (Complex real: 3 imaginary: 1) ].
|
|
65 |
self should: [ (Complex real: 3 imaginary: 1) ~= 3 ].
|
|
66 |
|
|
67 |
self should: [ 3.0 ~= (Complex real: 3 imaginary: 1) ].
|
|
68 |
self should: [ (Complex real: 3 imaginary: 1) ~= 3.0 ].
|
|
69 |
!
|
|
70 |
|
|
71 |
testModulus
|
|
72 |
| c1 c2 |
|
|
73 |
|
|
74 |
"Test case where |a| < |b| in complex number (a + ib)."
|
|
75 |
|
|
76 |
self shouldnt: [ c1 := Complex real: 2 imaginary: 3 ] raise: Exception.
|
|
77 |
self should: [ c1 modulus = (3 * ((1 + ((2 / 3) * (2 / 3))) sqrt)) ].
|
|
78 |
|
|
79 |
"Test case where |a| >= |b| in complex number (a + ib)."
|
|
80 |
|
|
81 |
self shouldnt: [ c2 := Complex real: 4 imaginary: -2 ] raise: Exception.
|
|
82 |
self should: [ c2 modulus = (4 * ((1 + ((-2 / 4) * (-2 / 4))) sqrt)) ].
|
|
83 |
!
|
|
84 |
|
|
85 |
testMultiplication
|
|
86 |
| c1 c2 |
|
|
87 |
|
|
88 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
89 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
|
90 |
|
|
91 |
self should: [ (c1 * c2) = (Complex real: -5 imaginary: 10) ].
|
|
92 |
self should: [ (c1 * Complex zero) = Complex zero ].
|
|
93 |
|
|
94 |
self should: [ c1 * 5 = (Complex real: 5 imaginary: 10) ].
|
|
95 |
self should: [ c1 * 1.1 = (Complex real: 1.1 imaginary: 2.2) ].
|
|
96 |
self should: [ c1 * (2/3) = (Complex real: 2/3 imaginary: 4/3) ].
|
|
97 |
!
|
|
98 |
|
|
99 |
testRaisedTo
|
256
|
100 |
| c |
|
192
|
101 |
|
256
|
102 |
self shouldnt: [ c := Complex real: 3 imaginary: 2 ] raise: Exception.
|
|
103 |
self assert: ((c raisedTo: 2) = (c * c)).
|
|
104 |
self assert: ((c raisedTo: 3) = (c * c * c)).
|
|
105 |
self assert: ((c raisedTo: 4) = (c * c * c * c)).
|
192
|
106 |
!
|
|
107 |
|
|
108 |
testSqrt
|
|
109 |
| c w |
|
|
110 |
|
|
111 |
self shouldnt: [ c := Complex real: 0 imaginary: 0 ] raise: Exception.
|
|
112 |
self should: [ c sqrt = 0 ].
|
|
113 |
|
|
114 |
self shouldnt: [ c := Complex real: 9 imaginary: 4 ] raise: Exception.
|
|
115 |
"cg: that cannot be tested easily, due to rounding errors"
|
|
116 |
"original" false ifTrue:[
|
|
117 |
self should: [ w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt).
|
|
118 |
c sqrt = (Complex real: w imaginary: 4 / (2 * w)) ].
|
|
119 |
] ifFalse:[
|
|
120 |
self should: [ |t1 t2 epsilon|
|
|
121 |
w := 3 * (((1 + (1 + ((4/9) * (4/9))) sqrt) / 2) sqrt).
|
|
122 |
t1 := c sqrt.
|
|
123 |
t2 := (Complex real: w imaginary: 4 / (2 * w)).
|
|
124 |
epsilon := 0.0000001.
|
|
125 |
(t1 realPart - t2 realPart) < epsilon
|
|
126 |
and:[ (t1 imaginaryPart - t2 imaginaryPart) < epsilon ]
|
|
127 |
].
|
|
128 |
].
|
|
129 |
|
|
130 |
self shouldnt: [ c := Complex imaginary: -2 ] raise: Exception.
|
|
131 |
"self should: [ c sqrt = (Complex real: 1 imaginary: -1) ]. Should be true, but rounding bites us..."
|
|
132 |
self should: [ (c sqrt realPart - 1) abs < 1.0e-10 ].
|
|
133 |
self should: [ (c sqrt imaginaryPart + 1) abs < 1.0e-10 ].
|
|
134 |
!
|
|
135 |
|
|
136 |
testSubtraction
|
|
137 |
| c1 c2 |
|
|
138 |
|
|
139 |
self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
|
|
140 |
self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
|
|
141 |
|
|
142 |
self should: [ (c1 - c2) = (Complex real: -2 imaginary: -2) ].
|
|
143 |
! !
|
|
144 |
|
|
145 |
!ComplexTest class methodsFor:'documentation'!
|
|
146 |
|
|
147 |
version
|
|
148 |
^ '$Header$'
|
|
149 |
! !
|