16f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/********************************************************************
26f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * COPYRIGHT:
36f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Copyright (c) 1997-2013, International Business Machines Corporation and
46f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * others. All Rights Reserved.
56f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org ********************************************************************/
66f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/********************************************************************************
76f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
86f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org* File CINTLTST.C
96f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org* Modification History:
116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*        Name                     Description
126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*     Madhu Katragadda               Creation
136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*********************************************************************************
146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*The main root for C API tests*/
176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <stdlib.h>
196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <stdio.h>
206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <string.h>
216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/utypes.h"
226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/putil.h"
236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "cstring.h"
246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "cintltst.h"
256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "uassert.h"
266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "cmemory.h"
276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/uchar.h"
286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/ustring.h"
296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/ucnv.h"
306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/ures.h"
316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/uclean.h"
326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/ucal.h"
336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "uoptions.h"
346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "putilimp.h" /* for uprv_getRawUTCtime() */
356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef URES_DEBUG
366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "uresimp.h" /* for ures_dumpCacheContents() */
376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef XP_MAC_CONSOLE
406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <console.h>
416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CTST_MAX_ALLOC 8192
446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Array used as a queue */
456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic int ctst_allocated = 0;
476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UBool ctst_free = FALSE;
486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic int ctst_allocated_total = 0;
496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CTST_LEAK_CHECK 1
516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef CTST_LEAK_CHECK
536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic void ctst_freeAll(void);
546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char* _testDataPath=NULL;
576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *  Forward Declarations
606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgvoid ctest_setICU_DATA(void);
626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if UCONFIG_NO_LEGACY_CONVERSION
666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define TRY_CNV_1 "iso-8859-1"
676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define TRY_CNV_2 "ibm-1208"
686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define TRY_CNV_1 "iso-8859-7"
706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define TRY_CNV_2 "sjis"
716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic int gOrigArgc;
746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char* const * gOrigArgv;
756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgint main(int argc, const char* const argv[])
776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int nerrors = 0;
796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UBool   defaultDataFound;
806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    TestNode *root;
816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *warnOrErr = "Failure";
826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UDate startTime, endTime;
836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t diffTime;
846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* initial check for the default converter */
866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UErrorCode errorCode = U_ZERO_ERROR;
876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UResourceBundle *rb;
886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UConverter *cnv;
896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    U_MAIN_INIT_ARGS(argc, argv);
916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    startTime = uprv_getRawUTCtime();
936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gOrigArgc = argc;
956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gOrigArgv = argv;
966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (!initArgs(argc, argv, NULL, NULL)) {
976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Error already displayed. */
986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return -1;
996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
1006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Check whether ICU will initialize without forcing the build data directory into
1026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *  the ICU_DATA path.  Success here means either the data dll contains data, or that
1036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *  this test program was run with ICU_DATA set externally.  Failure of this check
1046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *  is normal when ICU data is not packaged into a shared library.
1056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *
1066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *  Whether or not this test succeeds, we want to cleanup and reinitialize
1076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *  with a data path so that data loading from individual files can be tested.
1086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
1096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    defaultDataFound = TRUE;
1106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_init(&errorCode);
1116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(errorCode)) {
1126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        fprintf(stderr,
1136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            "#### Note:  ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
1146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        defaultDataFound = FALSE;
1156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
1166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_cleanup();
1176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef URES_DEBUG
1186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
1196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    while (getTestOption(REPEAT_TESTS_OPTION) > 0) {   /* Loop runs once per complete execution of the tests
1226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                  *   used for -r  (repeat) test option.                */
1236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (!initArgs(argc, argv, NULL, NULL)) {
1246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Error already displayed. */
1256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return -1;
1266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
1276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        errorCode = U_ZERO_ERROR;
1286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Initialize ICU */
1306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (!defaultDataFound) {
1316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ctest_setICU_DATA();    /* u_setDataDirectory() must happen Before u_init() */
1326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
1336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_init(&errorCode);
1346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (U_FAILURE(errorCode)) {
1356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,
1366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
1376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                "*** Check the ICU_DATA environment variable and \n"
1386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                "*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
1396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
1406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    fprintf(stderr, "*** Exiting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
1416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    u_cleanup();
1426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    return 1;
1436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
1446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
1456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* try more data */
1496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        cnv = ucnv_open(TRY_CNV_2, &errorCode);
1506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(cnv != 0) {
1516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* ok */
1526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ucnv_close(cnv);
1536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
1546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,
1556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
1566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** Check the ICU_DATA environment variable and \n"
1576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** check that the data files are present.\n", warnOrErr);
1586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
1596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
1606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                u_cleanup();
1616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return 1;
1626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
1636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
1646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        rb = ures_open(NULL, "en", &errorCode);
1666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(U_SUCCESS(errorCode)) {
1676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* ok */
1686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ures_close(rb);
1696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
1706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,
1716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
1726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** Check the ICU_DATA environment variable and \n"
1736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** check that the data files are present.\n", warnOrErr);
1746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
1756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
1766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                u_cleanup();
1776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return 1;
1786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
1796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
1806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        errorCode = U_ZERO_ERROR;
1826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        rb = ures_open(NULL, NULL, &errorCode);
1836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(U_SUCCESS(errorCode)) {
1846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* ok */
1856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
1866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fprintf(stderr,
1876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        "#### Note: The default locale %s is not available\n", uloc_getDefault());
1886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
1896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ures_close(rb);
1906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
1916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,
1926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
1936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
1946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fprintf(stderr, "*** Exitting.  Use the '-w' option if data files were\n"
1956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "*** purposely removed, to continue test anyway.\n");
1966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                u_cleanup();
1976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return 1;
1986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
1996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
2016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Build a tree of all tests.
2036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         *   Subsequently will be used to find / iterate the tests to run */
2046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        root = NULL;
2056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        addAllTests(&root);
2066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*  Tests acutally run HERE.   TODO:  separate command line option parsing & setting from test execution!! */
2086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        nerrors = runTestRequest(root, argc, argv);
2096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
2116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
2126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
2136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        cleanUpTestTree(root);
2156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef CTST_LEAK_CHECK
2176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ctst_freeAll();
2186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* To check for leaks */
2196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
2206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
2226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          fprintf(stderr,"ctst_freeAll():  cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
2236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef URES_DEBUG
2256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(ures_dumpCacheContents()) {
2266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
2276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          nerrors++;
2286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
2296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
2306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }  /* End of loop that repeats the entire test, if requested.  (Normally doesn't loop)  */
2356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    endTime = uprv_getRawUTCtime();
2376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    diffTime = (int32_t)(endTime - startTime);
2386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
2396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
2406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
2416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
2426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (int)(diffTime%U_MILLIS_PER_SECOND));
2436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return nerrors ? 1 : 0;
2456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
2486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic void ctest_appendToDataDirectory(const char *toAppend)
2496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
2506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *oldPath ="";
2516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char newBuf [1024];
2526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *newPath = newBuf;
2536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t oldLen;
2546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t newLen;
2556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if((toAppend == NULL) || (*toAppend == 0)) {
2576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return;
2586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
2596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    oldPath = u_getDataDirectory();
2616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if( (oldPath==NULL) || (*oldPath == 0)) {
2626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_setDataDirectory(toAppend);
2636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    } else {
2646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        oldLen = strlen(oldPath);
2656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        newLen = strlen(toAppend)+1+oldLen;
2666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(newLen > 1022)
2686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
2696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            newPath = (char *)ctst_malloc(newLen);
2706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(newPath, oldPath);
2736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(newPath+oldLen, U_PATH_SEP_STRING);
2746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(newPath+oldLen+1, toAppend);
2756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_setDataDirectory(newPath);
2776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(newPath != newBuf)
2796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
2806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            free(newPath);
2816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
2836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
2856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* returns the path to icu/source/data */
2876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgconst char *  ctest_dataSrcDir()
2886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
2896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static const char *dataSrcDir = NULL;
2906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(dataSrcDir) {
2926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return dataSrcDir;
2936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
2946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
2966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              to point to the top of the build hierarchy, which may or
2976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              may not be the same as the source directory, depending on
2986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              the configure options used.  At any rate,
2996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              set the data path to the built data from this directory.
3006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              The value is complete with quotes, so it can be used
3016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              as-is as a string constant.
3026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
3036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined (U_TOPSRCDIR)
3046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
3056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        dataSrcDir = U_TOPSRCDIR  U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
3066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
3076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* On Windows, the file name obtained from __FILE__ includes a full path.
3106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *             This file is "wherever\icu\source\test\cintltst\cintltst.c"
3116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *             Change to    "wherever\icu\source\data"
3126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
3136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
3146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static char p[sizeof(__FILE__) + 20];
3156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        char *pBackSlash;
3166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int i;
3176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(p, __FILE__);
3196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* We want to back over three '\' chars.                            */
3206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*   Only Windows should end up here, so looking for '\' is safe.   */
3216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for (i=1; i<=3; i++) {
3226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
3236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (pBackSlash != NULL) {
3246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                *pBackSlash = 0;        /* Truncate the string at the '\'   */
3256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
3266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
3276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (pBackSlash != NULL) {
3296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* We found and truncated three names from the path.
3306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             *  Now append "source\data" and set the environment
3316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             */
3326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
3336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            dataSrcDir = p;
3346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
3356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else {
3366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* __FILE__ on MSVC7 does not contain the directory */
3376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
3386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (file) {
3396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fclose(file);
3406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
3416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
3426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            else {
3436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
3446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
3456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
3466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
3476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
3486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return dataSrcDir;
3506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
3526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* returns the path to icu/source/data/out */
3546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgconst char *ctest_dataOutDir()
3556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
3566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static const char *dataOutDir = NULL;
3576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(dataOutDir) {
3596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return dataOutDir;
3606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
3616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
3636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              to point to the top of the build hierarchy, which may or
3646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              may not be the same as the source directory, depending on
3656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              the configure options used.  At any rate,
3666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              set the data path to the built data from this directory.
3676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              The value is complete with quotes, so it can be used
3686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    //              as-is as a string constant.
3696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
3706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined (U_TOPBUILDDIR)
3716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
3726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        dataOutDir = U_TOPBUILDDIR "data"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
3736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
3746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* On Windows, the file name obtained from __FILE__ includes a full path.
3776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *             This file is "wherever\icu\source\test\cintltst\cintltst.c"
3786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *             Change to    "wherever\icu\source\data"
3796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
3806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
3816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static char p[sizeof(__FILE__) + 20];
3826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        char *pBackSlash;
3836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int i;
3846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(p, __FILE__);
3866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* We want to back over three '\' chars.                            */
3876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*   Only Windows should end up here, so looking for '\' is safe.   */
3886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for (i=1; i<=3; i++) {
3896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
3906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (pBackSlash != NULL) {
3916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                *pBackSlash = 0;        /* Truncate the string at the '\'   */
3926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
3936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
3946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (pBackSlash != NULL) {
3966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* We found and truncated three names from the path.
3976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             *  Now append "source\data" and set the environment
3986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             */
3996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
4006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            dataOutDir = p;
4016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
4026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else {
4036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* __FILE__ on MSVC7 does not contain the directory */
4046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
4056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (file) {
4066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                fclose(file);
4076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
4086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
4096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            else {
4106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                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;
4116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
4126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
4136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
4146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return dataOutDir;
4176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*  ctest_setICU_DATA  - if the ICU_DATA environment variable is not already
4206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       set, try to deduce the directory in which ICU was built,
4216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       and set ICU_DATA to "icu/source/data" in that location.
4226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       The intent is to allow the tests to have a good chance
4236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       of running without requiring that the user manually set
4246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       ICU_DATA.  Common data isn't a problem, since it is
4256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       picked up via a static (build time) reference, but the
4266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *                       tests dynamically load some data.
4276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
4286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgvoid ctest_setICU_DATA() {
4296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* No location for the data dir was identifiable.
4316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *   Add other fallbacks for the test data location here if the need arises
4326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
4336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (getenv("ICU_DATA") == NULL) {
4346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* If ICU_DATA isn't set, set it to the usual location */
4356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_setDataDirectory(ctest_dataOutDir());
4366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
4376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*  These tests do cleanup and reinitialize ICU in the course of their operation.
4406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *    The ICU data directory must be preserved across these operations.
4416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *    Here is a helper function to assist with that.
4426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
4436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char *safeGetICUDataDirectory() {
4446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *dataDir = u_getDataDirectory();  /* Returned string vanashes with u_cleanup */
4456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *retStr = NULL;
4466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (dataDir != NULL) {
4476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        retStr = (char *)malloc(strlen(dataDir)+1);
4486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(retStr, dataDir);
4496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
4506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return retStr;
4516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgUBool ctest_resetICU() {
4546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UErrorCode   status = U_ZERO_ERROR;
4556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char         *dataDir = safeGetICUDataDirectory();
4566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_cleanup();
4586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
4596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Error already displayed. */
4606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return FALSE;
4616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
4626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_setDataDirectory(dataDir);
4636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    free(dataDir);
4646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_init(&status);
4656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(status)) {
4666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err_status(status, "u_init failed with %s\n", u_errorName(status));
4676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return FALSE;
4686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
4696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
4706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgUChar* CharsToUChars(const char* str) {
4736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Might be faster to just use uprv_strlen() as the preflight len - liu */
4746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t len = u_unescape(str, 0, 0); /* preflight */
4756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Do NOT use malloc() - we are supposed to be acting like user code! */
4766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1));
4776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_unescape(str, buf, len + 1);
4786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return buf;
4796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgchar *austrdup(const UChar* unichars)
4826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int   length;
4846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *newString;
4856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    length    = u_strlen ( unichars );
4876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*newString = (char*)malloc  ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
4886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    newString = (char*)ctst_malloc  ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */
4896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ( newString == NULL )
4916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return NULL;
4926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_austrcpy ( newString, unichars );
4946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return newString;
4966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgchar *aescstrdup(const UChar* unichars,int32_t length){
4996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *newString,*targetLimit,*target;
5006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UConverterFromUCallback cb;
5016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const void *p;
5026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UErrorCode errorCode = U_ZERO_ERROR;
5036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
5046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   if U_PLATFORM == U_PF_OS390
5056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static const char convName[] = "ibm-1047";
5066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   else
5076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static const char convName[] = "ibm-37";
5086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
5096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
5106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static const char convName[] = "US-ASCII";
5116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
5126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UConverter* conv = ucnv_open(convName, &errorCode);
5136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(length==-1){
5146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        length = u_strlen( unichars);
5156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
5166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
5176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    target = newString;
5186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    targetLimit = newString+sizeof(char) * 8 * (length +1);
5196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
5206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
5216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucnv_close(conv);
5226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *target = '\0';
5236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return newString;
5246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgconst char* loadTestData(UErrorCode* err){
5276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if( _testDataPath == NULL){
5286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char*      directory=NULL;
5296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        UResourceBundle* test =NULL;
5306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        char* tdpath=NULL;
5316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char* tdrelativepath;
5326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined (U_TOPBUILDDIR)
5336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
5346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        directory = U_TOPBUILDDIR;
5356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
5366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
5376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        directory= ctest_dataOutDir();
5386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
5396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10));
5416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* u_getDataDirectory shoul return \source\data ... set the
5446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * directory to ..\source\data\..\test\testdata\out\testdata
5456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         *
5466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * Fallback: When Memory mapped file is built
5476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * ..\source\data\out\..\..\test\testdata\out\testdata
5486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
5496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcpy(tdpath, directory);
5506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcat(tdpath, tdrelativepath);
5516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        strcat(tdpath,"testdata");
5526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        test=ures_open(tdpath, "testtypes", err);
5556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Fall back did not succeed either so return */
5576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(U_FAILURE(*err)){
5586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *err = U_FILE_ACCESS_ERROR;
5596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
5606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return "";
5616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
5626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ures_close(test);
5636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        _testDataPath = tdpath;
5646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return _testDataPath;
5656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
5666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return _testDataPath;
5676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CTEST_MAX_TIMEZONE_SIZE 256
5706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UChar gOriginalTimeZone[CTEST_MAX_TIMEZONE_SIZE] = {0};
5716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
5736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
5746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * @param optionalTimeZone Set this to a requested timezone.
5756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *      Set to NULL to use the standard test timezone (Pacific Time)
5766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
5776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC void ctest_setTimeZone(const char *optionalTimeZone, UErrorCode *status) {
5786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !UCONFIG_NO_FORMATTING
5796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UChar zoneID[CTEST_MAX_TIMEZONE_SIZE];
5806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (optionalTimeZone == NULL) {
5826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        optionalTimeZone = "America/Los_Angeles";
5836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
5846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gOriginalTimeZone[0]) {
5856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
5866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org               optionalTimeZone);
5876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
5886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
5896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(*status)) {
5906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err("*** Error: Failed to save default time zone: %s\n",
5916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org               u_errorName(*status));
5926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *status = U_ZERO_ERROR;
5936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
5946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_uastrncpy(zoneID, optionalTimeZone, CTEST_MAX_TIMEZONE_SIZE-1);
5966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    zoneID[CTEST_MAX_TIMEZONE_SIZE-1] = 0;
5976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucal_setDefaultTimeZone(zoneID, status);
5986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(*status)) {
5996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
6006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org               optionalTimeZone, u_errorName(*status));
6016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
6066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Call this once get back the original timezone
6076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
6086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC void ctest_resetTimeZone(void) {
6096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !UCONFIG_NO_FORMATTING
6106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UErrorCode status = U_ZERO_ERROR;
6116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucal_setDefaultTimeZone(gOriginalTimeZone, &status);
6136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(status)) {
6146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err("*** Error: Failed to reset default time zone: %s\n",
6156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org               u_errorName(status));
6166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Set to an empty state */
6186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gOriginalTimeZone[0] = 0;
6196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgvoid *ctst_malloc(size_t size) {
6246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ctst_allocated_total++;
6256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
6266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ctst_allocated = 0;
6276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ctst_free = TRUE;
6286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(ctst_allocated_stuff[ctst_allocated]) {
6306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        free(ctst_allocated_stuff[ctst_allocated]);
6316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return ctst_allocated_stuff[ctst_allocated++] = malloc(size);
6336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef CTST_LEAK_CHECK
6366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic void ctst_freeAll() {
6376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int i;
6386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(ctst_free == FALSE) { /* only free up to the allocated mark */
6396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for(i=0; i<ctst_allocated; i++) {
6406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            free(ctst_allocated_stuff[i]);
6416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ctst_allocated_stuff[i] = NULL;
6426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
6436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    } else { /* free all */
6446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for(i=0; i<CTST_MAX_ALLOC; i++) {
6456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            free(ctst_allocated_stuff[i]);
6466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ctst_allocated_stuff[i] = NULL;
6476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
6486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ctst_allocated = 0;
6506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    _testDataPath=NULL;
6516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define VERBOSE_ASSERTIONS
6546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
6566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    U_ASSERT(ec!=NULL);
6576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (U_FAILURE(*ec)) {
6586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (possibleDataError) {
6596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
6606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
6616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
6626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
6636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return FALSE;
6646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
6666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
6696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    U_ASSERT(ec!=NULL);
6706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return assertSuccessCheck(msg, ec, FALSE);
6716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* if 'condition' is a UBool, the compiler complains bitterly about
6746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   expressions like 'a > 0' which it evaluates as int */
6756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
6766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (!condition) {
6776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err("FAIL: assertTrue() failed: %s\n", msg);
6786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef VERBOSE_ASSERTIONS
6806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else {
6816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_verbose("Ok: %s\n", msg);
6826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)condition;
6856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CFUNC UBool assertEquals(const char* message, const char* expected,
6886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                           const char* actual) {
6896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (uprv_strcmp(expected, actual) != 0) {
6906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
6916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                message, actual, expected);
6926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return FALSE;
6936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef VERBOSE_ASSERTIONS
6956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else {
6966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        log_verbose("Ok: %s; got \"%s\"\n", message, actual);
6976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
7006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
7016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
703