1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * IntlTest is a base class for tests. */
16 // The following includes utypes.h, uobject.h and unistr.h
17 #include "unicode/fmtable.h"
18 #include "unicode/testlog.h"
19 #include "unicode/uniset.h"
23 #if U_PLATFORM == U_PF_OS390
24 // avoid collision with math.h/log()
25 // this must be after including utypes.h so that U_PLATFORM is actually defined
26 #pragma map(IntlTest::log( const UnicodeString &message ),"logos390")
29 //-----------------------------------------------------------------------------
30 //convenience classes to ease porting code that uses the Java
31 //string-concatenation operator (moved from findword test by rtg)
32 UnicodeString
UCharToUnicodeString(UChar c
);
33 UnicodeString
Int64ToUnicodeString(int64_t num
);
34 UnicodeString
DoubleToUnicodeString(double num
);
35 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
36 UnicodeString
operator+(const UnicodeString
& left
, long num
);
37 UnicodeString
operator+(const UnicodeString
& left
, unsigned long num
);
38 UnicodeString
operator+(const UnicodeString
& left
, double num
);
39 UnicodeString
operator+(const UnicodeString
& left
, char num
);
40 UnicodeString
operator+(const UnicodeString
& left
, short num
);
41 UnicodeString
operator+(const UnicodeString
& left
, int num
);
42 UnicodeString
operator+(const UnicodeString
& left
, unsigned char num
);
43 UnicodeString
operator+(const UnicodeString
& left
, unsigned short num
);
44 UnicodeString
operator+(const UnicodeString
& left
, unsigned int num
);
45 UnicodeString
operator+(const UnicodeString
& left
, float num
);
46 #if !UCONFIG_NO_FORMATTING
47 UnicodeString
toString(const Formattable
& f
); // liu
48 UnicodeString
toString(int32_t n
);
50 UnicodeString
toString(UBool b
);
52 //-----------------------------------------------------------------------------
54 // Use the TESTCASE macro in subclasses of IntlTest. Define the
55 // runIndexedTest method in this fashion:
57 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
58 //| const char* &name, char* /*par*/) {
60 //| TESTCASE(0,TestSomething);
61 //| TESTCASE(1,TestSomethingElse);
62 //| TESTCASE(2,TestAnotherThing);
63 //| default: name = ""; break;
66 #define TESTCASE(id,test) \
76 // More convenient macros. These allow easy reordering of the test cases.
78 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
79 //| const char* &name, char* /*par*/) {
80 //| TESTCASE_AUTO_BEGIN;
81 //| TESTCASE_AUTO(TestSomething);
82 //| TESTCASE_AUTO(TestSomethingElse);
83 //| TESTCASE_AUTO(TestAnotherThing);
84 //| TESTCASE_AUTO_END;
86 #define TESTCASE_AUTO_BEGIN \
88 int32_t testCaseAutoNumber = 0
90 #define TESTCASE_AUTO(test) \
91 if (index == testCaseAutoNumber++) { \
101 #define TESTCASE_AUTO_CLASS(TestClass) \
102 if (index == testCaseAutoNumber++) { \
105 logln(#TestClass "---"); \
108 callTest(test, par); \
113 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
114 if (index == testCaseAutoNumber++) { \
117 logln(#TestClass "---"); \
119 LocalPointer<IntlTest> test(create##TestClass()); \
120 callTest(*test, par); \
125 #define TESTCASE_AUTO_END \
130 #define TEST_ASSERT_TRUE(x) \
131 assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__)
133 #define TEST_ASSERT_STATUS(x) \
134 assertSuccess(#x, (x), FALSE, __FILE__, __LINE__)
136 class IntlTest
: public TestLog
{
140 // TestLog has a virtual destructor.
142 virtual UBool
runTest( char* name
= NULL
, char* par
= NULL
, char *baseName
= NULL
); // not to be overidden
144 virtual UBool
setVerbose( UBool verbose
= TRUE
);
145 virtual UBool
setNoErrMsg( UBool no_err_msg
= TRUE
);
146 virtual UBool
setQuick( UBool quick
= TRUE
);
147 virtual UBool
setLeaks( UBool leaks
= TRUE
);
148 virtual UBool
setNotime( UBool no_time
= TRUE
);
149 virtual UBool
setWarnOnMissingData( UBool warn_on_missing_data
= TRUE
);
150 virtual int32_t setThreadCount( int32_t count
= 1);
152 virtual int32_t getErrors( void );
153 virtual int32_t getDataErrors (void );
155 virtual void setCaller( IntlTest
* callingTest
); // for internal use only
156 virtual void setPath( char* path
); // for internal use only
158 virtual void log( const UnicodeString
&message
);
160 virtual void logln( const UnicodeString
&message
);
162 virtual void logln( void );
165 * Replaces isICUVersionAtLeast and isICUVersionBefore
166 * log that an issue is known.
167 * Usually used this way:
168 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
169 * @param ticket ticket string, "12345" or "cldrbug:1234"
170 * @param message optional message string
171 * @return true if test should be skipped
173 UBool
logKnownIssue( const char *ticket
, const UnicodeString
&message
);
175 * Replaces isICUVersionAtLeast and isICUVersionBefore
176 * log that an issue is known.
177 * Usually used this way:
178 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
179 * @param ticket ticket string, "12345" or "cldrbug:1234"
180 * @return true if test should be skipped
182 UBool
logKnownIssue( const char *ticket
);
184 * Replaces isICUVersionAtLeast and isICUVersionBefore
185 * log that an issue is known.
186 * Usually used this way:
187 * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
188 * @param ticket ticket string, "12345" or "cldrbug:1234"
189 * @param message optional message string
190 * @return true if test should be skipped
192 UBool
logKnownIssue( const char *ticket
, const char *fmt
, ...);
194 virtual void info( const UnicodeString
&message
);
196 virtual void infoln( const UnicodeString
&message
);
198 virtual void infoln( void );
200 virtual void err(void);
202 virtual void err( const UnicodeString
&message
);
204 virtual void errln( const UnicodeString
&message
);
206 virtual void dataerr( const UnicodeString
&message
);
208 virtual void dataerrln( const UnicodeString
&message
);
210 void errcheckln(UErrorCode status
, const UnicodeString
&message
);
212 // convenience functions: sprintf() + errln() etc.
213 void log(const char *fmt
, ...);
214 void logln(const char *fmt
, ...);
215 void info(const char *fmt
, ...);
216 void infoln(const char *fmt
, ...);
217 void err(const char *fmt
, ...);
218 void errln(const char *fmt
, ...);
219 void dataerr(const char *fmt
, ...);
220 void dataerrln(const char *fmt
, ...);
223 * logs an error (even if status==U_ZERO_ERROR), but
224 * calls dataerrln() or errln() depending on the type of error.
225 * Does not report the status code.
226 * @param status parameter for selecting whether errln or dataerrln is called.
228 void errcheckln(UErrorCode status
, const char *fmt
, ...);
230 // Print ALL named errors encountered so far
233 // print known issues. return TRUE if there were any.
234 UBool
printKnownIssues();
236 virtual void usage( void ) ;
239 * Returns a uniform random value x, with 0.0 <= x < 1.0. Use
240 * with care: Does not return all possible values; returns one of
241 * 714,025 values, uniformly spaced. However, the period is
242 * effectively infinite. See: Numerical Recipes, section 7.1.
244 * @param seedp pointer to seed. Set *seedp to any negative value
245 * to restart the sequence.
247 static float random(int32_t* seedp
);
250 * Convenience method using a global seed.
252 static float random();
256 * Integer random numbers, similar to C++ std::minstd_rand, with the same algorithm
257 * and constants. Allow additional access to internal state, for use by monkey tests,
258 * which need to recreate previous random sequences beginning near a failure point.
262 icu_rand(uint32_t seed
= 1);
264 void seed(uint32_t seed
);
265 uint32_t operator()();
267 * Get a seed corresponding to the current state of the generator.
268 * Seeding any generator with this value will cause it to produce the
269 * same sequence as this one will from this point forward.
278 enum { kMaxProps
= 16 };
280 virtual void setProperty(const char* propline
);
281 virtual const char* getProperty(const char* prop
);
283 /* JUnit-like assertions. Each returns TRUE if it succeeds. */
284 UBool
assertTrue(const char* message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
, const char *file
=NULL
, int line
=0);
285 UBool
assertFalse(const char* message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
);
287 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
288 * @return TRUE on success, FALSE on failure.
290 UBool
assertSuccess(const char* message
, UErrorCode ec
, UBool possibleDataError
=FALSE
, const char *file
=NULL
, int line
=0);
291 UBool
assertEquals(const char* message
, const UnicodeString
& expected
,
292 const UnicodeString
& actual
, UBool possibleDataError
=FALSE
);
293 UBool
assertEquals(const char* message
, const char* expected
, const char* actual
);
294 UBool
assertEquals(const char* message
, UBool expected
, UBool actual
);
295 UBool
assertEquals(const char* message
, int32_t expected
, int32_t actual
);
296 UBool
assertEquals(const char* message
, int64_t expected
, int64_t actual
);
297 UBool
assertEquals(const char* message
, double expected
, double actual
);
298 UBool
assertEquals(const char* message
, UErrorCode expected
, UErrorCode actual
);
299 UBool
assertEquals(const char* message
, const UnicodeSet
& expected
, const UnicodeSet
& actual
);
300 #if !UCONFIG_NO_FORMATTING
301 UBool
assertEquals(const char* message
, const Formattable
& expected
,
302 const Formattable
& actual
, UBool possibleDataError
=FALSE
);
303 UBool
assertEquals(const UnicodeString
& message
, const Formattable
& expected
,
304 const Formattable
& actual
);
306 UBool
assertTrue(const UnicodeString
& message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
);
307 UBool
assertFalse(const UnicodeString
& message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
);
308 UBool
assertSuccess(const UnicodeString
& message
, UErrorCode ec
);
309 UBool
assertEquals(const UnicodeString
& message
, const UnicodeString
& expected
,
310 const UnicodeString
& actual
, UBool possibleDataError
=FALSE
);
311 UBool
assertEquals(const UnicodeString
& message
, const char* expected
, const char* actual
);
312 UBool
assertEquals(const UnicodeString
& message
, UBool expected
, UBool actual
);
313 UBool
assertEquals(const UnicodeString
& message
, int32_t expected
, int32_t actual
);
314 UBool
assertEquals(const UnicodeString
& message
, int64_t expected
, int64_t actual
);
315 UBool
assertEquals(const UnicodeString
& message
, double expected
, double actual
);
316 UBool
assertEquals(const UnicodeString
& message
, UErrorCode expected
, UErrorCode actual
);
317 UBool
assertEquals(const UnicodeString
& message
, const UnicodeSet
& expected
, const UnicodeSet
& actual
);
319 virtual void runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
); // overide !
321 virtual UBool
runTestLoop( char* testname
, char* par
, char *baseName
);
323 virtual int32_t IncErrorCount( void );
325 virtual int32_t IncDataErrorCount( void );
327 virtual UBool
callTest( IntlTest
& testToBeCalled
, char* par
);
334 UBool warn_on_missing_data
;
340 int32_t LL_indentlevel
;
343 int32_t dataErrorCount
;
345 char* testPath
; // specifies subtests
348 char currName
[1024]; // current test name
353 const char* proplines
[kMaxProps
];
358 virtual void LL_message( UnicodeString message
, UBool newline
);
360 // used for collation result reporting, defined here for convenience
362 static UnicodeString
&prettify(const UnicodeString
&source
, UnicodeString
&target
);
363 static UnicodeString
prettify(const UnicodeString
&source
, UBool parseBackslash
=FALSE
);
364 // digits=-1 determines the number of digits automatically
365 static UnicodeString
&appendHex(uint32_t number
, int32_t digits
, UnicodeString
&target
);
366 static UnicodeString
toHex(uint32_t number
, int32_t digits
=-1);
367 static inline UnicodeString
toHex(int32_t number
, int32_t digits
=-1) {
368 return toHex((uint32_t)number
, digits
);
372 static void setICU_DATA(); // Set up ICU_DATA if necessary.
374 static const char* pathToDataDirectory();
377 UBool
run_phase2( char* name
, char* par
); // internally, supports reporting memory leaks
378 static const char* loadTestData(UErrorCode
& err
);
379 virtual const char* getTestDataPath(UErrorCode
& err
);
380 static const char* getSourceTestData(UErrorCode
& err
);
381 static char *getUnidataPath(char path
[]);
385 static IntlTest
* gTest
;
386 static const char* fgDataDir
;
390 void it_log( UnicodeString message
);
391 void it_logln( UnicodeString message
);
392 void it_logln( void );
393 void it_info( UnicodeString message
);
394 void it_infoln( UnicodeString message
);
395 void it_infoln( void );
397 void it_err( UnicodeString message
);
398 void it_errln( UnicodeString message
);
399 void it_dataerr( UnicodeString message
);
400 void it_dataerrln( UnicodeString message
);
403 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
404 * It converts a character string into a UnicodeString, with
405 * unescaping \u sequences.
407 extern UnicodeString
CharsToUnicodeString(const char* chars
);
409 /* alias for CharsToUnicodeString */
410 extern UnicodeString
ctou(const char* chars
);