1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2009, International Business Machines Corporation and
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
14// The following includes utypes.h, uobject.h and unistr.h
15#include "unicode/fmtable.h"
16#include "unicode/testlog.h"
17
18U_NAMESPACE_USE
19
20#ifdef OS390
21// avoid collision with math.h/log()
22// this must be after including utypes.h so that OS390 is actually defined
23#pragma map(IntlTest::log( const UnicodeString &message ),"logos390")
24#endif
25
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);
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.
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
44UnicodeString toString(int32_t n);
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 "---");       \
65            logln((UnicodeString)""); \
66            test();                   \
67        }                             \
68        break
69
70class IntlTest : public TestLog {
71public:
72
73    IntlTest();
74
75    virtual UBool runTest( char* name = NULL, char* par = NULL ); // not to be overidden
76
77    virtual UBool setVerbose( UBool verbose = TRUE );
78    virtual UBool setNoErrMsg( UBool no_err_msg = TRUE );
79    virtual UBool setQuick( UBool quick = TRUE );
80    virtual UBool setLeaks( UBool leaks = TRUE );
81    virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
82
83    virtual int32_t getErrors( void );
84    virtual int32_t getDataErrors (void );
85
86    virtual void setCaller( IntlTest* callingTest ); // for internal use only
87    virtual void setPath( char* path ); // for internal use only
88
89    virtual void log( const UnicodeString &message );
90
91    virtual void logln( const UnicodeString &message );
92
93    virtual void logln( void );
94
95    virtual void info( const UnicodeString &message );
96
97    virtual void infoln( const UnicodeString &message );
98
99    virtual void infoln( void );
100
101    virtual void err(void);
102
103    virtual void err( const UnicodeString &message );
104
105    virtual void errln( const UnicodeString &message );
106
107    virtual void dataerr( const UnicodeString &message );
108
109    virtual void dataerrln( const UnicodeString &message );
110
111    void errcheckln(UErrorCode status, const UnicodeString &message );
112
113    // convenience functions: sprintf() + errln() etc.
114    void log(const char *fmt, ...);
115    void logln(const char *fmt, ...);
116    void info(const char *fmt, ...);
117    void infoln(const char *fmt, ...);
118    void err(const char *fmt, ...);
119    void errln(const char *fmt, ...);
120    void dataerr(const char *fmt, ...);
121    void dataerrln(const char *fmt, ...);
122    void errcheckln(UErrorCode status, const char *fmt, ...);
123
124    // Print ALL named errors encountered so far
125    void printErrors();
126
127    virtual void usage( void ) ;
128
129    /**
130     * Returns a uniform random value x, with 0.0 <= x < 1.0.  Use
131     * with care: Does not return all possible values; returns one of
132     * 714,025 values, uniformly spaced.  However, the period is
133     * effectively infinite.  See: Numerical Recipes, section 7.1.
134     *
135     * @param seedp pointer to seed. Set *seedp to any negative value
136     * to restart the sequence.
137     */
138    static float random(int32_t* seedp);
139
140    /**
141     * Convenience method using a global seed.
142     */
143    static float random();
144
145    /**
146     * Ascertain the version of ICU. Useful for
147     * time bomb testing
148     */
149    UBool isICUVersionAtLeast(const UVersionInfo x);
150
151    enum { kMaxProps = 16 };
152
153    virtual void setProperty(const char* propline);
154    virtual const char* getProperty(const char* prop);
155
156protected:
157    /* JUnit-like assertions. Each returns TRUE if it succeeds. */
158    UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
159    UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE);
160    UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE);
161    UBool assertEquals(const char* message, const UnicodeString& expected,
162                       const UnicodeString& actual, UBool possibleDataError=FALSE);
163    UBool assertEquals(const char* message, const char* expected,
164                       const char* actual);
165#if !UCONFIG_NO_FORMATTING
166    UBool assertEquals(const char* message, const Formattable& expected,
167                       const Formattable& actual);
168    UBool assertEquals(const UnicodeString& message, const Formattable& expected,
169                       const Formattable& actual);
170#endif
171    UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
172    UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
173    UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
174    UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
175                       const UnicodeString& actual);
176    UBool assertEquals(const UnicodeString& message, const char* expected,
177                       const char* actual);
178
179    virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
180
181    virtual UBool runTestLoop( char* testname, char* par );
182
183    virtual int32_t IncErrorCount( void );
184
185    virtual int32_t IncDataErrorCount( void );
186
187    virtual UBool callTest( IntlTest& testToBeCalled, char* par );
188
189
190    UBool       verbose;
191    UBool       no_err_msg;
192    UBool       quick;
193    UBool       leaks;
194    UBool       warn_on_missing_data;
195
196private:
197    UBool       LL_linestart;
198    int32_t     LL_indentlevel;
199
200    int32_t     errorCount;
201    int32_t     dataErrorCount;
202    IntlTest*   caller;
203    char*       testPath;           // specifies subtests
204
205    //FILE *testoutfp;
206    void *testoutfp;
207
208    const char* proplines[kMaxProps];
209    int32_t     numProps;
210
211protected:
212
213    virtual void LL_message( UnicodeString message, UBool newline );
214
215    // used for collation result reporting, defined here for convenience
216
217    static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
218    static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
219    static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
220
221public:
222    static void setICU_DATA();       // Set up ICU_DATA if necessary.
223
224    static const char* pathToDataDirectory();
225
226public:
227    UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
228    static const char* loadTestData(UErrorCode& err);
229    virtual const char* getTestDataPath(UErrorCode& err);
230    static const char* getSourceTestData(UErrorCode& err);
231
232// static members
233public:
234    static IntlTest* gTest;
235    static const char* fgDataDir;
236
237};
238
239void it_log( UnicodeString message );
240void it_logln( UnicodeString message );
241void it_logln( void );
242void it_info( UnicodeString message );
243void it_infoln( UnicodeString message );
244void it_infoln( void );
245void it_err(void);
246void it_err( UnicodeString message );
247void it_errln( UnicodeString message );
248void it_dataerr( UnicodeString message );
249void it_dataerrln( UnicodeString message );
250
251/**
252 * This is a variant of cintltst/ccolltst.c:CharsToUChars().
253 * It converts a character string into a UnicodeString, with
254 * unescaping \u sequences.
255 */
256extern UnicodeString CharsToUnicodeString(const char* chars);
257
258/* alias for CharsToUnicodeString */
259extern UnicodeString ctou(const char* chars);
260
261#endif // _INTLTEST
262