Skip to content

Commit 18bd1fb

Browse files
Refactor complex number tests: Break up testEquality and Introduce Equivalence Relation Contract and Other Tests (#241)
* Named the categories so they follow convention. * Added contract tests for equals that checks the properties of an equivalence relation * Moved the tests to a test that has a clearer name. * Moved some assertions to test messages with clearer names, removed some whitespace. * Added a missing test to demonstrate two complex numbers with different real parts are not equal. * Moved an assertion to a test message with a clearer name. * Removed obsolete code. * Moved an assertion to a test message with a clearer name. * Moved an assertion to a clearly named test message. * Moved assertion to a clearly named test message. * Moved the assertions to a clearly named test message. * Clarified the names of the test messages.
1 parent 3f5808f commit 18bd1fb

File tree

1 file changed

+87
-21
lines changed

1 file changed

+87
-21
lines changed

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

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Class {
44
#category : #'Math-Tests-Complex'
55
}
66

7+
{ #category : #'testing - equality' }
8+
PMComplexTest >> testAPureImaginaryNumberIsNotEqualToZero [
9+
self deny: 1 i equals: 0.
10+
self deny: 0 equals: 1 i.
11+
]
12+
713
{ #category : #tests }
814
PMComplexTest >> testAbs [
915
"self run: #testAbs"
@@ -204,6 +210,11 @@ PMComplexTest >> testComplexConjugate [
204210
self assert: complexConjugateOfZ equals: expected.
205211
]
206212

213+
{ #category : #'testing - equality' }
214+
PMComplexTest >> testComplexNumberAndIntegerAreUnequalEvenIfRealPartIsEqualToInteger [
215+
self deny: 1 + 3 i equals: 1.
216+
]
217+
207218
{ #category : #tests }
208219
PMComplexTest >> testConversion [
209220
"self run: #testConversion"
@@ -313,31 +324,54 @@ PMComplexTest >> testDivision1 [
313324

314325
]
315326

316-
{ #category : #testing }
317-
PMComplexTest >> testEquality [
318-
"self run: #testEquality"
327+
{ #category : #'testing - equality' }
328+
PMComplexTest >> testEqualsIsReflexive [
329+
| z |
330+
z := -10 + 10 i.
319331

320-
"self debug: #testEquality"
332+
self assert: z equals: z.
333+
]
321334

322-
self assert: 0 i equals: 0.
323-
self assert: 2 - 5 i equals: 1 - 4 i + (1 - 1 i).
324-
self assert: 0 i isZero.
325-
self deny: 1 + 3 i = 1.
326-
self deny: 1 + 3 i = (1 + 2 i).
335+
{ #category : #'testing - equality' }
336+
PMComplexTest >> testEqualsIsSymmetric [
337+
| z w |
327338

328-
"Some more stuff"
329-
self deny: 1 i = nil.
330-
self deny: nil = 1 i.
331-
self deny: 1 i = #(1 2 3).
332-
self deny: #(1 2 3) = 1 i.
333-
self deny: 1 i = 0.
334-
self deny: 0 = 1 i.
339+
z := 13 + 14 i.
340+
w := 13 + 14 i.
341+
342+
self assert: z equals: w.
343+
self assert: w equals: z.
344+
]
345+
346+
{ #category : #'testing - equality' }
347+
PMComplexTest >> testEqualsIsSymmetricWithRespectToFractions [
348+
self assert: 1 / 2 + 0 i equals: 1 / 2.
349+
self assert: 1 / 2 equals: 1 / 2 + 0 i
350+
]
351+
352+
{ #category : #'testing - equality' }
353+
PMComplexTest >> testEqualsIsSymmetricWithRespectToIntegers [
335354
self assert: 1 + 0 i equals: 1.
336355
self assert: 1 equals: 1 + 0 i.
356+
]
357+
358+
{ #category : #'testing - equality' }
359+
PMComplexTest >> testEqualsIsSymmetricWithRespectToRealNumbers [
337360
self assert: 1 + 0 i equals: 1.0.
338361
self assert: 1.0 equals: 1 + 0 i.
339-
self assert: 1 / 2 + 0 i equals: 1 / 2.
340-
self assert: 1 / 2 equals: 1 / 2 + 0 i
362+
]
363+
364+
{ #category : #'testing - equality' }
365+
PMComplexTest >> testEqualsIsTransitive [
366+
| z w u |
367+
368+
z := 4 - 1 i.
369+
w := 4 - 1 i.
370+
u := 4 - 1 i.
371+
372+
self assert: z equals: w.
373+
self assert: w equals: u.
374+
self assert: z equals: u.
341375
]
342376

343377
{ #category : #tests }
@@ -511,6 +545,14 @@ PMComplexTest >> testProductWithVector [
511545
self assert: (c * v at: 1) equals: c
512546
]
513547

548+
{ #category : #'testing - equality' }
549+
PMComplexTest >> testPureImaginaryNumbersAreNotEqualToObjectsOfADifferentType [
550+
self deny: 1 i = nil.
551+
self deny: nil = 1 i.
552+
self deny: 1 i = #(1 2 3).
553+
self deny: #(1 2 3) = 1 i.
554+
]
555+
514556
{ #category : #tests }
515557
PMComplexTest >> testRaisedTo [
516558

@@ -750,7 +792,25 @@ PMComplexTest >> testTimesPolynomial [
750792
self assert: (poly * c at: 0) equals: c
751793
]
752794

753-
{ #category : #'expressing complex numbers' }
795+
{ #category : #'testing - equality' }
796+
PMComplexTest >> testTwoComplexNumbersWithDifferentImaginaryPartsAreNotEqual [
797+
| z w |
798+
z := 1 + 3 i.
799+
w := 1 + 2 i.
800+
801+
self deny: z equals: w.
802+
]
803+
804+
{ #category : #'testing - equality' }
805+
PMComplexTest >> testTwoComplexNumbersWithDifferentRealPartsAreNotEqual [
806+
| z w |
807+
z := 4 + 3 i.
808+
w := -7 + 3 i.
809+
810+
self deny: z equals: w.
811+
]
812+
813+
{ #category : #'testing - expressing complex numbers' }
754814
PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFractions [
755815
| z |
756816
z := PMComplex real: 3 / 5 imaginary: 4 / 5.
@@ -759,7 +819,7 @@ PMComplexTest >> testWeCanWriteComplexNumbersWhoseRealAndImaginaryPartsAreFracti
759819
self assert: (z imaginary) equals: (Fraction numerator: 4 denominator: 5).
760820
]
761821

762-
{ #category : #'expressing complex numbers' }
822+
{ #category : #'testing - expressing complex numbers' }
763823
PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormalized [
764824

765825
| z w numerator denominator |
@@ -771,7 +831,7 @@ PMComplexTest >> testWeCannotWriteFractionsOfComplexNumbersWithDenominatorNormal
771831
self should: [ Fraction numerator: numerator denominator: w squaredNorm ] raise: Exception.
772832
]
773833

774-
{ #category : #'expressing complex numbers' }
834+
{ #category : #'testing - expressing complex numbers' }
775835
PMComplexTest >> testWeCannotWriteFractionsWhoseNumeratorAndDenominatorAreComplexNumbers [
776836
| numerator denominator |
777837
"It is interesting that we cannot instanciate a fraction of the form z/w"
@@ -791,3 +851,9 @@ PMComplexTest >> testZero [
791851
self assert: z real isZero.
792852
self assert: z imaginary isZero.
793853
]
854+
855+
{ #category : #'testing - equality' }
856+
PMComplexTest >> testZeroComplexNumberIsEqualToIntegerZero [
857+
self assert: 0 i equals: 0.
858+
self assert: 0 equals: 0 i.
859+
]

0 commit comments

Comments
 (0)