Skip to content

Commit bc162b6

Browse files
committed
Display which part of complex number is being tested in error message for failing tests
1 parent d46a18f commit bc162b6

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

exercises/complex-numbers/src/test/java/ComplexNumberTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class ComplexNumberTest {
99

1010
private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
1111

12-
private void assertDoublesEqual(double d1, double d2) {
13-
assertEquals(d1, d2, DOUBLE_EQUALITY_TOLERANCE);
12+
private void assertDoublesEqual(double d1, double d2, String numberPart) {
13+
String errorMessage = "While testing " + numberPart + " part of number,";
14+
15+
assertEquals(errorMessage, d1, d2, DOUBLE_EQUALITY_TOLERANCE);
1416
}
1517

1618
private void assertComplexNumbersEqual(ComplexNumber c1, ComplexNumber c2) {
17-
assertDoublesEqual(c1.getReal(), c2.getReal());
18-
assertDoublesEqual(c1.getImag(), c2.getImag());
19+
assertDoublesEqual(c1.getReal(), c2.getReal(), "real");
20+
assertDoublesEqual(c1.getImag(), c2.getImag(), "imaginary");
1921
}
2022

2123
// Tests
@@ -128,39 +130,39 @@ public void testDivisionWithRealAndImaginaryParts() {
128130
public void testAbsoluteValueOfPositivePurelyRealNumber() {
129131
double expected = 5.0;
130132
double actual = new ComplexNumber(5.0, 0).abs();
131-
assertDoublesEqual(expected, actual);
133+
assertDoublesEqual(expected, actual, "real");
132134
}
133135

134136
@Ignore("Remove to run test")
135137
@Test
136138
public void testAbsoluteValueOfNegativePurelyRealNumber() {
137139
double expected = 5.0;
138140
double actual = new ComplexNumber(-5.0, 0).abs();
139-
assertDoublesEqual(expected, actual);
141+
assertDoublesEqual(expected, actual, "real");
140142
}
141143

142144
@Ignore("Remove to run test")
143145
@Test
144146
public void testAbsoluteValueOfPurelyImaginaryNumberWithPositiveImaginaryPart() {
145147
double expected = 5.0;
146148
double actual = new ComplexNumber(0, 5.0).abs();
147-
assertDoublesEqual(expected, actual);
149+
assertDoublesEqual(expected, actual, "real");
148150
}
149151

150152
@Ignore("Remove to run test")
151153
@Test
152154
public void testAbsoluteValueOfPurelyImaginaryNumberWithNegativeImaginaryPart() {
153155
double expected = 5.0;
154156
double actual = new ComplexNumber(0, -5.0).abs();
155-
assertDoublesEqual(expected, actual);
157+
assertDoublesEqual(expected, actual, "real");
156158
}
157159

158160
@Ignore("Remove to run test")
159161
@Test
160162
public void testAbsoluteValueOfNumberWithRealAndImaginaryParts() {
161163
double expected = 5.0;
162164
double actual = new ComplexNumber(3.0, 4.0).abs();
163-
assertDoublesEqual(expected, actual);
165+
assertDoublesEqual(expected, actual, "real");
164166
}
165167

166168
@Ignore("Remove to run test")
@@ -192,47 +194,47 @@ public void testConjugationOfNumberWithRealAndImaginaryParts() {
192194
public void testRealPartOfPurelyRealNumber() {
193195
double expected = 1.0;
194196
double actual = new ComplexNumber(1.0, 0).getReal();
195-
assertDoublesEqual(expected, actual);
197+
assertDoublesEqual(expected, actual, "real");
196198
}
197199

198200
@Ignore("Remove to run test")
199201
@Test
200202
public void testRealPartOfPurelyImaginaryNumber() {
201203
double expected = 0.0;
202204
double actual = new ComplexNumber(0, 1.0).getReal();
203-
assertDoublesEqual(expected, actual);
205+
assertDoublesEqual(expected, actual, "real");
204206
}
205207

206208
@Ignore("Remove to run test")
207209
@Test
208210
public void testRealPartOfNumberWithRealAndImaginaryParts() {
209211
double expected = 1.0;
210212
double actual = new ComplexNumber(1.0, 2.0).getReal();
211-
assertDoublesEqual(expected, actual);
213+
assertDoublesEqual(expected, actual, "real");
212214
}
213215

214216
@Ignore("Remove to run test")
215217
@Test
216218
public void testImaginaryPartOfPurelyRealNumber() {
217219
double expected = 0.0;
218220
double actual = new ComplexNumber(1.0, 0).getImag();
219-
assertDoublesEqual(expected, actual);
221+
assertDoublesEqual(expected, actual, "imaginary");
220222
}
221223

222224
@Ignore("Remove to run test")
223225
@Test
224226
public void testImaginaryPartOfPurelyImaginaryNumber() {
225227
double expected = 1.0;
226228
double actual = new ComplexNumber(0, 1.0).getImag();
227-
assertDoublesEqual(expected, actual);
229+
assertDoublesEqual(expected, actual, "imaginary");
228230
}
229231

230232
@Ignore("Remove to run test")
231233
@Test
232234
public void testImaginaryPartOfNumberWithRealAndImaginaryParts() {
233235
double expected = 2.0;
234236
double actual = new ComplexNumber(1.0, 2.0).getImag();
235-
assertDoublesEqual(expected, actual);
237+
assertDoublesEqual(expected, actual, "imaginary");
236238
}
237239

238240
@Ignore("Remove to run test")

0 commit comments

Comments
 (0)