]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f | 3 | /******************************************************************** |
2ca993e8 A |
4 | * COPYRIGHT: |
5 | * Copyright (c) 1997-2016, International Business Machines Corporation and | |
b75a7d8f A |
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 | ||
374ca955 | 16 | // The following includes utypes.h, uobject.h and unistr.h |
b75a7d8f | 17 | #include "unicode/fmtable.h" |
374ca955 | 18 | #include "unicode/testlog.h" |
0f5d89e8 | 19 | #include "unicode/uniset.h" |
51004dcb | 20 | |
340931cb A |
21 | #include <vector> |
22 | #include <string> | |
23 | ||
b75a7d8f A |
24 | U_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) | |
35 | UnicodeString UCharToUnicodeString(UChar c); | |
374ca955 | 36 | UnicodeString Int64ToUnicodeString(int64_t num); |
0f5d89e8 | 37 | UnicodeString DoubleToUnicodeString(double num); |
374ca955 | 38 | //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type. |
b75a7d8f A |
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); | |
57a6839d A |
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); | |
b75a7d8f A |
48 | UnicodeString operator+(const UnicodeString& left, float num); |
49 | #if !UCONFIG_NO_FORMATTING | |
50 | UnicodeString toString(const Formattable& f); // liu | |
374ca955 | 51 | UnicodeString toString(int32_t n); |
b75a7d8f | 52 | #endif |
57a6839d A |
53 | UnicodeString toString(UBool b); |
54 | ||
b75a7d8f A |
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 "---"); \ | |
729e4ab9 | 74 | logln(); \ |
b75a7d8f A |
75 | test(); \ |
76 | } \ | |
77 | break | |
78 | ||
729e4ab9 A |
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 \ | |
340931cb | 90 | do { \ |
729e4ab9 A |
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; \ | |
340931cb | 102 | } else (void)0 |
729e4ab9 | 103 | |
57a6839d A |
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; \ | |
340931cb | 114 | } else (void)0 |
57a6839d A |
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; \ | |
340931cb | 126 | } else (void)0 |
57a6839d | 127 | |
729e4ab9 A |
128 | #define TESTCASE_AUTO_END \ |
129 | name = ""; \ | |
130 | break; \ | |
340931cb | 131 | } while (TRUE) |
729e4ab9 | 132 | |
51004dcb | 133 | |
3d1f044b A |
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 | |
57a6839d | 138 | |
374ca955 | 139 | class IntlTest : public TestLog { |
b75a7d8f A |
140 | public: |
141 | ||
142 | IntlTest(); | |
729e4ab9 | 143 | // TestLog has a virtual destructor. |
b75a7d8f | 144 | |
729e4ab9 | 145 | virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden |
b75a7d8f A |
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 ); | |
51004dcb | 151 | virtual UBool setNotime( UBool no_time = TRUE ); |
73c04bcf | 152 | virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE ); |
340931cb | 153 | virtual UBool setWriteGoldenData( UBool write_golden_data = TRUE ); |
729e4ab9 | 154 | virtual int32_t setThreadCount( int32_t count = 1); |
b75a7d8f A |
155 | |
156 | virtual int32_t getErrors( void ); | |
73c04bcf | 157 | virtual int32_t getDataErrors (void ); |
b75a7d8f A |
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 | ||
57a6839d A |
168 | /** |
169 | * Replaces isICUVersionAtLeast and isICUVersionBefore | |
170 | * log that an issue is known. | |
2ca993e8 | 171 | * Usually used this way: |
57a6839d A |
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 | ||
b75a7d8f A |
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); | |
57a6839d | 205 | |
b75a7d8f A |
206 | virtual void err( const UnicodeString &message ); |
207 | ||
208 | virtual void errln( const UnicodeString &message ); | |
209 | ||
73c04bcf A |
210 | virtual void dataerr( const UnicodeString &message ); |
211 | ||
212 | virtual void dataerrln( const UnicodeString &message ); | |
57a6839d | 213 | |
729e4ab9 | 214 | void errcheckln(UErrorCode status, const UnicodeString &message ); |
73c04bcf | 215 | |
b75a7d8f A |
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, ...); | |
73c04bcf A |
223 | void dataerr(const char *fmt, ...); |
224 | void dataerrln(const char *fmt, ...); | |
57a6839d A |
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 | */ | |
729e4ab9 | 232 | void errcheckln(UErrorCode status, const char *fmt, ...); |
b75a7d8f A |
233 | |
234 | // Print ALL named errors encountered so far | |
2ca993e8 | 235 | void printErrors(); |
57a6839d A |
236 | |
237 | // print known issues. return TRUE if there were any. | |
238 | UBool printKnownIssues(); | |
2ca993e8 | 239 | |
b75a7d8f A |
240 | virtual void usage( void ) ; |
241 | ||
374ca955 A |
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 | ||
2ca993e8 A |
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 | ||
729e4ab9 A |
282 | enum { kMaxProps = 16 }; |
283 | ||
284 | virtual void setProperty(const char* propline); | |
285 | virtual const char* getProperty(const char* prop); | |
286 | ||
374ca955 | 287 | /* JUnit-like assertions. Each returns TRUE if it succeeds. */ |
51004dcb | 288 | UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0); |
0f5d89e8 | 289 | UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE); |
57a6839d A |
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); | |
374ca955 | 295 | UBool assertEquals(const char* message, const UnicodeString& expected, |
729e4ab9 | 296 | const UnicodeString& actual, UBool possibleDataError=FALSE); |
0f5d89e8 A |
297 | UBool assertEquals(const char* message, const char* expected, const char* actual); |
298 | UBool assertEquals(const char* message, UBool expected, UBool actual); | |
51004dcb | 299 | UBool assertEquals(const char* message, int32_t expected, int32_t actual); |
57a6839d | 300 | UBool assertEquals(const char* message, int64_t expected, int64_t actual); |
2ca993e8 | 301 | UBool assertEquals(const char* message, double expected, double actual); |
0f5d89e8 A |
302 | UBool assertEquals(const char* message, UErrorCode expected, UErrorCode actual); |
303 | UBool assertEquals(const char* message, const UnicodeSet& expected, const UnicodeSet& actual); | |
340931cb A |
304 | UBool assertEquals(const char* message, |
305 | const std::vector<std::string>& expected, const std::vector<std::string>& actual); | |
374ca955 A |
306 | #if !UCONFIG_NO_FORMATTING |
307 | UBool assertEquals(const char* message, const Formattable& expected, | |
57a6839d | 308 | const Formattable& actual, UBool possibleDataError=FALSE); |
374ca955 A |
309 | UBool assertEquals(const UnicodeString& message, const Formattable& expected, |
310 | const Formattable& actual); | |
311 | #endif | |
0f5d89e8 A |
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); | |
374ca955 A |
314 | UBool assertSuccess(const UnicodeString& message, UErrorCode ec); |
315 | UBool assertEquals(const UnicodeString& message, const UnicodeString& expected, | |
57a6839d | 316 | const UnicodeString& actual, UBool possibleDataError=FALSE); |
0f5d89e8 | 317 | UBool assertEquals(const UnicodeString& message, const char* expected, const char* actual); |
57a6839d A |
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); | |
0f5d89e8 A |
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); | |
340931cb A |
324 | UBool assertEquals(const UnicodeString& message, |
325 | const std::vector<std::string>& expected, const std::vector<std::string>& actual); | |
374ca955 | 326 | |
b75a7d8f A |
327 | virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! |
328 | ||
729e4ab9 | 329 | virtual UBool runTestLoop( char* testname, char* par, char *baseName ); |
b75a7d8f A |
330 | |
331 | virtual int32_t IncErrorCount( void ); | |
332 | ||
73c04bcf A |
333 | virtual int32_t IncDataErrorCount( void ); |
334 | ||
b75a7d8f A |
335 | virtual UBool callTest( IntlTest& testToBeCalled, char* par ); |
336 | ||
337 | ||
73c04bcf A |
338 | UBool verbose; |
339 | UBool no_err_msg; | |
340 | UBool quick; | |
341 | UBool leaks; | |
342 | UBool warn_on_missing_data; | |
340931cb | 343 | UBool write_golden_data; |
51004dcb | 344 | UBool no_time; |
729e4ab9 | 345 | int32_t threadCount; |
b75a7d8f A |
346 | |
347 | private: | |
73c04bcf | 348 | UBool LL_linestart; |
b75a7d8f A |
349 | int32_t LL_indentlevel; |
350 | ||
351 | int32_t errorCount; | |
73c04bcf | 352 | int32_t dataErrorCount; |
b75a7d8f | 353 | IntlTest* caller; |
73c04bcf | 354 | char* testPath; // specifies subtests |
2ca993e8 | 355 | |
729e4ab9 | 356 | char basePath[1024]; |
57a6839d | 357 | char currName[1024]; // current test name |
b75a7d8f | 358 | |
374ca955 A |
359 | //FILE *testoutfp; |
360 | void *testoutfp; | |
361 | ||
729e4ab9 A |
362 | const char* proplines[kMaxProps]; |
363 | int32_t numProps; | |
364 | ||
b75a7d8f | 365 | protected: |
374ca955 | 366 | |
b75a7d8f A |
367 | virtual void LL_message( UnicodeString message, UBool newline ); |
368 | ||
369 | // used for collation result reporting, defined here for convenience | |
370 | ||
371 | static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target); | |
372 | static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE); | |
51004dcb | 373 | // digits=-1 determines the number of digits automatically |
b75a7d8f | 374 | static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target); |
51004dcb A |
375 | static UnicodeString toHex(uint32_t number, int32_t digits=-1); |
376 | static inline UnicodeString toHex(int32_t number, int32_t digits=-1) { | |
377 | return toHex((uint32_t)number, digits); | |
378 | } | |
b75a7d8f | 379 | |
b75a7d8f A |
380 | public: |
381 | static void setICU_DATA(); // Set up ICU_DATA if necessary. | |
382 | ||
383 | static const char* pathToDataDirectory(); | |
384 | ||
385 | public: | |
386 | UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks | |
387 | static const char* loadTestData(UErrorCode& err); | |
374ca955 A |
388 | virtual const char* getTestDataPath(UErrorCode& err); |
389 | static const char* getSourceTestData(UErrorCode& err); | |
b331163b | 390 | static char *getUnidataPath(char path[]); |
b75a7d8f A |
391 | |
392 | // static members | |
393 | public: | |
394 | static IntlTest* gTest; | |
395 | static const char* fgDataDir; | |
396 | ||
397 | }; | |
398 | ||
399 | void it_log( UnicodeString message ); | |
400 | void it_logln( UnicodeString message ); | |
401 | void it_logln( void ); | |
402 | void it_info( UnicodeString message ); | |
403 | void it_infoln( UnicodeString message ); | |
404 | void it_infoln( void ); | |
405 | void it_err(void); | |
406 | void it_err( UnicodeString message ); | |
407 | void it_errln( UnicodeString message ); | |
73c04bcf A |
408 | void it_dataerr( UnicodeString message ); |
409 | void it_dataerrln( UnicodeString message ); | |
b75a7d8f | 410 | |
b75a7d8f A |
411 | /** |
412 | * This is a variant of cintltst/ccolltst.c:CharsToUChars(). | |
413 | * It converts a character string into a UnicodeString, with | |
414 | * unescaping \u sequences. | |
415 | */ | |
416 | extern UnicodeString CharsToUnicodeString(const char* chars); | |
417 | ||
374ca955 A |
418 | /* alias for CharsToUnicodeString */ |
419 | extern UnicodeString ctou(const char* chars); | |
420 | ||
b75a7d8f | 421 | #endif // _INTLTEST |