]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/intltest.h
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / intltest.h
CommitLineData
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
b75a7d8f
A
21U_NAMESPACE_USE
22
4388f060 23#if U_PLATFORM == U_PF_OS390
b75a7d8f 24// avoid collision with math.h/log()
4388f060 25// this must be after including utypes.h so that U_PLATFORM is actually defined
b75a7d8f
A
26#pragma map(IntlTest::log( const UnicodeString &message ),"logos390")
27#endif
28
b75a7d8f
A
29//-----------------------------------------------------------------------------
30//convenience classes to ease porting code that uses the Java
31//string-concatenation operator (moved from findword test by rtg)
32UnicodeString UCharToUnicodeString(UChar c);
374ca955 33UnicodeString Int64ToUnicodeString(int64_t num);
0f5d89e8 34UnicodeString DoubleToUnicodeString(double num);
374ca955 35//UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
b75a7d8f
A
36UnicodeString operator+(const UnicodeString& left, long num);
37UnicodeString operator+(const UnicodeString& left, unsigned long num);
38UnicodeString operator+(const UnicodeString& left, double num);
57a6839d
A
39UnicodeString operator+(const UnicodeString& left, char num);
40UnicodeString operator+(const UnicodeString& left, short num);
41UnicodeString operator+(const UnicodeString& left, int num);
42UnicodeString operator+(const UnicodeString& left, unsigned char num);
43UnicodeString operator+(const UnicodeString& left, unsigned short num);
44UnicodeString operator+(const UnicodeString& left, unsigned int num);
b75a7d8f
A
45UnicodeString operator+(const UnicodeString& left, float num);
46#if !UCONFIG_NO_FORMATTING
47UnicodeString toString(const Formattable& f); // liu
374ca955 48UnicodeString toString(int32_t n);
b75a7d8f 49#endif
57a6839d
A
50UnicodeString toString(UBool b);
51
b75a7d8f
A
52//-----------------------------------------------------------------------------
53
54// Use the TESTCASE macro in subclasses of IntlTest. Define the
55// runIndexedTest method in this fashion:
56//
57//| void MyTest::runIndexedTest(int32_t index, UBool exec,
58//| const char* &name, char* /*par*/) {
59//| switch (index) {
60//| TESTCASE(0,TestSomething);
61//| TESTCASE(1,TestSomethingElse);
62//| TESTCASE(2,TestAnotherThing);
63//| default: name = ""; break;
64//| }
65//| }
66#define TESTCASE(id,test) \
67 case id: \
68 name = #test; \
69 if (exec) { \
70 logln(#test "---"); \
729e4ab9 71 logln(); \
b75a7d8f
A
72 test(); \
73 } \
74 break
75
729e4ab9
A
76// More convenient macros. These allow easy reordering of the test cases.
77//
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;
85//| }
86#define TESTCASE_AUTO_BEGIN \
87 for(;;) { \
88 int32_t testCaseAutoNumber = 0
89
90#define TESTCASE_AUTO(test) \
91 if (index == testCaseAutoNumber++) { \
92 name = #test; \
93 if (exec) { \
94 logln(#test "---"); \
95 logln(); \
96 test(); \
97 } \
98 break; \
99 }
100
57a6839d
A
101#define TESTCASE_AUTO_CLASS(TestClass) \
102 if (index == testCaseAutoNumber++) { \
103 name = #TestClass; \
104 if (exec) { \
105 logln(#TestClass "---"); \
106 logln(); \
107 TestClass test; \
108 callTest(test, par); \
109 } \
110 break; \
111 }
112
113#define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
114 if (index == testCaseAutoNumber++) { \
115 name = #TestClass; \
116 if (exec) { \
117 logln(#TestClass "---"); \
118 logln(); \
119 LocalPointer<IntlTest> test(create##TestClass()); \
120 callTest(*test, par); \
121 } \
122 break; \
123 }
124
729e4ab9
A
125#define TESTCASE_AUTO_END \
126 name = ""; \
127 break; \
128 }
129
51004dcb 130
3d1f044b
A
131// WHERE Macro yields a literal string of the form "source_file_name:line number "
132#define WHERE __FILE__ ":" XLINE(__LINE__) " "
133#define XLINE(s) LINE(s)
134#define LINE(s) #s
57a6839d 135
374ca955 136class IntlTest : public TestLog {
b75a7d8f
A
137public:
138
139 IntlTest();
729e4ab9 140 // TestLog has a virtual destructor.
b75a7d8f 141
729e4ab9 142 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
b75a7d8f
A
143
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 );
51004dcb 148 virtual UBool setNotime( UBool no_time = TRUE );
73c04bcf 149 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
729e4ab9 150 virtual int32_t setThreadCount( int32_t count = 1);
b75a7d8f
A
151
152 virtual int32_t getErrors( void );
73c04bcf 153 virtual int32_t getDataErrors (void );
b75a7d8f
A
154
155 virtual void setCaller( IntlTest* callingTest ); // for internal use only
156 virtual void setPath( char* path ); // for internal use only
157
158 virtual void log( const UnicodeString &message );
159
160 virtual void logln( const UnicodeString &message );
161
162 virtual void logln( void );
163
57a6839d
A
164 /**
165 * Replaces isICUVersionAtLeast and isICUVersionBefore
166 * log that an issue is known.
2ca993e8 167 * Usually used this way:
57a6839d
A
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
172 */
173 UBool logKnownIssue( const char *ticket, const UnicodeString &message );
174 /**
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
181 */
182 UBool logKnownIssue( const char *ticket );
183 /**
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
191 */
192 UBool logKnownIssue( const char *ticket, const char *fmt, ...);
193
b75a7d8f
A
194 virtual void info( const UnicodeString &message );
195
196 virtual void infoln( const UnicodeString &message );
197
198 virtual void infoln( void );
199
200 virtual void err(void);
57a6839d 201
b75a7d8f
A
202 virtual void err( const UnicodeString &message );
203
204 virtual void errln( const UnicodeString &message );
205
73c04bcf
A
206 virtual void dataerr( const UnicodeString &message );
207
208 virtual void dataerrln( const UnicodeString &message );
57a6839d 209
729e4ab9 210 void errcheckln(UErrorCode status, const UnicodeString &message );
73c04bcf 211
b75a7d8f
A
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, ...);
73c04bcf
A
219 void dataerr(const char *fmt, ...);
220 void dataerrln(const char *fmt, ...);
57a6839d
A
221
222 /**
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.
227 */
729e4ab9 228 void errcheckln(UErrorCode status, const char *fmt, ...);
b75a7d8f
A
229
230 // Print ALL named errors encountered so far
2ca993e8 231 void printErrors();
57a6839d
A
232
233 // print known issues. return TRUE if there were any.
234 UBool printKnownIssues();
2ca993e8 235
b75a7d8f
A
236 virtual void usage( void ) ;
237
374ca955
A
238 /**
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.
243 *
244 * @param seedp pointer to seed. Set *seedp to any negative value
245 * to restart the sequence.
246 */
247 static float random(int32_t* seedp);
248
249 /**
250 * Convenience method using a global seed.
251 */
252 static float random();
253
2ca993e8
A
254
255 /**
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.
259 */
260 class icu_rand {
261 public:
262 icu_rand(uint32_t seed = 1);
263 ~icu_rand();
264 void seed(uint32_t seed);
265 uint32_t operator()();
266 /**
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.
270 */
271 uint32_t getSeed();
272 private:
273 uint32_t fLast;
274 };
275
276
277
729e4ab9
A
278 enum { kMaxProps = 16 };
279
280 virtual void setProperty(const char* propline);
281 virtual const char* getProperty(const char* prop);
282
374ca955 283 /* JUnit-like assertions. Each returns TRUE if it succeeds. */
51004dcb 284 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
0f5d89e8 285 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
57a6839d
A
286 /**
287 * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
288 * @return TRUE on success, FALSE on failure.
289 */
290 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
374ca955 291 UBool assertEquals(const char* message, const UnicodeString& expected,
729e4ab9 292 const UnicodeString& actual, UBool possibleDataError=FALSE);
0f5d89e8
A
293 UBool assertEquals(const char* message, const char* expected, const char* actual);
294 UBool assertEquals(const char* message, UBool expected, UBool actual);
51004dcb 295 UBool assertEquals(const char* message, int32_t expected, int32_t actual);
57a6839d 296 UBool assertEquals(const char* message, int64_t expected, int64_t actual);
2ca993e8 297 UBool assertEquals(const char* message, double expected, double actual);
0f5d89e8
A
298 UBool assertEquals(const char* message, UErrorCode expected, UErrorCode actual);
299 UBool assertEquals(const char* message, const UnicodeSet& expected, const UnicodeSet& actual);
374ca955
A
300#if !UCONFIG_NO_FORMATTING
301 UBool assertEquals(const char* message, const Formattable& expected,
57a6839d 302 const Formattable& actual, UBool possibleDataError=FALSE);
374ca955
A
303 UBool assertEquals(const UnicodeString& message, const Formattable& expected,
304 const Formattable& actual);
305#endif
0f5d89e8
A
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);
374ca955
A
308 UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
309 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
57a6839d 310 const UnicodeString& actual, UBool possibleDataError=FALSE);
0f5d89e8 311 UBool assertEquals(const UnicodeString& message, const char* expected, const char* actual);
57a6839d
A
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);
0f5d89e8
A
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);
374ca955 318
b75a7d8f
A
319 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
320
729e4ab9 321 virtual UBool runTestLoop( char* testname, char* par, char *baseName );
b75a7d8f
A
322
323 virtual int32_t IncErrorCount( void );
324
73c04bcf
A
325 virtual int32_t IncDataErrorCount( void );
326
b75a7d8f
A
327 virtual UBool callTest( IntlTest& testToBeCalled, char* par );
328
329
73c04bcf
A
330 UBool verbose;
331 UBool no_err_msg;
332 UBool quick;
333 UBool leaks;
334 UBool warn_on_missing_data;
51004dcb 335 UBool no_time;
729e4ab9 336 int32_t threadCount;
b75a7d8f
A
337
338private:
73c04bcf 339 UBool LL_linestart;
b75a7d8f
A
340 int32_t LL_indentlevel;
341
342 int32_t errorCount;
73c04bcf 343 int32_t dataErrorCount;
b75a7d8f 344 IntlTest* caller;
73c04bcf 345 char* testPath; // specifies subtests
2ca993e8 346
729e4ab9 347 char basePath[1024];
57a6839d 348 char currName[1024]; // current test name
b75a7d8f 349
374ca955
A
350 //FILE *testoutfp;
351 void *testoutfp;
352
729e4ab9
A
353 const char* proplines[kMaxProps];
354 int32_t numProps;
355
b75a7d8f 356protected:
374ca955 357
b75a7d8f
A
358 virtual void LL_message( UnicodeString message, UBool newline );
359
360 // used for collation result reporting, defined here for convenience
361
362 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
363 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
51004dcb 364 // digits=-1 determines the number of digits automatically
b75a7d8f 365 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
51004dcb
A
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);
369 }
b75a7d8f 370
b75a7d8f
A
371public:
372 static void setICU_DATA(); // Set up ICU_DATA if necessary.
373
374 static const char* pathToDataDirectory();
375
376public:
377 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
378 static const char* loadTestData(UErrorCode& err);
374ca955
A
379 virtual const char* getTestDataPath(UErrorCode& err);
380 static const char* getSourceTestData(UErrorCode& err);
b331163b 381 static char *getUnidataPath(char path[]);
b75a7d8f
A
382
383// static members
384public:
385 static IntlTest* gTest;
386 static const char* fgDataDir;
387
388};
389
390void it_log( UnicodeString message );
391void it_logln( UnicodeString message );
392void it_logln( void );
393void it_info( UnicodeString message );
394void it_infoln( UnicodeString message );
395void it_infoln( void );
396void it_err(void);
397void it_err( UnicodeString message );
398void it_errln( UnicodeString message );
73c04bcf
A
399void it_dataerr( UnicodeString message );
400void it_dataerrln( UnicodeString message );
b75a7d8f 401
b75a7d8f
A
402/**
403 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
404 * It converts a character string into a UnicodeString, with
405 * unescaping \u sequences.
406 */
407extern UnicodeString CharsToUnicodeString(const char* chars);
408
374ca955
A
409/* alias for CharsToUnicodeString */
410extern UnicodeString ctou(const char* chars);
411
b75a7d8f 412#endif // _INTLTEST