]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/intltest.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / intltest / intltest.cpp
index ab5ce8c1272ca130354baa43bb81565f3b3bfd5f..15cd2d30776e6a544a77057b7dfeda44a9ddc84b 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <cmath>
 
 #include "unicode/ctest.h" // for str_timeDelta
 #include "unicode/curramt.h"
@@ -29,6 +30,7 @@
 #include "unicode/ucnv.h"
 #include "unicode/unistr.h"
 #include "unicode/ures.h"
+#include "unicode/utf16.h"
 
 #include "intltest.h"
 
@@ -108,6 +110,18 @@ Int64ToUnicodeString(int64_t num)
     return buffer;
 }
 
+UnicodeString
+DoubleToUnicodeString(double num)
+{
+    char buffer[64];    // nos changed from 10 to 64
+    char danger = 'p';  // guard against overrunning the buffer (rtg)
+
+    sprintf(buffer, "%1.14e", num);
+    assert(danger == 'p');
+
+    return buffer;
+}
+
 // [LIU] Just to get things working
 UnicodeString
 operator+(const UnicodeString& left,
@@ -224,6 +238,12 @@ UnicodeString toString(UBool b) {
   return b ? UnicodeString("TRUE"):UnicodeString("FALSE");
 }
 
+UnicodeString toString(const UnicodeSet& uniset, UErrorCode& status) {
+    UnicodeString result;
+    uniset.toPattern(result, status);
+    return result;
+}
+
 // stephen - cleaned up 05/05/99
 UnicodeString operator+(const UnicodeString& left, char num)
 { return left + (long)num; }
@@ -573,6 +593,7 @@ void IntlTest::setCaller( IntlTest* callingTest )
         quick = caller->quick;
         threadCount = caller->threadCount;
         testoutfp = caller->testoutfp;
+        write_golden_data = caller->write_golden_data;
         LL_indentlevel = caller->LL_indentlevel + indentLevel_offset;
         numProps = caller->numProps;
         for (int32_t i = 0; i < numProps; i++) {
@@ -617,6 +638,13 @@ UBool IntlTest::setWarnOnMissingData( UBool warn_on_missing_dataVal )
     return rval;
 }
 
+UBool IntlTest::setWriteGoldenData( UBool write_golden_data )
+{
+    UBool rval = this->write_golden_data;
+    this->write_golden_data = write_golden_data;
+    return rval;
+}
+
 UBool IntlTest::setNoErrMsg( UBool no_err_msgVal )
 {
     UBool rval = this->no_err_msg;
@@ -696,10 +724,10 @@ UBool IntlTest::runTest( char* name, char* par, char *baseName )
     return rval;
 }
 
-// call individual tests, to be overriden to call implementations
+// call individual tests, to be overridden to call implementations
 void IntlTest::runIndexedTest( int32_t /*index*/, UBool /*exec*/, const char* & /*name*/, char* /*par*/ )
 {
-    // to be overriden by a method like:
+    // to be overridden by a method like:
     /*
     switch (index) {
         case 0: name = "First Test"; if (exec) FirstTest( par ); break;
@@ -707,7 +735,7 @@ void IntlTest::runIndexedTest( int32_t /*index*/, UBool /*exec*/, const char* &
         default: name = ""; break;
     }
     */
-    this->errln("*** runIndexedTest needs to be overriden! ***");
+    this->errln("*** runIndexedTest needs to be overridden! ***");
 }
 
 
@@ -1089,14 +1117,14 @@ UBool IntlTest::printKnownIssues()
   }
 }
 
