]> granicus.if.org Git - icu/commitdiff
Use JUnit-like assertEquals. Improve test messages.
authorHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Mon, 11 May 2020 14:53:29 +0000 (16:53 +0200)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Wed, 17 Jun 2020 20:39:00 +0000 (22:39 +0200)
icu4c/source/test/intltest/intltest.cpp
icu4c/source/test/intltest/unitstest.cpp

index 9f4ae348ab947db18cd761a1823b140f910cd70e..7d04283ea278f2184b3588bf0aff8363e669fe4f 100644 (file)
@@ -2197,26 +2197,29 @@ UBool IntlTest::assertNotEquals(const char* message,
     return TRUE;
 }
 
-// http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(java.lang.String,%20double,%20double,%20double)
-UBool IntlTest::assertEqualsNear(const char *message, double expected, double actual, double precision) {
-    double diff = std::abs(expected - actual);
-    double diffPercent =
-        expected != 0 ? diff / expected
-                      : diff; // If the expected is equals zero, we assume that
-                              // the `diffPercent` is equal to the difference
-                              // between the actual and the expected
-
-    if (diffPercent > precision) {
-        errln((UnicodeString) "FAIL: " + message + "; got " + actual + "; expected " + expected);
-        return FALSE;
-    }
-#ifdef VERBOSE_ASSERTIONS
-    else {
-        logln((UnicodeString) "Ok: " + message + "; got " + expected);
-    }
-#endif
-    return TRUE;
-}
+// // TODO: the following link is misleading, since JUnit does absolute numbers and
+// // this code does relative, and this code doesn't support NaN whereas JUnit
+// // does. JUnit's function is also simply overloading "assertEquals".
+// // http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(java.lang.String,%20double,%20double,%20double)
+// UBool IntlTest::assertEqualsNear(const char *message, double expected, double actual, double precision) {
+//     double diff = std::abs(expected - actual);
+//     double diffPercent =
+//         expected != 0 ? diff / expected
+//                       : diff; // If the expected is equals zero, we assume that
+//                               // the `diffPercent` is equal to the difference
+//                               // between the actual and the expected
+
+//     if (diffPercent > precision) {
+//         errln((UnicodeString) "FAIL: " + message + "; got " + actual + "; expected " + expected);
+//         return FALSE;
+//     }
+// #ifdef VERBOSE_ASSERTIONS
+//     else {
+//         logln((UnicodeString) "Ok: " + message + "; got " + expected);
+//     }
+// #endif
+//     return TRUE;
+// }
 
 static char ASSERT_BUF[256];
 
index bb5d216fab1ae56bd2078ad3066daf89969a8389..0d80b928a377e299063a7880de7ecdb5e3609453 100644 (file)
@@ -65,8 +65,8 @@ void UnitsTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
 
 void UnitsTest::testConversionCapability() {
     struct TestCase {
-        const StringPiece source;
-        const StringPiece target;
+        const char *source;
+        const char *target;
         const UnitsConvertibilityState expectedState;
     } testCases[]{
         {"meter", "foot", CONVERTIBLE},                                         //
@@ -88,7 +88,9 @@ void UnitsTest::testConversionCapability() {
         ConversionRates conversionRates(status);
         auto convertibility = checkConvertibility(source, target, conversionRates, status);
 
-        assertEquals("Conversion Capability", testCase.expectedState, convertibility);
+        assertEquals(UnicodeString("Conversion Capability: ") + testCase.source + " to " +
+                         testCase.target,
+                     testCase.expectedState, convertibility);
     }
 }
 
@@ -96,8 +98,8 @@ void UnitsTest::testSiPrefixes() {
     IcuTestErrorCode status(*this, "Units testSiPrefixes");
     // Test Cases
     struct TestCase {
-        StringPiece source;
-        StringPiece target;
+        const char *source;
+        const char *target;
         const double inputValue;
         const double expectedValue;
     } testCases[]{
@@ -121,8 +123,9 @@ void UnitsTest::testSiPrefixes() {
         ConversionRates conversionRates(status);
         UnitConverter converter(source, target, conversionRates, status);
 
-        assertEqualsNear("test conversion", testCase.expectedValue,
-                         converter.convert(testCase.inputValue), 0.001);
+        assertEquals(UnicodeString("testSiPrefixes: ") + testCase.source + " to " + testCase.target,
+                     testCase.expectedValue, converter.convert(testCase.inputValue),
+                     0.0001 * testCase.expectedValue);
     }
 }
 
@@ -131,8 +134,8 @@ void UnitsTest::testMass() {
 
     // Test Cases
     struct TestCase {
-        StringPiece source;
-        StringPiece target;
+        const char *source;
+        const char *target;
         const double inputValue;
         const double expectedValue;
     } testCases[]{
@@ -155,8 +158,9 @@ void UnitsTest::testMass() {
         ConversionRates conversionRates(status);
         UnitConverter converter(source, target, conversionRates, status);
 
-        assertEqualsNear("test conversion", testCase.expectedValue,
-                         converter.convert(testCase.inputValue), 0.001);
+        assertEquals(UnicodeString("testMass: ") + testCase.source + " to " + testCase.target,
+                     testCase.expectedValue, converter.convert(testCase.inputValue),
+                     0.0001 * testCase.expectedValue);
     }
 }
 
@@ -164,8 +168,8 @@ void UnitsTest::testTemperature() {
     IcuTestErrorCode status(*this, "Units testTemperature");
     // Test Cases
     struct TestCase {
-        StringPiece source;
-        StringPiece target;
+        const char *source;
+        const char *target;
         const double inputValue;
         const double expectedValue;
     } testCases[]{
@@ -188,8 +192,9 @@ void UnitsTest::testTemperature() {
         ConversionRates conversionRates(status);
         UnitConverter converter(source, target, conversionRates, status);
 
-        assertEqualsNear("test conversion", testCase.expectedValue,
-                         converter.convert(testCase.inputValue), 0.001);
+        assertEquals(UnicodeString("testTemperature: ") + testCase.source + " to " + testCase.target,
+                     testCase.expectedValue, converter.convert(testCase.inputValue),
+                     0.0001 * abs(testCase.expectedValue));
     }
 }
 
@@ -198,8 +203,8 @@ void UnitsTest::testArea() {
 
     // Test Cases
     struct TestCase {
-        StringPiece source;
-        StringPiece target;
+        const char *source;
+        const char *target;
         const double inputValue;
         const double expectedValue;
     } testCases[]{
@@ -225,8 +230,9 @@ void UnitsTest::testArea() {
         ConversionRates conversionRates(status);
         UnitConverter converter(source, target, conversionRates, status);
 
-        assertEqualsNear("test conversion", testCase.expectedValue,
-                         converter.convert(testCase.inputValue), 0.001);
+        assertEquals(UnicodeString("testArea: ") + testCase.source + " to " + testCase.target,
+                     testCase.expectedValue, converter.convert(testCase.inputValue),
+                     0.0001 * testCase.expectedValue);
     }
 }
 
@@ -337,7 +343,7 @@ void unitsTestDataLineFn(void *context, char *fields[][2], int32_t fieldCount, U
     double got = converter.convert(1000);
     msg.clear();
     msg.append("Converting 1000 ", status).append(x, status).append(" to ", status).append(y, status);
-    unitsTest->assertEqualsNear(msg.data(), expected, got, 0.0001);
+    unitsTest->assertEquals(msg.data(), expected, got, 0.0001 * expected);
 }
 
 /**