1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2015, International Business Machines Corporation and
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"
23#include "cstring.h"
24#include "cintltst.h"
25#include "uassert.h"
26#include "cmemory.h"
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"
32#include "unicode/ucal.h"
33#include "uoptions.h"
34#include "putilimp.h" /* for uprv_getRawUTCtime() */
35#ifdef URES_DEBUG
36#include "uresimp.h" /* for ures_dumpCacheContents() */
37#endif
38
39#ifdef XP_MAC_CONSOLE
40#   include <console.h>
41#endif
42
43#define CTST_MAX_ALLOC 8192
44/* Array used as a queue */
45static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
46static int ctst_allocated = 0;
47static UBool ctst_free = FALSE;
48static int ctst_allocated_total = 0;
49
50#define CTST_LEAK_CHECK 1
51
52#ifdef CTST_LEAK_CHECK
53static void ctst_freeAll(void);
54#endif
55
56static char* _testDataPath=NULL;
57
58/*
59 *  Forward Declarations
60 */
61void 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
73static int gOrigArgc;
74static const char* const * gOrigArgv;
75
76#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
77U_CAPI void unistr_printLengths();
78#endif
79
80int 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    return nerrors ? 1 : 0;
253}
254
255/*
256static void ctest_appendToDataDirectory(const char *toAppend)
257{
258    const char *oldPath ="";
259    char newBuf [1024];
260    char *newPath = newBuf;
261    int32_t oldLen;
262    int32_t newLen;
263
264    if((toAppend == NULL) || (*toAppend == 0)) {
265        return;
266    }
267
268    oldPath = u_getDataDirectory();
269    if( (oldPath==NULL) || (*oldPath == 0)) {
270        u_setDataDirectory(toAppend);
271    } else {
272        oldLen = strlen(oldPath);
273        newLen = strlen(toAppend)+1+oldLen;
274
275        if(newLen > 1022)
276        {
277            newPath = (char *)ctst_malloc(newLen);
278        }
279
280        strcpy(newPath, oldPath);
281        strcpy(newPath+oldLen, U_PATH_SEP_STRING);
282        strcpy(newPath+oldLen+1, toAppend);
283
284        u_setDataDirectory(newPath);
285
286        if(newPath != newBuf)
287        {
288            free(newPath);
289        }
290    }
291}
292*/
293
294/* returns the path to icu/source/data */
295const char *  ctest_dataSrcDir()
296{
297    static const char *dataSrcDir = NULL;
298
299    if(dataSrcDir) {
300        return dataSrcDir;
301    }
302
303    /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
304    //              to point to the top of the build hierarchy, which may or
305    //              may not be the same as the source directory, depending on
306    //              the configure options used.  At any rate,
307    //              set the data path to the built data from this directory.
308    //              The value is complete with quotes, so it can be used
309    //              as-is as a string constant.
310    */
311#if defined (U_TOPSRCDIR)
312    {
313        dataSrcDir = U_TOPSRCDIR  U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
314    }
315#else
316
317    /* On Windows, the file name obtained from __FILE__ includes a full path.
318     *             This file is "wherever\icu\source\test\cintltst\cintltst.c"
319     *             Change to    "wherever\icu\source\data"
320     */
321    {
322        static char p[sizeof(__FILE__) + 20];
323        char *pBackSlash;
324        int i;
325
326        strcpy(p, __FILE__);
327        /* We want to back over three '\' chars.                            */
328        /*   Only Windows should end up here, so looking for '\' is safe.   */
329        for (i=1; i<=3; i++) {
330            pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
331            if (pBackSlash != NULL) {
332                *pBackSlash = 0;        /* Truncate the string at the '\'   */
333            }
334        }
335
336        if (pBackSlash != NULL) {
337            /* We found and truncated three names from the path.
338             *  Now append "source\data" and set the environment
339             */
340            strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
341            dataSrcDir = p;
342        }
343        else {
344            /* __FILE__ on MSVC7 does not contain the directory */
345            FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
346            if (file) {
347                fclose(file);
348                dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
349            }
350            else {
351                dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
352            }
353        }
354    }
355#endif
356
357    return dataSrcDir;
358
359}
360
361/* returns the path to icu/source/data/out */
362const char *ctest_dataOutDir()
363{
364    static const char *dataOutDir = NULL;
365
366    if(dataOutDir) {
367        return dataOutDir;
368    }
369
370    /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
371    //              to point to the top of the build hierarchy, which may or
372    //              may not be the same as the source directory, depending on
373    //              the configure options used.  At any rate,
374    //              set the data path to the built data from this directory.
375    //              The value is complete with quotes, so it can be used
376    //              as-is as a string constant.
377    */
378#if defined (U_TOPBUILDDIR)
379    {
380        dataOutDir = U_TOPBUILDDIR "data"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
381    }
382#else
383
384    /* On Windows, the file name obtained from __FILE__ includes a full path.
385     *             This file is "wherever\icu\source\test\cintltst\cintltst.c"
386     *             Change to    "wherever\icu\source\data"
387     */
388    {
389        static char p[sizeof(__FILE__) + 20];
390        char *pBackSlash;
391        int i;
392
393        strcpy(p, __FILE__);
394        /* We want to back over three '\' chars.                            */
395        /*   Only Windows should end up here, so looking for '\' is safe.   */
396        for (i=1; i<=3; i++) {
397            pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
398            if (pBackSlash != NULL) {
399                *pBackSlash = 0;        /* Truncate the string at the '\'   */
400            }
401        }
402
403        if (pBackSlash != NULL) {
404            /* We found and truncated three names from the path.
405             *  Now append "source\data" and set the environment
406             */
407            strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
408            dataOutDir = p;
409        }
410        else {
411            /* __FILE__ on MSVC7 does not contain the directory */
412            FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
413            if (file) {
414                fclose(file);
415                dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
416            }
417            else {
418                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;
419            }
420        }
421    }
422#endif
423
424    return dataOutDir;
425}
426
427/*  ctest_setICU_DATA  - if the ICU_DATA environment variable is not already
428 *                       set, try to deduce the directory in which ICU was built,
429 *                       and set ICU_DATA to "icu/source/data" in that location.
430 *                       The intent is to allow the tests to have a good chance
431 *                       of running without requiring that the user manually set
432 *                       ICU_DATA.  Common data isn't a problem, since it is
433 *                       picked up via a static (build time) reference, but the
434 *                       tests dynamically load some data.
435 */
436void ctest_setICU_DATA() {
437
438    /* No location for the data dir was identifiable.
439     *   Add other fallbacks for the test data location here if the need arises
440     */
441    if (getenv("ICU_DATA") == NULL) {
442        /* If ICU_DATA isn't set, set it to the usual location */
443        u_setDataDirectory(ctest_dataOutDir());
444    }
445}
446
447/*  These tests do cleanup and reinitialize ICU in the course of their operation.
448 *    The ICU data directory must be preserved across these operations.
449 *    Here is a helper function to assist with that.
450 */
451static char *safeGetICUDataDirectory() {
452    const char *dataDir = u_getDataDirectory();  /* Returned string vanashes with u_cleanup */
453    char *retStr = NULL;
454    if (dataDir != NULL) {
455        retStr = (char *)malloc(strlen(dataDir)+1);
456        strcpy(retStr, dataDir);
457    }
458    return retStr;
459}
460
461UBool ctest_resetICU() {
462    UErrorCode   status = U_ZERO_ERROR;
463    char         *dataDir = safeGetICUDataDirectory();
464
465    u_cleanup();
466    if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
467        /* Error already displayed. */
468        return FALSE;
469    }
470    u_setDataDirectory(dataDir);
471    free(dataDir);
472    u_init(&status);
473    if (U_FAILURE(status)) {
474        log_err_status(status, "u_init failed with %s\n", u_errorName(status));
475        return FALSE;
476    }
477    return TRUE;
478}
479
480UChar* CharsToUChars(const char* str) {
481    /* Might be faster to just use uprv_strlen() as the preflight len - liu */
482    int32_t len = u_unescape(str, 0, 0); /* preflight */
483    /* Do NOT use malloc() - we are supposed to be acting like user code! */
484    UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
485    u_unescape(str, buf, len + 1);
486    return buf;
487}
488
489char *austrdup(const UChar* unichars)
490{
491    int   length;
492    char *newString;
493
494    length    = u_strlen ( unichars );
495    /*newString = (char*)malloc  ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
496    newString = (char*)ctst_malloc  ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
497
498    if ( newString == NULL )
499        return NULL;
500
501    u_austrcpy ( newString, unichars );
502
503    return newString;
504}
505
506char *aescstrdup(const UChar* unichars,int32_t length){
507    char *newString,*targetLimit,*target;
508    UConverterFromUCallback cb;
509    const void *p;
510    UErrorCode errorCode = U_ZERO_ERROR;
511#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
512#   if U_PLATFORM == U_PF_OS390
513        static const char convName[] = "ibm-1047";
514#   else
515        static const char convName[] = "ibm-37";
516#   endif
517#else
518    static const char convName[] = "US-ASCII";
519#endif
520    UConverter* conv = ucnv_open(convName, &errorCode);
521    if(length==-1){
522        length = u_strlen( unichars);
523    }
524    newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
525    target = newString;
526    targetLimit = newString+sizeof(char) * 8 * (length +1);
527    ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
528    ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
529    ucnv_close(conv);
530    *target = '\0';
531    return newString;
532}
533
534const char* loadTestData(UErrorCode* err){
535    if( _testDataPath == NULL){
536        const char*      directory=NULL;
537        UResourceBundle* test =NULL;
538        char* tdpath=NULL;
539        const char* tdrelativepath;
540#if defined (U_TOPBUILDDIR)
541        tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
542        directory = U_TOPBUILDDIR;
543#else
544        tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
545        directory= ctest_dataOutDir();
546#endif
547
548        tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10));
549
550
551        /* u_getDataDirectory shoul return \source\data ... set the
552         * directory to ..\source\data\..\test\testdata\out\testdata
553         *
554         * Fallback: When Memory mapped file is built
555         * ..\source\data\out\..\..\test\testdata\out\testdata
556         */
557        strcpy(tdpath, directory);
558        strcat(tdpath, tdrelativepath);
559        strcat(tdpath,"testdata");
560
561
562        test=ures_open(tdpath, "testtypes", err);
563
564        /* Fall back did not succeed either so return */
565        if(U_FAILURE(*err)){
566            *err = U_FILE_ACCESS_ERROR;
567            log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
568            return "";
569        }
570        ures_close(test);
571        _testDataPath = tdpath;
572        return _testDataPath;
573    }
574    return _testDataPath;
575}
576
577#define CTEST_MAX_TIMEZONE_SIZE 256
578static UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
579
580/**
581 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
582 * @param optionalTimeZone Set this to a requested timezone.
583 *      Set to NULL to use the standard test timezone (Pacific Time)
584 */
585U_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
586#if !UCONFIG_NO_FORMATTING
587    UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
588
589    if (optionalTimeZone == NULL) {
590        optionalTimeZone = "America/Los_Angeles";
591    }
592    if (gOriginalTimeZone[0]) {
593        log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
594               optionalTimeZone);
595    }
596    ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
597    if (U_FAILURE(*status)) {
598        log_err("*** Error: Failed to save default time zone: %s\n",
599               u_errorName(*status));
600        *status = U_ZERO_ERROR;
601    }
602
603    u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
604    zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
605    ucal_setDefaultTimeZone(zoneID, status);
606    if (U_FAILURE(*status)) {
607        log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
608               optionalTimeZone, u_errorName(*status));
609    }
610#endif
611}
612
613/**
614 * Call this once get back the original timezone
615 */
616U_CFUNC void ctest_resetTimeZone(void) {
617#if !UCONFIG_NO_FORMATTING
618    UErrorCode status = U_ZERO_ERROR;
619
620    ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
621    if (U_FAILURE(status)) {
622        log_err("*** Error: Failed to reset default time zone: %s\n",
623               u_errorName(status));
624    }
625    /* Set to an empty state */
626    gOriginalTimeZone[0] = 0;
627#endif
628}
629
630
631void *ctst_malloc(size_t size) {
632  ctst_allocated_total++;
633    if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
634        ctst_allocated = 0;
635        ctst_free = TRUE;
636    }
637    if(ctst_allocated_stuff[ctst_allocated]) {
638        free(ctst_allocated_stuff[ctst_allocated]);
639    }
640    return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
641}
642
643#ifdef CTST_LEAK_CHECK
644static void ctst_freeAll() {
645    int i;
646    if(ctst_free == FALSE) { /* only free up to the allocated mark */
647        for(i=0; i<ctst_allocated; i++) {
648            free(ctst_allocated_stuff[i]);
649            ctst_allocated_stuff[i] = NULL;
650        }
651    } else { /* free all */
652        for(i=0; i<CTST_MAX_ALLOC; i++) {
653            free(ctst_allocated_stuff[i]);
654            ctst_allocated_stuff[i] = NULL;
655        }
656    }
657    ctst_allocated = 0;
658    _testDataPath=NULL;
659}
660
661#define VERBOSE_ASSERTIONS
662
663U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
664    U_ASSERT(ec!=NULL);
665    if (U_FAILURE(*ec)) {
666        if (possibleDataError) {
667            log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
668        } else {
669            log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
670        }
671        return FALSE;
672    }
673    return TRUE;
674}
675
676U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
677    U_ASSERT(ec!=NULL);
678    return assertSuccessCheck(msg, ec, FALSE);
679}
680
681/* if 'condition' is a UBool, the compiler complains bitterly about
682   expressions like 'a > 0' which it evaluates as int */
683U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
684    if (!condition) {
685        log_err("FAIL: assertTrue() failed: %s\n", msg);
686    }
687#ifdef VERBOSE_ASSERTIONS
688    else {
689        log_verbose("Ok: %s\n", msg);
690    }
691#endif
692    return (UBool)condition;
693}
694
695U_CFUNC UBool assertEquals(const char* message, const char* expected,
696                           const char* actual) {
697    if (uprv_strcmp(expected, actual) != 0) {
698        log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
699                message, actual, expected);
700        return FALSE;
701    }
702#ifdef VERBOSE_ASSERTIONS
703    else {
704        log_verbose("Ok: %s; got \"%s\"\n", message, actual);
705    }
706#endif
707    return TRUE;
708}
709
710#endif
711