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 <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "unicode/utypes.h"
25 #include "unicode/putil.h"
26 #include "cstring.h"
27 #include "cintltst.h"
28 #include "uassert.h"
29 #include "cmemory.h"
30 #include "unicode/uchar.h"
31 #include "unicode/ustring.h"
32 #include "unicode/ucnv.h"
33 #include "unicode/ures.h"
34 #include "unicode/uclean.h"
35 #include "unicode/ucal.h"
36 #include "uoptions.h"
37 #include "putilimp.h" /* for uprv_getRawUTCtime() */
38 #ifdef URES_DEBUG
39 #include "uresimp.h" /* for ures_dumpCacheContents() */
40 #endif
41
42 #ifdef XP_MAC_CONSOLE
43 # include <console.h>
44 #endif
45
46 #define CTST_MAX_ALLOC 8192
47 /* Array used as a queue */
48 static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
49 static int ctst_allocated = 0;
50 static UBool ctst_free = false;
51 static int ctst_allocated_total = 0;
52
53 #define CTST_LEAK_CHECK 1
54
55 #ifdef CTST_LEAK_CHECK
56 static void ctst_freeAll(void);
57 #endif
58
59 static char* _testDataPath=NULL;
60
61 /*
62 * Forward Declarations
63 */
64 void ctest_setICU_DATA(void);
65
66
67
68 #if UCONFIG_NO_LEGACY_CONVERSION
69 # define TRY_CNV_1 "iso-8859-1"
70 # define TRY_CNV_2 "ibm-1208"
71 #else
72 # define TRY_CNV_1 "iso-8859-7"
73 # define TRY_CNV_2 "sjis"
74 #endif
75
76 static int gOrigArgc;
77 static const char* const * gOrigArgv;
78
79 #ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
80 U_CAPI void unistr_printLengths();
81 #endif
82
main(int argc,const char * const argv[])83 int main(int argc, const char* const argv[])
84 {
85 int nerrors = 0;
86 UBool defaultDataFound;
87 TestNode *root;
88 const char *warnOrErr = "Failure";
89 UDate startTime, endTime;
90 int32_t diffTime;
91
92 /* initial check for the default converter */
93 UErrorCode errorCode = U_ZERO_ERROR;
94 UResourceBundle *rb;
95 UConverter *cnv;
96
97 U_MAIN_INIT_ARGS(argc, argv);
98
99 startTime = uprv_getRawUTCtime();
100
101 gOrigArgc = argc;
102 gOrigArgv = argv;
103 if (!initArgs(argc, argv, NULL, NULL)) {
104 /* Error already displayed. */
105 return -1;
106 }
107
108 /* Check whether ICU will initialize without forcing the build data directory into
109 * the ICU_DATA path. Success here means either the data dll contains data, or that
110 * this test program was run with ICU_DATA set externally. Failure of this check
111 * is normal when ICU data is not packaged into a shared library.
112 *
113 * Whether or not this test succeeds, we want to cleanup and reinitialize
114 * with a data path so that data loading from individual files can be tested.
115 */
116 defaultDataFound = true;
117 u_init(&errorCode);
118 if (U_FAILURE(errorCode)) {
119 fprintf(stderr,
120 "#### Note: ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
121 defaultDataFound = false;
122 }
123 u_cleanup();
124 #ifdef URES_DEBUG
125 fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
126 #endif
127
128 while (getTestOption(REPEAT_TESTS_OPTION) > 0) { /* Loop runs once per complete execution of the tests
129 * used for -r (repeat) test option. */
130 if (!initArgs(argc, argv, NULL, NULL)) {
131 /* Error already displayed. */
132 return -1;
133 }
134 errorCode = U_ZERO_ERROR;
135
136 /* Initialize ICU */
137 if (!defaultDataFound) {
138 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
139 }
140 u_init(&errorCode);
141 if (U_FAILURE(errorCode)) {
142 fprintf(stderr,
143 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
144 "*** Check the ICU_DATA environment variable and \n"
145 "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
146 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
147 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
148 u_cleanup();
149 return 1;
150 }
151 }
152
153
154
155 /* try more data */
156 cnv = ucnv_open(TRY_CNV_2, &errorCode);
157 if(cnv != 0) {
158 /* ok */
159 ucnv_close(cnv);
160 } else {
161 fprintf(stderr,
162 "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
163 "*** Check the ICU_DATA environment variable and \n"
164 "*** check that the data files are present.\n", warnOrErr);
165 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
166 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
167 u_cleanup();
168 return 1;
169 }
170 }
171
172 rb = ures_open(NULL, "en", &errorCode);
173 if(U_SUCCESS(errorCode)) {
174 /* ok */
175 ures_close(rb);
176 } else {
177 fprintf(stderr,
178 "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
179 "*** Check the ICU_DATA environment variable and \n"
180 "*** check that the data files are present.\n", warnOrErr);
181 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
182 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
183 u_cleanup();
184 return 1;
185 }
186 }
187
188 errorCode = U_ZERO_ERROR;
189 rb = ures_open(NULL, NULL, &errorCode);
190 if(U_SUCCESS(errorCode)) {
191 /* ok */
192 if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
193 fprintf(stderr,
194 "#### Note: The default locale %s is not available\n", uloc_getDefault());
195 }
196 ures_close(rb);
197 } else {
198 fprintf(stderr,
199 "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
200 if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
201 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n"
202 "*** purposely removed, to continue test anyway.\n");
203 u_cleanup();
204 return 1;
205 }
206 }
207 fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
208
209 /* Build a tree of all tests.
210 * Subsequently will be used to find / iterate the tests to run */
211 root = NULL;
212 addAllTests(&root);
213
214 /* Tests actually run HERE. TODO: separate command line option parsing & setting from test execution!! */
215 nerrors = runTestRequest(root, argc, argv);
216
217 setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
218 if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
219 printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
220 }
221 cleanUpTestTree(root);
222
223 #ifdef CTST_LEAK_CHECK
224 ctst_freeAll();
225 /* To check for leaks */
226 u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
227
228 if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
229 fprintf(stderr,"ctst_freeAll(): cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
230 }
231 #ifdef URES_DEBUG
232 if(ures_dumpCacheContents()) {
233 fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
234 nerrors++;
235 } else {
236 fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
237 }
238 #endif
239 #endif
240
241 } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
242
243 #ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
244 unistr_printLengths();
245 #endif
246
247 endTime = uprv_getRawUTCtime();
248 diffTime = (int32_t)(endTime - startTime);
249 printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
250 (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
251 (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
252 (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
253 (int)(diffTime%U_MILLIS_PER_SECOND));
254
255 return nerrors ? 1 : 0;
256 }
257
258 /*
259 static void ctest_appendToDataDirectory(const char *toAppend)
260 {
261 const char *oldPath ="";
262 char newBuf [1024];
263 char *newPath = newBuf;
264 int32_t oldLen;
265 int32_t newLen;
266
267 if((toAppend == NULL) || (*toAppend == 0)) {
268 return;
269 }
270
271 oldPath = u_getDataDirectory();
272 if( (oldPath==NULL) || (*oldPath == 0)) {
273 u_setDataDirectory(toAppend);
274 } else {
275 oldLen = strlen(oldPath);
276 newLen = strlen(toAppend)+1+oldLen;
277
278 if(newLen > 1022)
279 {
280 newPath = (char *)ctst_malloc(newLen);
281 }
282
283 strcpy(newPath, oldPath);
284 strcpy(newPath+oldLen, U_PATH_SEP_STRING);
285 strcpy(newPath+oldLen+1, toAppend);
286
287 u_setDataDirectory(newPath);
288
289 if(newPath != newBuf)
290 {
291 free(newPath);
292 }
293 }
294 }
295 */
296
297 /* returns the path to icu/source/data */
ctest_dataSrcDir()298 const char * ctest_dataSrcDir()
299 {
300 static const char *dataSrcDir = NULL;
301
302 if(dataSrcDir) {
303 return dataSrcDir;
304 }
305
306 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
307 // to point to the top of the build hierarchy, which may or
308 // may not be the same as the source directory, depending on
309 // the configure options used. At any rate,
310 // set the data path to the built data from this directory.
311 // The value is complete with quotes, so it can be used
312 // as-is as a string constant.
313 */
314 #if defined (U_TOPSRCDIR)
315 {
316 dataSrcDir = U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
317 }
318 #else
319
320 /* On Windows, the file name obtained from __FILE__ includes a full path.
321 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
322 * Change to "wherever\icu\source\data"
323 */
324 {
325 static char p[sizeof(__FILE__) + 20];
326 char *pBackSlash;
327 int i;
328
329 strcpy(p, __FILE__);
330 /* We want to back over three '\' chars. */
331 /* Only Windows should end up here, so looking for '\' is safe. */
332 for (i=1; i<=3; i++) {
333 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
334 if (pBackSlash != NULL) {
335 *pBackSlash = 0; /* Truncate the string at the '\' */
336 }
337 }
338
339 if (pBackSlash != NULL) {
340 /* We found and truncated three names from the path.
341 * Now append "source\data" and set the environment
342 */
343 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
344 dataSrcDir = p;
345 }
346 else {
347 /* __FILE__ on MSVC7 does not contain the directory */
348 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
349 if (file) {
350 fclose(file);
351 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
352 }
353 else {
354 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
355 }
356 }
357 }
358 #endif
359
360 return dataSrcDir;
361
362 }
363
364 /* returns the path to icu/source/data/out */
ctest_dataOutDir()365 const char *ctest_dataOutDir()
366 {
367 static const char *dataOutDir = NULL;
368
369 if(dataOutDir) {
370 return dataOutDir;
371 }
372
373 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
374 // to point to the top of the build hierarchy, which may or
375 // may not be the same as the source directory, depending on
376 // the configure options used. At any rate,
377 // set the data path to the built data from this directory.
378 // The value is complete with quotes, so it can be used
379 // as-is as a string constant.
380 */
381 #if defined (U_TOPBUILDDIR)
382 {
383 dataOutDir = U_TOPBUILDDIR "data"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
384 }
385 #else
386
387 /* On Windows, the file name obtained from __FILE__ includes a full path.
388 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
389 * Change to "wherever\icu\source\data"
390 */
391 {
392 static char p[sizeof(__FILE__) + 20];
393 char *pBackSlash;
394 int i;
395
396 strcpy(p, __FILE__);
397 /* We want to back over three '\' chars. */
398 /* Only Windows should end up here, so looking for '\' is safe. */
399 for (i=1; i<=3; i++) {
400 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
401 if (pBackSlash != NULL) {
402 *pBackSlash = 0; /* Truncate the string at the '\' */
403 }
404 }
405
406 if (pBackSlash != NULL) {
407 /* We found and truncated three names from the path.
408 * Now append "source\data" and set the environment
409 */
410 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
411 dataOutDir = p;
412 }
413 else {
414 /* __FILE__ on MSVC7 does not contain the directory */
415 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
416 if (file) {
417 fclose(file);
418 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
419 }
420 else {
421 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
422 }
423 }
424 }
425 #endif
426
427 return dataOutDir;
428 }
429
430 /* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
431 * set, try to deduce the directory in which ICU was built,
432 * and set ICU_DATA to "icu/source/data" in that location.
433 * The intent is to allow the tests to have a good chance
434 * of running without requiring that the user manually set
435 * ICU_DATA. Common data isn't a problem, since it is
436 * picked up via a static (build time) reference, but the
437 * tests dynamically load some data.
438 */
ctest_setICU_DATA()439 void ctest_setICU_DATA() {
440
441 /* No location for the data dir was identifiable.
442 * Add other fallbacks for the test data location here if the need arises
443 */
444 if (getenv("ICU_DATA") == NULL) {
445 /* If ICU_DATA isn't set, set it to the usual location */
446 u_setDataDirectory(ctest_dataOutDir());
447 }
448 }
449
450 /* These tests do cleanup and reinitialize ICU in the course of their operation.
451 * The ICU data directory must be preserved across these operations.
452 * Here is a helper function to assist with that.
453 */
safeGetICUDataDirectory()454 static char *safeGetICUDataDirectory() {
455 const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
456 char *retStr = NULL;
457 if (dataDir != NULL) {
458 retStr = (char *)malloc(strlen(dataDir)+1);
459 strcpy(retStr, dataDir);
460 }
461 return retStr;
462 }
463
ctest_resetICU()464 UBool ctest_resetICU() {
465 UErrorCode status = U_ZERO_ERROR;
466 char *dataDir = safeGetICUDataDirectory();
467
468 u_cleanup();
469 if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
470 /* Error already displayed. */
471 return false;
472 }
473 u_setDataDirectory(dataDir);
474 free(dataDir);
475 u_init(&status);
476 if (U_FAILURE(status)) {
477 log_err_status(status, "u_init failed with %s\n", u_errorName(status));
478 return false;
479 }
480 return true;
481 }
482
CharsToUChars(const char * str)483 UChar* CharsToUChars(const char* str) {
484 /* Might be faster to just use uprv_strlen() as the preflight len - liu */
485 int32_t len = u_unescape(str, 0, 0); /* preflight */
486 /* Do NOT use malloc() - we are supposed to be acting like user code! */
487 UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
488 u_unescape(str, buf, len + 1);
489 return buf;
490 }
491
austrdup(const UChar * unichars)492 char *austrdup(const UChar* unichars)
493 {
494 int length;
495 char *newString;
496
497 length = u_strlen ( unichars );
498 /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
499 newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
500
501 if ( newString == NULL )
502 return NULL;
503
504 u_austrcpy ( newString, unichars );
505
506 return newString;
507 }
508
aescstrdup(const UChar * unichars,int32_t length)509 char *aescstrdup(const UChar* unichars,int32_t length){
510 char *newString,*targetLimit,*target;
511 UConverterFromUCallback cb;
512 const void *p;
513 UErrorCode errorCode = U_ZERO_ERROR;
514 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
515 # if U_PLATFORM == U_PF_OS390
516 static const char convName[] = "ibm-1047";
517 # else
518 static const char convName[] = "ibm-37";
519 # endif
520 #else
521 static const char convName[] = "US-ASCII";
522 #endif
523 UConverter* conv = ucnv_open(convName, &errorCode);
524 if(length==-1){
525 length = u_strlen( unichars);
526 }
527 newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
528 target = newString;
529 targetLimit = newString+sizeof(char) * 8 * (length +1);
530 ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
531 ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,true,&errorCode);
532 ucnv_close(conv);
533 *target = '\0';
534 return newString;
535 }
536
loadTestData(UErrorCode * err)537 const char* loadTestData(UErrorCode* err){
538 if( _testDataPath == NULL){
539 const char* directory=NULL;
540 UResourceBundle* test =NULL;
541 char* tdpath=NULL;
542 const char* tdrelativepath;
543 #if defined (APPLE_XCODE_BUILD)
544 tdrelativepath = "";
545 directory = U_TOPBUILDDIR;
546 #elif defined (U_TOPBUILDDIR)
547 tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
548 directory = U_TOPBUILDDIR;
549 #else
550 tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
551 directory= ctest_dataOutDir();
552 #endif
553
554 tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) + strlen(tdrelativepath)) + 10));
555
556
557 /* u_getDataDirectory shoul return \source\data ... set the
558 * directory to ..\source\data\..\test\testdata\out\testdata
559 *
560 * Fallback: When Memory mapped file is built
561 * ..\source\data\out\..\..\test\testdata\out\testdata
562 */
563 strcpy(tdpath, directory);
564 strcat(tdpath, tdrelativepath);
565 strcat(tdpath,"testdata");
566
567
568 test=ures_open(tdpath, "testtypes", err);
569
570 /* Fall back did not succeed either so return */
571 if(U_FAILURE(*err)){
572 *err = U_FILE_ACCESS_ERROR;
573 log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
574 return "";
575 }
576 ures_close(test);
577 _testDataPath = tdpath;
578 return _testDataPath;
579 }
580 return _testDataPath;
581 }
582
583 /**
584 * Returns the path to icu/source/test/testdata/
585 * Note: this function is parallel with C++ getSourceTestData in intltest.cpp
586 */
loadSourceTestData(UErrorCode * err)587 const char *loadSourceTestData(UErrorCode* err) {
588 (void)err;
589 const char *srcDataDir = NULL;
590 #ifdef U_TOPSRCDIR
591 srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING"test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
592 #else
593 srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
594 FILE *f = fopen(".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING "rbbitst.txt", "r");
595 if (f) {
596 /* We're in icu/source/test/intltest/ */
597 fclose(f);
598 }
599 else {
600 /* We're in icu/source/test/intltest/Platform/(Debug|Release) */
601 srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING
602 "test" U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
603 }
604 #endif
605 return srcDataDir;
606 }
607
608 #define CTEST_MAX_TIMEZONE_SIZE 256
609 static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
610
611 /**
612 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
613 * @param optionalTimeZone Set this to a requested timezone.
614 * Set to NULL to use the standard test timezone (Pacific Time)
615 */
ctest_setTimeZone(const char * optionalTimeZone,UErrorCode * status)616 U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
617 #if !UCONFIG_NO_FORMATTING
618 UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
619
620 if (optionalTimeZone == NULL) {
621 optionalTimeZone = "America/Los_Angeles";
622 }
623 if (gOriginalTimeZone[0]) {
624 log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
625 optionalTimeZone);
626 }
627 ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
628 if (U_FAILURE(*status)) {
629 log_err("*** Error: Failed to save default time zone: %s\n",
630 u_errorName(*status));
631 *status = U_ZERO_ERROR;
632 }
633
634 u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
635 zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
636 ucal_setDefaultTimeZone(zoneID, status);
637 if (U_FAILURE(*status)) {
638 log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
639 optionalTimeZone, u_errorName(*status));
640 }
641 #endif
642 }
643
644 /**
645 * Call this once get back the original timezone
646 */
ctest_resetTimeZone(void)647 U_CFUNC void ctest_resetTimeZone(void) {
648 #if !UCONFIG_NO_FORMATTING
649 UErrorCode status = U_ZERO_ERROR;
650
651 ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
652 if (U_FAILURE(status)) {
653 log_err("*** Error: Failed to reset default time zone: %s\n",
654 u_errorName(status));
655 }
656 /* Set to an empty state */
657 gOriginalTimeZone[0] = 0;
658 #endif
659 }
660
661
ctst_malloc(size_t size)662 void *ctst_malloc(size_t size) {
663 ctst_allocated_total++;
664 if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
665 ctst_allocated = 0;
666 ctst_free = true;
667 }
668 if(ctst_allocated_stuff[ctst_allocated]) {
669 free(ctst_allocated_stuff[ctst_allocated]);
670 }
671 return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
672 }
673
674 #ifdef CTST_LEAK_CHECK
ctst_freeAll()675 static void ctst_freeAll() {
676 int i;
677 if(ctst_free == false) { /* only free up to the allocated mark */
678 for(i=0; i<ctst_allocated; i++) {
679 free(ctst_allocated_stuff[i]);
680 ctst_allocated_stuff[i] = NULL;
681 }
682 } else { /* free all */
683 for(i=0; i<CTST_MAX_ALLOC; i++) {
684 free(ctst_allocated_stuff[i]);
685 ctst_allocated_stuff[i] = NULL;
686 }
687 }
688 ctst_allocated = 0;
689 _testDataPath=NULL;
690 }
691
692 #define VERBOSE_ASSERTIONS
693
assertSuccessCheck(const char * msg,UErrorCode * ec,UBool possibleDataError)694 U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
695 U_ASSERT(ec!=NULL);
696 if (U_FAILURE(*ec)) {
697 if (possibleDataError) {
698 log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
699 } else {
700 log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
701 }
702 return false;
703 }
704 return true;
705 }
706
assertSuccess(const char * msg,UErrorCode * ec)707 U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
708 U_ASSERT(ec!=NULL);
709 return assertSuccessCheck(msg, ec, false);
710 }
711
712 /* if 'condition' is a UBool, the compiler complains bitterly about
713 expressions like 'a > 0' which it evaluates as int */
assertTrue(const char * msg,int condition)714 U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
715 if (!condition) {
716 log_err("FAIL: assertTrue() failed: %s\n", msg);
717 }
718 #ifdef VERBOSE_ASSERTIONS
719 else {
720 log_verbose("Ok: %s\n", msg);
721 }
722 #endif
723 return (UBool)condition;
724 }
725
assertEquals(const char * message,const char * expected,const char * actual)726 U_CFUNC UBool assertEquals(const char* message, const char* expected,
727 const char* actual) {
728 if (expected == NULL) {
729 expected = "(null)";
730 }
731 if (actual == NULL) {
732 actual = "(null)";
733 }
734 if (uprv_strcmp(expected, actual) != 0) {
735 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
736 message, actual, expected);
737 return false;
738 }
739 #ifdef VERBOSE_ASSERTIONS
740 else {
741 log_verbose("Ok: %s; got \"%s\"\n", message, actual);
742 }
743 #endif
744 return true;
745 }
746
assertUEquals(const char * message,const UChar * expected,const UChar * actual)747 U_CFUNC UBool assertUEquals(const char* message, const UChar* expected,
748 const UChar* actual) {
749 if (expected == NULL) {
750 expected = u"(null)";
751 }
752 if (actual == NULL) {
753 actual = u"(null)";
754 }
755 for (int32_t i=0;; i++) {
756 if (expected[i] != actual[i]) {
757 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
758 message, austrdup(actual), austrdup(expected));
759 return false;
760 }
761 UChar curr = expected[i];
762 U_ASSERT(curr == actual[i]);
763 if (curr == 0) {
764 break;
765 }
766 }
767 #ifdef VERBOSE_ASSERTIONS
768 log_verbose("Ok: %s; got \"%s\"\n", message, austrdup(actual));
769 #endif
770 return true;
771 }
772
assertIntEquals(const char * message,int64_t expected,int64_t actual)773 U_CFUNC UBool assertIntEquals(const char* message, int64_t expected, int64_t actual) {
774 if (expected != actual) {
775 log_err("FAIL: %s; got \"%d\"; expected \"%d\"\n",
776 message, actual, expected);
777 return false;
778 }
779 #ifdef VERBOSE_ASSERTIONS
780 else {
781 log_verbose("Ok: %s; got \"%d\"\n", message, actual);
782 }
783 #endif
784 return true;
785 }
786
assertPtrEquals(const char * message,const void * expected,const void * actual)787 U_CFUNC UBool assertPtrEquals(const char* message, const void* expected, const void* actual) {
788 if (expected != actual) {
789 log_err("FAIL: %s; got 0x%llx; expected 0x%llx\n",
790 message, actual, expected);
791 return false;
792 }
793 #ifdef VERBOSE_ASSERTIONS
794 else {
795 log_verbose("Ok: %s; got 0x%llx\n", message, actual);
796 }
797 #endif
798 return true;
799 }
800
assertDoubleEquals(const char * message,double expected,double actual)801 U_CFUNC UBool assertDoubleEquals(const char *message, double expected, double actual) {
802 if (expected != actual) {
803 log_err("FAIL: %s; got \"%f\"; expected \"%f\"\n", message, actual, expected);
804 return false;
805 }
806 #ifdef VERBOSE_ASSERTIONS
807 else {
808 log_verbose("Ok: %s; got \"%f\"\n", message, actual);
809 }
810 #endif
811 return true;
812 }
813
814 #endif
815