]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cintltst.c
ICU-6.2.14.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cintltst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2004, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CINTLTST.C
9*
10* Modification History:
11* Name Description
12* Madhu Katragadda Creation
13*********************************************************************************
14*/
15
16/*The main root for C API tests*/
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include "unicode/utypes.h"
22#include "unicode/putil.h"
374ca955 23#include "cstring.h"
b75a7d8f
A
24#include "cintltst.h"
25#include "umutex.h"
374ca955 26#include "uassert.h"
b75a7d8f
A
27#include "unicode/uchar.h"
28#include "unicode/ustring.h"
29#include "unicode/ucnv.h"
30#include "unicode/ures.h"
31#include "unicode/uclean.h"
374ca955
A
32#include "unicode/ucal.h"
33#include "uoptions.h"
b75a7d8f
A
34
35#ifdef XP_MAC_CONSOLE
36# include <console.h>
37#endif
38
39#define CTST_LEAK_CHECK 1
40#ifdef CTST_LEAK_CHECK
41U_CFUNC void ctst_freeAll(void);
b75a7d8f
A
42#endif
43
44static char* _testDataPath=NULL;
45
46/*
47 * Forward Declarations
48 */
49void ctest_setICU_DATA(void);
50
b75a7d8f 51
b75a7d8f
A
52
53#if UCONFIG_NO_LEGACY_CONVERSION
54# define TRY_CNV_1 "iso-8859-1"
55# define TRY_CNV_2 "ibm-1208"
56#else
57# define TRY_CNV_1 "iso-8859-7"
58# define TRY_CNV_2 "sjis"
59#endif
60
374ca955
A
61
62/*
63 * Tracing functions.
64 */
65static int traceFnNestingDepth = 0;
66U_CDECL_BEGIN
67static void U_CALLCONV TraceEntry(const void *context, int32_t fnNumber) {
68 char buf[500];
69 utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() enter.\n", utrace_functionName(fnNumber));
70 buf[sizeof(buf)-1]=0;
71 fputs(buf, stdout);
72 traceFnNestingDepth++;
73}
74
75static void U_CALLCONV TraceExit(const void *context, int32_t fnNumber, const char *fmt, va_list args) {
76 char buf[500];
77
78 if (traceFnNestingDepth>0) {
79 traceFnNestingDepth--;
80 }
81 utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() ", utrace_functionName(fnNumber));
82 buf[sizeof(buf)-1]=0;
83 fputs(buf, stdout);
84 utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
85 buf[sizeof(buf)-1]=0;
86 fputs(buf, stdout);
87 putc('\n', stdout);
88}
89
90static void U_CALLCONV TraceData(const void *context, int32_t fnNumber,
91 int32_t level, const char *fmt, va_list args) {
92 char buf[500];
93 utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
94 buf[sizeof(buf)-1]=0;
95 fputs(buf, stdout);
96 putc('\n', stdout);
97}
98U_CDECL_END
99
100
101
b75a7d8f
A
102int main(int argc, const char* const argv[])
103{
104 int nerrors = 0;
105 int warnOnMissingData = 0;
374ca955
A
106 int i, j;
107 UBool defaultDataFound;
b75a7d8f
A
108 TestNode *root;
109 const char *warnOrErr = "Failure";
374ca955 110 const char** argv2;
b75a7d8f
A
111
112 /* initial check for the default converter */
113 UErrorCode errorCode = U_ZERO_ERROR;
114 UResourceBundle *rb;
115 UConverter *cnv;
116
374ca955
A
117 U_MAIN_INIT_ARGS(argc, argv);
118
119 argv2 = (const char**) malloc(sizeof(char*) * argc);
120 if (argv2 == NULL) {
121 printf("*** Error: Out of memory (too many cmd line args?)\n");
122 return 1;
123 }
124 argv2[0] = argv[0];
125
b75a7d8f
A
126
127 /* Checkargs */
374ca955
A
128 /* TODO: Test framework arg handling needs to be decoupled from test execution
129 * so that the args being processed here don't need special handling,
130 * separate from the other test args.
131 */
132 ICU_TRACE = UTRACE_OFF;
133 for(i=1,j=1;i<argc;i++) {
134 argv2[j++] = argv[i];
b75a7d8f
A
135 if(!strcmp(argv[i],"-w")) {
136 warnOnMissingData = 1;
137 warnOrErr = "Warning";
138 }
374ca955
A
139 else if (strcmp( argv[i], "-t_error") == 0) {
140 ICU_TRACE = UTRACE_ERROR;
141 }
142 else if (strcmp( argv[i], "-t_warn") == 0) {
143 ICU_TRACE = UTRACE_WARNING;
144 }
145 else if (strcmp( argv[i], "-t_oc") == 0) {
146 ICU_TRACE = UTRACE_OPEN_CLOSE;
147 }
148 else if (strcmp( argv[i], "-t_info") == 0) {
149 ICU_TRACE = UTRACE_INFO;
150 }
151 else if (strcmp( argv[i], "-t_verbose") == 0) {
152 ICU_TRACE = UTRACE_VERBOSE;
153 }
b75a7d8f 154 }
374ca955
A
155 argc = j;
156
157
158 utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData);
159 utrace_setLevel(ICU_TRACE);
160
161
162 while (REPEAT_TESTS > 0) { /* Loop runs once per complete execution of the tests
163 * used for -r (repeat) test option. */
164
165 /* Check whether ICU will initialize without forcing the build data directory into
166 * the ICU_DATA path. Success here means either the data dll contains data, or that
167 * this test program was run with ICU_DATA set externally. Failure of this check
168 * is normal when ICU data is not packaged into a shared library.
169 *
170 * Whether or not this test succeeds, we want to cleanup and reinitialize
171 * with a data path so that data loading from individual files can be tested.
172 */
173 defaultDataFound = TRUE;
174 u_init(&errorCode);
175 if (U_FAILURE(errorCode)) {
b75a7d8f 176 fprintf(stderr,
374ca955
A
177 "#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
178 defaultDataFound = FALSE;
b75a7d8f 179 }
374ca955
A
180 u_cleanup();
181 errorCode = U_ZERO_ERROR;
182 utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData);
183 utrace_setLevel(ICU_TRACE);
184
185 /* Initialize ICU */
186 if (!defaultDataFound) {
187 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
188 }
189 u_init(&errorCode);
190 if (U_FAILURE(errorCode)) {
b75a7d8f 191 fprintf(stderr,
374ca955 192 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
b75a7d8f 193 "*** Check the ICU_DATA environment variable and \n"
374ca955
A
194 "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
195 if(warnOnMissingData == 0) {
196 fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
197 u_cleanup();
198 return 1;
199 }
b75a7d8f 200 }
374ca955
A
201
202
b75a7d8f
A
203
204 /* try more data */
205 cnv = ucnv_open(TRY_CNV_2, &errorCode);
206 if(cnv != 0) {
207 /* ok */
208 ucnv_close(cnv);
209 } else {
210 fprintf(stderr,
211 "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
212 "*** Check the ICU_DATA environment variable and \n"
213 "*** check that the data files are present.\n", warnOrErr);
214 if(warnOnMissingData == 0) {
215 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
374ca955 216 u_cleanup();
b75a7d8f
A
217 return 1;
218 }
219 }
220
221 rb = ures_open(NULL, "en", &errorCode);
222 if(U_SUCCESS(errorCode)) {
223 /* ok */
224 ures_close(rb);
225 } else {
226 fprintf(stderr,
227 "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
228 "*** Check the ICU_DATA environment variable and \n"
229 "*** check that the data files are present.\n", warnOrErr);
230 if(warnOnMissingData == 0) {
231 fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
374ca955 232 u_cleanup();
b75a7d8f
A
233 return 1;
234 }
235 }
236
237 fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
238
374ca955
A
239 /* Build a tree of all tests.
240 * Subsequently will be used to find / iterate the tests to run */
b75a7d8f
A
241 root = NULL;
242 addAllTests(&root);
374ca955
A
243
244 /* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */
245 nerrors = processArgs(root, argc, argv2);
246
b75a7d8f
A
247 if (--REPEAT_TESTS > 0) {
248 printf("Repeating tests %d more time(s)\n", REPEAT_TESTS);
249 }
250 cleanUpTestTree(root);
374ca955 251
b75a7d8f
A
252#ifdef CTST_LEAK_CHECK
253 ctst_freeAll();
b75a7d8f 254 /* To check for leaks */
b75a7d8f
A
255 u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
256#endif
b75a7d8f 257
374ca955 258 } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
b75a7d8f 259
374ca955 260 free((void*)argv2);
b75a7d8f
A
261 return nerrors ? 1 : 0;
262}
263
264/*
265static void ctest_appendToDataDirectory(const char *toAppend)
266{
267 const char *oldPath ="";
268 char newBuf [1024];
269 char *newPath = newBuf;
270 int32_t oldLen;
271 int32_t newLen;
272
273 if((toAppend == NULL) || (*toAppend == 0)) {
274 return;
275 }
276
277 oldPath = u_getDataDirectory();
278 if( (oldPath==NULL) || (*oldPath == 0)) {
279 u_setDataDirectory(toAppend);
280 } else {
281 oldLen = strlen(oldPath);
282 newLen = strlen(toAppend)+1+oldLen;
283
284 if(newLen > 1022)
285 {
286 newPath = (char *)ctst_malloc(newLen);
287 }
288
289 strcpy(newPath, oldPath);
290 strcpy(newPath+oldLen, U_PATH_SEP_STRING);
291 strcpy(newPath+oldLen+1, toAppend);
292
293 u_setDataDirectory(newPath);
294
295 if(newPath != newBuf)
296 {
297 free(newPath);
298 }
299 }
300}
301*/
302
303void
304ctest_pathnameInContext( char* fullname, int32_t maxsize, const char* relPath )
305{
306 char mainDirBuffer[1024];
307 char* mainDir = NULL;
308 const char *dataDirectory = ctest_dataOutDir();
309 const char inpSepChar = '|';
310 char* tmp;
311 int32_t lenMainDir;
312 int32_t lenRelPath;
313
314#ifdef XP_MAC
315 Str255 volName;
316 int16_t volNum;
317 OSErr err = GetVol( volName, &volNum );
318 if (err != noErr)
319 volName[0] = 0;
320 mainDir = (char*) &(volName[1]);
321 mainDir[volName[0]] = 0;
322#else
323 if (dataDirectory != NULL) {
324 strcpy(mainDirBuffer, dataDirectory);
325 strcat(mainDirBuffer, ".." U_FILE_SEP_STRING);
326 } else {
327 mainDirBuffer[0]='\0';
328 }
329 mainDir = mainDirBuffer;
330#endif
331
332 lenMainDir = (int32_t)strlen(mainDir);
333 if(lenMainDir > 0 && mainDir[lenMainDir - 1] != U_FILE_SEP_CHAR) {
334 mainDir[lenMainDir++] = U_FILE_SEP_CHAR;
335 mainDir[lenMainDir] = 0;
336 }
337
338 if (relPath[0] == '|')
339 relPath++;
340 lenRelPath = (int32_t)strlen(relPath);
341 if (maxsize < lenMainDir + lenRelPath + 2) {
342 fullname[0] = 0;
343 return;
344 }
345 strcpy(fullname, mainDir);
346 /*strcat(fullname, U_FILE_SEP_STRING);*/
347 strcat(fullname, relPath);
348 strchr(fullname, inpSepChar);
349 tmp = strchr(fullname, inpSepChar);
350 while (tmp) {
351 *tmp = U_FILE_SEP_CHAR;
352 tmp = strchr(tmp+1, inpSepChar);
353 }
354}
355
356
357/* returns the path to icu/source/data */
358const char * ctest_dataSrcDir()
359{
360 static const char *dataSrcDir = NULL;
361
362 if(dataSrcDir) {
363 return dataSrcDir;
364 }
365
366 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
367 // to point to the top of the build hierarchy, which may or
368 // may not be the same as the source directory, depending on
369 // the configure options used. At any rate,
370 // set the data path to the built data from this directory.
371 // The value is complete with quotes, so it can be used
372 // as-is as a string constant.
373 */
374#if defined (U_TOPSRCDIR)
375 {
376 dataSrcDir = U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
377 }
378#else
379
380 /* On Windows, the file name obtained from __FILE__ includes a full path.
381 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
382 * Change to "wherever\icu\source\data"
383 */
384 {
385 static char p[sizeof(__FILE__) + 20];
386 char *pBackSlash;
387 int i;
388
b75a7d8f
A
389 strcpy(p, __FILE__);
390 /* We want to back over three '\' chars. */
391 /* Only Windows should end up here, so looking for '\' is safe. */
392 for (i=1; i<=3; i++) {
393 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
394 if (pBackSlash != NULL) {
395 *pBackSlash = 0; /* Truncate the string at the '\' */
396 }
397 }
398
399 if (pBackSlash != NULL) {
400 /* We found and truncated three names from the path.
401 * Now append "source\data" and set the environment
402 */
403 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
374ca955 404 dataSrcDir = p;
b75a7d8f
A
405 }
406 else {
407 /* __FILE__ on MSVC7 does not contain the directory */
374ca955
A
408 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
409 if (file) {
410 fclose(file);
411 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
412 }
413 else {
414 dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
415 }
b75a7d8f
A
416 }
417 }
418#endif
419
420 return dataSrcDir;
421
422}
423
424/* returns the path to icu/source/data/out */
425const char *ctest_dataOutDir()
426{
427 static const char *dataOutDir = NULL;
428
429 if(dataOutDir) {
430 return dataOutDir;
431 }
432
433 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
434 // to point to the top of the build hierarchy, which may or
435 // may not be the same as the source directory, depending on
436 // the configure options used. At any rate,
437 // set the data path to the built data from this directory.
438 // The value is complete with quotes, so it can be used
439 // as-is as a string constant.
440 */
441#if defined (U_TOPBUILDDIR)
442 {
374ca955 443 dataOutDir = U_TOPBUILDDIR "data"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
b75a7d8f
A
444 }
445#else
446
447 /* On Windows, the file name obtained from __FILE__ includes a full path.
448 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
449 * Change to "wherever\icu\source\data"
450 */
451 {
452 static char p[sizeof(__FILE__) + 20];
453 char *pBackSlash;
454 int i;
455
b75a7d8f
A
456 strcpy(p, __FILE__);
457 /* We want to back over three '\' chars. */
458 /* Only Windows should end up here, so looking for '\' is safe. */
459 for (i=1; i<=3; i++) {
460 pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
461 if (pBackSlash != NULL) {
462 *pBackSlash = 0; /* Truncate the string at the '\' */
463 }
464 }
465
466 if (pBackSlash != NULL) {
467 /* We found and truncated three names from the path.
468 * Now append "source\data" and set the environment
469 */
470 strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
374ca955 471 dataOutDir = p;
b75a7d8f
A
472 }
473 else {
474 /* __FILE__ on MSVC7 does not contain the directory */
374ca955
A
475 FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
476 if (file) {
477 fclose(file);
478 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
479 }
480 else {
481 dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
482 }
b75a7d8f
A
483 }
484 }
485#endif
486
487 return dataOutDir;
488}
489
490/* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
491 * set, try to deduce the directory in which ICU was built,
492 * and set ICU_DATA to "icu/source/data" in that location.
493 * The intent is to allow the tests to have a good chance
494 * of running without requiring that the user manually set
495 * ICU_DATA. Common data isn't a problem, since it is
496 * picked up via a static (build time) reference, but the
497 * tests dynamically load some data.
498 */
499void ctest_setICU_DATA() {
500
b75a7d8f
A
501 /* No location for the data dir was identifiable.
502 * Add other fallbacks for the test data location here if the need arises
503 */
374ca955
A
504 if (getenv("ICU_DATA") == NULL) {
505 /* If ICU_DATA isn't set, set it to the usual location */
506 u_setDataDirectory(ctest_dataOutDir());
507 }
b75a7d8f
A
508}
509
510UChar* CharsToUChars(const char* str) {
511 /* Might be faster to just use uprv_strlen() as the preflight len - liu */
512 int32_t len = u_unescape(str, 0, 0); /* preflight */
513 /* Do NOT use malloc() - we are supposed to be acting like user code! */
514 UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
515 u_unescape(str, buf, len + 1);
516 return buf;
517}
518
519char *austrdup(const UChar* unichars)
520{
521 int length;
522 char *newString;
523
524 length = u_strlen ( unichars );
525 /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
526 newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
527
528 if ( newString == NULL )
529 return NULL;
530
531 u_austrcpy ( newString, unichars );
532
533 return newString;
534}
535
536char *aescstrdup(const UChar* unichars,int32_t length){
537 char *newString,*targetLimit,*target;
538 UConverterFromUCallback cb;
539 const void *p;
540 UErrorCode errorCode = U_ZERO_ERROR;
541#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
542# ifdef OS390
543 static const char convName[] = "ibm-1047";
544# else
545 static const char convName[] = "ibm-37";
546# endif
547#else
548 static const char convName[] = "US-ASCII";
549#endif
550 UConverter* conv = ucnv_open(convName, &errorCode);
551 if(length==-1){
552 length = u_strlen( unichars);
553 }
554 newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
555 target = newString;
556 targetLimit = newString+sizeof(char) * 8 * (length +1);
557 ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_JAVA, &cb, &p, &errorCode);
558 ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
559 ucnv_close(conv);
560 *target = '\0';
561 return newString;
562}
563
564const char* loadTestData(UErrorCode* err){
565 const char* directory=NULL;
566 UResourceBundle* test =NULL;
567 char* tdpath=NULL;
568 const char* tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
569 if( _testDataPath == NULL){
570 directory= ctest_dataOutDir();
571
572 tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10));
573
574
575 /* u_getDataDirectory shoul return \source\data ... set the
576 * directory to ..\source\data\..\test\testdata\out\testdata
577 *
578 * Fallback: When Memory mapped file is built
579 * ..\source\data\out\..\..\test\testdata\out\testdata
580 */
581 strcpy(tdpath, directory);
582 strcat(tdpath, tdrelativepath);
583 strcat(tdpath,"testdata");
584
585
586 test=ures_open(tdpath, "testtypes", err);
587
588 /* Fall back did not succeed either so return */
589 if(U_FAILURE(*err)){
590 *err = U_FILE_ACCESS_ERROR;
591 log_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
592 return "";
593 }
594 ures_close(test);
595 _testDataPath = tdpath;
596 return _testDataPath;
597 }
598 return _testDataPath;
599}
600
374ca955
A
601#define CTEST_MAX_TIMEZONE_SIZE 256
602static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
603
604/**
605 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
606 * @param optionalTimeZone Set this to a requested timezone.
607 * Set to NULL to use the standard test timezone (Pacific Time)
608 */
609U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
610#if !UCONFIG_NO_FORMATTING
611 UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
612
613 if (optionalTimeZone == NULL) {
614 optionalTimeZone = "America/Los_Angeles";
615 }
616 if (gOriginalTimeZone[0]) {
617 log_err("*** Error: time zone saved twice. New value will be %s\n",
618 optionalTimeZone);
619 }
620 ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
621 if (U_FAILURE(*status)) {
622 log_err("*** Error: Failed to save default time zone: %s\n",
623 u_errorName(*status));
624 *status = U_ZERO_ERROR;
625 }
626
627 u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
628 zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
629 ucal_setDefaultTimeZone(zoneID, status);
630 if (U_FAILURE(*status)) {
631 log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
632 optionalTimeZone, u_errorName(*status));
633 }
634#endif
635}
636
637/**
638 * Call this once get back the original timezone
639 */
640U_CFUNC void ctest_resetTimeZone(void) {
641#if !UCONFIG_NO_FORMATTING
642 UErrorCode status = U_ZERO_ERROR;
643
644 ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
645 if (U_FAILURE(status)) {
646 log_err("*** Error: Failed to reset default time zone: %s\n",
647 u_errorName(status));
648 }
649 /* Set to an empty state */
650 gOriginalTimeZone[0] = 0;
651#endif
652}
653
b75a7d8f
A
654#define CTST_MAX_ALLOC 10000
655/* Array used as a queue */
374ca955 656static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
b75a7d8f
A
657static int ctst_allocated = 0;
658static UBool ctst_free = FALSE;
659
b75a7d8f
A
660void *ctst_malloc(size_t size) {
661 if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
662 ctst_allocated = 0;
663 ctst_free = TRUE;
664 }
665 if(ctst_allocated_stuff[ctst_allocated]) {
666 free(ctst_allocated_stuff[ctst_allocated]);
667 }
668 return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
669}
670
671#ifdef CTST_LEAK_CHECK
672void ctst_freeAll() {
673 int i;
674 if(ctst_free == 0) {
675 for(i=0; i<ctst_allocated; i++) {
676 free(ctst_allocated_stuff[i]);
374ca955 677 ctst_allocated_stuff[i] = NULL;
b75a7d8f
A
678 }
679 } else {
680 for(i=0; i<CTST_MAX_ALLOC; i++) {
681 free(ctst_allocated_stuff[i]);
374ca955 682 ctst_allocated_stuff[i] = NULL;
b75a7d8f
A
683 }
684 }
374ca955 685 ctst_allocated = 0;
b75a7d8f
A
686 _testDataPath=NULL;
687}
374ca955
A
688
689#define VERBOSE_ASSERTIONS
690
691U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
692 U_ASSERT(ec!=NULL);
693 if (U_FAILURE(*ec)) {
694 log_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
695 return FALSE;
696 }
697 return TRUE;
698}
699
700/* if 'condition' is a UBool, the compiler complains bitterly about
701 expressions like 'a > 0' which it evaluates as int */
702U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
703 if (!condition) {
704 log_err("FAIL: assertTrue() failed: %s\n", msg);
705 }
706#ifdef VERBOSE_ASSERTIONS
707 else {
708 log_verbose("Ok: %s\n", msg);
709 }
710#endif
711 return (UBool)condition;
712}
713
714U_CFUNC UBool assertEquals(const char* message, const char* expected,
715 const char* actual) {
716 if (uprv_strcmp(expected, actual) != 0) {
717 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
718 message, actual, expected);
719 return FALSE;
720 }
721#ifdef VERBOSE_ASSERTIONS
722 else {
723 log_verbose("Ok: %s; got \"%s\"\n", message, actual);
724 }
725#endif
726 return TRUE;
727}
728
b75a7d8f 729#endif