1 // Copyright (C) 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /******************************************************************** 4 * COPYRIGHT: 5 * Copyright (c) 1997-2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ********************************************************************/ 8 9 10 /** 11 * IntlTest is a base class for tests. */ 12 13 #ifndef _INTLTEST 14 #define _INTLTEST 15 16 // The following includes utypes.h, uobject.h and unistr.h 17 #include "unicode/fmtable.h" 18 #include "unicode/testlog.h" 19 20 21 #if U_NO_DEFAULT_INCLUDE_UTF_HEADERS 22 /* deprecated - make tests pass with U_NO_DEFAULT_INCLUDE_UTF_HEADERS */ 23 #include "unicode/utf_old.h" 24 #endif 25 26 U_NAMESPACE_USE 27 28 #if U_PLATFORM == U_PF_OS390 29 // avoid collision with math.h/log() 30 // this must be after including utypes.h so that U_PLATFORM is actually defined 31 #pragma map(IntlTest::log( const UnicodeString &message ),"logos390") 32 #endif 33 34 //----------------------------------------------------------------------------- 35 //convenience classes to ease porting code that uses the Java 36 //string-concatenation operator (moved from findword test by rtg) 37 UnicodeString UCharToUnicodeString(UChar c); 38 UnicodeString Int64ToUnicodeString(int64_t num); 39 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type. 40 UnicodeString operator+(const UnicodeString& left, long num); 41 UnicodeString operator+(const UnicodeString& left, unsigned long num); 42 UnicodeString operator+(const UnicodeString& left, double num); 43 UnicodeString operator+(const UnicodeString& left, char num); 44 UnicodeString operator+(const UnicodeString& left, short num); 45 UnicodeString operator+(const UnicodeString& left, int num); 46 UnicodeString operator+(const UnicodeString& left, unsigned char num); 47 UnicodeString operator+(const UnicodeString& left, unsigned short num); 48 UnicodeString operator+(const UnicodeString& left, unsigned int num); 49 UnicodeString operator+(const UnicodeString& left, float num); 50 #if !UCONFIG_NO_FORMATTING 51 UnicodeString toString(const Formattable& f); // liu 52 UnicodeString toString(int32_t n); 53 #endif 54 UnicodeString toString(UBool b); 55 56 //----------------------------------------------------------------------------- 57 58 // Use the TESTCASE macro in subclasses of IntlTest. Define the 59 // runIndexedTest method in this fashion: 60 // 61 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 62 //| const char* &name, char* /*par*/) { 63 //| switch (index) { 64 //| TESTCASE(0,TestSomething); 65 //| TESTCASE(1,TestSomethingElse); 66 //| TESTCASE(2,TestAnotherThing); 67 //| default: name = ""; break; 68 //| } 69 //| } 70 #define TESTCASE(id,test) \ 71 case id: \ 72 name = #test; \ 73 if (exec) { \ 74 logln(#test "---"); \ 75 logln(); \ 76 test(); \ 77 } \ 78 break 79 80 // More convenient macros. These allow easy reordering of the test cases. 81 // 82 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 83 //| const char* &name, char* /*par*/) { 84 //| TESTCASE_AUTO_BEGIN; 85 //| TESTCASE_AUTO(TestSomething); 86 //| TESTCASE_AUTO(TestSomethingElse); 87 //| TESTCASE_AUTO(TestAnotherThing); 88 //| TESTCASE_AUTO_END; 89 //| } 90 #define TESTCASE_AUTO_BEGIN \ 91 for(;;) { \ 92 int32_t testCaseAutoNumber = 0 93 94 #define TESTCASE_AUTO(test) \ 95 if (index == testCaseAutoNumber++) { \ 96 name = #test; \ 97 if (exec) { \ 98 logln(#test "---"); \ 99 logln(); \ 100 test(); \ 101 } \ 102 break; \ 103 } 104 105 #define TESTCASE_AUTO_CLASS(TestClass) \ 106 if (index == testCaseAutoNumber++) { \ 107 name = #TestClass; \ 108 if (exec) { \ 109 logln(#TestClass "---"); \ 110 logln(); \ 111 TestClass test; \ 112 callTest(test, par); \ 113 } \ 114 break; \ 115 } 116 117 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \ 118 if (index == testCaseAutoNumber++) { \ 119 name = #TestClass; \ 120 if (exec) { \ 121 logln(#TestClass "---"); \ 122 logln(); \ 123 LocalPointer<IntlTest> test(create##TestClass()); \ 124 callTest(*test, par); \ 125 } \ 126 break; \ 127 } 128 129 #define TESTCASE_AUTO_END \ 130 name = ""; \ 131 break; \ 132 } 133 134 #define TEST_ASSERT_TRUE(x) \ 135 assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__) 136 137 #define TEST_ASSERT_STATUS(x) \ 138 assertSuccess(#x, (x), FALSE, __FILE__, __LINE__) 139 140 class IntlTest : public TestLog { 141 public: 142 143 IntlTest(); 144 // TestLog has a virtual destructor. 145 146 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden 147 148 virtual UBool setVerbose( UBool verbose = TRUE ); 149 virtual UBool setNoErrMsg( UBool no_err_msg = TRUE ); 150 virtual UBool setQuick( UBool quick = TRUE ); 151 virtual UBool setLeaks( UBool leaks = TRUE ); 152 virtual UBool setNotime( UBool no_time = TRUE ); 153 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE ); 154 virtual int32_t setThreadCount( int32_t count = 1); 155 156 virtual int32_t getErrors( void ); 157 virtual int32_t getDataErrors (void ); 158 159 virtual void setCaller( IntlTest* callingTest ); // for internal use only 160 virtual void setPath( char* path ); // for internal use only 161 162 virtual void log( const UnicodeString &message ); 163 164 virtual void logln( const UnicodeString &message ); 165 166 virtual void logln( void ); 167 168 /** 169 * Replaces isICUVersionAtLeast and isICUVersionBefore 170 * log that an issue is known. 171 * Usually used this way: 172 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code> 173 * @param ticket ticket string, "12345" or "cldrbug:1234" 174 * @param message optional message string 175 * @return true if test should be skipped 176 */ 177 UBool logKnownIssue( const char *ticket, const UnicodeString &message ); 178 /** 179 * Replaces isICUVersionAtLeast and isICUVersionBefore 180 * log that an issue is known. 181 * Usually used this way: 182 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code> 183 * @param ticket ticket string, "12345" or "cldrbug:1234" 184 * @return true if test should be skipped 185 */ 186 UBool logKnownIssue( const char *ticket ); 187 /** 188 * Replaces isICUVersionAtLeast and isICUVersionBefore 189 * log that an issue is known. 190 * Usually used this way: 191 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code> 192 * @param ticket ticket string, "12345" or "cldrbug:1234" 193 * @param message optional message string 194 * @return true if test should be skipped 195 */ 196 UBool logKnownIssue( const char *ticket, const char *fmt, ...); 197 198 virtual void info( const UnicodeString &message ); 199 200 virtual void infoln( const UnicodeString &message ); 201 202 virtual void infoln( void ); 203 204 virtual void err(void); 205 206 virtual void err( const UnicodeString &message ); 207 208 virtual void errln( const UnicodeString &message ); 209 210 virtual void dataerr( const UnicodeString &message ); 211 212 virtual void dataerrln( const UnicodeString &message ); 213 214 void errcheckln(UErrorCode status, const UnicodeString &message ); 215 216 // convenience functions: sprintf() + errln() etc. 217 void log(const char *fmt, ...); 218 void logln(const char *fmt, ...); 219 void info(const char *fmt, ...); 220 void infoln(const char *fmt, ...); 221 void err(const char *fmt, ...); 222 void errln(const char *fmt, ...); 223 void dataerr(const char *fmt, ...); 224 void dataerrln(const char *fmt, ...); 225 226 /** 227 * logs an error (even if status==U_ZERO_ERROR), but 228 * calls dataerrln() or errln() depending on the type of error. 229 * Does not report the status code. 230 * @param status parameter for selecting whether errln or dataerrln is called. 231 */ 232 void errcheckln(UErrorCode status, const char *fmt, ...); 233 234 // Print ALL named errors encountered so far 235 void printErrors(); 236 237 // print known issues. return TRUE if there were any. 238 UBool printKnownIssues(); 239 240 virtual void usage( void ) ; 241 242 /** 243 * Returns a uniform random value x, with 0.0 <= x < 1.0. Use 244 * with care: Does not return all possible values; returns one of 245 * 714,025 values, uniformly spaced. However, the period is 246 * effectively infinite. See: Numerical Recipes, section 7.1. 247 * 248 * @param seedp pointer to seed. Set *seedp to any negative value 249 * to restart the sequence. 250 */ 251 static float random(int32_t* seedp); 252 253 /** 254 * Convenience method using a global seed. 255 */ 256 static float random(); 257 258 259 /** 260 * Integer random numbers, similar to C++ std::minstd_rand, with the same algorithm 261 * and constants. Allow additional access to internal state, for use by monkey tests, 262 * which need to recreate previous random sequences beginning near a failure point. 263 */ 264 class icu_rand { 265 public: 266 icu_rand(uint32_t seed = 1); 267 ~icu_rand(); 268 void seed(uint32_t seed); 269 uint32_t operator()(); 270 /** 271 * Get a seed corresponding to the current state of the generator. 272 * Seeding any generator with this value will cause it to produce the 273 * same sequence as this one will from this point forward. 274 */ 275 uint32_t getSeed(); 276 private: 277 uint32_t fLast; 278 }; 279 280 281 282 enum { kMaxProps = 16 }; 283 284 virtual void setProperty(const char* propline); 285 virtual const char* getProperty(const char* prop); 286 287 protected: 288 /* JUnit-like assertions. Each returns TRUE if it succeeds. */ 289 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); 290 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE); 291 /** 292 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure 293 * @return TRUE on success, FALSE on failure. 294 */ 295 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); 296 UBool assertEquals(const char* message, const UnicodeString& expected, 297 const UnicodeString& actual, UBool possibleDataError=FALSE); 298 UBool assertEquals(const char* message, const char* expected, 299 const char* actual); 300 UBool assertEquals(const char* message, UBool expected, 301 UBool actual); 302 UBool assertEquals(const char* message, int32_t expected, int32_t actual); 303 UBool assertEquals(const char* message, int64_t expected, int64_t actual); 304 UBool assertEquals(const char* message, double expected, double actual); 305 #if !UCONFIG_NO_FORMATTING 306 UBool assertEquals(const char* message, const Formattable& expected, 307 const Formattable& actual, UBool possibleDataError=FALSE); 308 UBool assertEquals(const UnicodeString& message, const Formattable& expected, 309 const Formattable& actual); 310 #endif 311 UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 312 UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 313 UBool assertSuccess(const UnicodeString& message, UErrorCode ec); 314 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected, 315 const UnicodeString& actual, UBool possibleDataError=FALSE); 316 UBool assertEquals(const UnicodeString& message, const char* expected, 317 const char* actual); 318 UBool assertEquals(const UnicodeString& message, UBool expected, UBool actual); 319 UBool assertEquals(const UnicodeString& message, int32_t expected, int32_t actual); 320 UBool assertEquals(const UnicodeString& message, int64_t expected, int64_t actual); 321 322 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! 323 324 virtual UBool runTestLoop( char* testname, char* par, char *baseName ); 325 326 virtual int32_t IncErrorCount( void ); 327 328 virtual int32_t IncDataErrorCount( void ); 329 330 virtual UBool callTest( IntlTest& testToBeCalled, char* par ); 331 332 333 UBool verbose; 334 UBool no_err_msg; 335 UBool quick; 336 UBool leaks; 337 UBool warn_on_missing_data; 338 UBool no_time; 339 int32_t threadCount; 340 341 private: 342 UBool LL_linestart; 343 int32_t LL_indentlevel; 344 345 int32_t errorCount; 346 int32_t dataErrorCount; 347 IntlTest* caller; 348 char* testPath; // specifies subtests 349 350 char basePath[1024]; 351 char currName[1024]; // current test name 352 353 //FILE *testoutfp; 354 void *testoutfp; 355 356 const char* proplines[kMaxProps]; 357 int32_t numProps; 358 359 protected: 360 361 virtual void LL_message( UnicodeString message, UBool newline ); 362 363 // used for collation result reporting, defined here for convenience 364 365 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target); 366 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE); 367 // digits=-1 determines the number of digits automatically 368 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target); 369 static UnicodeString toHex(uint32_t number, int32_t digits=-1); 370 static inline UnicodeString toHex(int32_t number, int32_t digits=-1) { 371 return toHex((uint32_t)number, digits); 372 } 373 374 public: 375 static void setICU_DATA(); // Set up ICU_DATA if necessary. 376 377 static const char* pathToDataDirectory(); 378 379 public: 380 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks 381 static const char* loadTestData(UErrorCode& err); 382 virtual const char* getTestDataPath(UErrorCode& err); 383 static const char* getSourceTestData(UErrorCode& err); 384 static char *getUnidataPath(char path[]); 385 386 // static members 387 public: 388 static IntlTest* gTest; 389 static const char* fgDataDir; 390 391 }; 392 393 void it_log( UnicodeString message ); 394 void it_logln( UnicodeString message ); 395 void it_logln( void ); 396 void it_info( UnicodeString message ); 397 void it_infoln( UnicodeString message ); 398 void it_infoln( void ); 399 void it_err(void); 400 void it_err( UnicodeString message ); 401 void it_errln( UnicodeString message ); 402 void it_dataerr( UnicodeString message ); 403 void it_dataerrln( UnicodeString message ); 404 405 /** 406 * This is a variant of cintltst/ccolltst.c:CharsToUChars(). 407 * It converts a character string into a UnicodeString, with 408 * unescaping \u sequences. 409 */ 410 extern UnicodeString CharsToUnicodeString(const char* chars); 411 412 /* alias for CharsToUnicodeString */ 413 extern UnicodeString ctou(const char* chars); 414 415 #endif // _INTLTEST 416