1 // © 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 #include "unicode/uniset.h" 20 21 #include <vector> 22 #include <string> 23 24 U_NAMESPACE_USE 25 26 #if U_PLATFORM == U_PF_OS390 27 // avoid collision with math.h/log() 28 // this must be after including utypes.h so that U_PLATFORM is actually defined 29 #pragma map(IntlTest::log( const UnicodeString &message ),"logos390") 30 #endif 31 32 //----------------------------------------------------------------------------- 33 //convenience classes to ease porting code that uses the Java 34 //string-concatenation operator (moved from findword test by rtg) 35 UnicodeString UCharToUnicodeString(UChar c); 36 UnicodeString Int64ToUnicodeString(int64_t num); 37 UnicodeString DoubleToUnicodeString(double num); 38 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type. 39 UnicodeString operator+(const UnicodeString& left, long num); 40 UnicodeString operator+(const UnicodeString& left, unsigned long num); 41 UnicodeString operator+(const UnicodeString& left, double num); 42 UnicodeString operator+(const UnicodeString& left, char num); 43 UnicodeString operator+(const UnicodeString& left, short num); 44 UnicodeString operator+(const UnicodeString& left, int num); 45 UnicodeString operator+(const UnicodeString& left, unsigned char num); 46 UnicodeString operator+(const UnicodeString& left, unsigned short num); 47 UnicodeString operator+(const UnicodeString& left, unsigned int num); 48 UnicodeString operator+(const UnicodeString& left, float num); 49 #if !UCONFIG_NO_FORMATTING 50 UnicodeString toString(const Formattable& f); // liu 51 UnicodeString toString(int32_t n); 52 #endif 53 UnicodeString toString(UBool b); 54 55 //----------------------------------------------------------------------------- 56 57 // Use the TESTCASE macro in subclasses of IntlTest. Define the 58 // runIndexedTest method in this fashion: 59 // 60 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 61 //| const char* &name, char* /*par*/) { 62 //| switch (index) { 63 //| TESTCASE(0,TestSomething); 64 //| TESTCASE(1,TestSomethingElse); 65 //| TESTCASE(2,TestAnotherThing); 66 //| default: name = ""; break; 67 //| } 68 //| } 69 #define TESTCASE(id,test) \ 70 case id: \ 71 name = #test; \ 72 if (exec) { \ 73 logln(#test "---"); \ 74 logln(); \ 75 test(); \ 76 } \ 77 break 78 79 // More convenient macros. These allow easy reordering of the test cases. 80 // 81 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 82 //| const char* &name, char* /*par*/) { 83 //| TESTCASE_AUTO_BEGIN; 84 //| TESTCASE_AUTO(TestSomething); 85 //| TESTCASE_AUTO(TestSomethingElse); 86 //| TESTCASE_AUTO(TestAnotherThing); 87 //| TESTCASE_AUTO_END; 88 //| } 89 #define TESTCASE_AUTO_BEGIN \ 90 do { \ 91 int32_t testCaseAutoNumber = 0 92 93 #define TESTCASE_AUTO(test) \ 94 if (index == testCaseAutoNumber++) { \ 95 name = #test; \ 96 if (exec) { \ 97 logln(#test "---"); \ 98 logln(); \ 99 test(); \ 100 } \ 101 break; \ 102 } else (void)0 103 104 #define TESTCASE_AUTO_CLASS(TestClass) \ 105 if (index == testCaseAutoNumber++) { \ 106 name = #TestClass; \ 107 if (exec) { \ 108 logln(#TestClass "---"); \ 109 logln(); \ 110 TestClass test; \ 111 callTest(test, par); \ 112 } \ 113 break; \ 114 } else (void)0 115 116 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \ 117 if (index == testCaseAutoNumber++) { \ 118 name = #TestClass; \ 119 if (exec) { \ 120 logln(#TestClass "---"); \ 121 logln(); \ 122 LocalPointer<IntlTest> test(create##TestClass()); \ 123 callTest(*test, par); \ 124 } \ 125 break; \ 126 } else (void)0 127 128 #define TESTCASE_AUTO_END \ 129 name = ""; \ 130 break; \ 131 } while (TRUE) 132 133 134 // WHERE Macro yields a literal string of the form "source_file_name:line number " 135 #define WHERE __FILE__ ":" XLINE(__LINE__) " " 136 #define XLINE(s) LINE(s) 137 #define LINE(s) #s 138 139 class IntlTest : public TestLog { 140 public: 141 142 IntlTest(); 143 // TestLog has a virtual destructor. 144 145 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden 146 147 virtual UBool setVerbose( UBool verbose = TRUE ); 148 virtual UBool setNoErrMsg( UBool no_err_msg = TRUE ); 149 virtual UBool setQuick( UBool quick = TRUE ); 150 virtual UBool setLeaks( UBool leaks = TRUE ); 151 virtual UBool setNotime( UBool no_time = TRUE ); 152 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE ); 153 virtual UBool setWriteGoldenData( UBool write_golden_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 /* JUnit-like assertions. Each returns TRUE if it succeeds. */ 288 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); 289 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE); 290 /** 291 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure 292 * @return TRUE on success, FALSE on failure. 293 */ 294 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); 295 UBool assertEquals(const char* message, const UnicodeString& expected, 296 const UnicodeString& actual, UBool possibleDataError=FALSE); 297 UBool assertEquals(const char* message, const char* expected, const char* actual); 298 UBool assertEquals(const char* message, UBool expected, UBool actual); 299 UBool assertEquals(const char* message, int32_t expected, int32_t actual); 300 UBool assertEquals(const char* message, int64_t expected, int64_t actual); 301 UBool assertEquals(const char* message, double expected, double actual); 302 UBool assertEquals(const char* message, UErrorCode expected, UErrorCode actual); 303 UBool assertEquals(const char* message, const UnicodeSet& expected, const UnicodeSet& actual); 304 UBool assertEquals(const char* message, 305 const std::vector<std::string>& expected, const std::vector<std::string>& actual); 306 #if !UCONFIG_NO_FORMATTING 307 UBool assertEquals(const char* message, const Formattable& expected, 308 const Formattable& actual, UBool possibleDataError=FALSE); 309 UBool assertEquals(const UnicodeString& message, const Formattable& expected, 310 const Formattable& actual); 311 #endif 312 UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE); 313 UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE); 314 UBool assertSuccess(const UnicodeString& message, UErrorCode ec); 315 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected, 316 const UnicodeString& actual, UBool possibleDataError=FALSE); 317 UBool assertEquals(const UnicodeString& message, const char* expected, 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 UBool assertEquals(const UnicodeString& message, double expected, double actual); 322 UBool assertEquals(const UnicodeString& message, UErrorCode expected, UErrorCode actual); 323 UBool assertEquals(const UnicodeString& message, const UnicodeSet& expected, const UnicodeSet& actual); 324 UBool assertEquals(const UnicodeString& message, 325 const std::vector<std::string>& expected, const std::vector<std::string>& actual); 326 327 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! 328 329 virtual UBool runTestLoop( char* testname, char* par, char *baseName ); 330 331 virtual int32_t IncErrorCount( void ); 332 333 virtual int32_t IncDataErrorCount( void ); 334 335 virtual UBool callTest( IntlTest& testToBeCalled, char* par, const char* basename = ""); 336 337 338 UBool verbose; 339 UBool no_err_msg; 340 UBool quick; 341 UBool leaks; 342 UBool warn_on_missing_data; 343 UBool write_golden_data; 344 UBool no_time; 345 int32_t threadCount; 346 347 private: 348 UBool LL_linestart; 349 int32_t LL_indentlevel; 350 351 int32_t errorCount; 352 int32_t dataErrorCount; 353 IntlTest* caller; 354 char* testPath; // specifies subtests 355 356 char basePath[1024]; 357 char currName[1024]; // current test name 358 359 //FILE *testoutfp; 360 void *testoutfp; 361 362 const char* proplines[kMaxProps]; 363 int32_t numProps; 364 365 protected: 366 std::string currErr; // Error message of the current test case 367 368 virtual void LL_err_message( const UnicodeString& message, UBool newline ); 369 virtual void LL_message( UnicodeString message, UBool newlin, UBool isErr = FALSE ); 370 371 // used for collation result reporting, defined here for convenience 372 373 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target); 374 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE); 375 // digits=-1 determines the number of digits automatically 376 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target); 377 static UnicodeString toHex(uint32_t number, int32_t digits=-1); 378 static inline UnicodeString toHex(int32_t number, int32_t digits=-1) { 379 return toHex((uint32_t)number, digits); 380 } 381 382 public: 383 static void setICU_DATA(); // Set up ICU_DATA if necessary. 384 385 static const char* pathToDataDirectory(); 386 387 public: 388 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks 389 static const char* loadTestData(UErrorCode& err); 390 virtual const char* getTestDataPath(UErrorCode& err); 391 static const char* getSourceTestData(UErrorCode& err); 392 static char *getUnidataPath(char path[]); 393 394 // static members 395 public: 396 static IntlTest* gTest; 397 398 }; 399 400 void it_log( UnicodeString message ); 401 void it_logln( UnicodeString message ); 402 void it_logln( void ); 403 void it_info( UnicodeString message ); 404 void it_infoln( UnicodeString message ); 405 void it_infoln( void ); 406 void it_err(void); 407 void it_err( UnicodeString message ); 408 void it_errln( UnicodeString message ); 409 void it_dataerr( UnicodeString message ); 410 void it_dataerrln( UnicodeString message ); 411 412 /** 413 * This is a variant of cintltst/ccolltst.c:CharsToUChars(). 414 * It converts a character string into a UnicodeString, with 415 * unescaping \u sequences. 416 */ 417 extern UnicodeString CharsToUnicodeString(const char* chars); 418 419 /* alias for CharsToUnicodeString */ 420 extern UnicodeString ctou(const char* chars); 421 422 #endif // _INTLTEST 423