more tests (from squeak)
authorClaus Gittinger <cg@exept.de>
Fri, 17 Feb 2006 12:30:27 +0100
changeset 308 0113f7be0cf9
parent 307 3999aae795d5
child 309 f5c2d68a0ad2
more tests (from squeak)
RegressionTests__ComplexTest.st
--- a/RegressionTests__ComplexTest.st	Tue Feb 14 18:59:15 2006 +0100
+++ b/RegressionTests__ComplexTest.st	Fri Feb 17 12:30:27 2006 +0100
@@ -12,26 +12,114 @@
 
 !ComplexTest methodsFor:'tests'!
 
-testAddition
-	| c1 c2 |
+testAbs
+    | c |
+
+    c := (6 - 6 i).
+    self assert: c abs  = 72 sqrt.
+
+    "
+     self new testAbs
+
+     self run: #testAbs
+     self debug: #testAbs
+    "
+!
 
-	self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
-	self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
+testAddition
+    | c1 c2 c3 |
+
+    self shouldnt: [ c1 := Complex real: 1 imaginary: 2 ] raise: Exception.
+    self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
+
+    self should: [ (c1 + c2) = (Complex real: 4 imaginary: 6) ].
 
-	self should: [ (c1 + c2) = (Complex real: 4 imaginary: 6) ].
+    c3 := (5 - 6 i) + (-5 + 8 i).     "Complex with Complex"
+    self assert: (c3 =  (0 + 2 i)).
+
+    "
+     self run: #testAddition
+     self new testAddition
+    "
 !
 
 testCreation
-        | c |
+    | c |
+
+    false ifTrue:[
+        "cg: I dont think this has to be known outside complex"
+        "i.e. should it really be allowed to create complex numbers with new ?"
+        self should: [ (c := Complex new) realPart = 0 ].
+        self should: [ c imaginaryPart = 0 ].
+    ].
+    self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ].
+    self should: [ c imaginaryPart = 2 ].
+
+    self should: [ (c := Complex real: -1 imaginary: 2) realPart = -1 ].
+    self should: [ c imaginaryPart = 2 ].
+
+    self should: [ (c := Complex real: 1 imaginary: -2) realPart = 1 ].
+    self should: [ c imaginaryPart = -2 ].
+
+    self should: [ (c := Complex real: -1 imaginary: -2) realPart = -1 ].
+    self should: [ c imaginaryPart = -2 ].
+
+    c := (1 % 2).   
+    self assert: (c real = 1).
+    self assert: (c imaginary = 2).
+
+    c := (-1 % 2).   
+    self assert: (c real = -1).
+    self assert: (c imaginary = 2).
+
+    c := (1 % -2).   
+    self assert: (c real = 1).
+    self assert: (c imaginary = -2).
+
+    c := (-1 % -2).   
+    self assert: (c real = -1).
+    self assert: (c imaginary = -2).
 
-        false ifTrue:[
-            "cg: I dont think this has to be known outside complex"
-            "i.e. should it really be allowed to create complex numbers with new ?"
-            self should: [ (c := Complex new) realPart = 0 ].
-            self should: [ c imaginaryPart = 0 ].
-        ].
-        self should: [ (c := Complex real: 1 imaginary: 2) realPart = 1 ].
-        self should: [ c imaginaryPart = 2 ]
+    c := (1.0 % 2.0).   
+    self assert: (c real = 1.0).
+    self assert: (c imaginary = 2.0).
+
+    c := (-1.0 % 2.0).   
+    self assert: (c real = -1.0).
+    self assert: (c imaginary = 2.0).
+
+    c := (1.0 % -2.0).   
+    self assert: (c real = 1.0).
+    self assert: (c imaginary = -2.0).
+
+    c := (-1.0 % -2.0).   
+    self assert: (c real = -1.0).
+    self assert: (c imaginary = -2.0).
+
+    c := 5 i.
+    self assert: (c real = 0).
+    self assert: (c imaginary = 5).
+    
+    c := 6 + 7 i.
+    self assert: (c real = 6).
+    self assert: ( c imaginary = 7).
+    
+    c := 5.6 - 8 i.
+    self assert: (c real = 5.6).
+    self assert: (c imaginary = -8).
+    
+    c := Complex real: 10 imaginary: 5.
+    self assert: (c real = 10).
+    self assert: (c imaginary = 5).
+    
+    c := Complex abs: 5 arg: (Float pi/2).
+    self assert: (c real rounded = 0).
+    self assert: (c imaginary = 5).
+
+    "
+     self run: #testCreation
+     self new testCreation
+    "
 !
 
 testDivision
@@ -49,23 +137,34 @@
 !
 
 testEquality
