1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2012, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 8 /** 9 * IntlTest is a base class for tests. */ 10 11 #ifndef _INTLTEST 12 #define _INTLTEST 13 14 // The following includes utypes.h, uobject.h and unistr.h 15 #include "unicode/fmtable.h" 16 #include "unicode/testlog.h" 17 18 19 #if U_NO_DEFAULT_INCLUDE_UTF_HEADERS 20 /* deprecated - make tests pass with U_NO_DEFAULT_INCLUDE_UTF_HEADERS */ 21 #include "unicode/utf_old.h" 22 #endif 23 24 /** 25 * \def ICU_USE_THREADS 26 * 27 * Enables multi-threaded testing. Moved here from uconfig.h. 28 * Default: enabled 29 * 30 * This switch used to allow thread support (use of mutexes) to be compiled out of ICU. 31 */ 32 #ifdef ICU_USE_THREADS 33 /* Use the predefined value. */ 34 #elif defined(APP_NO_THREADS) 35 /* APP_NO_THREADS is an old symbol. We'll honour it if present. */ 36 # define ICU_USE_THREADS 0 37 #else 38 # define ICU_USE_THREADS 1 39 #endif 40 41 U_NAMESPACE_USE 42 43 #if U_PLATFORM == U_PF_OS390 44 // avoid collision with math.h/log() 45 // this must be after including utypes.h so that U_PLATFORM is actually defined 46 #pragma map(IntlTest::log( const UnicodeString &message ),"logos390") 47 #endif 48 49 //----------------------------------------------------------------------------- 50 //convenience classes to ease porting code that uses the Java 51 //string-concatenation operator (moved from findword test by rtg) 52 UnicodeString UCharToUnicodeString(UChar c); 53 UnicodeString Int64ToUnicodeString(int64_t num); 54 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type. 55 UnicodeString operator+(const UnicodeString& left, long num); 56 UnicodeString operator+(const UnicodeString& left, unsigned long num); 57 UnicodeString operator+(const UnicodeString& left, double num); 58 UnicodeString operator+(const UnicodeString& left, char num); 59 UnicodeString operator+(const UnicodeString& left, short num); 60 UnicodeString operator+(const UnicodeString& left, int num); 61 UnicodeString operator+(const UnicodeString& left, unsigned char num); 62 UnicodeString operator+(const UnicodeString& left, unsigned short num); 63 UnicodeString operator+(const UnicodeString& left, unsigned int num); 64 UnicodeString operator+(const UnicodeString& left, float num); 65 #if !UCONFIG_NO_FORMATTING 66 UnicodeString toString(const Formattable& f); // liu 67 UnicodeString toString(int32_t n); 68 #endif 69 //----------------------------------------------------------------------------- 70 71 // Use the TESTCASE macro in subclasses of IntlTest. Define the 72 // runIndexedTest method in this fashion: 73 // 74 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 75 //| const char* &name, char* /*par*/) { 76 //| switch (index) { 77 //| TESTCASE(0,TestSomething); 78 //| TESTCASE(1,TestSomethingElse); 79 //| TESTCASE(2,TestAnotherThing); 80 //| default: name = ""; break; 81 //| } 82 //| } 83 #define TESTCASE(id,test) \ 84 case id: \ 85 name = #test; \ 86 if (exec) { \ 87 logln(#test "---"); \ 88 logln(); \ 89 test(); \ 90 } \ 91 break 92 93 // More convenient macros. These allow easy reordering of the test cases. 94 // 95 //| void MyTest::runIndexedTest(int32_t index, UBool exec, 96 //| const char* &name, char* /*par*/) { 97 //| TESTCASE_AUTO_BEGIN; 98 //| TESTCASE_AUTO(TestSomething); 99 //| TESTCASE_AUTO(TestSomethingElse); 100 //| TESTCASE_AUTO(TestAnotherThing); 101 //| TESTCASE_AUTO_END; 102 //| } 103 #define TESTCASE_AUTO_BEGIN \ 104 for(;;) { \ 105 int32_t testCaseAutoNumber = 0 106 107 #define TESTCASE_AUTO(test) \ 108 if (index == testCaseAutoNumber++) { \ 109 name = #test; \ 110 if (exec) { \ 111 logln(#test "---"); \ 112 logln(); \ 113 test(); \ 114 } \ 115 break; \ 116 } 117 118 #define TESTCASE_AUTO_END \ 119 name = ""; \ 120 break; \ 121 } 122 123 class IntlTest : public TestLog { 124 public: 125 126 IntlTest(); 127 // TestLog has a virtual destructor. 128 129 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden 130 131 virtual UBool setVerbose( UBool verbose = TRUE ); 132 virtual UBool setNoErrMsg( UBool no_err_msg = TRUE ); 133 virtual UBool setQuick( UBool quick = TRUE ); 134 virtual UBool setLeaks( UBool leaks = TRUE ); 135 virtual UBool setNotime( UBool no_time = TRUE ); 136 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE ); 137 virtual int32_t setThreadCount( int32_t count = 1); 138 139 virtual int32_t getErrors( void ); 140 virtual int32_t getDataErrors (void ); 141 142 virtual void setCaller( IntlTest* callingTest ); // for internal use only 143 virtual void setPath( char* path ); // for internal use only 144 145 virtual void log( const UnicodeString &message ); 146 147 virtual void logln( const UnicodeString &message ); 148 149 virtual void logln( void ); 150 151 virtual void info( const UnicodeString &message ); 152 153 virtual void infoln( const UnicodeString &message ); 154 155 virtual void infoln( void ); 156 157 virtual void err(void); 158 159 virtual void err( const UnicodeString &message ); 160 161 virtual void errln( const UnicodeString &message ); 162 163 virtual void dataerr( const UnicodeString &message ); 164 165 virtual void dataerrln( const UnicodeString &message ); 166 167 void errcheckln(UErrorCode status, const UnicodeString &message ); 168 169 // convenience functions: sprintf() + errln() etc. 170 void log(const char *fmt, ...); 171 void logln(const char *fmt, ...); 172 void info(const char *fmt, ...); 173 void infoln(const char *fmt, ...); 174 void err(const char *fmt, ...); 175 void errln(const char *fmt, ...); 176 void dataerr(const char *fmt, ...); 177 void dataerrln(const char *fmt, ...); 178 void errcheckln(UErrorCode status, const char *fmt, ...); 179 180 // Print ALL named errors encountered so far 181 void printErrors(); 182 183 virtual void usage( void ) ; 184 185 /** 186 * Returns a uniform random value x, with 0.0 <= x < 1.0. Use 187 * with care: Does not return all possible values; returns one of 188 * 714,025 values, uniformly spaced. However, the period is 189 * effectively infinite. See: Numerical Recipes, section 7.1. 190 * 191 * @param seedp pointer to seed. Set *seedp to any negative value 192 * to restart the sequence. 193 */ 194 static float random(int32_t* seedp); 195 196 /** 197 * Convenience method using a global seed. 198 */ 199 static float random(); 200 201 /** 202 * Returns true if u_getVersion() < major.minor. 203 */ isICUVersionBefore(int major,int minor)204 static UBool isICUVersionBefore(int major, int minor) { 205 return isICUVersionBefore(major, minor, 0); 206 } 207 208 /** 209 * Returns true if u_getVersion() < major.minor.milli. 210 */ 211 static UBool isICUVersionBefore(int major, int minor, int milli); 212 213 /** 214 * Returns true if u_getVersion() >= major.minor. 215 */ isICUVersionAtLeast(int major,int minor)216 static UBool isICUVersionAtLeast(int major, int minor) { 217 return isICUVersionAtLeast(major, minor, 0); 218 } 219 220 /** 221 * Returns true if u_getVersion() >= major.minor.milli. 222 */ isICUVersionAtLeast(int major,int minor,int milli)223 static UBool isICUVersionAtLeast(int major, int minor, int milli) { 224 return !isICUVersionBefore(major, minor, milli); 225 } 226 227 enum { kMaxProps = 16 }; 228 229 virtual void setProperty(const char* propline); 230 virtual const char* getProperty(const char* prop); 231 232 protected: 233 /* JUnit-like assertions. Each returns TRUE if it succeeds. */ 234 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE); 235 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE); 236 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE); 237 UBool assertEquals(const char* message, const UnicodeString& expected, 238 const UnicodeString& actual, UBool possibleDataError=FALSE); 239 UBool assertEquals(const char* message, const char* expected, 240 const char* actual); 241 UBool assertEquals(const char* message, int32_t expected, int32_t actual); 242 #if !UCONFIG_NO_FORMATTING 243 UBool assertEquals(const char* message, const Formattable& expected, 244 const Formattable& actual); 245 UBool assertEquals(const UnicodeString& message, const Formattable& expected, 246 const Formattable& actual); 247 #endif 248 UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 249 UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 250 UBool assertSuccess(const UnicodeString& message, UErrorCode ec); 251 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected, 252 const UnicodeString& actual); 253 UBool assertEquals(const UnicodeString& message, const char* expected, 254 const char* actual); 255 256 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! 257 258 virtual UBool runTestLoop( char* testname, char* par, char *baseName ); 259 260 virtual int32_t IncErrorCount( void ); 261 262 virtual int32_t IncDataErrorCount( void ); 263 264 virtual UBool callTest( IntlTest& testToBeCalled, char* par ); 265 266 267 UBool verbose; 268 UBool no_err_msg; 269 UBool quick; 270 UBool leaks; 271 UBool warn_on_missing_data; 272 UBool no_time; 273 int32_t threadCount; 274 275 private: 276 UBool LL_linestart; 277 int32_t LL_indentlevel; 278 279 int32_t errorCount; 280 int32_t dataErrorCount; 281 IntlTest* caller; 282 char* testPath; // specifies subtests 283 284 char basePath[1024]; 285 286 //FILE *testoutfp; 287 void *testoutfp; 288 289 const char* proplines[kMaxProps]; 290 int32_t numProps; 291 292 protected: 293 294 virtual void LL_message( UnicodeString message, UBool newline ); 295 296 // used for collation result reporting, defined here for convenience 297 298 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target); 299 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE); 300 // digits=-1 determines the number of digits automatically 301 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target); 302 static UnicodeString toHex(uint32_t number, int32_t digits=-1); 303 static inline UnicodeString toHex(int32_t number, int32_t digits=-1) { 304 return toHex((uint32_t)number, digits); 305 } 306 307 public: 308 static void setICU_DATA(); // Set up ICU_DATA if necessary. 309 310 static const char* pathToDataDirectory(); 311 312 public: 313 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks 314 static const char* loadTestData(UErrorCode& err); 315 virtual const char* getTestDataPath(UErrorCode& err); 316 static const char* getSourceTestData(UErrorCode& err); 317 318 // static members 319 public: 320 static IntlTest* gTest; 321 static const char* fgDataDir; 322 323 }; 324 325 void it_log( UnicodeString message ); 326 void it_logln( UnicodeString message ); 327 void it_logln( void ); 328 void it_info( UnicodeString message ); 329 void it_infoln( UnicodeString message ); 330 void it_infoln( void ); 331 void it_err(void); 332 void it_err( UnicodeString message ); 333 void it_errln( UnicodeString message ); 334 void it_dataerr( UnicodeString message ); 335 void it_dataerrln( UnicodeString message ); 336 337 /** 338 * This is a variant of cintltst/ccolltst.c:CharsToUChars(). 339 * It converts a character string into a UnicodeString, with 340 * unescaping \u sequences. 341 */ 342 extern UnicodeString CharsToUnicodeString(const char* chars); 343 344 /* alias for CharsToUnicodeString */ 345 extern UnicodeString ctou(const char* chars); 346 347 #endif // _INTLTEST 348