Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1997-2010, 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 
     18 U_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)
     29 UnicodeString UCharToUnicodeString(UChar c);
     30 UnicodeString Int64ToUnicodeString(int64_t num);
     31 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
     32 UnicodeString operator+(const UnicodeString& left, long num);
     33 UnicodeString operator+(const UnicodeString& left, unsigned long num);
     34 UnicodeString operator+(const UnicodeString& left, double num);
     35 UnicodeString operator+(const UnicodeString& left, char num);
     36 UnicodeString operator+(const UnicodeString& left, short num);
     37 UnicodeString operator+(const UnicodeString& left, int num);
     38 UnicodeString operator+(const UnicodeString& left, unsigned char num);
     39 UnicodeString operator+(const UnicodeString& left, unsigned short num);
     40 UnicodeString operator+(const UnicodeString& left, unsigned int num);
     41 UnicodeString operator+(const UnicodeString& left, float num);
     42 #if !UCONFIG_NO_FORMATTING
     43 UnicodeString toString(const Formattable& f); // liu
     44 UnicodeString 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();                  \
     66             test();                   \
     67         }                             \
     68         break
     69 
     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 
    100 class IntlTest : public TestLog {
    101 public:
    102 
    103     IntlTest();
    104     // TestLog has a virtual destructor.
    105 
    106     virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
    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 );
    112     virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
    113     virtual int32_t setThreadCount( int32_t count = 1);
    114 
    115     virtual int32_t getErrors( void );
    116     virtual int32_t getDataErrors (void );
    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 
    139     virtual void dataerr( const UnicodeString &message );
    140 
    141     virtual void dataerrln( const UnicodeString &message );
    142 
    143     void errcheckln(UErrorCode status, const UnicodeString &message );
    144 
    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, ...);
    152     void dataerr(const char *fmt, ...);
    153     void dataerrln(const char *fmt, ...);
    154     void errcheckln(UErrorCode status, const char *fmt, ...);
    155 
    156     // Print ALL named errors encountered so far
    157     void printErrors();
    158 
    159     virtual void usage( void ) ;
    160 
    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     /**
    178      * Ascertain the version of ICU. Useful for
    179      * time bomb testing
    180      */
    181     UBool isICUVersionAtLeast(const UVersionInfo x);
    182 
    183     enum { kMaxProps = 16 };
    184 
    185     virtual void setProperty(const char* propline);
    186     virtual const char* getProperty(const char* prop);
    187 
    188 protected:
    189     /* JUnit-like assertions. Each returns TRUE if it succeeds. */
    190     UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
    191     UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE);
    192     UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE);
    193     UBool assertEquals(const char* message, const UnicodeString& expected,
    194                        const UnicodeString& actual, UBool possibleDataError=FALSE);
    195     UBool assertEquals(const char* message, const char* expected,
    196                        const char* actual);
    197 #if !UCONFIG_NO_FORMATTING
    198     UBool assertEquals(const char* message, const Formattable& expected,
    199                        const Formattable& actual);
    200     UBool assertEquals(const UnicodeString& message, const Formattable& expected,
    201                        const Formattable& actual);
    202 #endif
    203     UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
    204     UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
    205     UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
    206     UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
    207                        const UnicodeString& actual);
    208     UBool assertEquals(const UnicodeString& message, const char* expected,
    209                        const char* actual);
    210 
    211     virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
    212 
    213     virtual UBool runTestLoop( char* testname, char* par, char *baseName );
    214 
    215     virtual int32_t IncErrorCount( void );
    216 
    217     virtual int32_t IncDataErrorCount( void );
    218 
    219     virtual UBool callTest( IntlTest& testToBeCalled, char* par );
    220 
    221 
    222     UBool       verbose;
    223     UBool       no_err_msg;
    224     UBool       quick;
    225     UBool       leaks;
    226     UBool       warn_on_missing_data;
    227     int32_t     threadCount;
    228 
    229 private:
    230     UBool       LL_linestart;
    231     int32_t     LL_indentlevel;
    232 
    233     int32_t     errorCount;
    234     int32_t     dataErrorCount;
    235     IntlTest*   caller;
    236     char*       testPath;           // specifies subtests
    237 
    238     char basePath[1024];
    239 
    240     //FILE *testoutfp;
    241     void *testoutfp;
    242 
    243     const char* proplines[kMaxProps];
    244     int32_t     numProps;
    245 
    246 protected:
    247 
    248     virtual void LL_message( UnicodeString message, UBool newline );
    249 
    250     // used for collation result reporting, defined here for convenience
    251 
    252     static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
    253     static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
    254     static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
    255 
    256 public:
    257     static void setICU_DATA();       // Set up ICU_DATA if necessary.
    258 
    259     static const char* pathToDataDirectory();
    260 
    261 public:
    262     UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
    263     static const char* loadTestData(UErrorCode& err);
    264     virtual const char* getTestDataPath(UErrorCode& err);
    265     static const char* getSourceTestData(UErrorCode& err);
    266 
    267 // static members
    268 public:
    269     static IntlTest* gTest;
    270     static const char* fgDataDir;
    271 
    272 };
    273 
    274 void it_log( UnicodeString message );
    275 void it_logln( UnicodeString message );
    276 void it_logln( void );
    277 void it_info( UnicodeString message );
    278 void it_infoln( UnicodeString message );
    279 void it_infoln( void );
    280 void it_err(void);
    281 void it_err( UnicodeString message );
    282 void it_errln( UnicodeString message );
    283 void it_dataerr( UnicodeString message );
    284 void it_dataerrln( UnicodeString message );
    285 
    286 /**
    287  * This is a variant of cintltst/ccolltst.c:CharsToUChars().
    288  * It converts a character string into a UnicodeString, with
    289  * unescaping \u sequences.
    290  */
    291 extern UnicodeString CharsToUnicodeString(const char* chars);
    292 
    293 /* alias for CharsToUnicodeString */
    294 extern UnicodeString ctou(const char* chars);
    295 
    296 #endif // _INTLTEST
    297