• 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-2015, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /********************************************************************************
9 *
10 * File CINTLTST.C
11 *
12 * Modification History:
13 *        Name                     Description
14 *     Madhu Katragadda               Creation
15 *********************************************************************************
16 */
17 
18 /*The main root for C API tests*/
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include "unicode/utypes.h"
24 #include "unicode/putil.h"
25 #include "cstring.h"
26 #include "cintltst.h"
27 #include "uassert.h"
28 #include "cmemory.h"
29 #include "unicode/uchar.h"
30 #include "unicode/ustring.h"
31 #include "unicode/ucnv.h"
32 #include "unicode/ures.h"
33 #include "unicode/uclean.h"
34 #include "unicode/ucal.h"
35 #include "uoptions.h"
36 #include "putilimp.h" /* for uprv_getRawUTCtime() */
37 #ifdef URES_DEBUG
38 #include "uresimp.h" /* for ures_dumpCacheContents() */
39 #endif
40 
41 #ifdef XP_MAC_CONSOLE
42 #   include <console.h>
43 #endif
44 
45 // ANDROID_USE_ICU_REG is defined when building against the Android OS source tree.
46 // androidicuinit is a static library which helps initialize ICU4C using the data
47 // on the device.
48 #if defined(__ANDROID__) && defined(ANDROID_USE_ICU_REG)
49 #include <androidicuinit/android_icu_reg.h>
50 #endif
51 
52 #define CTST_MAX_ALLOC 8192
53 /* Array used as a queue */
54 static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
55 static int ctst_allocated = 0;
56 static UBool ctst_free = FALSE;
57 static int ctst_allocated_total = 0;
58 
59 #define CTST_LEAK_CHECK 1
60 
61 #ifdef CTST_LEAK_CHECK
62 static void ctst_freeAll(void);
63 #endif
64 
65 /*
66  *  Forward Declarations
67  */
68 void ctest_setICU_DATA(void);
69 
70 
71 
72 #if UCONFIG_NO_LEGACY_CONVERSION
73 #   define TRY_CNV_1 "iso-8859-1"
74 #   define TRY_CNV_2 "ibm-1208"
75 #else
76 #   define TRY_CNV_1 "iso-8859-7"
77 #   define TRY_CNV_2 "sjis"
78 #endif
79 
80 static int gOrigArgc;
81 static const char* const * gOrigArgv;
82 
83 #ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
84 U_CAPI void unistr_printLengths();
85 #endif
86 
main(int argc,const char * const argv[])87 int main(int argc, const char* const argv[])
88 {
89     int nerrors = 0;
90     UBool   defaultDataFound;
91     TestNode *root;
92     const char *warnOrErr = "Failure";
93     UDate startTime, endTime;
94     int32_t diffTime;
95 
96     /* initial check for the default converter */
97     UErrorCode errorCode = U_ZERO_ERROR;
98     UResourceBundle *rb;
99     UConverter *cnv;
100 
101     U_MAIN_INIT_ARGS(argc, argv);
102 
103     startTime = uprv_getRawUTCtime();
104 
105     gOrigArgc = argc;
106     gOrigArgv = argv;
107     if (!initArgs(argc, argv, NULL, NULL)) {
108         /* Error already displayed. */
109         return -1;
110     }
111 
112     /* Check whether ICU will initialize without forcing the build data directory into
113      *  the ICU_DATA path.  Success here means either the data dll contains data, or that
114      *  this test program was run with ICU_DATA set externally.  Failure of this check
115      *  is normal when ICU data is not packaged into a shared library.
116      *
117      *  Whether or not this test succeeds, we want to cleanup and reinitialize
118      *  with a data path so that data loading from individual files can be tested.
119      */
120     defaultDataFound = TRUE;
121     u_init(&errorCode);
122     if (U_FAILURE(errorCode)) {
123         fprintf(stderr,
124             "#### Note:  ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
125         defaultDataFound = FALSE;
126     }
127     u_cleanup();
128 #ifdef URES_DEBUG
129     fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
130 #endif
131 
132     while (getTestOption(REPEAT_TESTS_OPTION) > 0) {   /* Loop runs once per complete execution of the tests
133                                   *   used for -r  (repeat) test option.                */
134         if (!initArgs(argc, argv, NULL, NULL)) {
135             /* Error already displayed. */
136             return -1;
137         }
138         errorCode = U_ZERO_ERROR;
139 
140         /* Initialize ICU */
141         if (!defaultDataFound) {
142             ctest_setICU_DATA();    /* u_setDataDirectory() must happen Before u_init() */
143         }
144         u_init(&errorCode);
145         if (U_FAILURE(errorCode)) {
146             fprintf(stderr,
147                 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
148                 "*** Check the ICU_DATA environment variable and \n"
149                 "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
150                 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
151                     fprintf(stderr, "*** Exiting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
152                     u_cleanup();
153                     return 1;
154                 }
155         }
156 
157 
158 
159         /* try more data */
160         cnv = ucnv_open(TRY_CNV_2, &errorCode);
161         if(cnv != 0) {
162             /* ok */
163             ucnv_close(cnv);
164         } else {
165             fprintf(stderr,
166                     "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
167                     "*** Check the ICU_DATA environment variable and \n"
168                     "*** check that the data files are present.\n", warnOrErr);
169             if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
170                 fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
171                 u_cleanup();
172                 return 1;
173             }
174         }
175 
176         rb = ures_open(NULL, "en", &errorCode);
177         if(U_SUCCESS(errorCode)) {
178             /* ok */
179             ures_close(rb);
180         } else {
181             fprintf(stderr,
182                     "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
183                     "*** Check the ICU_DATA environment variable and \n"
184                     "*** check that the data files are present.\n", warnOrErr);
185             if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
186                 fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
187                 u_cleanup();
188                 return 1;
189             }
190         }
191 
192         errorCode = U_ZERO_ERROR;
193         rb = ures_open(NULL, NULL, &errorCode);
194         if(U_SUCCESS(errorCode)) {
195             /* ok */
196             if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
197                 fprintf(stderr,
198                         "#### Note: The default locale %s is not available\n", uloc_getDefault());
199             }
200             ures_close(rb);
201         } else {
202             fprintf(stderr,
203                     "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
204             if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
205                 fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n"
206                     "*** purposely removed, to continue test anyway.\n");
207                 u_cleanup();
208                 return 1;
209             }
210         }
211         fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
212 
213         /* Build a tree of all tests.
214          *   Subsequently will be used to find / iterate the tests to run */
215         root = NULL;
216         addAllTests(&root);
217 
218         /*  Tests acutally run HERE.   TODO:  separate command line option parsing & setting from test execution!! */
219         nerrors = runTestRequest(root, argc, argv);
220 
221         setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
222         if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
223             printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
224         }
225         cleanUpTestTree(root);
226 
227 #ifdef CTST_LEAK_CHECK
228         ctst_freeAll();
229         /* To check for leaks */
230         u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
231 
232         if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
233           fprintf(stderr,"ctst_freeAll():  cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
234         }
235 #ifdef URES_DEBUG
236         if(ures_dumpCacheContents()) {
237           fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
238           nerrors++;
239         } else {
240           fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
241         }
242 #endif
243 #endif
244 
245     }  /* End of loop that repeats the entire test, if requested.  (Normally doesn't loop)  */
246 
247 #ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
248     unistr_printLengths();
249 #endif
250 
251     endTime = uprv_getRawUTCtime();
252     diffTime = (int32_t)(endTime - startTime);
253     printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
254         (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
255         (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
256         (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
257         (int)(diffTime%U_MILLIS_PER_SECOND));
258 
259 #ifdef ZERO_EXIT_CODE_FOR_FAILURES
260     return 0;
261 #else
262     return nerrors ? 1 : 0;
263 #endif
264 }
265 
266 /*
267 static void ctest_appendToDataDirectory(const char *toAppend)
268 {
269     const char *oldPath ="";
270     char newBuf [1024];
271     char *newPath = newBuf;
272     int32_t oldLen;
273     int32_t newLen;
274 
275     if((toAppend == NULL) || (*toAppend == 0)) {
276         return;
277     }
278 
279     oldPath = u_getDataDirectory();
280     if( (oldPath==NULL) || (*oldPath == 0)) {
281         u_setDataDirectory(toAppend);
282     } else {
283         oldLen = strlen(oldPath);
284         newLen = strlen(toAppend)+1+oldLen;
285 
286         if(newLen > 1022)
287         {
288             newPath = (char *)ctst_malloc(newLen);
289         }
290 
291         strcpy(newPath, oldPath);
292         strcpy(newPath+oldLen, U_PATH_SEP_STRING);
293         strcpy(newPath+oldLen+1, toAppend);
294 
295         u_setDataDirectory(newPath);
296 
297         if(newPath != newBuf)
298         {
299             free(newPath);
300         }
301     }
302 }
303 */
304 
305 /*  ctest_setICU_DATA  - if the ICU_DATA environment variable is not already
306  *                       set, try to deduce the directory in which ICU was built,
307  *                       and set ICU_DATA to "icu/source/data" in that location.
308  *                       The intent is to allow the tests to have a good chance
309  *                       of running without requiring that the user manually set
310  *                       ICU_DATA.  Common data isn't a problem, since it is
311  *                       picked up via a static (build time) reference, but the
312  *                       tests dynamically load some data.
313  */
ctest_setICU_DATA()314 void ctest_setICU_DATA() {
315     #if defined(__ANDROID__) && defined(ANDROID_USE_ICU_REG)
316     android_icu_register();
317     #else
318     u_setDataDirectory(ctest_dataOutDir());
319     #endif
320 }
321 
322 /*  These tests do cleanup and reinitialize ICU in the course of their operation.
323  *    The ICU data directory must be preserved across these operations.
324  *    Here is a helper function to assist with that.
325  */
safeGetICUDataDirectory()326 static char *safeGetICUDataDirectory() {
327     const char *dataDir = u_getDataDirectory();  /* Returned string vanashes with u_cleanup */
328     char *retStr = NULL;
329     if (dataDir != NULL) {
330         retStr = (char *)malloc(strlen(dataDir)+1);
331         strcpy(retStr, dataDir);
332     }
333     return retStr;
334 }
335 
ctest_resetICU()336 UBool ctest_resetICU() {
337     UErrorCode   status = U_ZERO_ERROR;
338     char         *dataDir = safeGetICUDataDirectory();
339 
340     #if defined(__ANDROID__) && defined(ANDROID_USE_ICU_REG)
341     android_icu_deregister();
342     #else
343     u_cleanup();
344     #endif
345     if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
346         /* Error already displayed. */
347         return FALSE;
348     }
349     ctest_setICU_DATA();
350     free(dataDir);
351     u_init(&status);
352     if (U_FAILURE(status)) {
353         log_err_status(status, "u_init failed with %s\n", u_errorName(status));
354         return FALSE;
355     }
356     return TRUE;
357 }
358 
CharsToUChars(const char * str)359 UChar* CharsToUChars(const char* str) {
360     /* Might be faster to just use uprv_strlen() as the preflight len - liu */
361     int32_t len = u_unescape(str, 0, 0); /* preflight */
362     /* Do NOT use malloc() - we are supposed to be acting like user code! */
363     UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
364     u_unescape(str, buf, len + 1);
365     return buf;
366 }
367 
austrdup(const UChar * unichars)368 char *austrdup(const UChar* unichars)
369 {
370     int   length;
371     char *newString;
372 
373     length    = u_strlen ( unichars );
374     /*newString = (char*)malloc  ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
375     newString = (char*)ctst_malloc  ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
376 
377     if ( newString == NULL )
378         return NULL;
379 
380     u_austrcpy ( newString, unichars );
381 
382     return newString;
383 }
384 
aescstrdup(const UChar * unichars,int32_t length)385 char *aescstrdup(const UChar* unichars,int32_t length){
386     char *newString,*targetLimit,*target;
387     UConverterFromUCallback cb;
388     const void *p;
389     UErrorCode errorCode = U_ZERO_ERROR;
390 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
391 #   if U_PLATFORM == U_PF_OS390
392         static const char convName[] = "ibm-1047";
393 #   else
394         static const char convName[] = "ibm-37";
395 #   endif
396 #else
397     static const char convName[] = "US-ASCII";
398 #endif
399     UConverter* conv = ucnv_open(convName, &errorCode);
400     if(length==-1){
401         length = u_strlen( unichars);
402     }
403     newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
404     target = newString;
405     targetLimit = newString+sizeof(char) * 8 * (length +1);
406     ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
407     ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
408     ucnv_close(conv);
409     *target = '\0';
410     return newString;
411 }
412 
loadTestData(UErrorCode * err)413 const char* loadTestData(UErrorCode* err) {
414     return ctest_loadTestData(err);
415 }
416 
417 #define CTEST_MAX_TIMEZONE_SIZE 256
418 static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
419 
420 /**
421  * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
422  * @param optionalTimeZone Set this to a requested timezone.
423  *      Set to NULL to use the standard test timezone (Pacific Time)
424  */
ctest_setTimeZone(const char * optionalTimeZone,UErrorCode * status)425 U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
426 #if !UCONFIG_NO_FORMATTING
427     UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
428 
429     if (optionalTimeZone == NULL) {
430         optionalTimeZone = "America/Los_Angeles";
431     }
432     if (gOriginalTimeZone[0]) {
433         log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
434                optionalTimeZone);
435     }
436     ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
437     if (U_FAILURE(*status)) {
438         log_err("*** Error: Failed to save default time zone: %s\n",
439                u_errorName(*status));
440         *status = U_ZERO_ERROR;
441     }
442 
443     u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
444     zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
445     ucal_setDefaultTimeZone(zoneID, status);
446     if (U_FAILURE(*status)) {
447         log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
448                optionalTimeZone, u_errorName(*status));
449     }
450 #endif
451 }
452 
453 /**
454  * Call this once get back the original timezone
455  */
ctest_resetTimeZone(void)456 U_CFUNC void ctest_resetTimeZone(void) {
457 #if !UCONFIG_NO_FORMATTING
458     UErrorCode status = U_ZERO_ERROR;
459 
460     ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
461     if (U_FAILURE(status)) {
462         log_err("*** Error: Failed to reset default time zone: %s\n",
463                u_errorName(status));
464     }
465     /* Set to an empty state */
466     gOriginalTimeZone[0] = 0;
467 #endif
468 }
469 
470 
ctst_malloc(size_t size)471 void *ctst_malloc(size_t size) {
472   ctst_allocated_total++;
473     if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
474         ctst_allocated = 0;
475         ctst_free = TRUE;
476     }
477     if(ctst_allocated_stuff[ctst_allocated]) {
478         free(ctst_allocated_stuff[ctst_allocated]);
479     }
480     return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
481 }
482 
483 #ifdef CTST_LEAK_CHECK
ctst_freeAll()484 static void ctst_freeAll() {
485     int i;
486     if(ctst_free == FALSE) { /* only free up to the allocated mark */
487         for(i=0; i<ctst_allocated; i++) {
488             free(ctst_allocated_stuff[i]);
489             ctst_allocated_stuff[i] = NULL;
490         }
491     } else { /* free all */
492         for(i=0; i<CTST_MAX_ALLOC; i++) {
493             free(ctst_allocated_stuff[i]);
494             ctst_allocated_stuff[i] = NULL;
495         }
496     }
497     ctst_allocated = 0;
498 }
499 
500 #define VERBOSE_ASSERTIONS
501 
assertSuccessCheck(const char * msg,UErrorCode * ec,UBool possibleDataError)502 U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
503     U_ASSERT(ec!=NULL);
504     if (U_FAILURE(*ec)) {
505         if (possibleDataError) {
506             log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
507         } else {
508             log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
509         }
510         return FALSE;
511     }
512     return TRUE;
513 }
514 
assertSuccess(const char * msg,UErrorCode * ec)515 U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
516     U_ASSERT(ec!=NULL);
517     return assertSuccessCheck(msg, ec, FALSE);
518 }
519 
520 /* if 'condition' is a UBool, the compiler complains bitterly about
521    expressions like 'a > 0' which it evaluates as int */
assertTrue(const char * msg,int condition)522 U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
523     if (!condition) {
524         log_err("FAIL: assertTrue() failed: %s\n", msg);
525     }
526 #ifdef VERBOSE_ASSERTIONS
527     else {
528         log_verbose("Ok: %s\n", msg);
529     }
530 #endif
531     return (UBool)condition;
532 }
533 
assertEquals(const char * message,const char * expected,const char * actual)534 U_CFUNC UBool assertEquals(const char* message, const char* expected,
535                            const char* actual) {
536     if (uprv_strcmp(expected, actual) != 0) {
537         log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
538                 message, actual, expected);
539         return FALSE;
540     }
541 #ifdef VERBOSE_ASSERTIONS
542     else {
543         log_verbose("Ok: %s; got \"%s\"\n", message, actual);
544     }
545 #endif
546     return TRUE;
547 }
548 
assertUEquals(const char * message,const UChar * expected,const UChar * actual)549 U_CFUNC UBool assertUEquals(const char* message, const UChar* expected,
550                             const UChar* actual) {
551     for (int32_t i=0;; i++) {
552         if (expected[i] != actual[i]) {
553             log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
554                     message, austrdup(actual), austrdup(expected));
555             return FALSE;
556         }
557         UChar curr = expected[i];
558         U_ASSERT(curr == actual[i]);
559         if (curr == 0) {
560             break;
561         }
562     }
563 #ifdef VERBOSE_ASSERTIONS
564     log_verbose("Ok: %s; got \"%s\"\n", message, austrdup(actual));
565 #endif
566     return TRUE;
567 }
568 
assertIntEquals(const char * message,int64_t expected,int64_t actual)569 U_CFUNC UBool assertIntEquals(const char* message, int64_t expected, int64_t actual) {
570     if (expected != actual) {
571         log_err("FAIL: %s; got \"%d\"; expected \"%d\"\n",
572                 message, actual, expected);
573         return FALSE;
574     }
575 #ifdef VERBOSE_ASSERTIONS
576     else {
577         log_verbose("Ok: %s; got \"%d\"\n", message, actual);
578     }
579 #endif
580     return TRUE;
581 }
582 
assertPtrEquals(const char * message,const void * expected,const void * actual)583 U_CFUNC UBool assertPtrEquals(const char* message, const void* expected, const void* actual) {
584     if (expected != actual) {
585         log_err("FAIL: %s; got 0x%llx; expected 0x%llx\n",
586                 message, actual, expected);
587         return FALSE;
588     }
589 #ifdef VERBOSE_ASSERTIONS
590     else {
591         log_verbose("Ok: %s; got 0x%llx\n", message, actual);
592     }
593 #endif
594     return TRUE;
595 }
596 
597 #endif
598