• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2016, International Business Machines Corporation and
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 
16 // The following includes utypes.h, uobject.h and unistr.h
17 #include "unicode/fmtable.h"
18 #include "unicode/testlog.h"
19 #include "unicode/uniset.h"
20 
21 #include <vector>
22 #include <string>
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 DoubleToUnicodeString(double num);
38 //UnicodeString operator+(const UnicodeString& left, int64_t num); // Some compilers don't allow this because of the long type.
39 UnicodeString operator+(const UnicodeString& left, long num);
40 UnicodeString operator+(const UnicodeString& left, unsigned long num);
41 UnicodeString operator+(const UnicodeString& left, double num);
42 UnicodeString operator+(const UnicodeString& left, char num);
43 UnicodeString operator+(const UnicodeString& left, short num);
44 UnicodeString operator+(const UnicodeString& left, int num);
45 UnicodeString operator+(const UnicodeString& left, unsigned char num);
46 UnicodeString operator+(const UnicodeString& left, unsigned short num);
47 UnicodeString operator+(const UnicodeString& left, unsigned int num);
48 UnicodeString operator+(const UnicodeString& left, float num);
49 #if !UCONFIG_NO_FORMATTING
50 UnicodeString toString(const Formattable& f); // liu
51 UnicodeString toString(int32_t n);
52 #endif
53 UnicodeString toString(UBool b);
54 
55 //-----------------------------------------------------------------------------
56 
57 // Use the TESTCASE macro in subclasses of IntlTest.  Define the
58 // runIndexedTest method in this fashion:
59 //
60 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
61 //|                             const char* &name, char* /*par*/) {
62 //|     switch (index) {
63 //|         TESTCASE(0,TestSomething);
64 //|         TESTCASE(1,TestSomethingElse);
65 //|         TESTCASE(2,TestAnotherThing);
66 //|         default: name = ""; break;
67 //|     }
68 //| }
69 #define TESTCASE(id,test)             \
70     case id:                          \
71         name = #test;                 \
72         if (exec) {                   \
73             logln(#test "---");       \
74             logln();                  \
75             test();                   \
76         }                             \
77         break
78 
79 // More convenient macros. These allow easy reordering of the test cases.
80 //
81 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
82 //|                             const char* &name, char* /*par*/) {
83 //|     TESTCASE_AUTO_BEGIN;
84 //|     TESTCASE_AUTO(TestSomething);
85 //|     TESTCASE_AUTO(TestSomethingElse);
86 //|     TESTCASE_AUTO(TestAnotherThing);
87 //|     TESTCASE_AUTO_END;
88 //| }
89 #define TESTCASE_AUTO_BEGIN \
90     do { \
91         int32_t testCaseAutoNumber = 0
92 
93 #define TESTCASE_AUTO(test) \
94         if (index == testCaseAutoNumber++) { \
95             name = #test; \
96             if (exec) { \
97                 logln(#test "---"); \
98                 logln(); \
99                 test(); \
100             } \
101             break; \
102         } else (void)0
103 
104 #define TESTCASE_AUTO_CLASS(TestClass) \
105         if (index == testCaseAutoNumber++) { \
106             name = #TestClass; \
107             if (exec) { \
108                 logln(#TestClass "---"); \
109                 logln(); \
110                 TestClass test; \
111                 callTest(test, par); \
112             } \
113             break; \
114         } else (void)0
115 
116 #define TESTCASE_AUTO_CREATE_CLASS(TestClass) \
117         if (index == testCaseAutoNumber++) { \
118             name = #TestClass; \
119             if (exec) { \
120                 logln(#TestClass "---"); \
121                 logln(); \
122                 LocalPointer<IntlTest> test(create##TestClass()); \
123                 callTest(*test, par); \
124             } \
125             break; \
126         } else (void)0
127 
128 #define TESTCASE_AUTO_END \
129         name = ""; \
130         break; \
131     } while (TRUE)
132 
133 
134 // WHERE Macro yields a literal string of the form "source_file_name:line number "
135 #define WHERE __FILE__ ":" XLINE(__LINE__) " "
136 #define XLINE(s) LINE(s)
137 #define LINE(s) #s
138 
139 class IntlTest : public TestLog {
140 public:
141 
142     IntlTest();
143     // TestLog has a virtual destructor.
144 
145     virtual UBool runTest( char* name = NULL, char* par = NULL, char *baseName = NULL); // not to be overidden
146 
147     virtual UBool setVerbose( UBool verbose = TRUE );
148     virtual UBool setNoErrMsg( UBool no_err_msg = TRUE );
149     virtual UBool setQuick( UBool quick = TRUE );
150     virtual UBool setLeaks( UBool leaks = TRUE );
151     virtual UBool setNotime( UBool no_time = TRUE );
152     virtual UBool setWarnOnMissingData( UBool warn_on_missing_data = TRUE );
153     virtual UBool setWriteGoldenData( UBool write_golden_data = TRUE );
154     virtual int32_t setThreadCount( int32_t count = 1);
155 
156     virtual int32_t getErrors( void );
157     virtual int32_t getDataErrors (void );
158 
159     virtual void setCaller( IntlTest* callingTest ); // for internal use only
160     virtual void setPath( char* path ); // for internal use only
161 
162     virtual void log( const UnicodeString &message );
163 
164     virtual void logln( const UnicodeString &message );
165 
166     virtual void logln( void );
167 
168     /**
169      * Logs that an issue is known. Can be called multiple times.
170      * Usually used this way:
171      * <code>if( ... && logKnownIssue("ICU-12345", "some bug")) continue; </code>
172      * @param ticket ticket string, "ICU-12345" or "CLDR-1234"
173      * @param message optional message string
174      * @return true if test should be skipped
175      */
176     UBool logKnownIssue( const char *ticket, const UnicodeString &message );
177     /**
178      * Logs that an issue is known. Can be called multiple times.
179      * Usually used this way:
180      * <code>if( ... && logKnownIssue("ICU-12345", "some bug")) continue; </code>
181      * @param ticket ticket string, "ICU-12345" or "CLDR-1234"
182      * @return true if test should be skipped
183      */
184     UBool logKnownIssue( const char *ticket );
185     /**
186      * Log that an issue is known. Can be called multiple times.
187      * Usually used this way:
188      * <code>if( ... && logKnownIssue("ICU-12345", "some bug")) continue; </code>
189      * @param ticket ticket string, "ICU-12345" or "CLDR-1234"
190      * @param message optional message string
191      * @return true if test should be skipped
192      */
193     UBool logKnownIssue( const char *ticket, const char *fmt, ...);
194 
195     virtual void info( const UnicodeString &message );
196 
197     virtual void infoln( const UnicodeString &message );
198 
199     virtual void infoln( void );
200 
201     virtual void err(void);
202 
203     virtual void err( const UnicodeString &message );
204 
205     virtual void errln( const UnicodeString &message );
206 
207     virtual void dataerr( const UnicodeString &message );
208 
209     virtual void dataerrln( const UnicodeString &message );
210 
211     void errcheckln(UErrorCode status, const UnicodeString &message );
212 
213     // convenience functions: sprintf() + errln() etc.
214     void log(const char *fmt, ...);
215     void logln(const char *fmt, ...);
216     void info(const char *fmt, ...);
217     void infoln(const char *fmt, ...);
218     void err(const char *fmt, ...);
219     void errln(const char *fmt, ...);
220     void dataerr(const char *fmt, ...);
221     void dataerrln(const char *fmt, ...);
222 
223     /**
224      * logs an error (even if status==U_ZERO_ERROR), but
225      * calls dataerrln() or errln() depending on the type of error.
226      * Does not report the status code.
227      * @param status parameter for selecting whether errln or dataerrln is called.
228      */
229     void errcheckln(UErrorCode status, const char *fmt, ...);
230 
231     // Print ALL named errors encountered so far
232     void printErrors();
233 
234     // print known issues. return TRUE if there were any.
235     UBool printKnownIssues();
236 
237     virtual void usage( void ) ;
238 
239     /**
240      * Returns a uniform random value x, with 0.0 <= x < 1.0.  Use
241      * with care: Does not return all possible values; returns one of
242      * 714,025 values, uniformly spaced.  However, the period is
243      * effectively infinite.  See: Numerical Recipes, section 7.1.
244      *
245      * @param seedp pointer to seed. Set *seedp to any negative value
246      * to restart the sequence.
247      */
248     static float random(int32_t* seedp);
249 
250     /**
251      * Convenience method using a global seed.
252      */
253     static float random();
254 
255 
256     /**
257      *   Integer random numbers, similar to C++ std::minstd_rand, with the same algorithm
258      *   and constants.  Allow additional access to internal state, for use by monkey tests,
259      *   which need to recreate previous random sequences beginning near a failure point.
260      */
261     class icu_rand {
262       public:
263         icu_rand(uint32_t seed = 1);
264         ~icu_rand();
265         void seed(uint32_t seed);
266         uint32_t operator()();
267         /**
268           * Get a seed corresponding to the current state of the generator.
269           * Seeding any generator with this value will cause it to produce the
270           * same sequence as this one will from this point forward.
271           */
272         uint32_t getSeed();
273       private:
274         uint32_t fLast;
275     };
276 
277 
278 
279     enum { kMaxProps = 16 };
280 
281     virtual void setProperty(const char* propline);
282     virtual const char* getProperty(const char* prop);
283 
284     /* JUnit-like assertions. Each returns TRUE if it succeeds. */
285     UBool assertTrue(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
286     UBool assertFalse(const char* message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
287     /**
288      * @param possibleDataError - if TRUE, use dataerrln instead of errcheckln on failure
289      * @return TRUE on success, FALSE on failure.
290      */
291     UBool assertSuccess(const char* message, UErrorCode ec, UBool possibleDataError=FALSE, const char *file=NULL, int line=0);
292     UBool assertEquals(const char* message, const UnicodeString& expected,
293                        const UnicodeString& actual, UBool possibleDataError=FALSE);
294     UBool assertEquals(const char* message, const char* expected, const char* actual);
295     UBool assertEquals(const char* message, UBool expected, UBool actual);
296     UBool assertEquals(const char* message, int32_t expected, int32_t actual);
297     UBool assertEquals(const char* message, int64_t expected, int64_t actual);
298     UBool assertEquals(const char* message, double expected, double actual);
299     /**
300      * Asserts that two doubles are equal to within a positive delta. Returns
301      * false if they are not.
302      *
303      * NaNs are considered equal: assertEquals(msg, NaN, NaN, *) passes.
304      * Infs are considered equal: assertEquals(msg, inf, inf, *) passes.
305      *
306      * @param message - the identifying message for the AssertionError.
307      * @param expected - expected value.
308      * @param actual - the value to check against expected.
309      * @param delta - the maximum delta for the absolute difference between
310      * expected and actual for which both numbers are still considered equal.
311      */
312     UBool assertEqualsNear(const char* message, double expected, double actual, double delta);
313     UBool assertEquals(const char* message, UErrorCode expected, UErrorCode actual);
314     UBool assertEquals(const char* message, const UnicodeSet& expected, const UnicodeSet& actual);
315     UBool assertEquals(const char* message,
316         const std::vector<std::string>& expected, const std::vector<std::string>& actual);
317 
318 #if !UCONFIG_NO_FORMATTING
319     UBool assertEquals(const char* message, const Formattable& expected,
320                        const Formattable& actual, UBool possibleDataError=FALSE);
321     UBool assertEquals(const UnicodeString& message, const Formattable& expected,
322                        const Formattable& actual);
323 #endif
324     UBool assertNotEquals(const char* message, int32_t expectedNot, int32_t actual);
325     UBool assertTrue(const UnicodeString& message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
326     UBool assertFalse(const UnicodeString& message, UBool condition, UBool quiet=FALSE, UBool possibleDataError=FALSE);
327     UBool assertSuccess(const UnicodeString& message, UErrorCode ec);
328     UBool assertEquals(const UnicodeString& message, const UnicodeString& expected,
329                        const UnicodeString& actual, UBool possibleDataError=FALSE);
330     UBool assertEquals(const UnicodeString& message, const char* expected, const char* actual);
331     UBool assertEquals(const UnicodeString& message, UBool expected, UBool actual);
332     UBool assertEquals(const UnicodeString& message, int32_t expected, int32_t actual);
333     UBool assertEquals(const UnicodeString& message, int64_t expected, int64_t actual);
334     UBool assertEquals(const UnicodeString& message, double expected, double actual);
335     /**
336      * Asserts that two doubles are equal to within a positive delta. Returns
337      * false if they are not.
338      *
339      * NaNs are considered equal: assertEquals(msg, NaN, NaN, *) passes.
340      * Infs are considered equal: assertEquals(msg, inf, inf, *) passes.
341      *
342      * @param message - the identifying message for the AssertionError.
343      * @param expected - expected value.
344      * @param actual - the value to check against expected.
345      * @param delta - the maximum delta between expected and actual for which
346      * both numbers are still considered equal.
347      */
348     UBool assertEqualsNear(const UnicodeString& message, double expected, double actual, double delta);
349     UBool assertEquals(const UnicodeString& message, UErrorCode expected, UErrorCode actual);
350     UBool assertEquals(const UnicodeString& message, const UnicodeSet& expected, const UnicodeSet& actual);
351     UBool assertEquals(const UnicodeString& message,
352         const std::vector<std::string>& expected, const std::vector<std::string>& actual);
353     UBool assertNotEquals(const UnicodeString& message, int32_t expectedNot, int32_t actual);
354 
355     virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
356 
357     virtual UBool runTestLoop( char* testname, char* par, char *baseName );
358 
359     virtual int32_t IncErrorCount( void );
360 
361     virtual int32_t IncDataErrorCount( void );
362 
363     virtual UBool callTest( IntlTest& testToBeCalled, char* par, const char* basename = "");
364 
365 
366     UBool       verbose;
367     UBool       no_err_msg;
368     UBool       quick;
369     UBool       leaks;
370     UBool       warn_on_missing_data;
371     UBool       write_golden_data;
372     UBool       no_time;
373     int32_t     threadCount;
374 
375 private:
376     UBool       LL_linestart;
377     int32_t     LL_indentlevel;
378 
379     int32_t     errorCount;
380     int32_t     dataErrorCount;
381     IntlTest*   caller;
382     char*       testPath;           // specifies subtests
383 
384     char basePath[1024];
385     char currName[1024]; // current test name
386 
387     //FILE *testoutfp;
388     void *testoutfp;
389 
390     const char* proplines[kMaxProps];
391     int32_t     numProps;
392 
393 protected:
394     std::string   currErr; // Error message of the current test case
395 
396     virtual void LL_err_message( const UnicodeString& message, UBool newline );
397     virtual void LL_message( UnicodeString message, UBool newlin, UBool isErr = FALSE );
398 
399     // used for collation result reporting, defined here for convenience
400 
401     static UnicodeString &prettify(const UnicodeString &source, UnicodeString &target);
402     static UnicodeString prettify(const UnicodeString &source, UBool parseBackslash=FALSE);
403     // digits=-1 determines the number of digits automatically
404     static UnicodeString &appendHex(uint32_t number, int32_t digits, UnicodeString &target);
405     static UnicodeString toHex(uint32_t number, int32_t digits=-1);
406     static inline UnicodeString toHex(int32_t number, int32_t digits=-1) {
407         return toHex((uint32_t)number, digits);
408     }
409 
410 public:
411     static void setICU_DATA();       // Set up ICU_DATA if necessary.
412 
413     static const char* pathToDataDirectory();
414 
415 public:
416     UBool run_phase2( char* name, char* par ); // internally, supports reporting memory leaks
417     static const char* loadTestData(UErrorCode& err);
418     virtual const char* getTestDataPath(UErrorCode& err);
419     static const char* getSourceTestData(UErrorCode& err);
420     static char *getUnidataPath(char path[]);
421 
422 // static members
423 public:
424     static IntlTest* gTest;
425 
426 };
427 
428 void it_log( UnicodeString message );
429 void it_logln( UnicodeString message );
430 void it_logln( void );
431 void it_info( UnicodeString message );
432 void it_infoln( UnicodeString message );
433 void it_infoln( void );
434 void it_err(void);
435 void it_err( UnicodeString message );
436 void it_errln( UnicodeString message );
437 void it_dataerr( UnicodeString message );
438 void it_dataerrln( UnicodeString message );
439 
440 /**
441  * This is a variant of cintltst/ccolltst.c:CharsToUChars().
442  * It converts a character string into a UnicodeString, with
443  * unescaping \u sequences.
444  */
445 extern UnicodeString CharsToUnicodeString(const char* chars);
446 
447 /* alias for CharsToUnicodeString */
448 extern UnicodeString ctou(const char* chars);
449 
450 #endif // _INTLTEST
451