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"
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
DoubleToUnicodeString(double num
);
38 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
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
);
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
);
48 UnicodeString
operator+(const UnicodeString
& left
, float num
);
49 #if !UCONFIG_NO_FORMATTING
50 UnicodeString
toString(const Formattable
& f
); // liu
51 UnicodeString
toString(int32_t n
);
53 UnicodeString
toString(UBool b
);
55 //-----------------------------------------------------------------------------
57 // Use the TESTCASE macro in subclasses of IntlTest. Define the
58 // runIndexedTest method in this fashion:
60 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
61 //| const char* &name, char* /*par*/) {
63 //| TESTCASE(0,TestSomething);
64 //| TESTCASE(1,TestSomethingElse);
65 //| TESTCASE(2,TestAnotherThing);
66 //| default: name = ""; break;
69 #define TESTCASE(id,test) \
79 // More convenient macros. These allow easy reordering of the test cases.
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;
89 #define TESTCASE_AUTO_BEGIN \
91 int32_t testCaseAutoNumber = 0
93 #define TESTCASE_AUTO(test) \
94 if (index == testCaseAutoNumber++) { \
104 #define TESTCASE_AUTO_CLASS(TestClass) \
105 if (index == testCaseAutoNumber++) { \
108 logln(#TestClass "---"); \
111 callTest(test, par); \
116 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
117 if (index == testCaseAutoNumber++) { \
120 logln(#TestClass "---"); \
122 LocalPointer<IntlTest> test(create##TestClass()); \
123 callTest(*test, par); \
128 #define TESTCASE_AUTO_END \
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)
139 class IntlTest
: public TestLog
{
143 // TestLog has a virtual destructor.
145 virtual UBool
runTest( char* name
= NULL
, char* par
= NULL
, char *baseName
= NULL
); // not to be overidden
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
);
151 virtual UBool
setNotime( UBool no_time
= TRUE
);
152 virtual UBool
setWarnOnMissingData( UBool warn_on_missing_data
= TRUE
);
153 virtual UBool
setWriteGoldenData( UBool write_golden_data
= TRUE
);
154 virtual int32_t setThreadCount( int32_t count
= 1);
156 virtual int32_t getErrors( void );
157 virtual int32_t getDataErrors (void );
159 virtual void setCaller( IntlTest
* callingTest
); // for internal use only
160 virtual void setPath( char* path
); // for internal use only
162 virtual void log( const UnicodeString
&message
);
164 virtual void logln( const UnicodeString
&message
);
166 virtual void logln( void );
169 * Replaces isICUVersionAtLeast and isICUVersionBefore
170 * log that an issue is known.
171 * Usually used this way:
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
177 UBool
logKnownIssue( const char *ticket
, const UnicodeString
&message
);
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
186 UBool
logKnownIssue( const char *ticket
);
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
196 UBool
logKnownIssue( const char *ticket
, const char *fmt
, ...);
198 virtual void info( const UnicodeString
&message
);
200 virtual void infoln( const UnicodeString
&message
);
202 virtual void infoln( void );
204 virtual void err(void);
206 virtual void err( const UnicodeString
&message
);
208 virtual void errln( const UnicodeString
&message
);
210 virtual void dataerr( const UnicodeString
&message
);
212 virtual void dataerrln( const UnicodeString
&message
);
214 void errcheckln(UErrorCode status
, const UnicodeString
&message
);
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
, ...);
223 void dataerr(const char *fmt
, ...);
224 void dataerrln(const char *fmt
, ...);
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.
232 void errcheckln(UErrorCode status
, const char *fmt
, ...);
234 // Print ALL named errors encountered so far
237 // print known issues. return TRUE if there were any.
238 UBool
printKnownIssues();
240 virtual void usage( void ) ;
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.
248 * @param seedp pointer to seed. Set *seedp to any negative value
249 * to restart the sequence.
251 static float random(int32_t* seedp
);
254 * Convenience method using a global seed.
256 static float random();
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.
266 icu_rand(uint32_t seed
= 1);
268 void seed(uint32_t seed
);
269 uint32_t operator()();
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.
282 enum { kMaxProps
= 16 };
284 virtual void setProperty(const char* propline
);
285 virtual const char* getProperty(const char* prop
);
287 /* JUnit-like assertions. Each returns TRUE if it succeeds. */
288 UBool
assertTrue(const char* message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
, const char *file
=NULL
, int line
=0);
289 UBool
assertFalse(const char* message
, UBool condition
, UBool quiet
=FALSE
, UBool possibleDataError
=FALSE
);
291 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
292 * @return TRUE on success, FALSE on failure.
294 UBool
assertSuccess(const char* message
, UErrorCode ec
, UBool possibleDataError
=FALSE
, const char *file
=NULL
, int line
=0);
295 UBool
assertEquals(const char* message
, const UnicodeString
& expected
,
296 const UnicodeString
& actual
, UBool possibleDataError
=FALSE
);
297 UBool
assertEquals(const char* message
, const char* expected
, const char* actual
);
298 UBool
assertEquals(const char* message
, UBool expected
, UBool actual
);
299 UBool
assertEquals(const char* message
, int32_t expected
, int32_t actual
);
300 UBool
assertEquals(const char* message
, int64_t expected
, int64_t actual
);
301 UBool
assertEquals(const char* message
, double expected
, double actual
);
302 UBool
assertEquals(const char* message
, UErrorCode expected
, UErrorCode actual
);
303 UBool
assertEquals(const char* message
, const UnicodeSet
& expected
, const UnicodeSet
& actual
);
304 UBool
assertEquals(const char* message
,
305 const std::vector
<std::string
>& expected
, const std::vector
<std::string
>& actual
);
306 #if !UCONFIG_NO_FORMATTING
307 UBool
assertEquals(const char* message
, const Formattable
& expected
,
308 const Formattable
& actual
, UBool possibleDataError
=FALSE
);
309 UBool
assertEquals(const UnicodeString
& message
, const Formattable
& expected
,
310 const Formattable
& actual
);
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
);
314 UBool
assertSuccess(const UnicodeString
& message
, UErrorCode ec
);
315 UBool
assertEquals(const UnicodeString
& message
, const UnicodeString
& expected
,
316 const UnicodeString
& actual
, UBool possibleDataError
=FALSE
);
317 UBool
assertEquals(const UnicodeString
& message
, const char* expected
, const char* actual
);
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
);
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
);
324 UBool
assertEquals(const UnicodeString
& message
,
325 const std::vector
<std::string
>& expected
, const std::vector
<std::string
>& actual
);
327 virtual void runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
); // overide !
329 virtual UBool
runTestLoop( char* testname
, char* par
, char *baseName
);
331 virtual int32_t IncErrorCount( void );
333 virtual int32_t IncDataErrorCount( void );
335 virtual UBool
callTest( IntlTest
& testToBeCalled
, char* par
);
342 UBool warn_on_missing_data
;
343 UBool write_golden_data
;
349 int32_t LL_indentlevel
;
352 int32_t dataErrorCount
;
354 char* testPath
; // specifies subtests
357 char currName
[1024]; // current test name
362 const char* proplines
[kMaxProps
];
367 virtual void LL_message( UnicodeString message
, UBool newline
);
369 // used for collation result reporting, defined here for convenience
371 static UnicodeString
&prettify(const UnicodeString
&source
, UnicodeString
&target
);
372 static UnicodeString
prettify(const UnicodeString
&source
, UBool parseBackslash
=FALSE
);
373 // digits=-1 determines the number of digits automatically
374 static UnicodeString
&appendHex(uint32_t number
, int32_t digits
, UnicodeString
&target
);
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
);
381 static void setICU_DATA(); // Set up ICU_DATA if necessary.
383 static const char* pathToDataDirectory();
386 UBool
run_phase2( char* name
, char* par
); // internally, supports reporting memory leaks
387 static const char* loadTestData(UErrorCode
& err
);
388 virtual const char* getTestDataPath(UErrorCode
& err
);
389 static const char* getSourceTestData(UErrorCode
& err
);
390 static char *getUnidataPath(char path
[]);
394 static IntlTest
* gTest
;
395 static const char* fgDataDir
;
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 );
406 void it_err( UnicodeString message
);
407 void it_errln( UnicodeString message
);
408 void it_dataerr( UnicodeString message
);
409 void it_dataerrln( UnicodeString message
);
412 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
413 * It converts a character string into a UnicodeString, with
414 * unescaping \u sequences.
416 extern UnicodeString
CharsToUnicodeString(const char* chars
);
418 /* alias for CharsToUnicodeString */
419 extern UnicodeString
ctou(const char* chars
);