Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1997-2015, 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 
     19 #if U_NO_DEFAULT_INCLUDE_UTF_HEADERS
     20 /* deprecated  - make tests pass with U_NO_DEFAULT_INCLUDE_UTF_HEADERS */
     21 #include "unicode/utf_old.h"
     22 #endif
     23 
     24 U_NAMESPACE_USE
     25 
     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")
     30 #endif
     31 
     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 operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
     38 UnicodeString operator+(const UnicodeString& left, long num);
     39 UnicodeString operator+(const UnicodeString& left, unsigned long num);
     40 UnicodeString operator+(const UnicodeString& left, double num);
     41 UnicodeString operator+(const UnicodeString& left, char num);
     42 UnicodeString operator+(const UnicodeString& left, short num);
     43 UnicodeString operator+(const UnicodeString& left, int num);
     44 UnicodeString operator+(const UnicodeString& left, unsigned char num);
     45 UnicodeString operator+(const UnicodeString& left, unsigned short num);
     46 UnicodeString operator+(const UnicodeString& left, unsigned int num);
     47 UnicodeString operator+(const UnicodeString& left, float num);
     48 #if !UCONFIG_NO_FORMATTING
     49 UnicodeString toString(const Formattable& f); // liu
     50 UnicodeString toString(int32_t n);
     51 #endif
     52 UnicodeString toString(UBool b);
     53 
     54 //-----------------------------------------------------------------------------
     55 
     56 // Use the TESTCASE macro in subclasses of IntlTest.  Define the
     57 // runIndexedTest method in this fashion:
     58 //
     59 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
     60 //|                             const char* &name, char* /*par*/) {
     61 //|     switch (index) {
     62 //|         TESTCASE(0,TestSomething);
     63 //|         TESTCASE(1,TestSomethingElse);
     64 //|         TESTCASE(2,TestAnotherThing);
     65 //|         default: name = ""; break;
     66 //|     }
     67 //| }
     68 #define TESTCASE(id,test)             \
     69     case id:                          \
     70         name = #test;                 \
     71         if (exec) {                   \
     72             logln(#test "---");       \
     73             logln();                  \
     74             test();                   \
     75         }                             \
     76         break
     77 
     78 // More convenient macros. These allow easy reordering of the test cases.
     79 //
     80 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
     81 //|                             const char* &name, char* /*par*/) {
     82 //|     TESTCASE_AUTO_BEGIN;
     83 //|     TESTCASE_AUTO(TestSomething);
     84 //|     TESTCASE_AUTO(TestSomethingElse);
     85 //|     TESTCASE_AUTO(TestAnotherThing);
     86 //|     TESTCASE_AUTO_END;
     87 //| }
     88 #define TESTCASE_AUTO_BEGIN \
     89     for(;;) { \
     90         int32_t testCaseAutoNumber = 0
     91 
     92 #define TESTCASE_AUTO(test) \
     93         if (index == testCaseAutoNumber++) { \
     94             name = #test; \
     95             if (exec) { \
     96                 logln(#test "---"); \
     97                 logln(); \
     98                 test(); \
     99             } \
    100             break; \
    101         }
    102 
    103 #define TESTCASE_AUTO_CLASS(TestClass) \
    104         if (index == testCaseAutoNumber++) { \
    105             name = #TestClass; \
    106             if (exec) { \
    107                 logln(#TestClass "---"); \
    108                 logln(); \
    109                 TestClass test; \
    110                 callTest(test, par); \
    111             } \
    112             break; \
    113         }
    114 
    115 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
    116         if (index == testCaseAutoNumber++) { \
    117             name = #TestClass; \
    118             if (exec) { \
    119                 logln(#TestClass "---"); \
    120                 logln(); \
    121                 LocalPointer<IntlTest> test(create##TestClass()); \
    122                 callTest(*test, par); \
    123             } \
    124             break; \
    125         }
    126 
    127 #define TESTCASE_AUTO_END \
    128         name = ""; \
    129         break; \
    130     }
    131 
    132 #define TEST_ASSERT_TRUE(x) \
    133   assertTrue(#x, (x), FALSE, FALSE, __FILE__, __LINE__)
    134 
    135 #define TEST_ASSERT_STATUS(x) \
    136   assertSuccess(#x, (x), FALSE, __FILE__, __LINE__)
    137 
    138 class IntlTest : public TestLog {
    139 public:
    140 
    141     IntlTest();
    142     // TestLog has a virtual destructor.
    143 
    144     virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
    145 
    146     virtual UBool setVerbose( UBool verbose = TRUE );
    147     virtual UBool setNoErrMsg( UBool no_err_msg = TRUE );
    148     virtual UBool setQuick( UBool quick = TRUE );
    149     virtual UBool setLeaks( UBool leaks = TRUE );
    150     virtual UBool setNotime( UBool no_time = TRUE );
    151     virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
    152     virtual int32_t setThreadCount( int32_t count = 1);
    153 
    154     virtual int32_t getErrors( void );
    155     virtual int32_t getDataErrors (void );
    156 
    157     virtual void setCaller( IntlTest* callingTest ); // for internal use only
    158     virtual void setPath( char* path ); // for internal use only
    159 
    160     virtual void log( const UnicodeString &message );
    161 
    162     virtual void logln( const UnicodeString &message );
    163 
    164     virtual void logln( void );
    165 
    166     /**
    167      * Replaces isICUVersionAtLeast and isICUVersionBefore
    168      * log that an issue is known.
    169      * Usually used this way:
    170      * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
    171      * @param ticket ticket string, "12345" or "cldrbug:1234"
    172      * @param message optional message string
    173      * @return true if test should be skipped
    174      */
    175     UBool logKnownIssue( const char *ticket, const UnicodeString &message );
    176     /**
    177      * Replaces isICUVersionAtLeast and isICUVersionBefore
    178      * log that an issue is known.
    179      * Usually used this way:
    180      * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
    181      * @param ticket ticket string, "12345" or "cldrbug:1234"
    182      * @return true if test should be skipped
    183      */
    184     UBool logKnownIssue( const char *ticket );
    185     /**
    186      * Replaces isICUVersionAtLeast and isICUVersionBefore
    187      * log that an issue is known.
    188      * Usually used this way:
    189      * <code>if( ... && logKnownIssue("12345", "some bug")) continue; </code>
    190      * @param ticket ticket string, "12345" or "cldrbug:1234"
    191      * @param message optional message string
    192      * @return true if test should be skipped
    193      */
    194     UBool logKnownIssue( const char *ticket, const char *fmt, ...);
    195 
    196     virtual void info( const UnicodeString &message );
    197 
    198     virtual void infoln( const UnicodeString &message );
    199 
    200     virtual void infoln( void );
    201 
    202     virtual void err(void);
    203 
    204     virtual void err( const UnicodeString &message );
    205 
    206     virtual void errln( const UnicodeString &message );
    207 
    208     virtual void dataerr( const UnicodeString &message );
    209 
    210     virtual void dataerrln( const UnicodeString &message );
    211 
    212     void errcheckln(UErrorCode status, const UnicodeString &message );
    213 
    214     // convenience functions: sprintf() + errln() etc.
    215     void log(const char *fmt, ...);
    216     void logln(const char *fmt, ...);
    217     void info(const char *fmt, ...);
    218     void infoln(const char *fmt, ...);
    219     void err(const char *fmt, ...);
    220     void errln(const char *fmt, ...);
    221     void dataerr(const char *fmt, ...);
    222     void dataerrln(const char *fmt, ...);
    223 
    224     /**
    225      * logs an error (even if status==U_ZERO_ERROR), but
    226      * calls dataerrln() or errln() depending on the type of error.
    227      * Does not report the status code.
    228      * @param status parameter for selecting whether errln or dataerrln is called.
    229      */
    230     void errcheckln(UErrorCode status, const char *fmt, ...);
    231 
    232     // Print ALL named errors encountered so far
    233     void printErrors();
    234 
    235     // print known issues. return TRUE if there were any.
    236     UBool printKnownIssues();
    237 
    238     virtual void usage( void ) ;
    239 
    240     /**
    241      * Returns a uniform random value x, with 0.0 <= x < 1.0.  Use
    242      * with care: Does not return all possible values; returns one of
    243      * 714,025 values, uniformly spaced.  However, the period is
    244      * effectively infinite.  See: Numerical Recipes, section 7.1.
    245      *
    246      * @param seedp pointer to seed. Set *seedp to any negative value
    247      * to restart the sequence.
    248      */
    249     static float random(int32_t* seedp);
    250 
    251     /**
    252      * Convenience method using a global seed.
    253      */
    254     static float random();
    255 
    256     enum { kMaxProps = 16 };
    257 
    258     virtual void setProperty(const char* propline);
    259     virtual const char* getProperty(const char* prop);
    260 
    261 protected:
    262     /* JUnit-like assertions. Each returns TRUE if it succeeds. */
    263     UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
    264     UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE);
    265     /**
    266      * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
    267      * @return TRUE on success, FALSE on failure.
    268      */
    269     UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
    270     UBool assertEquals(const char* message, const UnicodeString& expected,
    271                        const UnicodeString& actual, UBool possibleDataError=FALSE);
    272     UBool assertEquals(const char* message, const char* expected,
    273                        const char* actual);
    274     UBool assertEquals(const char* message, UBool expected,
    275                        UBool actual);
    276     UBool assertEquals(const char* message, int32_t expected, int32_t actual);
    277     UBool assertEquals(const char* message, int64_t expected, int64_t actual);
    278     UBool assertEquals(const char* message, double expected, double actual);
    279 #if !UCONFIG_NO_FORMATTING
    280     UBool assertEquals(const char* message, const Formattable& expected,
    281                        const Formattable& actual, UBool possibleDataError=FALSE);
    282     UBool assertEquals(const UnicodeString& message, const Formattable& expected,
    283                        const Formattable& actual);
    284 #endif
    285     UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
    286     UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE);
    287     UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
    288     UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
    289                        const UnicodeString& actual, UBool possibleDataError=FALSE);
    290     UBool assertEquals(const UnicodeString& message, const char* expected,
    291                        const char* actual);
    292     UBool assertEquals(const UnicodeString& message, UBool expected, UBool actual);
    293     UBool assertEquals(const UnicodeString& message, int32_t expected, int32_t actual);
    294     UBool assertEquals(const UnicodeString& message, int64_t expected, int64_t actual);
    295 
    296     virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
    297 
    298     virtual UBool runTestLoop( char* testname, char* par, char *baseName );
    299 
    300     virtual int32_t IncErrorCount( void );
    301 
    302     virtual int32_t IncDataErrorCount( void );
    303 
    304     virtual UBool callTest( IntlTest& testToBeCalled, char* par );
    305 
    306 
    307     UBool       verbose;
    308     UBool       no_err_msg;
    309     UBool       quick;
    310     UBool       leaks;
    311     UBool       warn_on_missing_data;
    312     UBool       no_time;
    313     int32_t     threadCount;
    314 
    315 private:
    316     UBool       LL_linestart;
    317     int32_t     LL_indentlevel;
    318 
    319     int32_t     errorCount;
    320     int32_t     dataErrorCount;
    321     IntlTest*   caller;
    322     char*       testPath;           // specifies subtests
    323 
    324     char basePath[1024];
    325     char currName[1024]; // current test name
    326 
    327     //FILE *testoutfp;
    328     void *testoutfp;
    329 
    330     const char* proplines[kMaxProps];
    331     int32_t     numProps;
    332 
    333 protected:
    334 
    335     virtual void LL_message( UnicodeString message, UBool newline );
    336 
    337     // used for collation result reporting, defined here for convenience
    338 
    339     static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
    340     static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
    341     // digits=-1 determines the number of digits automatically
    342     static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
    343     static UnicodeString toHex(uint32_t number, int32_t digits=-1);
    344     static inline UnicodeString toHex(int32_t number, int32_t digits=-1) {
    345         return toHex((uint32_t)number, digits);
    346     }
    347 
    348 public:
    349     static void setICU_DATA();       // Set up ICU_DATA if necessary.
    350 
    351     static const char* pathToDataDirectory();
    352 
    353 public:
    354     UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
    355     static const char* loadTestData(UErrorCode& err);
    356     virtual const char* getTestDataPath(UErrorCode& err);
    357     static const char* getSourceTestData(UErrorCode& err);
    358     static char *getUnidataPath(char path[]);
    359 
    360 // static members
    361 public:
    362     static IntlTest* gTest;
    363     static const char* fgDataDir;
    364 
    365 };
    366 
    367 void it_log( UnicodeString message );
    368 void it_logln( UnicodeString message );
    369 void it_logln( void );
    370 void it_info( UnicodeString message );
    371 void it_infoln( UnicodeString message );
    372 void it_infoln( void );
    373 void it_err(void);
    374 void it_err( UnicodeString message );
    375 void it_errln( UnicodeString message );
    376 void it_dataerr( UnicodeString message );
    377 void it_dataerrln( UnicodeString message );
    378 
    379 /**
    380  * This is a variant of cintltst/ccolltst.c:CharsToUChars().
    381  * It converts a character string into a UnicodeString, with
    382  * unescaping \u sequences.
    383  */
    384 extern UnicodeString CharsToUnicodeString(const char* chars);
    385 
    386 /* alias for CharsToUnicodeString */
    387 extern UnicodeString ctou(const char* chars);
    388 
    389 #endif // _INTLTEST
    390