1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * IntlTest is a base class for tests. */
14 // The following includes utypes.h, uobject.h and unistr.h
15 #include "unicode/fmtable.h"
16 #include "unicode/testlog.h"
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"
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")
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
);
52 UnicodeString
toString(UBool b
);
54 //-----------------------------------------------------------------------------
56 // Use the TESTCASE macro in subclasses of IntlTest. Define the
57 // runIndexedTest method in this fashion:
59 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
60 //| const char* &name, char* /*par*/) {
62 //| TESTCASE(0,TestSomething);
63 //| TESTCASE(1,TestSomethingElse);
64 //| TESTCASE(2,TestAnotherThing);
65 //| default: name = ""; break;
68 #define TESTCASE(id,test) \
78 // More convenient macros. These allow easy reordering of the test cases.
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;
88 #define TESTCASE_AUTO_BEGIN \
90 int32_t testCaseAutoNumber = 0
92 #define TESTCASE_AUTO(test) \
93 if (index == testCaseAutoNumber++) { \
103 #define TESTCASE_AUTO_CLASS(TestClass) \
104 if (index == testCaseAutoNumber++) { \
107 logln(#TestClass "---"); \
110 callTest(test, par); \
115 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
116 if (index == testCaseAutoNumber++) { \
119 logln(#TestClass "---"); \
121 LocalPointer<IntlTest> test(create##TestClass()); \
122 callTest(*test, par); \
127 #define TESTCASE_AUTO_END \
132 #define TEST_ASSERT_TRUE(x) \
133 assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__)
135 #define TEST_ASSERT_STATUS(x) \
136 assertSuccess(#x, (x), FALSE, __FILE__, __LINE__)
138 class IntlTest
: public TestLog
{
142 // TestLog has a virtual destructor.
144 virtual UBool
runTest( char* name
= NULL
, char* par
= NULL
, char *baseName
= NULL
); // not to be overidden
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);
154 virtual int32_t getErrors( void );
155 virtual int32_t getDataErrors (void );
157 virtual void setCaller( IntlTest
* callingTest
); // for internal use only
158 virtual void setPath( char* path
); // for internal use only
160 virtual void log( const UnicodeString
&message
);
162 virtual void logln( const UnicodeString
&message
);
164 virtual void logln( void );
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
175 UBool
logKnownIssue( const char *ticket
, const UnicodeString
&message
);
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
184 UBool
logKnownIssue( const char *ticket
);
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
194 UBool
logKnownIssue( const char *ticket
, const char *fmt
, ...);
196 virtual void info( const UnicodeString
&message
);
198 virtual void infoln( const UnicodeString
&message
);
200 virtual void infoln( void );
202 virtual void err(void);
204 virtual void err( const UnicodeString
&message
);
206 virtual void errln( const UnicodeString
&message
);
208 virtual void dataerr( const UnicodeString
&message
);
210 virtual void dataerrln( const UnicodeString
&message
);
212 void errcheckln(UErrorCode status
, const UnicodeString
&message
);
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
, ...);
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.
230 void errcheckln(UErrorCode status
, const char *fmt
, ...);
232 // Print ALL named errors encountered so far
235 // print known issues. return TRUE if there were any.
236 UBool
printKnownIssues();
238 virtual void usage( void ) ;
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.
246 * @param seedp pointer to seed. Set *seedp to any negative value
247 * to restart the sequence.
249 static float random(int32_t* seedp
);
252 * Convenience method using a global seed.
254 static float random();
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.
264 icu_rand(uint32_t seed
= 1);
266 void seed(uint32_t seed
);
267 uint32_t operator()();
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.
280 enum { kMaxProps
= 16 };
282 virtual void setProperty(const char* propline
);
283 virtual const char* getProperty(const char* prop
);
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
);
290 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
291 * @return TRUE on success, FALSE on failure.
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
,
298 UBool
assertEquals(const char* message
, UBool expected
,
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
);
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
,
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
);
320 virtual void runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
); // overide !
322 virtual UBool
runTestLoop( char* testname
, char* par
, char *baseName
);
324 virtual int32_t IncErrorCount( void );
326 virtual int32_t IncDataErrorCount( void );
328 virtual UBool
callTest( IntlTest
& testToBeCalled
, char* par
);
335 UBool warn_on_missing_data
;
341 int32_t LL_indentlevel
;
344 int32_t dataErrorCount
;
346 char* testPath
; // specifies subtests
349 char currName
[1024]; // current test name
354 const char* proplines
[kMaxProps
];
359 virtual void LL_message( UnicodeString message
, UBool newline
);
361 // used for collation result reporting, defined here for convenience
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
);
373 static void setICU_DATA(); // Set up ICU_DATA if necessary.
375 static const char* pathToDataDirectory();
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
[]);
386 static IntlTest
* gTest
;
387 static const char* fgDataDir
;
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 );
398 void it_err( UnicodeString message
);
399 void it_errln( UnicodeString message
);
400 void it_dataerr( UnicodeString message
);
401 void it_dataerrln( UnicodeString message
);
404 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
405 * It converts a character string into a UnicodeString, with
406 * unescaping \u sequences.
408 extern UnicodeString
CharsToUnicodeString(const char* chars
);
410 /* alias for CharsToUnicodeString */
411 extern UnicodeString
ctou(const char* chars
);