-	self should: [ 3 = (Complex real: 3 imaginary: 0) ].
-	self should: [ (Complex real: 3 imaginary: 0) = 3 ].
+    self should: [ 3 = (Complex real: 3 imaginary: 0) ].
+    self should: [ (Complex real: 3 imaginary: 0) = 3 ].
+
+    self should: [ 3.0 = (Complex real: 3 imaginary: 0) ].
+    self should: [ (Complex real: 3 imaginary: 0) = 3.0 ].
 
-	self should: [ 3.0 = (Complex real: 3 imaginary: 0) ].
-	self should: [ (Complex real: 3 imaginary: 0) = 3.0 ].
+    self shouldnt: [ 3 = (Complex real: 3 imaginary: 1) ].
+    self shouldnt: [ (Complex real: 3 imaginary: 1) = 3 ].
 
-	self shouldnt: [ 3 = (Complex real: 3 imaginary: 1) ].
-	self shouldnt: [ (Complex real: 3 imaginary: 1) = 3 ].
+    self shouldnt: [ 3.0 = (Complex real: 3 imaginary: 1) ].
+    self shouldnt: [ (Complex real: 3 imaginary: 1) = 3.0 ].
 
-	self shouldnt: [ 3.0 = (Complex real: 3 imaginary: 1) ].
-	self shouldnt: [ (Complex real: 3 imaginary: 1) = 3.0 ].
+    self should: [ 3 ~= (Complex real: 3 imaginary: 1) ].
+    self should: [ (Complex real: 3 imaginary: 1) ~= 3 ].
+
+    self should: [ 3.0 ~= (Complex real: 3 imaginary: 1) ].
+    self should: [ (Complex real: 3 imaginary: 1) ~= 3.0 ].
 
-	self should: [ 3 ~= (Complex real: 3 imaginary: 1) ].
-	self should: [ (Complex real: 3 imaginary: 1) ~= 3 ].
+    self assert: 0i = 0.
+    self assert: (2 - 5i) = ((1 -4 i) + (1 - 1i)).
+    self assert: 0i isZero.
+    self deny: (1 + 3 i) = 1.
+    self deny: (1 + 3 i) = (1 + 2i).
 
-	self should: [ 3.0 ~= (Complex real: 3 imaginary: 1) ].
-	self should: [ (Complex real: 3 imaginary: 1) ~= 3.0 ].
+    "
+     self run: #testEquality
+     self new testEquality
+    "
 !
 
 testModulus
@@ -96,6 +195,17 @@
 	self should: [ c1 * (2/3) = (Complex real: 2/3 imaginary: 4/3) ].
 !
 
+testNegated
+    | c |
+    c := (2 + 5 i) .
+    self assert: c negated  = (-2 - 5i).
+
+    "
+     self run: #testNegated
+     self new testNegated
+    "
+!
+
 testRaisedTo
         | c |
 
@@ -105,6 +215,27 @@
         self assert:  ((c raisedTo: 4) = (c * c * c * c)).
 !
 
+testReciprocal
+    | c |
+    c := (2 + 5 i).
+    self assert: c reciprocal  = ((2/29) - (5/29)i).
+        
+    "
+     self run: #testReciprocal
+     self new testReciprocal
+    "
+!
+
+testReciprocalError
+    | c |
+    c := (0 i).
+        self should: [c reciprocal] raise: ZeroDivide        
+    "
+     self run: #testReciprocalError
+     self new testReciprocalError
+    "
+!
+
 testSqrt
         | c w |
 
@@ -133,6 +264,21 @@
         self should: [ (c sqrt imaginaryPart + 1) abs < 1.0e-10 ].
 !
 
+testSquared
+    | c c2 |
+    c := (6 - 6 i).
+    c2 := (c squared).
+    self assert: c2 imaginary = -72.
+    self assert: c2 real = 0.
+
+    "
+     self new testSquared
+
+     self run: #testSquared
+     self debug: #testSquared
+    "
+!
+
 testSubtraction
 	| c1 c2 |
 
@@ -140,6 +286,32 @@
 	self shouldnt: [ c2 := Complex real: 3 imaginary: 4 ] raise: Exception.
 
 	self should: [ (c1 - c2) = (Complex real: -2 imaginary: -2) ].
+!
+
+xxtestSecureDivision1
+    | c1 c2 quotient |
+    c1 := 2.0e252 + 3.0e70 i.
+    c2 := c1.
+    quotient := c1 divideSecureBy: c2.
+    self assert: (quotient - 1) isZero.    
+
+    "
+     self run: #testSecureDivision1
+     self new testSecureDivision1
+    "
+!
+
+xxtestSecureDivision2
+    | c1 c2 quotient |
+    c1 := 2.0e252 + 3.0e70 i.
+    c2 := c1.
+    quotient := c1 divideFastAndSecureBy: c2.
+    self assert: (quotient - 1) isZero.
+
+    "
+     self run: #testSecureDivision2
+     self new testSecureDivision2
+    "
 ! !
 
 !ComplexTest class methodsFor:'documentation'!