-static UMutex messageMutex = U_MUTEX_INITIALIZER;
 
 void IntlTest::LL_message( UnicodeString message, UBool newline )
 {
     // Synchronize this function.
     // All error messages generated by tests funnel through here.
-    // Multithreaded tests can concurrently generate errors, requiring syncronization
+    // Multithreaded tests can concurrently generate errors, requiring synchronization
     // to keep each message together.
+    static UMutex messageMutex;
     Mutex lock(&messageMutex);
 
     // string that starts with a LineFeed character and continues
@@ -1204,6 +1232,7 @@ main(int argc, char* argv[])
     UBool utf8 = FALSE;
     const char *summary_file = NULL;
     UBool warnOnMissingData = FALSE;
+    UBool writeGoldenData = FALSE;
     UBool defaultDataFound = FALSE;
     int32_t threadCount = 12;
     UErrorCode errorCode = U_ZERO_ERROR;
@@ -1245,6 +1274,9 @@ main(int argc, char* argv[])
             else if (strcmp("notime", str) == 0 ||
                      strcmp("T", str) == 0)
                 no_time = TRUE;
+            else if (strcmp("goldens", str) == 0 ||
+                     strcmp("G", str) == 0)
+                writeGoldenData = TRUE;
             else if (strncmp("E", str, 1) == 0)
                 summary_file = str+1;
             else if (strcmp("x", str)==0) {
@@ -1318,6 +1350,7 @@ main(int argc, char* argv[])
     major.setLeaks( leaks );
     major.setThreadCount( threadCount );
     major.setWarnOnMissingData( warnOnMissingData );
+    major.setWriteGoldenData( writeGoldenData );
     major.setNotime (no_time);
     for (int32_t i = 0; i < nProps; i++) {
         major.setProperty(props[i]);
@@ -1351,9 +1384,10 @@ main(int argc, char* argv[])
     fprintf(stdout, "   Exhaustive (e)           : %s\n", (!quick?            "On" : "Off"));
     fprintf(stdout, "   Leaks (l)                : %s\n", (leaks?             "On" : "Off"));
     fprintf(stdout, "   utf-8 (u)                : %s\n", (utf8?              "On" : "Off"));
-    fprintf(stdout, "   notime (T)               : %s\n", (no_time?             "On" : "Off"));
-    fprintf(stdout, "   noknownissues (K)        : %s\n", (noKnownIssues?      "On" : "Off"));
+    fprintf(stdout, "   notime (T)               : %s\n", (no_time?           "On" : "Off"));
+    fprintf(stdout, "   noknownissues (K)        : %s\n", (noKnownIssues?     "On" : "Off"));
     fprintf(stdout, "   Warn on missing data (w) : %s\n", (warnOnMissingData? "On" : "Off"));
+    fprintf(stdout, "   Write golden data (G)    : %s\n", (writeGoldenData?   "On" : "Off"));
     fprintf(stdout, "   Threads                  : %d\n", threadCount);
     for (int32_t i = 0; i < nProps; i++) {
         fprintf(stdout, "   Custom property (prop:)  : %s\n", props[i]);
@@ -1881,9 +1915,13 @@ UBool IntlTest::assertTrue(const char* message, UBool condition, UBool quiet, UB
     return condition;
 }
 
-UBool IntlTest::assertFalse(const char* message, UBool condition, UBool quiet) {
+UBool IntlTest::assertFalse(const char* message, UBool condition, UBool quiet, UBool possibleDataError) {
     if (condition) {
-        errln("FAIL: assertFalse() failed: %s", message);
+        if (possibleDataError) {
+            dataerrln("FAIL: assertTrue() failed: %s", message);
+        } else {
+            errln("FAIL: assertTrue() failed: %s", message);
+        }
     } else if (!quiet) {
         logln("Ok: %s", message);
     }
@@ -1985,7 +2023,8 @@ UBool IntlTest::assertEquals(const char* message,
 UBool IntlTest::assertEquals(const char* message,
                              double expected,
                              double actual) {
-    if (expected != actual) {
+    bool bothNaN = std::isnan(expected) && std::isnan(actual);
+    if (expected != actual && !bothNaN) {
         errln((UnicodeString)"FAIL: " + message + "; got " +
               actual + 
               "; expected " + expected);
@@ -2017,6 +2056,43 @@ UBool IntlTest::assertEquals(const char* message,
     return TRUE;
 }
 
+
+UBool IntlTest::assertEquals(const char* message,
+                             UErrorCode expected,
+                             UErrorCode actual) {
+    if (expected != actual) {
+        errln((UnicodeString)"FAIL: " + message + "; got " +
+              u_errorName(actual) + 
+              "; expected " + u_errorName(expected));
+        return FALSE;
+    }
+#ifdef VERBOSE_ASSERTIONS
+    else {
+        logln((UnicodeString)"Ok: " + message + "; got " + u_errorName(actual));
+    }
+#endif
+    return TRUE;
+}
+
+UBool IntlTest::assertEquals(const char* message,
+                             const UnicodeSet& expected,
+                             const UnicodeSet& actual) {
+    IcuTestErrorCode status(*this, "assertEqualsUniSet");
+    if (expected != actual) {
+        errln((UnicodeString)"FAIL: " + message + "; got " +
+              toString(actual, status) +
+              "; expected " + toString(expected, status));
+        return FALSE;
+    }
+#ifdef VERBOSE_ASSERTIONS
+    else {
+        logln((UnicodeString)"Ok: " + message + "; got " + toString(actual, status));
+    }
+#endif
+    return TRUE;
+}
+
+
 #if !UCONFIG_NO_FORMATTING
 UBool IntlTest::assertEquals(const char* message,
                              const Formattable& expected,
@@ -2043,6 +2119,42 @@ UBool IntlTest::assertEquals(const char* message,
 }
 #endif
 
+std::string vectorToString(const std::vector<std::string>& strings) {
+    std::string result = "{";
+    bool first = true;
+    for (auto element : strings) {
+        if (first) {
+            first = false;
+        } else {
+            result += ", ";
+        }
+        result += "\"";
+        result += element;
+        result += "\"";
+    }
+    result += "}";
+    return result;
+}
+
+UBool IntlTest::assertEquals(const char* message,
+                             const std::vector<std::string>& expected,
+                             const std::vector<std::string>& actual) {
+    if (expected != actual) {
+        std::string expectedAsString = vectorToString(expected);
+        std::string actualAsString = vectorToString(actual);
+        errln((UnicodeString)"FAIL: " + message +
+            "; got " + actualAsString.c_str() +
+            "; expected " + expectedAsString.c_str());
+        return FALSE;
+    }
+#ifdef VERBOSE_ASSERTIONS
+    else {
+        logln((UnicodeString)"Ok: " + message + "; got " + vectorToString(actual).c_str());
+    }
+#endif
+    return TRUE;
+}
+
 static char ASSERT_BUF[256];
 
 static const char* extractToAssertBuf(const UnicodeString& message) {
@@ -2053,12 +2165,12 @@ static const char* extractToAssertBuf(const UnicodeString& message) {
     return ASSERT_BUF;
 }
 
-UBool IntlTest::assertTrue(const UnicodeString& message, UBool condition, UBool quiet) {
-    return assertTrue(extractToAssertBuf(message), condition, quiet);
+UBool IntlTest::assertTrue(const UnicodeString& message, UBool condition, UBool quiet, UBool possibleDataError) {
+    return assertTrue(extractToAssertBuf(message), condition, quiet, possibleDataError);
 }
 
-UBool IntlTest::assertFalse(const UnicodeString& message, UBool condition, UBool quiet) {
-    return assertFalse(extractToAssertBuf(message), condition, quiet);
+UBool IntlTest::assertFalse(const UnicodeString& message, UBool condition, UBool quiet, UBool possibleDataError) {
+    return assertFalse(extractToAssertBuf(message), condition, quiet, possibleDataError);
 }
 
 UBool IntlTest::assertSuccess(const UnicodeString& message, UErrorCode ec) {
@@ -2092,6 +2204,26 @@ UBool IntlTest::assertEquals(const UnicodeString& message,
                              int64_t actual) {
     return assertEquals(extractToAssertBuf(message), expected, actual);
 }
+UBool IntlTest::assertEquals(const UnicodeString& message,
+                             double expected,
+                             double actual) {
+    return assertEquals(extractToAssertBuf(message), expected, actual);
+}
+UBool IntlTest::assertEquals(const UnicodeString& message,
+                             UErrorCode expected,
+                             UErrorCode actual) {
+    return assertEquals(extractToAssertBuf(message), expected, actual);
+}
+UBool IntlTest::assertEquals(const UnicodeString& message,
+                             const UnicodeSet& expected,
+                             const UnicodeSet& actual) {
+    return assertEquals(extractToAssertBuf(message), expected, actual);
+}
+UBool IntlTest::assertEquals(const UnicodeString& message,
+                             const std::vector<std::string>& expected,
+                             const std::vector<std::string>& actual) {
+    return assertEquals(extractToAssertBuf(message), expected, actual);
+}
 
 #if !UCONFIG_NO_FORMATTING
 UBool IntlTest::assertEquals(const UnicodeString& message,
@@ -2111,7 +2243,7 @@ void IntlTest::setProperty(const char* propline) {
 const char* IntlTest::getProperty(const char* prop) {
     const char* val = NULL;
     for (int32_t i = 0; i < numProps; i++) {
-        int32_t plen = uprv_strlen(prop);
+        int32_t plen = static_cast<int32_t>(uprv_strlen(prop));
         if ((int32_t)uprv_strlen(proplines[i]) > plen + 1
                 && proplines[i][plen] == '='
                 && uprv_strncmp(proplines[i], prop, plen) == 0) {