]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/intltest.h
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / intltest.h
CommitLineData
b75a7d8f 1/********************************************************************
2ca993e8
A
2 * COPYRIGHT:
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
b75a7d8f
A
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
374ca955 14// The following includes utypes.h, uobject.h and unistr.h
b75a7d8f 15#include "unicode/fmtable.h"
374ca955 16#include "unicode/testlog.h"
b75a7d8f 17
51004dcb
A
18
19#if U_NO_DEFAULT_INCLUDE_UTF_HEADERS
20/* deprecated - make tests pass with U_NO_DEFAULT_INCLUDE_UTF_HEADERS */
2ca993e8 21#include "unicode/utf_old.h"
51004dcb
A
22#endif
23
b75a7d8f
A
24U_NAMESPACE_USE
25
4388f060 26#if U_PLATFORM == U_PF_OS390
b75a7d8f 27// avoid collision with math.h/log()
4388f060 28// this must be after including utypes.h so that U_PLATFORM is actually defined
b75a7d8f
A
29#pragma map(IntlTest::log( const UnicodeString &message ),"logos390")
30#endif
31
b75a7d8f
A
32//-----------------------------------------------------------------------------
33//convenience classes to ease porting code that uses the Java
34//string-concatenation operator (moved from findword test by rtg)
35UnicodeString UCharToUnicodeString(UChar c);
374ca955
A
36UnicodeString Int64ToUnicodeString(int64_t num);
37//UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
b75a7d8f
A
38UnicodeString operator+(const UnicodeString& left, long num);
39UnicodeString operator+(const UnicodeString& left, unsigned long num);
40UnicodeString operator+(const UnicodeString& left, double num);
57a6839d
A
41UnicodeString operator+(const UnicodeString& left, char num);
42UnicodeString operator+(const UnicodeString& left, short num);
43UnicodeString operator+(const UnicodeString& left, int num);
44UnicodeString operator+(const UnicodeString& left, unsigned char num);
45UnicodeString operator+(const UnicodeString& left, unsigned short num);
46UnicodeString operator+(const UnicodeString& left, unsigned int num);
b75a7d8f
A
47UnicodeString operator+(const UnicodeString& left, float num);
48#if !UCONFIG_NO_FORMATTING
49UnicodeString toString(const Formattable& f); // liu
374ca955 50UnicodeString toString(int32_t n);
b75a7d8f 51#endif
57a6839d
A
52UnicodeString toString(UBool b);
53
b75a7d8f
A
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 "---"); \
729e4ab9 73 logln(); \
b75a7d8f
A
74 test(); \
75 } \
76 break
77
729e4ab9
A
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
57a6839d
A
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
729e4ab9
A
127#define TESTCASE_AUTO_END \
128 name = ""; \
129 break; \
130 }
131
51004dcb
A
132#define TEST_ASSERT_TRUE(x) \
133 assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__)
134
57a6839d
A
135#define TEST_ASSERT_STATUS(x) \
136 assertSuccess(#x, (x), FALSE, __FILE__, __LINE__)
137
374ca955 138class IntlTest : public TestLog {
b75a7d8f
A
139public:
140
141 IntlTest();
729e4ab9 142 // TestLog has a virtual destructor.
b75a7d8f 143
729e4ab9 144 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
b75a7d8f
A
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 );
51004dcb 150 virtual UBool setNotime( UBool no_time = TRUE );
73c04bcf 151 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
729e4ab9 152 virtual int32_t setThreadCount( int32_t count = 1);
b75a7d8f
A
153
154 virtual int32_t getErrors( void );
73c04bcf 155 virtual int32_t getDataErrors (void );
b75a7d8f
A
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
57a6839d
A
166 /**
167 * Replaces isICUVersionAtLeast and isICUVersionBefore
168 * log that an issue is known.
2ca993e8 169 * Usually used this way:
57a6839d
A
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
b75a7d8f
A
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);
57a6839d 203
b75a7d8f
A
204 virtual void err( const UnicodeString &message );
205
206 virtual void errln( const UnicodeString &message );
207
73c04bcf
A
208 virtual void dataerr( const UnicodeString &message );
209
210 virtual void dataerrln( const UnicodeString &message );
57a6839d 211
729e4ab9 212 void errcheckln(UErrorCode status, const UnicodeString &message );
73c04bcf 213
b75a7d8f
A
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, ...);
73c04bcf
A
221 void dataerr(const char *fmt, ...);
222 void dataerrln(const char *fmt, ...);
57a6839d
A
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 */
729e4ab9 230 void errcheckln(UErrorCode status, const char *fmt, ...);
b75a7d8f
A
231
232 // Print ALL named errors encountered so far
2ca993e8 233 void printErrors();
57a6839d
A
234
235 // print known issues. return TRUE if there were any.
236 UBool printKnownIssues();
2ca993e8 237
b75a7d8f
A
238 virtual void usage( void ) ;
239
374ca955
A
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
2ca993e8
A
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
729e4ab9
A
280 enum { kMaxProps = 16 };
281
282 virtual void setProperty(const char* propline);
283 virtual const char* getProperty(const char* prop);
284
b75a7d8f 285protected:
374ca955 286 /* JUnit-like assertions. Each returns TRUE if it succeeds. */
51004dcb 287 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
374ca955 288 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE);
57a6839d
A
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);
374ca955 294 UBool assertEquals(const char* message, const UnicodeString& expected,
729e4ab9 295 const UnicodeString& actual, UBool possibleDataError=FALSE);
374ca955
A
296 UBool assertEquals(const char* message, const char* expected,
297 const char* actual);
57a6839d
A
298 UBool assertEquals(const char* message, UBool expected,
299 UBool actual);
51004dcb 300 UBool assertEquals(const char* message, int32_t expected, int32_t actual);
57a6839d 301 UBool assertEquals(const char* message, int64_t expected, int64_t actual);
2ca993e8 302 UBool assertEquals(const char* message, double expected, double actual);
374ca955
A
303#if !UCONFIG_NO_FORMATTING
304 UBool assertEquals(const char* message, const Formattable& expected,
57a6839d 305 const Formattable& actual, UBool possibleDataError=FALSE);
374ca955
A
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,
57a6839d 313 const UnicodeString& actual, UBool possibleDataError=FALSE);
374ca955
A
314 UBool assertEquals(const UnicodeString& message, const char* expected,
315 const char* actual);
57a6839d
A
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);
374ca955 319
b75a7d8f
A
320 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
321
729e4ab9 322 virtual UBool runTestLoop( char* testname, char* par, char *baseName );
b75a7d8f
A
323
324 virtual int32_t IncErrorCount( void );
325
73c04bcf
A
326 virtual int32_t IncDataErrorCount( void );
327
b75a7d8f
A
328 virtual UBool callTest( IntlTest& testToBeCalled, char* par );
329
330
73c04bcf
A
331 UBool verbose;
332 UBool no_err_msg;
333 UBool quick;
334 UBool leaks;
335 UBool warn_on_missing_data;
51004dcb 336 UBool no_time;
729e4ab9 337 int32_t threadCount;
b75a7d8f
A
338
339private:
73c04bcf 340 UBool LL_linestart;
b75a7d8f
A
341 int32_t LL_indentlevel;
342
343 int32_t errorCount;
73c04bcf 344 int32_t dataErrorCount;
b75a7d8f 345 IntlTest* caller;
73c04bcf 346 char* testPath; // specifies subtests
2ca993e8 347
729e4ab9 348 char basePath[1024];
57a6839d 349 char currName[1024]; // current test name
b75a7d8f 350
374ca955
A
351 //FILE *testoutfp;
352 void *testoutfp;
353
729e4ab9
A
354 const char* proplines[kMaxProps];
355 int32_t numProps;
356
b75a7d8f 357protected:
374ca955 358
b75a7d8f
A
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);
51004dcb 365 // digits=-1 determines the number of digits automatically
b75a7d8f 366 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
51004dcb
A
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 }
b75a7d8f 371
b75a7d8f
A
372public:
373 static void setICU_DATA(); // Set up ICU_DATA if necessary.
374
375 static const char* pathToDataDirectory();
376
377public:
378 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
379 static const char* loadTestData(UErrorCode& err);
374ca955
A
380 virtual const char* getTestDataPath(UErrorCode& err);
381 static const char* getSourceTestData(UErrorCode& err);
b331163b 382 static char *getUnidataPath(char path[]);
b75a7d8f
A
383
384// static members
385public:
386 static IntlTest* gTest;
387 static const char* fgDataDir;
388
389};
390
391void it_log( UnicodeString message );
392void it_logln( UnicodeString message );
393void it_logln( void );
394void it_info( UnicodeString message );
395void it_infoln( UnicodeString message );
396void it_infoln( void );
397void it_err(void);
398void it_err( UnicodeString message );
399void it_errln( UnicodeString message );
73c04bcf
A
400void it_dataerr( UnicodeString message );
401void it_dataerrln( UnicodeString message );
b75a7d8f 402
b75a7d8f
A
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 */
408extern UnicodeString CharsToUnicodeString(const char* chars);
409
374ca955
A
410/* alias for CharsToUnicodeString */
411extern UnicodeString ctou(const char* chars);
412
b75a7d8f 413#endif // _INTLTEST