Skip to content

Commit 8070e6f

Browse files
Merge pull request #256 from PolyMathOrg/refactor-complex-number-tests
Refactor complex number tests: Grouped all arithmetic tests into a clearly named category, added missing interesting test
2 parents c799e39 + f368cca commit 8070e6f

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

src/Math-Complex/PMComplex.class.st

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ PMComplex >> arcTan: denominator [
390390
res]
391391
]
392392

393-
{ #category : #arithmetic }
393+
{ #category : #'polar coordinates' }
394394
PMComplex >> arg [
395395
"Answer the argument of the receiver."
396396

src/Math-Complex/PMComplex.extension.st

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ PMComplex >> productWithVector: aVector [
1717
^ aVector collect: [ :each | each * self ]
1818
]
1919

20-
{ #category : #'*Math-Complex' }
21-
PMComplex >> random [
22-
"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."
23-
^ self class random * self
24-
25-
]
26-
2720
{ #category : #'*Math-Complex' }
2821
PMComplex class >> random [
2922
"Answers a random number with abs between 0 and 1."
3023

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

27+
{ #category : #'*Math-Complex' }
28+
PMComplex >> random [
29+
"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."
30+
^ self class random * self
31+
32+
]
33+
3434
{ #category : #'*Math-Complex' }
3535
PMComplex >> subtractToPolynomial: aPolynomial [
3636
^ aPolynomial addNumber: self negated

src/Math-Tests-Complex/PMComplexTest.class.st

+36-31
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ PMComplexTest >> testAdaptToCollectionAndSend [
4545
self assert: (arr * c at: 2) equals: c
4646
]
4747

48-
{ #category : #tests }
49-
PMComplexTest >> testAddPolynomial [
48+
{ #category : #'testing - arithmetic' }
49+
PMComplexTest >> testAddingToAPolynomial [
5050
| c poly |
5151
c := 6 - 6 i.
5252
poly := PMPolynomial coefficients: #(1 1 1).
5353
self assert: (poly + c at: 0) equals: 7 - 6 i.
5454
self assert: (c + poly at: 0) equals: 7 - 6 i
5555
]
5656

57-
{ #category : #tests }
58-
PMComplexTest >> testAdding [
57+
{ #category : #'testing - arithmetic' }
58+
PMComplexTest >> testAddingTwoComplexNumbers [
5959
"self run: #testAdding"
6060

6161
| c |
@@ -247,7 +247,7 @@ PMComplexTest >> testComplexCollection [
247247
do: [ :one :two | self assert: 2 * one equals: two ]
248248
]
249249

250-
{ #category : #tests }
250+
{ #category : #'testing - arithmetic' }
251251
PMComplexTest >> testComplexConjugate [
252252

253253
self assert: (5 - 6i) complexConjugate equals: (5 + 6i).
@@ -341,16 +341,8 @@ PMComplexTest >> testCreation [
341341
self assert: c imaginary equals: 5
342342
]
343343

344-
{ #category : #tests }
345-
PMComplexTest >> testDividingPolynomial [
346-
| c poly |
347-
c := 4 + 4 i.
348-
poly := PMPolynomial coefficients: #(1 0 1).
349-
self assert: poly / c equals: 1 / c * poly
350-
]
351-
352-
{ #category : #tests }
353-
PMComplexTest >> testDivision1 [
344+
{ #category : #'testing - arithmetic' }
345+
PMComplexTest >> testDividingALargeComplexNumbersByItself [
354346
"self run: #testDivision1"
355347
"self debug: #testDivision1"
356348

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

368360
]
369361

362+
{ #category : #'testing - arithmetic' }
363+
PMComplexTest >> testDividingPolynomialByAComplexNumber [
364+
| c poly |
365+
c := 4 + 4 i.
366+
poly := PMPolynomial coefficients: #(1 0 1).
367+
self assert: poly / c equals: 1 / c * poly
368+
]
369+
370370
{ #category : #'testing - equality' }
371371
PMComplexTest >> testEqualsIsReflexive [
372372
| z |
@@ -479,14 +479,23 @@ PMComplexTest >> testLog [
479479
self assert: (2 + 0 i log: 2) equals: 1
480480
]
481481

482-
{ #category : #tests }
482+
{ #category : #'testing - arithmetic' }
483483
PMComplexTest >> testMultiplyByI [
484484
| c |
485485
c := 5 - 6 i.
486486
self assert: c * 1 i equals: c i
487487
]
488488

489-
{ #category : #tests }
489+
{ #category : #'testing - arithmetic' }
490+
PMComplexTest >> testMultiplyingByPolynomials [
491+
| c poly |
492+
c := 1 + 1 i.
493+
poly := PMPolynomial coefficients: #(1).
494+
self assert: (c * poly at: 0) equals: c.
495+
self assert: (poly * c at: 0) equals: c
496+
]
497+
498+
{ #category : #'testing - arithmetic' }
490499
PMComplexTest >> testNegated [
491500
"self run: #testNegated"
492501

@@ -585,7 +594,7 @@ PMComplexTest >> testRandom [
585594
self assert: r abs < c abs
586595
]
587596

588-
{ #category : #tests }
597+
{ #category : #'testing - arithmetic' }
589598
PMComplexTest >> testReciprocal [
590599
"self run: #testReciprocal"
591600

@@ -607,7 +616,7 @@ PMComplexTest >> testReciprocalError [
607616

608617
]
609618

610-
{ #category : #tests }
619+
{ #category : #'testing - arithmetic' }
611620
PMComplexTest >> testSecureDivision1 [
612621
"self run: #testSecureDivision1"
613622
"self debug: #testSecureDivision1"
@@ -620,7 +629,7 @@ PMComplexTest >> testSecureDivision1 [
620629

621630
]
622631

623-
{ #category : #tests }
632+
{ #category : #'testing - arithmetic' }
624633
PMComplexTest >> testSecureDivision2 [
625634
"self run: #testSecureDivision2"
626635
"self debug: #testSecureDivision2"
@@ -792,8 +801,8 @@ PMComplexTest >> testSquared [
792801
self assert: c2 real equals: 0
793802
]
794803

795-
{ #category : #tests }
796-
PMComplexTest >> testSubtractToPolynomial [
804+
{ #category : #'testing - arithmetic' }
805+
PMComplexTest >> testSubtractingPolynomials [
797806
| c poly |
798807
poly := PMPolynomial coefficients: #(1 2 3).
799808
c := 1 + 3 i.
@@ -825,15 +834,6 @@ PMComplexTest >> testTanh [
825834
self assert: (c2 imaginary closeTo: c tanh imaginary).
826835
]
827836

828-
{ #category : #tests }
829-
PMComplexTest >> testTimesPolynomial [
830-
| c poly |
831-
c := 1 + 1 i.
832-
poly := PMPolynomial coefficients: #(1).
833-
self assert: (c * poly at: 0) equals: c.
834-
self assert: (poly * c at: 0) equals: c
835-
]
836-
837837
{ #category : #'testing - equality' }
838838
PMComplexTest >> testTwoComplexNumbersWithDifferentImaginaryPartsAreNotEqual [
839839
| z w |
@@ -861,6 +861,11 @@ PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFracti
861861
self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5).
862862
]
863863

864+
{ #category : #'testing - arithmetic' }
865+
PMComplexTest >> testWeCannotTakeReciprocalOfZeroComplexNumbers [
866+
self should: [ PMComplex zero reciprocal ] raise: ZeroDivide.
867+
]
868+
864869
{ #category : #'testing - expressing complex numbers' }
865870
PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [
866871

0 commit comments

Comments
 (0)