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