Selaa lähdekoodia

Merge pull request #1298 from jackattack24/complexEqual

complex-numbers: improve error message for failing test
FridaTveit 8 vuotta sitten
vanhempi
commit
e458c40a87
No account linked to committer's email
1 muutettua tiedostoa jossa 17 lisäystä ja 15 poistoa
  1. 17
    15
      exercises/complex-numbers/src/test/java/ComplexNumberTest.java

+ 17
- 15
exercises/complex-numbers/src/test/java/ComplexNumberTest.java Näytä tiedosto

9
 
9
 
10
     private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
10
     private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
11
 
11
 
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);
14
     }
16
     }
15
 
17
 
16
     private void assertComplexNumbersEqual(ComplexNumber c1, ComplexNumber c2) {
18
     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");
19
     }
21
     }
20
 
22
 
21
     // Tests
23
     // Tests
128
     public void testAbsoluteValueOfPositivePurelyRealNumber() {
130
     public void testAbsoluteValueOfPositivePurelyRealNumber() {
129
         double expected = 5.0;
131
         double expected = 5.0;
130
         double actual = new ComplexNumber(5.0, 0).abs();
132
         double actual = new ComplexNumber(5.0, 0).abs();
131
-        assertDoublesEqual(expected, actual);
133
+        assertDoublesEqual(expected, actual, "real");
132
     }
134
     }
133
 
135
 
134
     @Ignore("Remove to run test")
136
     @Ignore("Remove to run test")
136
     public void testAbsoluteValueOfNegativePurelyRealNumber() {
138
     public void testAbsoluteValueOfNegativePurelyRealNumber() {
137
         double expected = 5.0;
139
         double expected = 5.0;
138
         double actual = new ComplexNumber(-5.0, 0).abs();
140
         double actual = new ComplexNumber(-5.0, 0).abs();
139
-        assertDoublesEqual(expected, actual);
141
+        assertDoublesEqual(expected, actual, "real");
140
     }
142
     }
141
 
143
 
142
     @Ignore("Remove to run test")
144
     @Ignore("Remove to run test")
144
     public void testAbsoluteValueOfPurelyImaginaryNumberWithPositiveImaginaryPart() {
146
     public void testAbsoluteValueOfPurelyImaginaryNumberWithPositiveImaginaryPart() {
145
         double expected = 5.0;
147
         double expected = 5.0;
146
         double actual = new ComplexNumber(0, 5.0).abs();
148
         double actual = new ComplexNumber(0, 5.0).abs();
147
-        assertDoublesEqual(expected, actual);
149
+        assertDoublesEqual(expected, actual, "real");
148
     }
150
     }
149
 
151
 
150
     @Ignore("Remove to run test")
152
     @Ignore("Remove to run test")
152
     public void testAbsoluteValueOfPurelyImaginaryNumberWithNegativeImaginaryPart() {
154
     public void testAbsoluteValueOfPurelyImaginaryNumberWithNegativeImaginaryPart() {
153
         double expected = 5.0;
155
         double expected = 5.0;
154
         double actual = new ComplexNumber(0, -5.0).abs();
156
         double actual = new ComplexNumber(0, -5.0).abs();
155
-        assertDoublesEqual(expected, actual);
157
+        assertDoublesEqual(expected, actual, "real");
156
     }
158
     }
157
 
159
 
158
     @Ignore("Remove to run test")
160
     @Ignore("Remove to run test")
160
     public void testAbsoluteValueOfNumberWithRealAndImaginaryParts() {
162
     public void testAbsoluteValueOfNumberWithRealAndImaginaryParts() {
161
         double expected = 5.0;
163
         double expected = 5.0;
162
         double actual = new ComplexNumber(3.0, 4.0).abs();
164
         double actual = new ComplexNumber(3.0, 4.0).abs();
163
-        assertDoublesEqual(expected, actual);
165
+        assertDoublesEqual(expected, actual, "real");
164
     }
166
     }
165
 
167
 
166
     @Ignore("Remove to run test")
168
     @Ignore("Remove to run test")
192
     public void testRealPartOfPurelyRealNumber() {
194
     public void testRealPartOfPurelyRealNumber() {
193
         double expected = 1.0;
195
         double expected = 1.0;
194
         double actual = new ComplexNumber(1.0, 0).getReal();
196
         double actual = new ComplexNumber(1.0, 0).getReal();
195
-        assertDoublesEqual(expected, actual);
197
+        assertDoublesEqual(expected, actual, "real");
196
     }
198
     }
197
 
199
 
198
     @Ignore("Remove to run test")
200
     @Ignore("Remove to run test")
200
     public void testRealPartOfPurelyImaginaryNumber() {
202
     public void testRealPartOfPurelyImaginaryNumber() {
201
         double expected = 0.0;
203
         double expected = 0.0;
202
         double actual = new ComplexNumber(0, 1.0).getReal();
204
         double actual = new ComplexNumber(0, 1.0).getReal();
203
-        assertDoublesEqual(expected, actual);
205
+        assertDoublesEqual(expected, actual, "real");
204
     }
206
     }
205
 
207
 
206
     @Ignore("Remove to run test")
208
     @Ignore("Remove to run test")
208
     public void testRealPartOfNumberWithRealAndImaginaryParts() {
210
     public void testRealPartOfNumberWithRealAndImaginaryParts() {
209
         double expected = 1.0;
211
         double expected = 1.0;
210
         double actual = new ComplexNumber(1.0, 2.0).getReal();
212
         double actual = new ComplexNumber(1.0, 2.0).getReal();
211
-        assertDoublesEqual(expected, actual);
213
+        assertDoublesEqual(expected, actual, "real");
212
     }
214
     }
213
 
215
 
214
     @Ignore("Remove to run test")
216
     @Ignore("Remove to run test")
216
     public void testImaginaryPartOfPurelyRealNumber() {
218
     public void testImaginaryPartOfPurelyRealNumber() {
217
         double expected = 0.0;
219
         double expected = 0.0;
218
         double actual = new ComplexNumber(1.0, 0).getImag();
220
         double actual = new ComplexNumber(1.0, 0).getImag();
219
-        assertDoublesEqual(expected, actual);
221
+        assertDoublesEqual(expected, actual, "imaginary");
220
     }
222
     }
221
 
223
 
222
     @Ignore("Remove to run test")
224
     @Ignore("Remove to run test")
224
     public void testImaginaryPartOfPurelyImaginaryNumber() {
226
     public void testImaginaryPartOfPurelyImaginaryNumber() {
225
         double expected = 1.0;
227
         double expected = 1.0;
226
         double actual = new ComplexNumber(0, 1.0).getImag();
228
         double actual = new ComplexNumber(0, 1.0).getImag();
227
-        assertDoublesEqual(expected, actual);
229
+        assertDoublesEqual(expected, actual, "imaginary");
228
     }
230
     }
229
 
231
 
230
     @Ignore("Remove to run test")
232
     @Ignore("Remove to run test")
232
     public void testImaginaryPartOfNumberWithRealAndImaginaryParts() {
234
     public void testImaginaryPartOfNumberWithRealAndImaginaryParts() {
233
         double expected = 2.0;
235
         double expected = 2.0;
234
         double actual = new ComplexNumber(1.0, 2.0).getImag();
236
         double actual = new ComplexNumber(1.0, 2.0).getImag();
235
-        assertDoublesEqual(expected, actual);
237
+        assertDoublesEqual(expected, actual, "imaginary");
236
     }
238
     }
237
 
239
 
238
     @Ignore("Remove to run test")
240
     @Ignore("Remove to run test")