]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/intltest.h
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / test / intltest / intltest.h
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
4388f060 3 * Copyright (c) 1997-2011, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7
8/**
9 * IntlTest is a base class for tests. */
10
11#ifndef _INTLTEST
12#define _INTLTEST
13
374ca955 14// The following includes utypes.h, uobject.h and unistr.h
b75a7d8f 15#include "unicode/fmtable.h"
374ca955 16#include "unicode/testlog.h"
b75a7d8f
A
17
18U_NAMESPACE_USE
19
4388f060 20#if U_PLATFORM == U_PF_OS390
b75a7d8f 21// avoid collision with math.h/log()
4388f060 22// this must be after including utypes.h so that U_PLATFORM is actually defined
b75a7d8f
A
23#pragma map(IntlTest::log( const UnicodeString &message ),"logos390")
24#endif
25
b75a7d8f
A
26//-----------------------------------------------------------------------------
27//convenience classes to ease porting code that uses the Java
28//string-concatenation operator (moved from findword test by rtg)
29UnicodeString UCharToUnicodeString(UChar c);
374ca955
A
30UnicodeString Int64ToUnicodeString(int64_t num);
31//UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
b75a7d8f
A
32UnicodeString operator+(const UnicodeString& left, long num);
33UnicodeString operator+(const UnicodeString& left, unsigned long num);
34UnicodeString operator+(const UnicodeString& left, double num);
35UnicodeString operator+(const UnicodeString& left, char num);
36UnicodeString operator+(const UnicodeString& left, short num);
37UnicodeString operator+(const UnicodeString& left, int num);
38UnicodeString operator+(const UnicodeString& left, unsigned char num);
39UnicodeString operator+(const UnicodeString& left, unsigned short num);
40UnicodeString operator+(const UnicodeString& left, unsigned int num);
41UnicodeString operator+(const UnicodeString& left, float num);
42#if !UCONFIG_NO_FORMATTING
43UnicodeString toString(const Formattable& f); // liu
374ca955 44UnicodeString toString(int32_t n);
b75a7d8f
A
45#endif
46//-----------------------------------------------------------------------------
47
48// Use the TESTCASE macro in subclasses of IntlTest. Define the
49// runIndexedTest method in this fashion:
50//
51//| void MyTest::runIndexedTest(int32_t index, UBool exec,
52//| const char* &name, char* /*par*/) {
53//| switch (index) {
54//| TESTCASE(0,TestSomething);
55//| TESTCASE(1,TestSomethingElse);
56//| TESTCASE(2,TestAnotherThing);
57//| default: name = ""; break;
58//| }
59//| }
60#define TESTCASE(id,test) \
61 case id: \
62 name = #test; \
63 if (exec) { \
64 logln(#test "---"); \
729e4ab9 65 logln(); \
b75a7d8f
A
66 test(); \
67 } \
68 break
69
729e4ab9
A
70// More convenient macros. These allow easy reordering of the test cases.
71//
72//| void MyTest::runIndexedTest(int32_t index, UBool exec,
73//| const char* &name, char* /*par*/) {
74//| TESTCASE_AUTO_BEGIN;
75//| TESTCASE_AUTO(TestSomething);
76//| TESTCASE_AUTO(TestSomethingElse);
77//| TESTCASE_AUTO(TestAnotherThing);
78//| TESTCASE_AUTO_END;
79//| }
80#define TESTCASE_AUTO_BEGIN \
81 for(;;) { \
82 int32_t testCaseAutoNumber = 0
83
84#define TESTCASE_AUTO(test) \
85 if (index == testCaseAutoNumber++) { \
86 name = #test; \
87 if (exec) { \
88 logln(#test "---"); \
89 logln(); \
90 test(); \
91 } \
92 break; \
93 }
94
95#define TESTCASE_AUTO_END \
96 name = ""; \
97 break; \
98 }
99
374ca955 100class IntlTest : public TestLog {
b75a7d8f
A
101public:
102
103 IntlTest();
729e4ab9 104 // TestLog has a virtual destructor.
b75a7d8f 105
729e4ab9 106 virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
b75a7d8f
A
107
108 virtual UBool setVerbose( UBool verbose = TRUE );
109 virtual UBool setNoErrMsg( UBool no_err_msg = TRUE );
110 virtual UBool setQuick( UBool quick = TRUE );
111 virtual UBool setLeaks( UBool leaks = TRUE );
73c04bcf 112 virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
729e4ab9 113 virtual int32_t setThreadCount( int32_t count = 1);
b75a7d8f
A
114
115 virtual int32_t getErrors( void );
73c04bcf 116 virtual int32_t getDataErrors (void );
b75a7d8f
A
117
118 virtual void setCaller( IntlTest* callingTest ); // for internal use only
119 virtual void setPath( char* path ); // for internal use only
120
121 virtual void log( const UnicodeString &message );
122
123 virtual void logln( const UnicodeString &message );
124
125 virtual void logln( void );
126
127 virtual void info( const UnicodeString &message );
128
129 virtual void infoln( const UnicodeString &message );
130
131 virtual void infoln( void );
132
133 virtual void err(void);
134
135 virtual void err( const UnicodeString &message );
136
137 virtual void errln( const UnicodeString &message );
138
73c04bcf
A
139 virtual void dataerr( const UnicodeString &message );
140
141 virtual void dataerrln( const UnicodeString &message );
729e4ab9
A
142
143 void errcheckln(UErrorCode status, const UnicodeString &message );
73c04bcf 144
b75a7d8f
A
145 // convenience functions: sprintf() + errln() etc.
146 void log(const char *fmt, ...);
147 void logln(const char *fmt, ...);
148 void info(const char *fmt, ...);
149 void infoln(const char *fmt, ...);
150 void err(const char *fmt, ...);
151 void errln(const char *fmt, ...);
73c04bcf
A
152 void dataerr(const char *fmt, ...);
153 void dataerrln(const char *fmt, ...);
729e4ab9 154 void errcheckln(UErrorCode status, const char *fmt, ...);
b75a7d8f
A
155
156 // Print ALL named errors encountered so far
157 void printErrors();
158
159 virtual void usage( void ) ;
160
374ca955
A
161 /**
162 * Returns a uniform random value x, with 0.0 <= x < 1.0. Use
163 * with care: Does not return all possible values; returns one of
164 * 714,025 values, uniformly spaced. However, the period is
165 * effectively infinite. See: Numerical Recipes, section 7.1.
166 *
167 * @param seedp pointer to seed. Set *seedp to any negative value
168 * to restart the sequence.
169 */
170 static float random(int32_t* seedp);
171
172 /**
173 * Convenience method using a global seed.
174 */
175 static float random();
176
177 /**
4388f060 178 * Returns true if u_getVersion() < major.minor.
374ca955 179 */
4388f060
A
180 static UBool isICUVersionBefore(int major, int minor) {
181 return isICUVersionBefore(major, minor, 0);
182 }
183
184 /**
185 * Returns true if u_getVersion() < major.minor.milli.
186 */
187 static UBool isICUVersionBefore(int major, int minor, int milli);
188
189 /**
190 * Returns true if u_getVersion() >= major.minor.
191 */
192 static UBool isICUVersionAtLeast(int major, int minor) {
193 return isICUVersionAtLeast(major, minor, 0);
194 }
195
196 /**
197 * Returns true if u_getVersion() >= major.minor.milli.
198 */
199 static UBool isICUVersionAtLeast(int major, int minor, int milli) {
200 return !isICUVersionBefore(major, minor, milli);
201 }
b75a7d8f 202
729e4ab9
A
203 enum { kMaxProps = 16 };
204
205 virtual void setProperty(const char* propline);
206 virtual const char* getProperty(const char* prop);
207
b75a7d8f 208protected:
374ca955 209 /* JUnit-like assertions. Each returns TRUE if it succeeds. */
729e4ab9 210 UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
374ca955 211 UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE);
729e4ab9 212 UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE);
374ca955 213 UBool assertEquals(const char* message, const UnicodeString& expected,
729e4ab9 214 const UnicodeString& actual, UBool possibleDataError=FALSE);
374ca955
A
215 UBool assertEquals(const char* message, const char* expected,
216 const char* actual);
217#if !UCONFIG_NO_FORMATTING
218 UBool assertEquals(const char* message, const Formattable& expected,
219 const Formattable& actual);
220 UBool assertEquals(const UnicodeString& message, const Formattable& expected,
221 const Formattable& actual);
222#endif
223 UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
224 UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
225 UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
226 UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
227 const UnicodeString& actual);
228 UBool assertEquals(const UnicodeString& message, const char* expected,
229 const char* actual);
230
b75a7d8f
A
231 virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
232
729e4ab9 233 virtual UBool runTestLoop( char* testname, char* par, char *baseName );
b75a7d8f
A
234
235 virtual int32_t IncErrorCount( void );
236
73c04bcf
A
237 virtual int32_t IncDataErrorCount( void );
238
b75a7d8f
A
239 virtual UBool callTest( IntlTest& testToBeCalled, char* par );
240
241
73c04bcf
A
242 UBool verbose;
243 UBool no_err_msg;
244 UBool quick;
245 UBool leaks;
246 UBool warn_on_missing_data;
729e4ab9 247 int32_t threadCount;
b75a7d8f
A
248
249private:
73c04bcf 250 UBool LL_linestart;
b75a7d8f
A
251 int32_t LL_indentlevel;
252
253 int32_t errorCount;
73c04bcf 254 int32_t dataErrorCount;
b75a7d8f 255 IntlTest* caller;
73c04bcf 256 char* testPath; // specifies subtests
729e4ab9
A
257
258 char basePath[1024];
b75a7d8f 259
374ca955
A
260 //FILE *testoutfp;
261 void *testoutfp;
262
729e4ab9
A
263 const char* proplines[kMaxProps];
264 int32_t numProps;
265
b75a7d8f 266protected:
374ca955 267
b75a7d8f
A
268 virtual void LL_message( UnicodeString message, UBool newline );
269
270 // used for collation result reporting, defined here for convenience
271
272 static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
273 static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
274 static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
275
b75a7d8f
A
276public:
277 static void setICU_DATA(); // Set up ICU_DATA if necessary.
278
279 static const char* pathToDataDirectory();
280
281public:
282 UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
283 static const char* loadTestData(UErrorCode& err);
374ca955
A
284 virtual const char* getTestDataPath(UErrorCode& err);
285 static const char* getSourceTestData(UErrorCode& err);
b75a7d8f
A
286
287// static members
288public:
289 static IntlTest* gTest;
290 static const char* fgDataDir;
291
292};
293
294void it_log( UnicodeString message );
295void it_logln( UnicodeString message );
296void it_logln( void );
297void it_info( UnicodeString message );
298void it_infoln( UnicodeString message );
299void it_infoln( void );
300void it_err(void);
301void it_err( UnicodeString message );
302void it_errln( UnicodeString message );
73c04bcf
A
303void it_dataerr( UnicodeString message );
304void it_dataerrln( UnicodeString message );
b75a7d8f 305
b75a7d8f
A
306/**
307 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
308 * It converts a character string into a UnicodeString, with
309 * unescaping \u sequences.
310 */
311extern UnicodeString CharsToUnicodeString(const char* chars);
312
374ca955
A
313/* alias for CharsToUnicodeString */
314extern UnicodeString ctou(const char* chars);
315
b75a7d8f 316#endif // _INTLTEST