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