1/******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2013, 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 41U_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) 52UnicodeString UCharToUnicodeString(UChar c); 53UnicodeString Int64ToUnicodeString(int64_t num); 54//UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type. 55UnicodeString operator+(const UnicodeString& left, long num); 56UnicodeString operator+(const UnicodeString& left, unsigned long num); 57UnicodeString operator+(const UnicodeString& left, double num); 58UnicodeString operator+(const UnicodeString& left, char num); 59UnicodeString operator+(const UnicodeString& left, short num); 60UnicodeString operator+(const UnicodeString& left, int num); 61UnicodeString operator+(const UnicodeString& left, unsigned char num); 62UnicodeString operator+(const UnicodeString& left, unsigned short num); 63UnicodeString operator+(const UnicodeString& left, unsigned int num); 64UnicodeString operator+(const UnicodeString& left, float num); 65#if !UCONFIG_NO_FORMATTING 66UnicodeString toString(const Formattable& f); // liu 67UnicodeString 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#define TEST_ASSERT_TRUE(x) \ 124 assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__) 125 126class IntlTest : public TestLog { 127public: 128 129 IntlTest(); 130 // TestLog has a virtual destructor. 131 132 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden 133 134 virtual UBool setVerbose( UBool verbose = TRUE ); 135 virtual UBool setNoErrMsg( UBool no_err_msg = TRUE ); 136 virtual UBool setQuick( UBool quick = TRUE ); 137 virtual UBool setLeaks( UBool leaks = TRUE ); 138 virtual UBool setNotime( UBool no_time = TRUE ); 139 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE ); 140 virtual int32_t setThreadCount( int32_t count = 1); 141 142 virtual int32_t getErrors( void ); 143 virtual int32_t getDataErrors (void ); 144 145 virtual void setCaller( IntlTest* callingTest ); // for internal use only 146 virtual void setPath( char* path ); // for internal use only 147 148 virtual void log( const UnicodeString &message ); 149 150 virtual void logln( const UnicodeString &message ); 151 152 virtual void logln( void ); 153 154 virtual void info( const UnicodeString &message ); 155 156 virtual void infoln( const UnicodeString &message ); 157 158 virtual void infoln( void ); 159 160 virtual void err(void); 161 162 virtual void err( const UnicodeString &message ); 163 164 virtual void errln( const UnicodeString &message ); 165 166 virtual void dataerr( const UnicodeString &message ); 167 168 virtual void dataerrln( const UnicodeString &message ); 169 170 void errcheckln(UErrorCode status, const UnicodeString &message ); 171 172 // convenience functions: sprintf() + errln() etc. 173 void log(const char *fmt, ...); 174 void logln(const char *fmt, ...); 175 void info(const char *fmt, ...); 176 void infoln(const char *fmt, ...); 177 void err(const char *fmt, ...); 178 void errln(const char *fmt, ...); 179 void dataerr(const char *fmt, ...); 180 void dataerrln(const char *fmt, ...); 181 void errcheckln(UErrorCode status, const char *fmt, ...); 182 183 // Print ALL named errors encountered so far 184 void printErrors(); 185 186 virtual void usage( void ) ; 187 188 /** 189 * Returns a uniform random value x, with 0.0 <= x < 1.0. Use 190 * with care: Does not return all possible values; returns one of 191 * 714,025 values, uniformly spaced. However, the period is 192 * effectively infinite. See: Numerical Recipes, section 7.1. 193 * 194 * @param seedp pointer to seed. Set *seedp to any negative value 195 * to restart the sequence. 196 */ 197 static float random(int32_t* seedp); 198 199 /** 200 * Convenience method using a global seed. 201 */ 202 static float random(); 203 204 /** 205 * Returns true if u_getVersion() < major.minor. 206 */ 207 static UBool isICUVersionBefore(int major, int minor) { 208 return isICUVersionBefore(major, minor, 0); 209 } 210 211 /** 212 * Returns true if u_getVersion() < major.minor.milli. 213 */ 214 static UBool isICUVersionBefore(int major, int minor, int milli); 215 216 /** 217 * Returns true if u_getVersion() >= major.minor. 218 */ 219 static UBool isICUVersionAtLeast(int major, int minor) { 220 return isICUVersionAtLeast(major, minor, 0); 221 } 222 223 /** 224 * Returns true if u_getVersion() >= major.minor.milli. 225 */ 226 static UBool isICUVersionAtLeast(int major, int minor, int milli) { 227 return !isICUVersionBefore(major, minor, milli); 228 } 229 230 enum { kMaxProps = 16 }; 231 232 virtual void setProperty(const char* propline); 233 virtual const char* getProperty(const char* prop); 234 235protected: 236 /* JUnit-like assertions. Each returns TRUE if it succeeds. */ 237 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); 238 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE); 239 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE); 240 UBool assertEquals(const char* message, const UnicodeString& expected, 241 const UnicodeString& actual, UBool possibleDataError=FALSE); 242 UBool assertEquals(const char* message, const char* expected, 243 const char* actual); 244 UBool assertEquals(const char* message, int32_t expected, int32_t actual); 245#if !UCONFIG_NO_FORMATTING 246 UBool assertEquals(const char* message, const Formattable& expected, 247 const Formattable& actual); 248 UBool assertEquals(const UnicodeString& message, const Formattable& expected, 249 const Formattable& actual); 250#endif 251 UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 252 UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE); 253 UBool assertSuccess(const UnicodeString& message, UErrorCode ec); 254 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected, 255 const UnicodeString& actual); 256 UBool assertEquals(const UnicodeString& message, const char* expected, 257 const char* actual); 258 259 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! 260 261 virtual UBool runTestLoop( char* testname, char* par, char *baseName ); 262 263 virtual int32_t IncErrorCount( void ); 264 265 virtual int32_t IncDataErrorCount( void ); 266 267 virtual UBool callTest( IntlTest& testToBeCalled, char* par ); 268 269 270 UBool verbose; 271 UBool no_err_msg; 272 UBool quick; 273 UBool leaks; 274 UBool warn_on_missing_data; 275 UBool no_time; 276 int32_t threadCount; 277 278private: 279 UBool LL_linestart; 280 int32_t LL_indentlevel; 281 282 int32_t errorCount; 283 int32_t dataErrorCount; 284 IntlTest* caller; 285 char* testPath; // specifies subtests 286 287 char basePath[1024]; 288 289 //FILE *testoutfp; 290 void *testoutfp; 291 292 const char* proplines[kMaxProps]; 293 int32_t numProps; 294 295protected: 296 297 virtual void LL_message( UnicodeString message, UBool newline ); 298 299 // used for collation result reporting, defined here for convenience 300 301 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target); 302 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE); 303 // digits=-1 determines the number of digits automatically 304 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target); 305 static UnicodeString toHex(uint32_t number, int32_t digits=-1); 306 static inline UnicodeString toHex(int32_t number, int32_t digits=-1) { 307 return toHex((uint32_t)number, digits); 308 } 309 310public: 311 static void setICU_DATA(); // Set up ICU_DATA if necessary. 312 313 static const char* pathToDataDirectory(); 314 315public: 316 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks 317 static const char* loadTestData(UErrorCode& err); 318 virtual const char* getTestDataPath(UErrorCode& err); 319 static const char* getSourceTestData(UErrorCode& err); 320 321// static members 322public: 323 static IntlTest* gTest; 324 static const char* fgDataDir; 325 326}; 327 328void it_log( UnicodeString message ); 329void it_logln( UnicodeString message ); 330void it_logln( void ); 331void it_info( UnicodeString message ); 332void it_infoln( UnicodeString message ); 333void it_infoln( void ); 334void it_err(void); 335void it_err( UnicodeString message ); 336void it_errln( UnicodeString message ); 337void it_dataerr( UnicodeString message ); 338void it_dataerrln( UnicodeString message ); 339 340/** 341 * This is a variant of cintltst/ccolltst.c:CharsToUChars(). 342 * It converts a character string into a UnicodeString, with 343 * unescaping \u sequences. 344 */ 345extern UnicodeString CharsToUnicodeString(const char* chars); 346 347/* alias for CharsToUnicodeString */ 348extern UnicodeString ctou(const char* chars); 349 350#endif // _INTLTEST 351