Skip to content

Commit

Permalink
Merge pull request #256 from PolyMathOrg/refactor-complex-number-tests
Browse files Browse the repository at this point in the history
Refactor complex number tests: Grouped all arithmetic tests into a clearly named category, added missing interesting test
  • Loading branch information
SergeStinckwich committed Apr 25, 2022
2 parents c799e39 + f368cca commit 8070e6f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Math-Complex/PMComplex.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ PMComplex >> arcTan: denominator [
res]
]

{ #category : #arithmetic }
{ #category : #'polar coordinates' }
PMComplex >> arg [
"Answer the argument of the receiver."

Expand Down
14 changes: 7 additions & 7 deletions src/Math-Complex/PMComplex.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ PMComplex >> productWithVector: aVector [
^ aVector collect: [ :each | each * self ]
]

{ #category : #'*Math-Complex' }
PMComplex >> random [
"analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced."
^ self class random * self

]

{ #category : #'*Math-Complex' }
PMComplex class >> random [
"Answers a random number with abs between 0 and 1."

^ self abs: 1.0 random arg: 2 * Float pi random
]

{ #category : #'*Math-Complex' }
PMComplex >> random [
"analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced."
^ self class random * self

]

{ #category : #'*Math-Complex' }
PMComplex >> subtractToPolynomial: aPolynomial [
^ aPolynomial addNumber: self negated
Expand Down
67 changes: 36 additions & 31 deletions src/Math-Tests-Complex/PMComplexTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ PMComplexTest >> testAdaptToCollectionAndSend [
self assert: (arr * c at: 2) equals: c
]

{ #category : #tests }
PMComplexTest >> testAddPolynomial [
{ #category : #'testing - arithmetic' }
PMComplexTest >> testAddingToAPolynomial [
| c poly |
c := 6 - 6 i.
poly := PMPolynomial coefficients: #(1 1 1).
self assert: (poly + c at: 0) equals: 7 - 6 i.
self assert: (c + poly at: 0) equals: 7 - 6 i
]

{ #category : #tests }
PMComplexTest >> testAdding [
{ #category : #'testing - arithmetic' }
PMComplexTest >> testAddingTwoComplexNumbers [
"self run: #testAdding"

| c |
Expand Down Expand Up @@ -247,7 +247,7 @@ PMComplexTest >> testComplexCollection [
do: [ :one :two | self assert: 2 * one equals: two ]
]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testComplexConjugate [

self assert: (5 - 6i) complexConjugate equals: (5 + 6i).
Expand Down Expand Up @@ -341,16 +341,8 @@ PMComplexTest >> testCreation [
self assert: c imaginary equals: 5
]

{ #category : #tests }
PMComplexTest >> testDividingPolynomial [
| c poly |
c := 4 + 4 i.
poly := PMPolynomial coefficients: #(1 0 1).
self assert: poly / c equals: 1 / c * poly
]

{ #category : #tests }
PMComplexTest >> testDivision1 [
{ #category : #'testing - arithmetic' }
PMComplexTest >> testDividingALargeComplexNumbersByItself [
"self run: #testDivision1"
"self debug: #testDivision1"

Expand All @@ -367,6 +359,14 @@ PMComplexTest >> testDivision1 [

]

{ #category : #'testing - arithmetic' }
PMComplexTest >> testDividingPolynomialByAComplexNumber [
| c poly |
c := 4 + 4 i.
poly := PMPolynomial coefficients: #(1 0 1).
self assert: poly / c equals: 1 / c * poly
]

{ #category : #'testing - equality' }
PMComplexTest >> testEqualsIsReflexive [
| z |
Expand Down Expand Up @@ -479,14 +479,23 @@ PMComplexTest >> testLog [
self assert: (2 + 0 i log: 2) equals: 1
]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testMultiplyByI [
| c |
c := 5 - 6 i.
self assert: c * 1 i equals: c i
]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testMultiplyingByPolynomials [
| c poly |
c := 1 + 1 i.
poly := PMPolynomial coefficients: #(1).
self assert: (c * poly at: 0) equals: c.
self assert: (poly * c at: 0) equals: c
]

{ #category : #'testing - arithmetic' }
PMComplexTest >> testNegated [
"self run: #testNegated"

Expand Down Expand Up @@ -585,7 +594,7 @@ PMComplexTest >> testRandom [
self assert: r abs < c abs
]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testReciprocal [
"self run: #testReciprocal"

Expand All @@ -607,7 +616,7 @@ PMComplexTest >> testReciprocalError [

]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testSecureDivision1 [
"self run: #testSecureDivision1"
"self debug: #testSecureDivision1"
Expand All @@ -620,7 +629,7 @@ PMComplexTest >> testSecureDivision1 [

]

{ #category : #tests }
{ #category : #'testing - arithmetic' }
PMComplexTest >> testSecureDivision2 [
"self run: #testSecureDivision2"
"self debug: #testSecureDivision2"
Expand Down Expand Up @@ -792,8 +801,8 @@ PMComplexTest >> testSquared [
self assert: c2 real equals: 0
]

{ #category : #tests }
PMComplexTest >> testSubtractToPolynomial [
{ #category : #'testing - arithmetic' }
PMComplexTest >> testSubtractingPolynomials [
| c poly |
poly := PMPolynomial coefficients: #(1 2 3).
c := 1 + 3 i.
Expand Down Expand Up @@ -825,15 +834,6 @@ PMComplexTest >> testTanh [
self assert: (c2 imaginary closeTo: c tanh imaginary).
]

{ #category : #tests }
PMComplexTest >> testTimesPolynomial [
| c poly |
c := 1 + 1 i.
poly := PMPolynomial coefficients: #(1).
self assert: (c * poly at: 0) equals: c.
self assert: (poly * c at: 0) equals: c
]

{ #category : #'testing - equality' }
PMComplexTest >> testTwoComplexNumbersWithDifferentImaginaryPartsAreNotEqual [
| z w |
Expand Down Expand Up @@ -861,6 +861,11 @@ PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFracti
self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5).
]

{ #category : #'testing - arithmetic' }
PMComplexTest >> testWeCannotTakeReciprocalOfZeroComplexNumbers [
self should: [ PMComplex zero reciprocal ] raise: ZeroDivide.
]

{ #category : #'testing - expressing complex numbers' }
PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [

Expand Down

0 comments on commit 8070e6f

Please sign in to comment.