1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2011, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6/*******************************************************************************
7*
8* File CRESTST.C
9*
10* Modification History:
11*        Name              Date               Description
12*   Madhu Katragadda    05/09/2000   Ported Tests for New ResourceBundle API
13*   Madhu Katragadda    05/24/2000   Added new tests to test RES_BINARY for collationElements
14********************************************************************************
15*/
16
17
18#include <time.h>
19#include "unicode/utypes.h"
20#include "cintltst.h"
21#include "unicode/putil.h"
22#include "unicode/ustring.h"
23#include "unicode/ucnv.h"
24#include "string.h"
25#include "cstring.h"
26#include "unicode/uchar.h"
27#include "ucol_imp.h"  /* for U_ICUDATA_COLL */
28#include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */
29#define RESTEST_HEAP_CHECK 0
30
31#include "unicode/uloc.h"
32#include "unicode/ulocdata.h"
33#include "uresimp.h"
34#include "creststn.h"
35#include "unicode/ctest.h"
36#include "ucbuf.h"
37#include "ureslocs.h"
38
39static int32_t pass;
40static int32_t fail;
41
42/*****************************************************************************/
43/**
44 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
45 */
46
47static uint32_t
48randul()
49{
50    uint32_t l=0;
51    int32_t i;
52    static UBool initialized = FALSE;
53    if (!initialized)
54    {
55        srand((unsigned)time(NULL));
56        initialized = TRUE;
57    }
58    /* Assume rand has at least 12 bits of precision */
59
60    for (i=0; i<sizeof(l); ++i)
61        ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
62    return l;
63}
64
65/**
66 * Return a random double x where 0.0 <= x < 1.0.
67 */
68static double
69randd()
70{
71    return ((double)randul()) / UINT32_MAX;
72}
73
74/**
75 * Return a random integer i where 0 <= i < n.
76 */
77static int32_t randi(int32_t n)
78{
79    return (int32_t)(randd() * n);
80}
81/***************************************************************************************/
82/**
83 * Convert an integer, positive or negative, to a character string radix 10.
84 */
85static char*
86itoa1(int32_t i, char* buf)
87{
88  char *p = 0;
89  char* result = buf;
90  /* Handle negative */
91  if(i < 0) {
92    *buf++ = '-';
93    i = -i;
94  }
95
96  /* Output digits in reverse order */
97  p = buf;
98  do {
99    *p++ = (char)('0' + (i % 10));
100    i /= 10;
101  }
102  while(i);
103  *p-- = 0;
104
105  /* Reverse the string */
106  while(buf < p) {
107    char c = *buf;
108    *buf++ = *p;
109    *p-- = c;
110  }
111
112  return result;
113}
114static const int32_t kERROR_COUNT = -1234567;
115static const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
116             0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
117
118/*****************************************************************************/
119
120enum E_Where
121{
122  e_Root,
123  e_te,
124  e_te_IN,
125  e_Where_count
126};
127typedef enum E_Where E_Where;
128/*****************************************************************************/
129
130#define CONFIRM_EQ(actual,expected) if (u_strcmp(expected,actual)==0){ record_pass(); } else { record_fail(); log_err("%s  returned  %s  instead of %s\n", action, austrdup(actual), austrdup(expected)); }
131#define CONFIRM_INT_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of %d\n",  action, actual, expected); }
132#define CONFIRM_INT_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x >= %d\n",  action, actual, expected); }
133#define CONFIRM_INT_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x != %d\n",  action, actual, expected); }
134/*#define CONFIRM_ErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail();  log_err("%s returned  %s  instead of %s\n", action, myErrorName(actual), myErrorName(expected)); } */
135static void
136CONFIRM_ErrorCode(UErrorCode actual,UErrorCode expected)
137{
138  if ((expected)==(actual))
139  {
140    record_pass();
141  } else {
142    record_fail();
143    /*log_err("%s returned  %s  instead of %s\n", action, myErrorName(actual), myErrorName(expected)); */
144    log_err("returned  %s  instead of %s\n", myErrorName(actual), myErrorName(expected));
145  }
146}
147
148
149/* Array of our test objects */
150
151static struct
152{
153  const char* name;
154  UErrorCode expected_constructor_status;
155  E_Where where;
156  UBool like[e_Where_count];
157  UBool inherits[e_Where_count];
158}
159param[] =
160{
161  /* "te" means test */
162  /* "IN" means inherits */
163  /* "NE" or "ne" means "does not exist" */
164
165  { "root",         U_ZERO_ERROR,             e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
166  { "te",           U_ZERO_ERROR,             e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE  } },
167  { "te_IN",        U_ZERO_ERROR,             e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE   } },
168  { "te_NE",        U_USING_FALLBACK_WARNING, e_te,      { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE  } },
169  { "te_IN_NE",     U_USING_FALLBACK_WARNING, e_te_IN,   { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE   } },
170  { "ne",           U_USING_DEFAULT_WARNING,  e_Root,    { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
171};
172
173static int32_t bundles_count = sizeof(param) / sizeof(param[0]);
174
175
176
177/*static void printUChars(UChar*);*/
178static void TestDecodedBundle(void);
179static void TestGetKeywordValues(void);
180static void TestGetFunctionalEquivalent(void);
181static void TestCLDRStyleAliases(void);
182static void TestFallbackCodes(void);
183static void TestGetUTF8String(void);
184static void TestCLDRVersion(void);
185
186/***************************************************************************************/
187
188/* Array of our test objects */
189
190void addNEWResourceBundleTest(TestNode** root)
191{
192    addTest(root, &TestErrorCodes,            "tsutil/creststn/TestErrorCodes");
193#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
194    addTest(root, &TestEmptyBundle,           "tsutil/creststn/TestEmptyBundle");
195    addTest(root, &TestConstruction1,         "tsutil/creststn/TestConstruction1");
196    addTest(root, &TestResourceBundles,       "tsutil/creststn/TestResourceBundles");
197    addTest(root, &TestNewTypes,              "tsutil/creststn/TestNewTypes");
198    addTest(root, &TestEmptyTypes,            "tsutil/creststn/TestEmptyTypes");
199    addTest(root, &TestBinaryCollationData,   "tsutil/creststn/TestBinaryCollationData");
200    addTest(root, &TestAPI,                   "tsutil/creststn/TestAPI");
201    addTest(root, &TestErrorConditions,       "tsutil/creststn/TestErrorConditions");
202    addTest(root, &TestDecodedBundle,         "tsutil/creststn/TestDecodedBundle");
203    addTest(root, &TestResourceLevelAliasing, "tsutil/creststn/TestResourceLevelAliasing");
204    addTest(root, &TestDirectAccess,          "tsutil/creststn/TestDirectAccess");
205    addTest(root, &TestXPath,                 "tsutil/creststn/TestXPath");
206    addTest(root, &TestCLDRStyleAliases,      "tsutil/creststn/TestCLDRStyleAliases");
207    addTest(root, &TestFallbackCodes,         "tsutil/creststn/TestFallbackCodes");
208    addTest(root, &TestGetUTF8String,         "tsutil/creststn/TestGetUTF8String");
209    addTest(root, &TestCLDRVersion,           "tsutil/creststn/TestCLDRVersion");
210#endif
211    addTest(root, &TestFallback,              "tsutil/creststn/TestFallback");
212    addTest(root, &TestGetVersion,            "tsutil/creststn/TestGetVersion");
213    addTest(root, &TestGetVersionColl,        "tsutil/creststn/TestGetVersionColl");
214    addTest(root, &TestAliasConflict,         "tsutil/creststn/TestAliasConflict");
215    addTest(root, &TestGetKeywordValues,      "tsutil/creststn/TestGetKeywordValues");
216    addTest(root, &TestGetFunctionalEquivalent,"tsutil/creststn/TestGetFunctionalEquivalent");
217    addTest(root, &TestJB3763,                "tsutil/creststn/TestJB3763");
218    addTest(root, &TestStackReuse,            "tsutil/creststn/TestStackReuse");
219}
220
221
222/***************************************************************************************/
223static const char* norwayNames[] = {
224    "no_NO_NY",
225    "no_NO",
226    "no",
227    "nn_NO",
228    "nn",
229    "nb_NO",
230    "nb"
231};
232
233static const char* norwayLocales[] = {
234    "nn_NO",
235    "nb_NO",
236    "nb",
237    "nn_NO",
238    "nn",
239    "nb_NO",
240    "nb"
241};
242
243static void checkStatus(int32_t line, UErrorCode expected, UErrorCode status) {
244  if(U_FAILURE(status)) {
245    log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__, line);
246  }
247  if(status != expected) {
248    log_err_status(status, "%s:%d: Expected error code %s, got error code %s\n", __FILE__, line, u_errorName(expected), u_errorName(status));
249  }
250}
251
252static void TestErrorCodes(void) {
253  static const UVersionInfo icu49 = { 4, 9, 0, 0 };
254  UErrorCode status = U_USING_DEFAULT_WARNING;
255
256  UResourceBundle *r = NULL, *r2 = NULL;
257
258  /* First check with ICUDATA */
259  /* first bundle should return fallback warning */
260  r = ures_open(NULL, "ti_ER_ASSAB", &status);
261  checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
262  ures_close(r);
263
264  /* this bundle should return zero error, so it shouldn't change the status */
265  status = U_USING_DEFAULT_WARNING;
266  r = ures_open(NULL, "ti_ER", &status);
267  checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
268
269  /* we look up the resource which is aliased, but it lives in fallback */
270
271  if(U_SUCCESS(status) && r != NULL) {
272    status = U_USING_DEFAULT_WARNING;
273    r2 = ures_getByKey(r, "LocaleScript", NULL, &status);  /* LocaleScript lives in ti */
274    checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
275  }
276  ures_close(r);
277
278  /* this bundle should return zero error, so it shouldn't change the status */
279  status = U_USING_DEFAULT_WARNING;
280  r = ures_open(U_ICUDATA_REGION, "ti", &status);
281  checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
282
283  /* we look up the resource which is aliased and at our level */
284  /* TODO: restore the following test when cldrbug 3058: is fixed */
285  if(U_SUCCESS(status) && r != NULL && isICUVersionAtLeast(icu49)) {
286    status = U_USING_DEFAULT_WARNING;
287    r2 = ures_getByKey(r, "Countries", r2, &status);
288    checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
289  }
290  ures_close(r);
291
292  status = U_USING_FALLBACK_WARNING;
293  r = ures_open(NULL, "nolocale", &status);
294  checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
295  ures_close(r);
296  ures_close(r2);
297
298  /** Now, with the collation bundle **/
299
300  /* first bundle should return fallback warning */
301  r = ures_open(U_ICUDATA_COLL, "sr_YU_VOJVODINA", &status);
302  checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
303  ures_close(r);
304
305  /* this bundle should return zero error, so it shouldn't change the status */
306  status = U_USING_FALLBACK_WARNING;
307  r = ures_open(U_ICUDATA_COLL, "sr", &status);
308  checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
309
310  /* we look up the resource which is aliased  */
311  if(U_SUCCESS(status) && r != NULL) {
312    status = U_USING_DEFAULT_WARNING;
313    r2 = ures_getByKey(r, "collations", NULL, &status);
314    checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
315  }
316  ures_close(r);
317
318  /* this bundle should return zero error, so it shouldn't change the status */
319  status = U_USING_DEFAULT_WARNING;
320  r = ures_open(U_ICUDATA_COLL, "sr", &status);
321  checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
322
323  /* we look up the resource which is aliased and at our level */
324  if(U_SUCCESS(status) && r != NULL) {
325    status = U_USING_DEFAULT_WARNING;
326    r2 = ures_getByKey(r, "collations", r2, &status);
327    checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
328  }
329  ures_close(r);
330
331  status = U_USING_FALLBACK_WARNING;
332  r = ures_open(U_ICUDATA_COLL, "nolocale", &status);
333  checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
334  ures_close(r);
335  ures_close(r2);
336}
337
338static void TestAliasConflict(void) {
339    UErrorCode status = U_ZERO_ERROR;
340    UResourceBundle *he = NULL;
341    UResourceBundle *iw = NULL;
342    UResourceBundle *norway = NULL;
343    const UChar *result = NULL;
344    int32_t resultLen;
345    uint32_t size = 0;
346    uint32_t i = 0;
347    const char *realName = NULL;
348
349    he = ures_open(NULL, "he", &status);
350    iw = ures_open(NULL, "iw", &status);
351    if(U_FAILURE(status)) {
352        log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
353    }
354    ures_close(iw);
355    result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
356    if(U_FAILURE(status) || result == NULL) {
357        log_err_status(status, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status));
358    }
359    ures_close(he);
360
361    size = sizeof(norwayNames)/sizeof(norwayNames[0]);
362    for(i = 0; i < size; i++) {
363        status = U_ZERO_ERROR;
364        norway = ures_open(NULL, norwayNames[i], &status);
365        if(U_FAILURE(status)) {
366            log_err_status(status, "Failed to get resource with %s for %s\n", myErrorName(status), norwayNames[i]);
367            continue;
368        }
369        realName = ures_getLocale(norway, &status);
370        log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames[i], realName);
371        if(realName == NULL || strcmp(norwayLocales[i], realName) != 0) {
372            log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames[i], norwayLocales[i], realName);
373        }
374        ures_close(norway);
375    }
376}
377
378static void TestDecodedBundle(){
379
380    UErrorCode error = U_ZERO_ERROR;
381
382    UResourceBundle* resB;
383
384    const UChar* srcFromRes;
385    int32_t len;
386    static const UChar uSrc[] = {
387        0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F,
388        0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928,
389        0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A,
390        0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938,
391        0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020,
392        0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938,
393        0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947,
394        0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020,
395        0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915,
396        0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E,
397        0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917,
398        0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D,
399        0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020,
400        0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915,
401        0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B,
402        0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E,
403        0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902,
404        0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932,
405        0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F,
406        0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938,
407        0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930,
408        0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020,
409        0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020,
410        0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F,
411        0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C,
412        0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941,
413        0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917,
414        0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E,
415        0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930,
416        0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E,
417        0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,
418        0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,
419        0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935,
420        0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915,
421        0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F,
422        0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033,
423        0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F,
424        0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915,
425        0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020,
426        0
427    };
428
429    /* pre-flight */
430    int32_t num =0;
431    const char *testdatapath = loadTestData(&error);
432    resB = ures_open(testdatapath, "iscii", &error);
433    srcFromRes=tres_getString(resB,-1,"str",&len,&error);
434    if(U_FAILURE(error)){
435#if UCONFIG_NO_LEGACY_CONVERSION
436        log_info("Couldn't load iscii.bin from test data bundle, (because UCONFIG_NO_LEGACY_CONVERSION  is turned on)\n");
437#else
438        log_data_err("Could not find iscii.bin from test data bundle. Error: %s\n", u_errorName(error));
439#endif
440        ures_close(resB);
441        return;
442    }
443    if(u_strncmp(srcFromRes,uSrc,len)!=0){
444        log_err("Genrb produced res files after decoding failed\n");
445    }
446    while(num<len){
447        if(uSrc[num]!=srcFromRes[num]){
448            log_verbose(" Expected:  0x%04X Got: 0x%04X \n", uSrc[num],srcFromRes[num]);
449        }
450        num++;
451    }
452    if (len != u_strlen(uSrc)) {
453        log_err("Genrb produced a string larger than expected\n");
454    }
455    ures_close(resB);
456}
457
458static void TestNewTypes() {
459    UResourceBundle* theBundle = NULL;
460    char action[256];
461    const char* testdatapath;
462    UErrorCode status = U_ZERO_ERROR;
463    UResourceBundle* res = NULL;
464    uint8_t *binResult = NULL;
465    int32_t len = 0;
466    int32_t i = 0;
467    int32_t intResult = 0;
468    uint32_t uintResult = 0;
469    const UChar *empty = NULL;
470    const UChar *zeroString;
471    UChar expected[] = { 'a','b','c','\0','d','e','f' };
472    const char* expect ="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
473    UChar uExpect[200];
474
475    testdatapath=loadTestData(&status);
476
477    if(U_FAILURE(status))
478    {
479        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
480        return;
481    }
482
483    theBundle = ures_open(testdatapath, "testtypes", &status);
484
485    empty = tres_getString(theBundle, -1, "emptystring", &len, &status);
486    if(empty && (*empty != 0 || len != 0)) {
487      log_err("Empty string returned invalid value\n");
488    }
489
490    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
491
492    CONFIRM_INT_NE(theBundle, NULL);
493
494    /* This test reads the string "abc\u0000def" from the bundle   */
495    /* if everything is working correctly, the size of this string */
496    /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
497
498    strcpy(action, "getting and testing of string with embeded zero");
499    res = ures_getByKey(theBundle, "zerotest", res, &status);
500    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
501    CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
502    zeroString=tres_getString(res, -1, NULL, &len, &status);
503    if(U_SUCCESS(status)){
504        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
505        CONFIRM_INT_EQ(len, 7);
506        CONFIRM_INT_NE(len, 3);
507    }
508    for(i=0;i<len;i++){
509        if(zeroString[i]!= expected[i]){
510            log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected[i], zeroString[i]);
511        }
512    }
513
514    strcpy(action, "getting and testing of binary type");
515    res = ures_getByKey(theBundle, "binarytest", res, &status);
516    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
517    CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
518    binResult=(uint8_t*)ures_getBinary(res,  &len, &status);
519    if(U_SUCCESS(status)){
520        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
521        CONFIRM_INT_EQ(len, 15);
522        for(i = 0; i<15; i++) {
523            CONFIRM_INT_EQ(binResult[i], i);
524        }
525    }
526
527    strcpy(action, "getting and testing of imported binary type");
528    res = ures_getByKey(theBundle, "importtest", res, &status);
529    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
530    CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
531    binResult=(uint8_t*)ures_getBinary(res,  &len, &status);
532    if(U_SUCCESS(status)){
533        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
534        CONFIRM_INT_EQ(len, 15);
535        for(i = 0; i<15; i++) {
536            CONFIRM_INT_EQ(binResult[i], i);
537        }
538    }
539
540    strcpy(action, "getting and testing of integer types");
541    res = ures_getByKey(theBundle, "one", res, &status);
542    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
543    CONFIRM_INT_EQ(ures_getType(res), URES_INT);
544    intResult=ures_getInt(res, &status);
545    uintResult = ures_getUInt(res, &status);
546    if(U_SUCCESS(status)){
547        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
548        CONFIRM_INT_EQ(uintResult, (uint32_t)intResult);
549        CONFIRM_INT_EQ(intResult, 1);
550    }
551
552    strcpy(action, "getting minusone");
553    res = ures_getByKey(theBundle, "minusone", res, &status);
554    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
555    CONFIRM_INT_EQ(ures_getType(res), URES_INT);
556    intResult=ures_getInt(res, &status);
557    uintResult = ures_getUInt(res, &status);
558    if(U_SUCCESS(status)){
559        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
560        CONFIRM_INT_EQ(uintResult, 0x0FFFFFFF); /* a 28 bit integer */
561        CONFIRM_INT_EQ(intResult, -1);
562        CONFIRM_INT_NE(uintResult, (uint32_t)intResult);
563    }
564
565    strcpy(action, "getting plusone");
566    res = ures_getByKey(theBundle, "plusone", res, &status);
567    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
568    CONFIRM_INT_EQ(ures_getType(res), URES_INT);
569    intResult=ures_getInt(res, &status);
570    uintResult = ures_getUInt(res, &status);
571    if(U_SUCCESS(status)){
572        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
573        CONFIRM_INT_EQ(uintResult, (uint32_t)intResult);
574        CONFIRM_INT_EQ(intResult, 1);
575    }
576
577    res = ures_getByKey(theBundle, "onehundredtwentythree", res, &status);
578    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
579    CONFIRM_INT_EQ(ures_getType(res), URES_INT);
580    intResult=ures_getInt(res, &status);
581    if(U_SUCCESS(status)){
582        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
583        CONFIRM_INT_EQ(intResult, 123);
584    }
585
586    /* this tests if escapes are preserved or not */
587    {
588        const UChar* str = tres_getString(theBundle,-1,"testescape",&len,&status);
589        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
590        if(U_SUCCESS(status)){
591            u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1);
592            if(u_strcmp(uExpect,str)){
593                log_err("Did not get the expected string for testescape\n");
594            }
595        }
596    }
597    /* this tests if unescaping works are expected */
598    len=0;
599    {
600        char pattern[2048] = "";
601        int32_t patternLen;
602        UChar* expectedEscaped;
603        const UChar* got;
604        int32_t expectedLen;
605
606        /* This strcpy fixes compiler warnings about long strings */
607        strcpy(pattern, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 "
608            "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F "
609            "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 "
610            "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD "
611            "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF "
612            );
613        strcat(pattern,
614            "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF "
615            "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF "
616            "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF "
617            "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB "
618            "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C "
619            );
620        strcat(pattern,
621            "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F "
622            "]"
623            );
624
625        patternLen = (int32_t)uprv_strlen(pattern);
626        expectedEscaped = (UChar*)malloc(U_SIZEOF_UCHAR * patternLen);
627        got = tres_getString(theBundle,-1,"test_unescaping",&len,&status);
628        expectedLen = u_unescape(pattern,expectedEscaped,patternLen);
629        if(got==NULL || u_strncmp(expectedEscaped,got,expectedLen)!=0 || expectedLen != len){
630            log_err("genrb failed to unescape string\n");
631        }
632        if(got != NULL){
633            for(i=0;i<expectedLen;i++){
634                if(expectedEscaped[i] != got[i]){
635                    log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped[i], got[i]);
636                }
637            }
638        }
639        free(expectedEscaped);
640        status = U_ZERO_ERROR;
641    }
642    /* test for jitterbug#1435 */
643    {
644        const UChar* str = tres_getString(theBundle,-1,"test_underscores",&len,&status);
645        expect ="test message ....";
646        u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1);
647        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
648        if(str == NULL || u_strcmp(uExpect,str)){
649            log_err("Did not get the expected string for test_underscores.\n");
650        }
651    }
652    /* test for jitterbug#2626 */
653#if !UCONFIG_NO_COLLATION
654    {
655        UResourceBundle* resB = NULL;
656        const UChar* str  = NULL;
657        int32_t strLength = 0;
658        const UChar my[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */
659        status = U_ZERO_ERROR;
660        resB = ures_getByKey(theBundle, "collations", resB, &status);
661        resB = ures_getByKey(resB, "standard", resB, &status);
662        str  = tres_getString(resB,-1,"Sequence",&strLength,&status);
663        if(!str || U_FAILURE(status)) {
664            log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status));
665        } else if(u_strcmp(my,str) != 0){
666            log_err("Did not get the expected string for escaped \\u0075\n");
667        }
668        ures_close(resB);
669    }
670#endif
671    {
672        const char *sourcePath = ctest_dataSrcDir();
673        int32_t srcPathLen = (int32_t)strlen(sourcePath);
674        const char *deltaPath = ".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
675        int32_t deltaPathLen = (int32_t)strlen(deltaPath);
676        char *testDataFileName = (char *) malloc( srcPathLen+ deltaPathLen + 50 );
677        char *path = testDataFileName;
678
679        strcpy(path, sourcePath);
680        path += srcPathLen;
681        strcpy(path, deltaPath);
682        path += deltaPathLen;
683        status = U_ZERO_ERROR;
684        {
685            int32_t strLen =0;
686            const UChar* str = tres_getString(theBundle, -1, "testincludeUTF",&strLen,&status);
687            strcpy(path, "riwords.txt");
688            path[strlen("riwords.txt")]=0;
689            if(U_FAILURE(status)){
690                log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status));
691            }else{
692                /* open the file */
693                const char* cp = NULL;
694                UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status);
695                len = 0;
696                if(U_SUCCESS(status)){
697                    const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status);
698                    if(U_SUCCESS(status)){
699                        /* verify the contents */
700                        if(strLen != len ){
701                            log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len ,strLen);
702                        }
703                        /* test string termination */
704                        if(u_strlen(str) != strLen || str[strLen]!= 0 ){
705                            log_err("testinclude not null terminated!\n");
706                        }
707                        if(u_strncmp(str, buffer,strLen)!=0){
708                            log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n");
709                        }
710                    }else{
711                        log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status));
712                    }
713
714                    ucbuf_close(ucbuf);
715                }else{
716                    log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status));
717                }
718            }
719        }
720        status = U_ZERO_ERROR;
721        {
722            int32_t strLen =0;
723            const UChar* str = tres_getString(theBundle, -1, "testinclude",&strLen,&status);
724            strcpy(path, "translit_rules.txt");
725            path[strlen("translit_rules.txt")]=0;
726
727            if(U_FAILURE(status)){
728                log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status));
729            }else{
730                /* open the file */
731                const char* cp=NULL;
732                UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status);
733                len = 0;
734                if(U_SUCCESS(status)){
735                    const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status);
736                    if(U_SUCCESS(status)){
737                        /* verify the contents */
738                        if(strLen != len ){
739                            log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len ,strLen);
740                        }
741                        if(u_strncmp(str, buffer,strLen)!=0){
742                            log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n");
743                        }
744                    }else{
745                        log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status));
746                    }
747                    ucbuf_close(ucbuf);
748                }else{
749                    log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status));
750                }
751            }
752        }
753        free(testDataFileName);
754    }
755    ures_close(res);
756    ures_close(theBundle);
757
758}
759
760static void TestEmptyTypes() {
761    UResourceBundle* theBundle = NULL;
762    char action[256];
763    const char* testdatapath;
764    UErrorCode status = U_ZERO_ERROR;
765    UResourceBundle* res = NULL;
766    UResourceBundle* resArray = NULL;
767    const uint8_t *binResult = NULL;
768    int32_t len = 0;
769    int32_t intResult = 0;
770    const UChar *zeroString;
771    const int32_t *zeroIntVect;
772
773    strcpy(action, "Construction of testtypes bundle");
774    testdatapath=loadTestData(&status);
775    if(U_FAILURE(status))
776    {
777        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
778        return;
779    }
780
781    theBundle = ures_open(testdatapath, "testtypes", &status);
782
783    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
784
785    CONFIRM_INT_NE(theBundle, NULL);
786
787    /* This test reads the string "abc\u0000def" from the bundle   */
788    /* if everything is working correctly, the size of this string */
789    /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
790
791    status = U_ZERO_ERROR;
792    strcpy(action, "getting and testing of explicit string of zero length string");
793    res = ures_getByKey(theBundle, "emptyexplicitstring", res, &status);
794    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
795    CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
796    zeroString=tres_getString(res, -1, NULL, &len, &status);
797    if(U_SUCCESS(status)){
798        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
799        CONFIRM_INT_EQ(len, 0);
800        CONFIRM_INT_EQ(u_strlen(zeroString), 0);
801    }
802    else {
803        log_err("Couldn't get emptyexplicitstring\n");
804    }
805
806    status = U_ZERO_ERROR;
807    strcpy(action, "getting and testing of normal string of zero length string");
808    res = ures_getByKey(theBundle, "emptystring", res, &status);
809    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
810    CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
811    zeroString=tres_getString(res, -1, NULL, &len, &status);
812    if(U_SUCCESS(status)){
813        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
814        CONFIRM_INT_EQ(len, 0);
815        CONFIRM_INT_EQ(u_strlen(zeroString), 0);
816    }
817    else {
818        log_err("Couldn't get emptystring\n");
819    }
820
821    status = U_ZERO_ERROR;
822    strcpy(action, "getting and testing of empty int");
823    res = ures_getByKey(theBundle, "emptyint", res, &status);
824    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
825    CONFIRM_INT_EQ(ures_getType(res), URES_INT);
826    intResult=ures_getInt(res, &status);
827    if(U_SUCCESS(status)){
828        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
829        CONFIRM_INT_EQ(intResult, 0);
830    }
831    else {
832        log_err("Couldn't get emptystring\n");
833    }
834
835    status = U_ZERO_ERROR;
836    strcpy(action, "getting and testing of zero length intvector");
837    res = ures_getByKey(theBundle, "emptyintv", res, &status);
838    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
839    CONFIRM_INT_EQ(ures_getType(res), URES_INT_VECTOR);
840
841    if(U_FAILURE(status)){
842        log_err("Couldn't get emptyintv key %s\n", u_errorName(status));
843    }
844    else {
845        zeroIntVect=ures_getIntVector(res, &len, &status);
846        if(!U_SUCCESS(status) || resArray != NULL || len != 0) {
847            log_err("Shouldn't get emptyintv\n");
848        }
849    }
850
851    status = U_ZERO_ERROR;
852    strcpy(action, "getting and testing of zero length emptybin");
853    res = ures_getByKey(theBundle, "emptybin", res, &status);
854    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
855    CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
856
857    if(U_FAILURE(status)){
858        log_err("Couldn't get emptybin key %s\n", u_errorName(status));
859    }
860    else {
861        binResult=ures_getBinary(res, &len, &status);
862        if(!U_SUCCESS(status) || len != 0) {
863            log_err("Couldn't get emptybin, or it's not empty\n");
864        }
865    }
866
867    status = U_ZERO_ERROR;
868    strcpy(action, "getting and testing of zero length emptyarray");
869    res = ures_getByKey(theBundle, "emptyarray", res, &status);
870    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
871    CONFIRM_INT_EQ(ures_getType(res), URES_ARRAY);
872
873    if(U_FAILURE(status)){
874        log_err("Couldn't get emptyarray key %s\n", u_errorName(status));
875    }
876    else {
877        resArray=ures_getByIndex(res, 0, resArray, &status);
878        if(U_SUCCESS(status) || resArray != NULL){
879            log_err("Shouldn't get emptyarray[0]\n");
880        }
881    }
882
883    status = U_ZERO_ERROR;
884    strcpy(action, "getting and testing of zero length emptytable");
885    res = ures_getByKey(theBundle, "emptytable", res, &status);
886    CONFIRM_ErrorCode(status, U_ZERO_ERROR);
887    CONFIRM_INT_EQ(ures_getType(res), URES_TABLE);
888
889    if(U_FAILURE(status)){
890        log_err("Couldn't get emptytable key %s\n", u_errorName(status));
891    }
892    else {
893        resArray=ures_getByIndex(res, 0, resArray, &status);
894        if(U_SUCCESS(status) || resArray != NULL){
895            log_err("Shouldn't get emptytable[0]\n");
896        }
897    }
898
899    ures_close(res);
900    ures_close(theBundle);
901}
902
903static void TestEmptyBundle(){
904    UErrorCode status = U_ZERO_ERROR;
905    const char* testdatapath=NULL;
906    UResourceBundle *resb=0, *dResB=0;
907
908    testdatapath=loadTestData(&status);
909    if(U_FAILURE(status))
910    {
911        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
912        return;
913    }
914    resb = ures_open(testdatapath, "testempty", &status);
915
916    if(U_SUCCESS(status)){
917        dResB =  ures_getByKey(resb,"test",dResB,&status);
918        if(status!= U_MISSING_RESOURCE_ERROR){
919            log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n",
920                u_errorName(U_MISSING_RESOURCE_ERROR),u_errorName(status));
921        }
922    }
923    ures_close(dResB);
924    ures_close(resb);
925}
926
927static void TestBinaryCollationData(){
928    UErrorCode status=U_ZERO_ERROR;
929    const char*      locale="te";
930#if !UCONFIG_NO_COLLATION
931    const char* testdatapath;
932#endif
933    UResourceBundle *teRes = NULL;
934    UResourceBundle *coll=NULL;
935    UResourceBundle *binColl = NULL;
936    uint8_t *binResult = NULL;
937    int32_t len=0;
938    const char* action="testing the binary collaton data";
939
940#if !UCONFIG_NO_COLLATION
941    log_verbose("Testing binary collation data resource......\n");
942
943    testdatapath=loadTestData(&status);
944    if(U_FAILURE(status))
945    {
946        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
947        return;
948    }
949
950
951    teRes=ures_open(testdatapath, locale, &status);
952    if(U_FAILURE(status)){
953        log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status));
954        return;
955    }
956    status=U_ZERO_ERROR;
957    coll = ures_getByKey(teRes, "collations", coll, &status);
958    coll = ures_getByKey(coll, "standard", coll, &status);
959    if(U_SUCCESS(status)){
960        CONFIRM_ErrorCode(status, U_ZERO_ERROR);
961        CONFIRM_INT_EQ(ures_getType(coll), URES_TABLE);
962        binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status);
963        if(U_SUCCESS(status)){
964            CONFIRM_ErrorCode(status, U_ZERO_ERROR);
965            CONFIRM_INT_EQ(ures_getType(binColl), URES_BINARY);
966            binResult=(uint8_t*)ures_getBinary(binColl,  &len, &status);
967            if(U_SUCCESS(status)){
968                CONFIRM_ErrorCode(status, U_ZERO_ERROR);
969                CONFIRM_INT_GE(len, 1);
970            }
971
972        }else{
973            log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n");
974        }
975    }
976    else{
977        log_err("ERROR: ures_getByKey(locale(te), collations) failed\n");
978        return;
979    }
980    ures_close(binColl);
981    ures_close(coll);
982    ures_close(teRes);
983#endif
984}
985
986static void TestAPI() {
987    UErrorCode status=U_ZERO_ERROR;
988    int32_t len=0;
989    const char* key=NULL;
990    const UChar* value=NULL;
991    const char* testdatapath;
992    UChar* utestdatapath=NULL;
993    char convOutput[256];
994    UChar largeBuffer[1025];
995    UResourceBundle *teRes = NULL;
996    UResourceBundle *teFillin=NULL;
997    UResourceBundle *teFillin2=NULL;
998
999    log_verbose("Testing ures_openU()......\n");
1000
1001    testdatapath=loadTestData(&status);
1002    if(U_FAILURE(status))
1003    {
1004        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1005        return;
1006    }
1007    len =(int32_t)strlen(testdatapath);
1008    utestdatapath = (UChar*) malloc((len+10)*sizeof(UChar));
1009
1010    u_charsToUChars(testdatapath, utestdatapath, (int32_t)strlen(testdatapath)+1);
1011#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\'
1012    {
1013        /* Convert all backslashes to forward slashes so that we can make sure that ures_openU
1014           can handle invariant characters. */
1015        UChar *backslash;
1016        while ((backslash = u_strchr(utestdatapath, 0x005C))) {
1017            *backslash = 0x002F;
1018        }
1019    }
1020#endif
1021
1022    u_memset(largeBuffer, 0x0030, sizeof(largeBuffer)/sizeof(largeBuffer[0]));
1023    largeBuffer[sizeof(largeBuffer)/sizeof(largeBuffer[0])-1] = 0;
1024
1025    /*Test ures_openU */
1026
1027    status = U_ZERO_ERROR;
1028    ures_close(ures_openU(largeBuffer, "root", &status));
1029    if(status != U_ILLEGAL_ARGUMENT_ERROR){
1030        log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status));
1031    }
1032
1033    status = U_ZERO_ERROR;
1034    ures_close(ures_openU(NULL, "root", &status));
1035    if(U_FAILURE(status)){
1036        log_err_status(status, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status));
1037    }
1038
1039    status = U_ILLEGAL_ARGUMENT_ERROR;
1040    if(ures_openU(NULL, "root", &status) != NULL){
1041        log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status));
1042    }
1043
1044    status = U_ZERO_ERROR;
1045    teRes=ures_openU(utestdatapath, "te", &status);
1046    if(U_FAILURE(status)){
1047        log_err_status(status, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status));
1048        return;
1049    }
1050    /*Test ures_getLocale() */
1051    log_verbose("Testing ures_getLocale() .....\n");
1052    if(strcmp(ures_getLocale(teRes, &status), "te") != 0){
1053        log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes, &status));
1054    }
1055    /*Test ures_getNextString() */
1056    teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status);
1057    key=ures_getKey(teFillin);
1058    value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
1059    ures_resetIterator(NULL);
1060    value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
1061    if(status !=U_INDEX_OUTOFBOUNDS_ERROR){
1062        log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1063                       myErrorName(status));
1064    }
1065    ures_resetIterator(teRes);
1066    /*Test ures_getNextResource() where resource is table*/
1067    status=U_ZERO_ERROR;
1068#if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1069    /* The next key varies depending on the charset. */
1070    teFillin=ures_getNextResource(teRes, teFillin, &status);
1071    if(U_FAILURE(status)){
1072        log_err("ERROR: ures_getNextResource() failed \n");
1073    }
1074    key=ures_getKey(teFillin);
1075    /*if(strcmp(key, "%%CollationBin") != 0){*/
1076    /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */
1077    if(strcmp(key, "a") != 0){
1078        log_err("ERROR: ures_getNextResource() failed\n");
1079    }
1080#endif
1081
1082    /*Test ures_getByIndex on string Resource*/
1083    teFillin=ures_getByKey(teRes, "string_only_in_te", teFillin, &status);
1084    teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status);
1085    if(U_FAILURE(status)){
1086        log_err("ERROR: ures_getByIndex on string resource failed\n");
1087    }
1088    if(strcmp(u_austrcpy(convOutput, tres_getString(teFillin2, -1, NULL, &len, &status)), "TE") != 0){
1089        status=U_ZERO_ERROR;
1090        log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2, &len, &status)));
1091    }
1092
1093    /*ures_close(teRes);*/
1094
1095    /*Test ures_openFillIn*/
1096    log_verbose("Testing ures_openFillIn......\n");
1097    status=U_ZERO_ERROR;
1098    ures_openFillIn(teRes, testdatapath, "te", &status);
1099    if(U_FAILURE(status)){
1100        log_err("ERROR: ures_openFillIn failed\n");
1101        return;
1102    }
1103    if(strcmp(ures_getLocale(teRes, &status), "te") != 0){
1104        log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n");
1105    }
1106    ures_getByKey(teRes, "string_only_in_te", teFillin, &status);
1107    teFillin2=ures_getNextResource(teFillin, teFillin2, &status);
1108    if(ures_getType(teFillin2) != URES_STRING){
1109        log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n");
1110    }
1111    teFillin2=ures_getNextResource(teFillin, teFillin2, &status);
1112    if(status !=U_INDEX_OUTOFBOUNDS_ERROR){
1113        log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1114                       myErrorName(status));
1115    }
1116
1117    ures_close(teFillin);
1118    ures_close(teFillin2);
1119    ures_close(teRes);
1120
1121    /* Test that ures_getLocale() returns the "real" locale ID */
1122    status=U_ZERO_ERROR;
1123    teRes=ures_open(NULL, "dE_At_NOWHERE_TO_BE_FOUND", &status);
1124    if(U_FAILURE(status)) {
1125        log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status));
1126    } else {
1127        if(0!=strcmp("de_AT", ures_getLocale(teRes, &status))) {
1128            log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes, &status));
1129        }
1130        ures_close(teRes);
1131    }
1132
1133    /* same test, but with an aliased locale resource bundle */
1134    status=U_ZERO_ERROR;
1135    teRes=ures_open(NULL, "iW_Il_depRecaTed_HebreW", &status);
1136    if(U_FAILURE(status)) {
1137        log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status));
1138    } else {
1139        if(0!=strcmp("he_IL", ures_getLocale(teRes, &status))) {
1140            log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes, &status));
1141        }
1142        ures_close(teRes);
1143    }
1144    free(utestdatapath);
1145}
1146
1147static void TestErrorConditions(){
1148    UErrorCode status=U_ZERO_ERROR;
1149    const char *key=NULL;
1150    const UChar *value=NULL;
1151    const char* testdatapath;
1152    UChar* utestdatapath;
1153    int32_t len=0;
1154    UResourceBundle *teRes = NULL;
1155    UResourceBundle *coll=NULL;
1156    UResourceBundle *binColl = NULL;
1157    UResourceBundle *teFillin=NULL;
1158    UResourceBundle *teFillin2=NULL;
1159    uint8_t *binResult = NULL;
1160    int32_t resultLen;
1161
1162
1163    testdatapath = loadTestData(&status);
1164    if(U_FAILURE(status))
1165    {
1166        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1167        return;
1168    }
1169    len = (int32_t)strlen(testdatapath);
1170    utestdatapath = (UChar*) malloc(sizeof(UChar) *(len+10));
1171    u_uastrcpy(utestdatapath, testdatapath);
1172
1173    /*Test ures_openU with status != U_ZERO_ERROR*/
1174    log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n");
1175    status=U_ILLEGAL_ARGUMENT_ERROR;
1176    teRes=ures_openU(utestdatapath, "te", &status);
1177    if(U_FAILURE(status)){
1178        log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath);
1179    }else{
1180        log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath));
1181        ures_close(teRes);
1182    }
1183    /*Test ures_openFillIn with UResourceBundle = NULL*/
1184    log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n");
1185    status=U_ZERO_ERROR;
1186    ures_openFillIn(NULL, testdatapath, "te", &status);
1187    if(status != U_ILLEGAL_ARGUMENT_ERROR){
1188        log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail.  Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1189                        myErrorName(status));
1190    }
1191    /*Test ures_getLocale() with status != U_ZERO_ERROR*/
1192    status=U_ZERO_ERROR;
1193    teRes=ures_openU(utestdatapath, "te", &status);
1194    if(U_FAILURE(status)){
1195        log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status));
1196        return;
1197    }
1198    status=U_ILLEGAL_ARGUMENT_ERROR;
1199    if(ures_getLocale(teRes, &status) != NULL){
1200        log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n");
1201    }
1202    /*Test ures_getLocale() with UResourceBundle = NULL*/
1203    status=U_ZERO_ERROR;
1204    if(ures_getLocale(NULL, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1205        log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1206                                           myErrorName(status));
1207    }
1208    /*Test ures_getSize() with UResourceBundle = NULL */
1209    status=U_ZERO_ERROR;
1210    if(ures_getSize(NULL) != 0){
1211        log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL.  Got =%d\n", ures_getSize(NULL));
1212    }
1213    /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/
1214    status=U_ZERO_ERROR;
1215    if(ures_getType(NULL) != URES_NONE){
1216        log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL.  Got =%d\n", ures_getType(NULL));
1217    }
1218    /*Test ures_getKey() with UResourceBundle = NULL*/
1219    status=U_ZERO_ERROR;
1220    if(ures_getKey(NULL) != NULL){
1221        log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL.  Got =%d\n", ures_getKey(NULL));
1222    }
1223    /*Test ures_hasNext() with UResourceBundle = NULL*/
1224    status=U_ZERO_ERROR;
1225    if(ures_hasNext(NULL) != FALSE){
1226        log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL.  Got =%d\n", ures_hasNext(NULL));
1227    }
1228    /*Test ures_get() with UResourceBundle = NULL*/
1229    status=U_ZERO_ERROR;
1230    if(ures_getStringByKey(NULL, "string_only_in_te", &resultLen, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1231        log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1232                                           myErrorName(status));
1233    }
1234    /*Test ures_getByKey() with UResourceBundle = NULL*/
1235    status=U_ZERO_ERROR;
1236    teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status);
1237    if( teFillin != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1238        log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1239                                           myErrorName(status));
1240    }
1241    /*Test ures_getByKey() with status != U_ZERO_ERROR*/
1242    teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status);
1243    if(teFillin != NULL ){
1244        log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n");
1245    }
1246    /*Test ures_getStringByKey() with UResourceBundle = NULL*/
1247    status=U_ZERO_ERROR;
1248    if(ures_getStringByKey(NULL, "string_only_in_te", &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1249        log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1250                                           myErrorName(status));
1251    }
1252    /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/
1253    if(ures_getStringByKey(teRes, "string_only_in_te", &len, &status) != NULL){
1254        log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1255                                           myErrorName(status));
1256    }
1257    /*Test ures_getString() with UResourceBundle = NULL*/
1258    status=U_ZERO_ERROR;
1259    if(ures_getString(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1260        log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1261                                           myErrorName(status));
1262    }
1263    /*Test ures_getString() with status != U_ZERO_ERROR*/
1264    if(ures_getString(teRes, &len, &status) != NULL){
1265        log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1266                                           myErrorName(status));
1267    }
1268    /*Test ures_getBinary() with UResourceBundle = NULL*/
1269    status=U_ZERO_ERROR;
1270    if(ures_getBinary(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1271        log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1272                                           myErrorName(status));
1273    }
1274    /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/
1275    status=U_ZERO_ERROR;
1276    coll = ures_getByKey(teRes, "collations", coll, &status);
1277    coll = ures_getByKey(teRes, "standard", coll, &status);
1278    binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status);
1279
1280    status=U_ILLEGAL_ARGUMENT_ERROR;
1281    binResult=(uint8_t*)ures_getBinary(binColl,  &len, &status);
1282    if(binResult != NULL){
1283        log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n");
1284    }
1285
1286    /*Test ures_getNextResource() with status != U_ZERO_ERROR*/
1287    teFillin=ures_getNextResource(teRes, teFillin, &status);
1288    if(teFillin != NULL){
1289        log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1290    }
1291    /*Test ures_getNextResource() with UResourceBundle = NULL*/
1292    status=U_ZERO_ERROR;
1293    teFillin=ures_getNextResource(NULL, teFillin, &status);
1294    if(teFillin != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1295        log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail.  Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n",
1296                                          myErrorName(status));
1297    }
1298    /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/
1299    teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status);
1300    key=ures_getKey(teFillin);
1301    status = U_ILLEGAL_ARGUMENT_ERROR;
1302    value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
1303    if(value != NULL){
1304        log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1305    }
1306    /*Test ures_getNextString with UResourceBundle = NULL*/
1307    status=U_ZERO_ERROR;
1308    value=(UChar*)ures_getNextString(NULL, &len, &key, &status);
1309    if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1310        log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1311                                    myErrorName(status));
1312    }
1313    /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/
1314    status=U_ZERO_ERROR;
1315    teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status);
1316    if(ures_countArrayItems(teRes, "array_only_in_te", &status) != 4) {
1317      log_err("ERROR: Wrong number of items in an array!\n");
1318    }
1319    status=U_ILLEGAL_ARGUMENT_ERROR;
1320    teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status);
1321    if(teFillin2 != NULL){
1322        log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1323    }
1324    /*Test ures_getByIndex with UResourceBundle = NULL */
1325    status=U_ZERO_ERROR;
1326    teFillin2=ures_getByIndex(NULL, 0, teFillin2, &status);
1327    if(status != U_ILLEGAL_ARGUMENT_ERROR){
1328        log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1329                                    myErrorName(status));
1330    }
1331    /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/
1332    status=U_ZERO_ERROR;
1333    teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status);
1334    status=U_ILLEGAL_ARGUMENT_ERROR;
1335    value=(UChar*)ures_getStringByIndex(teFillin, 0, &len, &status);
1336    if( value != NULL){
1337        log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1338    }
1339    /*Test ures_getStringByIndex with UResourceBundle = NULL */
1340    status=U_ZERO_ERROR;
1341    value=(UChar*)ures_getStringByIndex(NULL, 0, &len, &status);
1342    if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1343        log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1344                                    myErrorName(status));
1345    }
1346    /*Test ures_getStringByIndex with UResourceBundle = NULL */
1347    status=U_ZERO_ERROR;
1348    value=(UChar*)ures_getStringByIndex(teFillin, 9999, &len, &status);
1349    if(value != NULL || status != U_MISSING_RESOURCE_ERROR){
1350        log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n",
1351                                    myErrorName(status));
1352    }
1353    /*Test ures_getInt() where UResourceBundle = NULL */
1354    status=U_ZERO_ERROR;
1355    if(ures_getInt(NULL, &status) != -1 && status != U_ILLEGAL_ARGUMENT_ERROR){
1356        log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n",
1357                           myErrorName(status));
1358    }
1359    /*Test ures_getInt() where status != U_ZERO_ERROR */
1360    if(ures_getInt(teRes, &status) != -1){
1361        log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n");
1362    }
1363
1364    ures_close(teFillin);
1365    ures_close(teFillin2);
1366    ures_close(coll);
1367    ures_close(binColl);
1368    ures_close(teRes);
1369    free(utestdatapath);
1370
1371
1372}
1373
1374static void TestGetVersion(){
1375    UVersionInfo minVersionArray = {0x01, 0x00, 0x00, 0x00};
1376    UVersionInfo maxVersionArray = {0x50, 0xff, 0xcf, 0xcf};
1377    UVersionInfo versionArray;
1378    UErrorCode status= U_ZERO_ERROR;
1379    UResourceBundle* resB = NULL;
1380    int i=0, j = 0;
1381    int locCount = uloc_countAvailable();
1382    const char *locName = "root";
1383
1384    log_verbose("The ures_getVersion tests begin : \n");
1385
1386    for(j = -1; j < locCount; j++) {
1387        if(j >= 0) {
1388            locName = uloc_getAvailable(j);
1389        }
1390        log_verbose("Testing version number for locale %s\n", locName);
1391        resB = ures_open(NULL,locName, &status);
1392        if (U_FAILURE(status)) {
1393            log_err_status(status, "Resource bundle creation for locale %s failed.: %s\n", locName, myErrorName(status));
1394            ures_close(resB);
1395            return;
1396        }
1397        ures_getVersion(resB, versionArray);
1398        for (i=0; i<4; ++i) {
1399            if (versionArray[i] < minVersionArray[i] ||
1400                versionArray[i] > maxVersionArray[i])
1401            {
1402                log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1403                    locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
1404                break;
1405            }
1406        }
1407        ures_close(resB);
1408    }
1409}
1410
1411
1412static void TestGetVersionColl(){
1413    UVersionInfo minVersionArray = {0x00, 0x00, 0x00, 0x00};
1414    UVersionInfo maxVersionArray = {0x50, 0x80, 0xcf, 0xcf};
1415    UVersionInfo versionArray;
1416    UErrorCode status= U_ZERO_ERROR;
1417    UResourceBundle* resB = NULL;
1418    UEnumeration *locs= NULL;
1419    int i=0;
1420    const char *locName = "root";
1421    int32_t locLen;
1422    const UChar* rules =NULL;
1423    int32_t len = 0;
1424
1425    log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL);
1426    locs = ures_openAvailableLocales(U_ICUDATA_COLL, &status);
1427    if (U_FAILURE(status)) {
1428       log_err_status(status, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL, myErrorName(status));
1429       return;
1430    }
1431
1432    do{
1433        log_verbose("Testing version number for locale %s\n", locName);
1434        resB = ures_open(U_ICUDATA_COLL,locName, &status);
1435        if (U_FAILURE(status)) {
1436            log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL, locName, myErrorName(status));
1437            ures_close(resB);
1438            return;
1439        }
1440        /* test NUL termination of UCARules */
1441        rules = tres_getString(resB,-1,"UCARules",&len, &status);
1442        if(!rules || U_FAILURE(status)) {
1443          /* BEGIN android-changd
1444             To save space, Android does not include the collation tailoring rules.
1445             Skip the error log.
1446          log_data_err("Could not load UCARules for locale %s\n", locName);
1447          */
1448          status = U_ZERO_ERROR;
1449          /* END android-changed */
1450          continue;
1451        }
1452        if(u_strlen(rules) != len){
1453            log_err("UCARules string not nul terminated! \n");
1454        }
1455        ures_getVersion(resB, versionArray);
1456        for (i=0; i<4; ++i) {
1457            if (versionArray[i] < minVersionArray[i] ||
1458                versionArray[i] > maxVersionArray[i])
1459            {
1460                log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1461                    locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
1462                break;
1463            }
1464        }
1465        ures_close(resB);
1466    } while((locName = uenum_next(locs,&locLen,&status))&&U_SUCCESS(status));
1467
1468    if(U_FAILURE(status)) {
1469        log_err("Err %s testing Collation locales.\n", u_errorName(status));
1470    }
1471    uenum_close(locs);
1472}
1473
1474static void TestResourceBundles()
1475{
1476    UErrorCode status = U_ZERO_ERROR;
1477    loadTestData(&status);
1478    if(U_FAILURE(status)) {
1479        log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
1480        return;
1481    }
1482
1483    testTag("only_in_Root", TRUE, FALSE, FALSE);
1484    testTag("in_Root_te", TRUE, TRUE, FALSE);
1485    testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
1486    testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
1487    testTag("only_in_te", FALSE, TRUE, FALSE);
1488    testTag("only_in_te_IN", FALSE, FALSE, TRUE);
1489    testTag("in_te_te_IN", FALSE, TRUE, TRUE);
1490    testTag("nonexistent", FALSE, FALSE, FALSE);
1491
1492    log_verbose("Passed:=  %d   Failed=   %d \n", pass, fail);
1493
1494}
1495
1496
1497static void TestConstruction1()
1498{
1499    UResourceBundle *test1 = 0, *test2 = 0,*empty = 0;
1500    const UChar *result1, *result2;
1501    UErrorCode status= U_ZERO_ERROR;
1502    UErrorCode   err = U_ZERO_ERROR;
1503    const char*      locale="te_IN";
1504    const char* testdatapath;
1505
1506    int32_t len1=0;
1507    int32_t len2=0;
1508    UVersionInfo versionInfo;
1509    char versionString[256];
1510    char verboseOutput[256];
1511
1512    U_STRING_DECL(rootVal, "ROOT", 4);
1513    U_STRING_DECL(te_inVal, "TE_IN", 5);
1514
1515    U_STRING_INIT(rootVal, "ROOT", 4);
1516    U_STRING_INIT(te_inVal, "TE_IN", 5);
1517
1518    testdatapath=loadTestData(&status);
1519    if(U_FAILURE(status))
1520    {
1521        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1522        return;
1523    }
1524
1525    log_verbose("Testing ures_open()......\n");
1526
1527    empty = ures_open(testdatapath, "testempty", &status);
1528    if(empty == NULL || U_FAILURE(status)) {
1529        log_err("opening empty failed!\n");
1530    }
1531    ures_close(empty);
1532
1533    test1=ures_open(testdatapath, NULL, &err);
1534
1535    if(U_FAILURE(err))
1536    {
1537        log_err("construction of NULL did not succeed :  %s \n", myErrorName(status));
1538        return;
1539    }
1540    test2=ures_open(testdatapath, locale, &err);
1541    if(U_FAILURE(err))
1542    {
1543        log_err("construction of %s did not succeed :  %s \n", locale, myErrorName(status));
1544        return;
1545    }
1546    result1= tres_getString(test1, -1, "string_in_Root_te_te_IN", &len1, &err);
1547    result2= tres_getString(test2, -1, "string_in_Root_te_te_IN", &len2, &err);
1548    if (U_FAILURE(err) || len1==0 || len2==0) {
1549        log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status));
1550        return;
1551    }
1552    log_verbose("for string_in_Root_te_te_IN, default.txt had  %s\n", u_austrcpy(verboseOutput, result1));
1553    log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput, result2));
1554    if(u_strcmp(result1, rootVal) !=0  || u_strcmp(result2, te_inVal) !=0 ){
1555        log_err("construction test failed. Run Verbose for more information");
1556    }
1557
1558
1559    /* Test getVersionNumber*/
1560    log_verbose("Testing version number\n");
1561    log_verbose("for getVersionNumber :  %s\n", ures_getVersionNumber(test1));
1562
1563    log_verbose("Testing version \n");
1564    ures_getVersion(test1, versionInfo);
1565    u_versionToString(versionInfo, versionString);
1566
1567    log_verbose("for getVersion :  %s\n", versionString);
1568
1569    if(strcmp(versionString, ures_getVersionNumber(test1)) != 0) {
1570        log_err("Versions differ: %s vs %s\n", versionString, ures_getVersionNumber(test1));
1571    }
1572
1573    ures_close(test1);
1574    ures_close(test2);
1575
1576}
1577
1578/*****************************************************************************/
1579/*****************************************************************************/
1580
1581static UBool testTag(const char* frag,
1582           UBool in_Root,
1583           UBool in_te,
1584           UBool in_te_IN)
1585{
1586    int32_t failNum = fail;
1587
1588    /* Make array from input params */
1589
1590    UBool is_in[3];
1591    const char *NAME[] = { "ROOT", "TE", "TE_IN" };
1592
1593    /* Now try to load the desired items */
1594    UResourceBundle* theBundle = NULL;
1595    char tag[99];
1596    char action[256];
1597    UErrorCode expected_status,status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
1598    UChar* base = NULL;
1599    UChar* expected_string = NULL;
1600    const UChar* string = NULL;
1601    char buf[5];
1602    char item_tag[10];
1603    int32_t i,j,row,col, len;
1604    int32_t actual_bundle;
1605    int32_t count = 0;
1606    int32_t row_count=0;
1607    int32_t column_count=0;
1608    int32_t index = 0;
1609    int32_t tag_count= 0;
1610    const char* testdatapath;
1611    char verboseOutput[256];
1612    UResourceBundle* array=NULL;
1613    UResourceBundle* array2d=NULL;
1614    UResourceBundle* tags=NULL;
1615    UResourceBundle* arrayItem1=NULL;
1616
1617    testdatapath = loadTestData(&status);
1618    if(U_FAILURE(status))
1619    {
1620        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1621        return FALSE;
1622    }
1623
1624    is_in[0] = in_Root;
1625    is_in[1] = in_te;
1626    is_in[2] = in_te_IN;
1627
1628    strcpy(item_tag, "tag");
1629
1630    for (i=0; i<bundles_count; ++i)
1631    {
1632        strcpy(action,"construction for ");
1633        strcat(action, param[i].name);
1634
1635
1636        status = U_ZERO_ERROR;
1637
1638        theBundle = ures_open(testdatapath, param[i].name, &status);
1639        CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
1640
1641        if(i == 5)
1642            actual_bundle = 0; /* ne -> default */
1643        else if(i == 3)
1644            actual_bundle = 1; /* te_NE -> te */
1645        else if(i == 4)
1646            actual_bundle = 2; /* te_IN_NE -> te_IN */
1647        else
1648            actual_bundle = i;
1649
1650        expected_resource_status = U_MISSING_RESOURCE_ERROR;
1651        for (j=e_te_IN; j>=e_Root; --j)
1652        {
1653            if (is_in[j] && param[i].inherits[j])
1654            {
1655
1656                if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1657                    expected_resource_status = U_ZERO_ERROR;
1658                else if(j == 0)
1659                    expected_resource_status = U_USING_DEFAULT_WARNING;
1660                else
1661                    expected_resource_status = U_USING_FALLBACK_WARNING;
1662
1663                log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>.  actual_bundle=%s\n",
1664                            param[i].name,
1665                            i,
1666                            frag,
1667                            j,
1668                            is_in[j]?"Yes":"No",
1669                            j,
1670                            param[i].inherits[j]?"Yes":"No",
1671                            param[actual_bundle].name);
1672
1673                break;
1674            }
1675        }
1676
1677        for (j=param[i].where; j>=0; --j)
1678        {
1679            if (is_in[j])
1680            {
1681                if(base != NULL) {
1682                    free(base);
1683                    base = NULL;
1684                }
1685                base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
1686                u_uastrcpy(base,NAME[j]);
1687
1688                break;
1689            }
1690            else {
1691                if(base != NULL) {
1692                    free(base);
1693                    base = NULL;
1694                }
1695                base = (UChar*) malloc(sizeof(UChar) * 1);
1696                *base = 0x0000;
1697            }
1698        }
1699
1700        /*----string---------------------------------------------------------------- */
1701
1702        strcpy(tag,"string_");
1703        strcat(tag,frag);
1704
1705        strcpy(action,param[i].name);
1706        strcat(action, ".ures_getStringByKey(" );
1707        strcat(action,tag);
1708        strcat(action, ")");
1709
1710
1711        status = U_ZERO_ERROR;
1712        len=0;
1713
1714        string=tres_getString(theBundle, -1, tag, &len, &status);
1715        if(U_SUCCESS(status)) {
1716            expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 4));
1717            u_strcpy(expected_string,base);
1718            CONFIRM_INT_EQ(len, u_strlen(expected_string));
1719        }else{
1720            expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
1721            u_strcpy(expected_string,kERROR);
1722            string=kERROR;
1723        }
1724        log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
1725
1726        CONFIRM_ErrorCode(status, expected_resource_status);
1727        CONFIRM_EQ(string, expected_string);
1728
1729
1730
1731        /*--------------array------------------------------------------------- */
1732
1733        strcpy(tag,"array_");
1734        strcat(tag,frag);
1735
1736        strcpy(action,param[i].name);
1737        strcat(action, ".ures_getByKey(" );
1738        strcat(action,tag);
1739        strcat(action, ")");
1740
1741        len=0;
1742
1743        count = kERROR_COUNT;
1744        status = U_ZERO_ERROR;
1745        array=ures_getByKey(theBundle, tag, array, &status);
1746        CONFIRM_ErrorCode(status,expected_resource_status);
1747        if (U_SUCCESS(status)) {
1748            /*confirm the resource type is an array*/
1749            CONFIRM_INT_EQ(ures_getType(array), URES_ARRAY);
1750            /*confirm the size*/
1751            count=ures_getSize(array);
1752            CONFIRM_INT_GE(count,1);
1753            for (j=0; j<count; ++j) {
1754                UChar element[3];
1755                u_strcpy(expected_string, base);
1756                u_uastrcpy(element, itoa1(j,buf));
1757                u_strcat(expected_string, element);
1758                arrayItem1=ures_getNextResource(array, arrayItem1, &status);
1759                if(U_SUCCESS(status)){
1760                    CONFIRM_EQ(tres_getString(arrayItem1, -1, NULL, &len, &status),expected_string);
1761                }
1762            }
1763
1764        }
1765        else {
1766            CONFIRM_INT_EQ(count,kERROR_COUNT);
1767            CONFIRM_ErrorCode(status, U_MISSING_RESOURCE_ERROR);
1768            /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1769            count = 0;
1770        }
1771
1772        /*--------------arrayItem------------------------------------------------- */
1773
1774        strcpy(tag,"array_");
1775        strcat(tag,frag);
1776
1777        strcpy(action,param[i].name);
1778        strcat(action, ".ures_getStringByIndex(");
1779        strcat(action, tag);
1780        strcat(action, ")");
1781
1782
1783        for (j=0; j<10; ++j){
1784            index = count ? (randi(count * 3) - count) : (randi(200) - 100);
1785            status = U_ZERO_ERROR;
1786            string=kERROR;
1787            array=ures_getByKey(theBundle, tag, array, &status);
1788            if(!U_FAILURE(status)){
1789                UChar *t=NULL;
1790                t=(UChar*)ures_getStringByIndex(array, index, &len, &status);
1791                if(!U_FAILURE(status)){
1792                    UChar element[3];
1793                    string=t;
1794                    u_strcpy(expected_string, base);
1795                    u_uastrcpy(element, itoa1(index,buf));
1796                    u_strcat(expected_string, element);
1797                } else {
1798                    u_strcpy(expected_string, kERROR);
1799                }
1800
1801            }
1802            expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
1803            CONFIRM_ErrorCode(status,expected_status);
1804            CONFIRM_EQ(string,expected_string);
1805
1806        }
1807
1808
1809        /*--------------2dArray------------------------------------------------- */
1810
1811        strcpy(tag,"array_2d_");
1812        strcat(tag,frag);
1813
1814        strcpy(action,param[i].name);
1815        strcat(action, ".ures_getByKey(" );
1816        strcat(action,tag);
1817        strcat(action, ")");
1818
1819
1820
1821        row_count = kERROR_COUNT, column_count = kERROR_COUNT;
1822        status = U_ZERO_ERROR;
1823        array2d=ures_getByKey(theBundle, tag, array2d, &status);
1824
1825        CONFIRM_ErrorCode(status,expected_resource_status);
1826        if (U_SUCCESS(status))
1827        {
1828            /*confirm the resource type is an 2darray*/
1829            CONFIRM_INT_EQ(ures_getType(array2d), URES_ARRAY);
1830            row_count=ures_getSize(array2d);
1831            CONFIRM_INT_GE(row_count,1);
1832
1833            for(row=0; row<row_count; ++row){
1834                UResourceBundle *tableRow=NULL;
1835                tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1836                CONFIRM_ErrorCode(status, expected_resource_status);
1837                if(U_SUCCESS(status)){
1838                    /*confirm the resourcetype of each table row is an array*/
1839                    CONFIRM_INT_EQ(ures_getType(tableRow), URES_ARRAY);
1840                    column_count=ures_getSize(tableRow);
1841                    CONFIRM_INT_GE(column_count,1);
1842
1843                    for (col=0; j<column_count; ++j) {
1844                        UChar element[3];
1845                        u_strcpy(expected_string, base);
1846                        u_uastrcpy(element, itoa1(row, buf));
1847                        u_strcat(expected_string, element);
1848                        u_uastrcpy(element, itoa1(col, buf));
1849                        u_strcat(expected_string, element);
1850                        arrayItem1=ures_getNextResource(tableRow, arrayItem1, &status);
1851                        if(U_SUCCESS(status)){
1852                            const UChar *stringValue=tres_getString(arrayItem1, -1, NULL, &len, &status);
1853                            CONFIRM_EQ(stringValue, expected_string);
1854                        }
1855                    }
1856                }
1857                ures_close(tableRow);
1858            }
1859        }else{
1860            CONFIRM_INT_EQ(row_count,kERROR_COUNT);
1861            CONFIRM_INT_EQ(column_count,kERROR_COUNT);
1862            row_count=column_count=0;
1863        }
1864
1865
1866        /*------2dArrayItem-------------------------------------------------------------- */
1867        /* 2dArrayItem*/
1868        for (j=0; j<10; ++j)
1869        {
1870            row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100);
1871            col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100);
1872            status = U_ZERO_ERROR;
1873            string = kERROR;
1874            len=0;
1875            array2d=ures_getByKey(theBundle, tag, array2d, &status);
1876            if(U_SUCCESS(status)){
1877                UResourceBundle *tableRow=NULL;
1878                tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1879                if(U_SUCCESS(status)) {
1880                    UChar *t=NULL;
1881                    t=(UChar*)ures_getStringByIndex(tableRow, col, &len, &status);
1882                    if(U_SUCCESS(status)){
1883                        string=t;
1884                    }
1885                }
1886                ures_close(tableRow);
1887            }
1888            expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
1889                                   expected_resource_status: U_MISSING_RESOURCE_ERROR;
1890            CONFIRM_ErrorCode(status,expected_status);
1891
1892            if (U_SUCCESS(status)){
1893                UChar element[3];
1894                u_strcpy(expected_string, base);
1895                u_uastrcpy(element, itoa1(row, buf));
1896                u_strcat(expected_string, element);
1897                u_uastrcpy(element, itoa1(col, buf));
1898                u_strcat(expected_string, element);
1899            } else {
1900                u_strcpy(expected_string,kERROR);
1901            }
1902            CONFIRM_EQ(string,expected_string);
1903
1904        }
1905
1906
1907        /*--------------taggedArray----------------------------------------------- */
1908        strcpy(tag,"tagged_array_");
1909        strcat(tag,frag);
1910
1911        strcpy(action,param[i].name);
1912        strcat(action,".ures_getByKey(");
1913        strcat(action, tag);
1914        strcat(action,")");
1915
1916
1917        status = U_ZERO_ERROR;
1918        tag_count=0;
1919        tags=ures_getByKey(theBundle, tag, tags, &status);
1920        CONFIRM_ErrorCode(status, expected_resource_status);
1921        if (U_SUCCESS(status)) {
1922            UResType bundleType=ures_getType(tags);
1923            CONFIRM_INT_EQ(bundleType, URES_TABLE);
1924
1925            tag_count=ures_getSize(tags);
1926            CONFIRM_INT_GE((int32_t)tag_count, (int32_t)0);
1927
1928            for(index=0; index <tag_count; index++){
1929                UResourceBundle *tagelement=NULL;
1930                const char *key=NULL;
1931                UChar* value=NULL;
1932                tagelement=ures_getByIndex(tags, index, tagelement, &status);
1933                key=ures_getKey(tagelement);
1934                value=(UChar*)ures_getNextString(tagelement, &len, &key, &status);
1935                log_verbose("tag = %s, value = %s\n", key, u_austrcpy(verboseOutput, value));
1936                if(strncmp(key, "tag", 3) == 0 && u_strncmp(value, base, u_strlen(base)) == 0){
1937                    record_pass();
1938                }else{
1939                    record_fail();
1940                }
1941                ures_close(tagelement);
1942            }
1943        }else{
1944            tag_count=0;
1945        }
1946
1947        /*---------taggedArrayItem----------------------------------------------*/
1948        count = 0;
1949        for (index=-20; index<20; ++index)
1950        {
1951
1952            status = U_ZERO_ERROR;
1953            string = kERROR;
1954            strcpy(item_tag, "tag");
1955            strcat(item_tag, itoa1(index,buf));
1956            tags=ures_getByKey(theBundle, tag, tags, &status);
1957            if(U_SUCCESS(status)){
1958                UResourceBundle *tagelement=NULL;
1959                UChar *t=NULL;
1960                tagelement=ures_getByKey(tags, item_tag, tagelement, &status);
1961                if(!U_FAILURE(status)){
1962                    UResType elementType=ures_getType(tagelement);
1963                    CONFIRM_INT_EQ(elementType, URES_STRING);
1964                    if(strcmp(ures_getKey(tagelement), item_tag) == 0){
1965                        record_pass();
1966                    }else{
1967                        record_fail();
1968                    }
1969                    t=(UChar*)tres_getString(tagelement, -1, NULL, &len, &status);
1970                    if(!U_FAILURE(status)){
1971                        string=t;
1972                    }
1973                }
1974                if (index < 0) {
1975                    CONFIRM_ErrorCode(status,U_MISSING_RESOURCE_ERROR);
1976                }
1977                else{
1978                    if (status != U_MISSING_RESOURCE_ERROR) {
1979                        UChar element[3];
1980                        u_strcpy(expected_string, base);
1981                        u_uastrcpy(element, itoa1(index,buf));
1982                        u_strcat(expected_string, element);
1983                        CONFIRM_EQ(string,expected_string);
1984                        count++;
1985                    }
1986                }
1987                ures_close(tagelement);
1988            }
1989        }
1990        CONFIRM_INT_EQ(count, tag_count);
1991
1992        free(expected_string);
1993        ures_close(theBundle);
1994    }
1995    ures_close(array);
1996    ures_close(array2d);
1997    ures_close(tags);
1998    ures_close(arrayItem1);
1999    free(base);
2000    return (UBool)(failNum == fail);
2001}
2002
2003static void record_pass()
2004{
2005    ++pass;
2006}
2007
2008static void record_fail()
2009{
2010    ++fail;
2011}
2012
2013/**
2014 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2015 * are set correctly
2016 */
2017
2018static void TestFallback()
2019{
2020    UErrorCode status = U_ZERO_ERROR;
2021    UResourceBundle *fr_FR = NULL;
2022    UResourceBundle *subResource = NULL;
2023    const UChar *junk; /* ignored */
2024    int32_t resultLen;
2025
2026    log_verbose("Opening fr_FR..");
2027    fr_FR = ures_open(NULL, "fr_FR", &status);
2028    if(U_FAILURE(status))
2029    {
2030        log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
2031        return;
2032    }
2033
2034    status = U_ZERO_ERROR;
2035
2036
2037    /* clear it out..  just do some calls to get the gears turning */
2038    junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
2039    status = U_ZERO_ERROR;
2040    junk = tres_getString(fr_FR, -1, "LocaleString", &resultLen, &status);
2041    status = U_ZERO_ERROR;
2042    junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
2043    status = U_ZERO_ERROR;
2044
2045    /* OK first one. This should be a Default value. */
2046    subResource = ures_getByKey(fr_FR, "MeasurementSystem", NULL, &status);
2047    if(status != U_USING_DEFAULT_WARNING)
2048    {
2049        log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n",
2050            u_errorName(status));
2051    }
2052
2053    status = U_ZERO_ERROR;
2054    ures_close(subResource);
2055
2056    /* and this is a Fallback, to fr */
2057    junk = tres_getString(fr_FR, -1, "ExemplarCharacters", &resultLen, &status);
2058    if(status != U_USING_FALLBACK_WARNING)
2059    {
2060        log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
2061            status);
2062    }
2063
2064    status = U_ZERO_ERROR;
2065
2066    ures_close(fr_FR);
2067    /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2068    /* Test Jitterbug 552 fallback mechanism of aliased data */
2069    {
2070        UErrorCode err =U_ZERO_ERROR;
2071        UResourceBundle* myResB = ures_open(NULL,"no_NO_NY",&err);
2072        UResourceBundle* resLocID = ures_getByKey(myResB, "Version", NULL, &err);
2073        UResourceBundle* tResB;
2074        UResourceBundle* zoneResource;
2075        const UChar* version = NULL;
2076        static const UChar versionStr[] = { 0x0032, 0x002E, 0x0030, 0x002E, 0x0034, 0x0031, 0x002E, 0x0032, 0x0033, 0x0000};
2077
2078        if(err != U_ZERO_ERROR){
2079            log_data_err("Expected U_ZERO_ERROR when trying to test no_NO_NY aliased to nn_NO for Version err=%s\n",u_errorName(err));
2080            return;
2081        }
2082        version = tres_getString(resLocID, -1, NULL, &resultLen, &err);
2083        if(u_strcmp(version, versionStr) != 0){
2084            char x[100];
2085            char g[100];
2086            u_austrcpy(x, versionStr);
2087            u_austrcpy(g, version);
2088            log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2089                    x, g);
2090        }
2091        zoneResource = ures_open(U_ICUDATA_ZONE, "no_NO_NY", &err);
2092        tResB = ures_getByKey(zoneResource, "zoneStrings", NULL, &err);
2093        if(err != U_USING_FALLBACK_WARNING){
2094            log_err("Expected U_USING_FALLBACK_ERROR when trying to test no_NO_NY aliased with nn_NO_NY for zoneStrings err=%s\n",u_errorName(err));
2095        }
2096        ures_close(tResB);
2097        ures_close(zoneResource);
2098        ures_close(resLocID);
2099        ures_close(myResB);
2100    }
2101
2102}
2103
2104/* static void printUChars(UChar* uchars){
2105/    int16_t i=0;
2106/    for(i=0; i<u_strlen(uchars); i++){
2107/        log_err("%04X ", *(uchars+i));
2108/    }
2109/ } */
2110
2111static void TestResourceLevelAliasing(void) {
2112    UErrorCode status = U_ZERO_ERROR;
2113    UResourceBundle *aliasB = NULL, *tb = NULL;
2114    UResourceBundle *en = NULL, *uk = NULL, *testtypes = NULL;
2115    const char* testdatapath = NULL;
2116    const UChar *string = NULL, *sequence = NULL;
2117    /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2118    int32_t strLen = 0, seqLen = 0;/*, binLen = 0, binSeqLen = 0;*/
2119    char buffer[100];
2120    char *s;
2121
2122    testdatapath=loadTestData(&status);
2123    if(U_FAILURE(status))
2124    {
2125        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2126        return;
2127    }
2128
2129    aliasB = ures_open(testdatapath, "testaliases", &status);
2130
2131    if(U_FAILURE(status))
2132    {
2133        log_data_err("Could not load testaliases.res %s \n",myErrorName(status));
2134        return;
2135    }
2136    /* this should fail - circular alias */
2137    tb = ures_getByKey(aliasB, "aaa", tb, &status);
2138    if(status != U_TOO_MANY_ALIASES_ERROR) {
2139        log_err("Failed to detect circular alias\n");
2140    }
2141    else {
2142        status = U_ZERO_ERROR;
2143    }
2144    tb = ures_getByKey(aliasB, "aab", tb, &status);
2145    if(status != U_TOO_MANY_ALIASES_ERROR) {
2146        log_err("Failed to detect circular alias\n");
2147    } else {
2148        status = U_ZERO_ERROR;
2149    }
2150    if(U_FAILURE(status) ) {
2151        log_data_err("err loading tb resource\n");
2152    }  else {
2153      /* testing aliasing to a non existing resource */
2154      tb = ures_getByKey(aliasB, "nonexisting", tb, &status);
2155      if(status != U_MISSING_RESOURCE_ERROR) {
2156        log_err("Managed to find an alias to non-existing resource\n");
2157      } else {
2158        status = U_ZERO_ERROR;
2159      }
2160      /* testing referencing/composed alias */
2161      uk = ures_findResource("ja/LocaleScript/2", uk, &status);
2162      if((uk == NULL) || U_FAILURE(status)) {
2163        log_err_status(status, "Couldn't findResource('ja/LocaleScript/2') err %s\n", u_errorName(status));
2164        goto cleanup;
2165      }
2166
2167      sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
2168
2169      tb = ures_getByKey(aliasB, "referencingalias", tb, &status);
2170      string = tres_getString(tb, -1, NULL, &strLen, &status);
2171
2172      if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2173        log_err("Referencing alias didn't get the right string (1)\n");
2174      }
2175
2176      string = tres_getString(aliasB, -1, "referencingalias", &strLen, &status);
2177      if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2178        log_err("Referencing alias didn't get the right string (2)\n");
2179      }
2180
2181      checkStatus(__LINE__, U_ZERO_ERROR, status);
2182      tb = ures_getByKey(aliasB, "LocaleScript", tb, &status);
2183      checkStatus(__LINE__, U_ZERO_ERROR, status);
2184      tb = ures_getByIndex(tb, 2, tb, &status);
2185      checkStatus(__LINE__, U_ZERO_ERROR, status);
2186      string = tres_getString(tb, -1, NULL, &strLen, &status);
2187      checkStatus(__LINE__, U_ZERO_ERROR, status);
2188
2189      if(U_FAILURE(status)) {
2190        log_err("%s trying to get string via separate getters\n", u_errorName(status));
2191      } else if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2192        log_err("Referencing alias didn't get the right string (3)\n");
2193      }
2194
2195
2196      {
2197            UResourceBundle* ja = ures_open(U_ICUDATA_BRKITR,"ja", &status);
2198            const UChar *got = NULL, *exp=NULL;
2199            int32_t gotLen = 0, expLen=0;
2200            ja = ures_getByKey(ja, "boundaries", ja, &status);
2201            exp = tres_getString(ja, -1, "word", &expLen, &status);
2202
2203            tb = ures_getByKey(aliasB, "boundaries", tb, &status);
2204            got = tres_getString(tb, -1, "word", &gotLen, &status);
2205
2206            if(U_FAILURE(status)) {
2207                log_err("%s trying to read str boundaries\n", u_errorName(status));
2208            } else if(gotLen != expLen || u_strncmp(exp, got, gotLen) != 0) {
2209                log_err("Referencing alias didn't get the right data\n");
2210            }
2211            ures_close(ja);
2212            status = U_ZERO_ERROR;
2213      }
2214      /* simple alias */
2215      testtypes = ures_open(testdatapath, "testtypes", &status);
2216      strcpy(buffer, "menu/file/open");
2217      s = buffer;
2218      uk = ures_findSubResource(testtypes, s, uk, &status);
2219      sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
2220
2221      tb = ures_getByKey(aliasB, "simplealias", tb, &status);
2222      string = tres_getString(tb, -1, NULL, &strLen, &status);
2223
2224      if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2225        log_err("Referencing alias didn't get the right string (4)\n");
2226      }
2227
2228      /* test indexed aliasing */
2229
2230      tb = ures_getByKey(aliasB, "zoneTests", tb, &status);
2231      tb = ures_getByKey(tb, "zoneAlias2", tb, &status);
2232      string = tres_getString(tb, -1, NULL, &strLen, &status);
2233
2234      en = ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en, &status);
2235      sequence = tres_getString(en, -1, NULL, &seqLen, &status);
2236
2237      if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2238        log_err("Referencing alias didn't get the right string (5)\n");
2239      }
2240    }
2241    /* test getting aliased string by index */
2242    {
2243        const char* keys[] = {
2244                "KeyAlias0PST",
2245                "KeyAlias1PacificStandardTime",
2246                "KeyAlias2PDT",
2247                "KeyAlias3LosAngeles"
2248        };
2249
2250        const char* strings[] = {
2251                "America/Los_Angeles",
2252                "Pacific Standard Time",
2253                "PDT",
2254                "Los Angeles",
2255        };
2256        UChar uBuffer[256];
2257        const UChar* result;
2258        int32_t uBufferLen = 0, resultLen = 0;
2259        int32_t i = 0;
2260        const char *key = NULL;
2261        tb = ures_getByKey(aliasB, "testGetStringByKeyAliasing", tb, &status);
2262        if(U_FAILURE(status)) {
2263          log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status));
2264        } else {
2265            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2266                result = tres_getString(tb, -1, keys[i], &resultLen, &status);
2267                if(U_FAILURE(status)){
2268                    log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2269                    continue;
2270                }
2271                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2272                if(resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2273                  log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys[i]);
2274                }
2275            }
2276            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2277                result = tres_getString(tb, i, NULL, &resultLen, &status);
2278                if(U_FAILURE(status)){
2279                    log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2280                    continue;
2281                }
2282                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2283                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2284                  log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings[i]);
2285                }
2286            }
2287            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2288                result = ures_getNextString(tb, &resultLen, &key, &status);
2289                if(U_FAILURE(status)){
2290                    log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2291                    continue;
2292                }
2293                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2294                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2295                  log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings[i]);
2296                }
2297            }
2298        }
2299        tb = ures_getByKey(aliasB, "testGetStringByIndexAliasing", tb, &status);
2300        if(U_FAILURE(status)) {
2301          log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status));
2302        } else {
2303            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2304                result = tres_getString(tb, i, NULL, &resultLen, &status);
2305                if(U_FAILURE(status)){
2306                    log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2307                    continue;
2308                }
2309                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2310                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2311                  log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings[i]);
2312                }
2313            }
2314            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2315                result = ures_getNextString(tb, &resultLen, &key, &status);
2316                if(U_FAILURE(status)){
2317                    log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2318                    continue;
2319                }
2320                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2321                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2322                  log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings[i]);
2323                }
2324            }
2325        }
2326    }
2327    tb = ures_getByKey(aliasB, "testAliasToTree", tb, &status);
2328    if(U_FAILURE(status)){
2329        log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status));
2330        goto cleanup;
2331    }
2332    if (strcmp(ures_getKey(tb), "collations") != 0) {
2333        log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb));
2334    }
2335cleanup:
2336    ures_close(aliasB);
2337    ures_close(tb);
2338    ures_close(en);
2339    ures_close(uk);
2340    ures_close(testtypes);
2341}
2342
2343static void TestDirectAccess(void) {
2344    UErrorCode status = U_ZERO_ERROR;
2345    UResourceBundle *t = NULL, *t2 = NULL;
2346    const char* key = NULL;
2347
2348    char buffer[100];
2349    char *s;
2350    /*const char* testdatapath=loadTestData(&status);
2351    if(U_FAILURE(status)){
2352        log_err("Could not load testdata.dat %s \n",myErrorName(status));
2353        return;
2354    }*/
2355
2356    t = ures_findResource("/testdata/te/zoneStrings/3/2", t, &status);
2357    if(U_FAILURE(status)) {
2358        log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
2359        status = U_ZERO_ERROR;
2360    } else {
2361        key = ures_getKey(t);
2362        if(key != NULL) {
2363            log_err("Got a strange key, expected NULL, got %s\n", key);
2364        }
2365    }
2366    t = ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t, &status);
2367    if(U_FAILURE(status)) {
2368        log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
2369        status = U_ZERO_ERROR;
2370    } else {
2371        key = ures_getKey(t);
2372        if(key != NULL) {
2373            log_err("Got a strange key, expected NULL, got %s\n", key);
2374        }
2375    }
2376
2377    t = ures_findResource("ja/LocaleScript", t, &status);
2378    if(U_FAILURE(status)) {
2379        log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
2380        status = U_ZERO_ERROR;
2381    } else {
2382        key = ures_getKey(t);
2383        if(strcmp(key, "LocaleScript")!=0) {
2384            log_err("Got a strange key, expected 'LocaleScript', got %s\n", key);
2385        }
2386    }
2387
2388    t2 = ures_open(U_ICUDATA_LANG, "sr", &status);
2389    if(U_FAILURE(status)) {
2390        log_err_status(status, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status));
2391        log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2392                     "You probably have no data! Aborting this test\n");
2393    }
2394
2395    if(U_SUCCESS(status)) {
2396        strcpy(buffer, "Languages/hr");
2397        s = buffer;
2398        t = ures_findSubResource(t2, s, t, &status);
2399        if(U_FAILURE(status)) {
2400            log_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
2401            status = U_ZERO_ERROR;
2402        } else {
2403            key = ures_getKey(t);
2404            if(strcmp(key, "hr")!=0) {
2405                log_err("Got a strange key, expected 'hr', got %s\n", key);
2406            }
2407        }
2408    }
2409
2410    t = ures_findResource("root/calendar/islamic-civil/DateTime", t, &status);
2411    if(U_SUCCESS(status)) {
2412        log_data_err("This resource does not exist. How did it get here?\n");
2413    }
2414    status = U_ZERO_ERROR;
2415
2416    /* this one will freeze */
2417    t = ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t, &status);
2418    if(U_SUCCESS(status)) {
2419        log_data_err("Second resource does not exist. How did it get here?\n");
2420    }
2421    status = U_ZERO_ERROR;
2422
2423    ures_close(t2);
2424    t2 = ures_open(NULL, "he", &status);
2425    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2426    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2427    t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
2428    if(U_SUCCESS(status)) {
2429        log_err("This resource does not exist. How did it get here?\n");
2430    }
2431    status = U_ZERO_ERROR;
2432
2433    ures_close(t2);
2434    t2 = ures_open(NULL, "he", &status);
2435    /* George's fix */
2436    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2437    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2438    t2 = ures_getByKeyWithFallback(t2, "eras", t2, &status);
2439    if(U_FAILURE(status)) {
2440        log_err_status(status, "Didn't get Eras. I know they are there!\n");
2441    }
2442    status = U_ZERO_ERROR;
2443
2444    ures_close(t2);
2445    t2 = ures_open(NULL, "root", &status);
2446    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2447    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2448    t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
2449    if(U_SUCCESS(status)) {
2450        log_err("This resource does not exist. How did it get here?\n");
2451    }
2452    status = U_ZERO_ERROR;
2453
2454    ures_close(t2);
2455    ures_close(t);
2456}
2457
2458static void TestJB3763(void) {
2459    /* Nasty bug prevented using parent as fill-in, since it would
2460     * stomp the path information.
2461     */
2462    UResourceBundle *t = NULL;
2463    UErrorCode status = U_ZERO_ERROR;
2464    t = ures_open(NULL, "sr_Latn", &status);
2465    t = ures_getByKeyWithFallback(t, "calendar", t, &status);
2466    t = ures_getByKeyWithFallback(t, "gregorian", t, &status);
2467    t = ures_getByKeyWithFallback(t, "AmPmMarkers", t, &status);
2468    if(U_FAILURE(status)) {
2469        log_err_status(status, "This resource should be available?\n");
2470    }
2471    status = U_ZERO_ERROR;
2472
2473    ures_close(t);
2474
2475}
2476
2477static void TestGetKeywordValues(void) {
2478    UEnumeration *kwVals;
2479    UBool foundStandard = FALSE;
2480    UErrorCode status = U_ZERO_ERROR;
2481    const char *kw;
2482#if !UCONFIG_NO_COLLATION
2483    kwVals = ures_getKeywordValues( U_ICUDATA_COLL, "collations", &status);
2484
2485    log_verbose("Testing getting collation keyword values:\n");
2486
2487    while((kw=uenum_next(kwVals, NULL, &status))) {
2488        log_verbose("  %s\n", kw);
2489        if(!strcmp(kw,"standard")) {
2490            if(foundStandard == FALSE) {
2491                foundStandard = TRUE;
2492            } else {
2493                log_err("'standard' was found twice in the keyword list.\n");
2494            }
2495        }
2496    }
2497    if(foundStandard == FALSE) {
2498        log_err_status(status, "'standard' was not found in the keyword list.\n");
2499    }
2500    uenum_close(kwVals);
2501    if(U_FAILURE(status)) {
2502        log_err_status(status, "err %s getting collation values\n", u_errorName(status));
2503    }
2504    status = U_ZERO_ERROR;
2505#endif
2506    foundStandard = FALSE;
2507    kwVals = ures_getKeywordValues( "ICUDATA", "calendar", &status);
2508
2509    log_verbose("Testing getting calendar keyword values:\n");
2510
2511    while((kw=uenum_next(kwVals, NULL, &status))) {
2512        log_verbose("  %s\n", kw);
2513        if(!strcmp(kw,"japanese")) {
2514            if(foundStandard == FALSE) {
2515                foundStandard = TRUE;
2516            } else {
2517                log_err("'japanese' was found twice in the calendar keyword list.\n");
2518            }
2519        }
2520    }
2521    if(foundStandard == FALSE) {
2522        log_err_status(status, "'japanese' was not found in the calendar keyword list.\n");
2523    }
2524    uenum_close(kwVals);
2525    if(U_FAILURE(status)) {
2526        log_err_status(status, "err %s getting calendar values\n", u_errorName(status));
2527    }
2528}
2529
2530static void TestGetFunctionalEquivalentOf(const char *path, const char *resName, const char *keyword, UBool truncate, const char * const testCases[]) {
2531    int32_t i;
2532    for(i=0;testCases[i];i+=3) {
2533        UBool expectAvail = (testCases[i][0]=='t')?TRUE:FALSE;
2534        UBool gotAvail = FALSE;
2535        const char *inLocale = testCases[i+1];
2536        const char *expectLocale = testCases[i+2];
2537        char equivLocale[256];
2538        int32_t len;
2539        UErrorCode status = U_ZERO_ERROR;
2540        log_verbose("%d:   %c      %s\texpect %s\n",i/3,  expectAvail?'t':'f', inLocale, expectLocale);
2541        len = ures_getFunctionalEquivalent(equivLocale, 255, path,
2542            resName, keyword, inLocale,
2543            &gotAvail, truncate, &status);
2544        if(U_FAILURE(status) || (len <= 0)) {
2545            log_err_status(status, "FAIL: got len %d, err %s  on #%d: %c\t%s\t%s\n",
2546                len, u_errorName(status),
2547                i/3,expectAvail?'t':'f', inLocale, expectLocale);
2548        } else {
2549            log_verbose("got:  %c   %s\n", expectAvail?'t':'f',equivLocale);
2550
2551            if((gotAvail != expectAvail) || strcmp(equivLocale, expectLocale)) {
2552                log_err("FAIL: got avail=%c, loc=%s but  expected #%d: %c\t%s\t-> loc=%s\n",
2553                    gotAvail?'t':'f', equivLocale,
2554                    i/3,
2555                    expectAvail?'t':'f', inLocale, expectLocale);
2556
2557            }
2558        }
2559    }
2560}
2561
2562static void TestGetFunctionalEquivalent(void) {
2563
2564    /* BEGIN android-changed
2565       To save space, Android does not build full CJK collation tables.
2566       We skip the tests for big5han, gb2312* and unihan. */
2567    static const char * const collCases[] = {
2568        /*   avail   locale          equiv   */
2569        "f",    "de_US_CALIFORNIA",               "de",
2570        "f",    "zh_TW@collation=stroke",         "zh@collation=stroke", /* alias of zh_Hant_TW */
2571        "t",    "zh_Hant_TW@collation=stroke",    "zh@collation=stroke",
2572        "f",    "de_CN@collation=pinyin",         "de",
2573        "t",    "zh@collation=pinyin",            "zh",
2574        "f",    "zh_CN@collation=pinyin",         "zh", /* alias of zh_Hans_CN */
2575        "t",    "zh_Hans_CN@collation=pinyin",    "zh",
2576        "f",    "zh_HK@collation=pinyin",         "zh", /* alias of zh_Hant_HK */
2577        "t",    "zh_Hant_HK@collation=pinyin",    "zh",
2578        "f",    "zh_HK@collation=stroke",         "zh@collation=stroke", /* alias of zh_Hant_HK */
2579        "t",    "zh_Hant_HK@collation=stroke",    "zh@collation=stroke",
2580        "f",    "zh_HK",                          "zh@collation=stroke", /* alias of zh_Hant_HK */
2581        "t",    "zh_Hant_HK",                     "zh@collation=stroke",
2582        "f",    "zh_MO",                          "zh@collation=stroke", /* alias of zh_Hant_MO */
2583        "t",    "zh_Hant_MO",                     "zh@collation=stroke",
2584        "f",    "zh_TW_STROKE",                   "zh@collation=stroke",
2585        /* "f",    "zh_TW_STROKE@collation=big5han", "zh@collation=big5han", */
2586        "f",    "de_CN@calendar=japanese",        "de",
2587        "t",    "de@calendar=japanese",           "de",
2588        /* "f",    "zh_TW@collation=big5han",        "zh@collation=big5han", */ /* alias of zh_Hant_TW */
2589        /* "t",    "zh_Hant_TW@collation=big5han",   "zh@collation=big5han", */
2590        /* "f",    "zh_TW@collation=gb2312han",      "zh@collation=gb2312han", */ /* alias of zh_Hant_TW */
2591        /* "t",    "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han", */
2592        /* "f",    "zh_CN@collation=big5han",        "zh@collation=big5han", *//* alias of zh_Hans_CN */
2593        /* "t",    "zh_Hans_CN@collation=big5han",   "zh@collation=big5han", */
2594        /* "f",    "zh_CN@collation=gb2312han",      "zh@collation=gb2312han", */ /* alias of zh_Hans_CN */
2595        /* "t",    "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han", */
2596        /* "t",    "zh@collation=big5han",           "zh@collation=big5han", */
2597        /* "t",    "zh@collation=gb2312han",         "zh@collation=gb2312han", */
2598        "t",    "hi_IN@collation=direct",         "hi@collation=direct",
2599        "t",    "hi@collation=standard",          "hi",
2600        "t",    "hi@collation=direct",            "hi@collation=direct",
2601        "f",    "hi_AU@collation=direct;currency=CHF;calendar=buddhist",      "hi@collation=direct",
2602        "f",    "hi_AU@collation=standard;currency=CHF;calendar=buddhist",    "hi",
2603        "t",    "de_DE@collation=pinyin",         "de", /* bug 4582 tests */
2604        "f",    "de_DE_BONN@collation=pinyin",    "de",
2605        "t",    "nl",                             "root",
2606        "t",    "nl_NL",                          "root",
2607        "f",    "nl_NL_EEXT",                     "root",
2608        "t",    "nl@collation=stroke",            "root",
2609        "t",    "nl_NL@collation=stroke",         "root",
2610        "f",    "nl_NL_EEXT@collation=stroke",    "root",
2611        NULL
2612    };
2613    /* END android-changed */
2614
2615    static const char *calCases[] = {
2616        /*   avail   locale                       equiv   */
2617        "t",    "en_US_POSIX",                   "en@calendar=gregorian",
2618        "f",    "ja_JP_TOKYO",                   "ja@calendar=gregorian",
2619        "f",    "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2620        "t",    "sr@calendar=gregorian", "sr@calendar=gregorian",
2621        "t",    "en", "en@calendar=gregorian",
2622        NULL
2623    };
2624
2625#if !UCONFIG_NO_COLLATION
2626    TestGetFunctionalEquivalentOf(U_ICUDATA_COLL, "collations", "collation", TRUE, collCases);
2627#endif
2628    TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE, calCases);
2629
2630#if !UCONFIG_NO_COLLATION
2631    log_verbose("Testing error conditions:\n");
2632    {
2633        char equivLocale[256] = "???";
2634        int32_t len;
2635        UErrorCode status = U_ZERO_ERROR;
2636        UBool gotAvail = FALSE;
2637
2638        len = ures_getFunctionalEquivalent(equivLocale, 255, U_ICUDATA_COLL,
2639            "calendar", "calendar", "ar_EG@calendar=islamic",
2640            &gotAvail, FALSE, &status);
2641
2642        if(status == U_MISSING_RESOURCE_ERROR) {
2643            log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2644        } else {
2645            log_err("ures_getFunctionalEquivalent  returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2646                equivLocale, gotAvail?'t':'f', u_errorName(status));
2647        }
2648    }
2649#endif
2650}
2651
2652static void TestXPath(void) {
2653    UErrorCode status = U_ZERO_ERROR;
2654    UResourceBundle *rb = NULL, *alias = NULL;
2655    int32_t len = 0;
2656    const UChar* result = NULL;
2657    const UChar expResult[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2658    /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2659
2660    const char *testdatapath=loadTestData(&status);
2661    if(U_FAILURE(status))
2662    {
2663        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2664        return;
2665    }
2666
2667    log_verbose("Testing ures_open()......\n");
2668
2669    rb = ures_open(testdatapath, "te_IN", &status);
2670    if(U_FAILURE(status)) {
2671      log_err("Could not open te_IN (%s)\n", myErrorName(status));
2672      return;
2673    }
2674    alias = ures_getByKey(rb, "rootAliasClient", alias, &status);
2675    if(U_FAILURE(status)) {
2676      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2677      ures_close(rb);
2678      return;
2679    }
2680
2681    result = tres_getString(alias, -1, NULL, &len, &status);
2682    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2683      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2684    }
2685
2686    alias = ures_getByKey(rb, "aliasClient", alias, &status);
2687    if(U_FAILURE(status)) {
2688      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2689      ures_close(rb);
2690      return;
2691    }
2692
2693    result = tres_getString(alias, -1, NULL, &len, &status);
2694    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2695      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2696    }
2697
2698    alias = ures_getByKey(rb, "nestedRootAliasClient", alias, &status);
2699    if(U_FAILURE(status)) {
2700      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2701      ures_close(rb);
2702      return;
2703    }
2704
2705    result = tres_getString(alias, -1, NULL, &len, &status);
2706    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2707      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2708    }
2709
2710    ures_close(alias);
2711    ures_close(rb);
2712}
2713static void TestCLDRStyleAliases(void) {
2714    UErrorCode status = U_ZERO_ERROR;
2715    UResourceBundle *rb = NULL, *alias = NULL, *a=NULL;
2716    int32_t i, len;
2717    char resource[256];
2718    const UChar *result = NULL;
2719    UChar expected[256];
2720    const char *expects[7] = { "", "a41", "a12", "a03", "ar4" };
2721    const char *testdatapath=loadTestData(&status);
2722    if(U_FAILURE(status)) {
2723        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2724        return;
2725    }
2726    log_verbose("Testing CLDR style aliases......\n");
2727
2728    rb = ures_open(testdatapath, "te_IN_REVISED", &status);
2729    if(U_FAILURE(status)) {
2730      log_err("Could not open te_IN (%s)\n", myErrorName(status));
2731      return;
2732    }
2733    alias = ures_getByKey(rb, "a", alias, &status);
2734    if(U_FAILURE(status)) {
2735      log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status));
2736      ures_close(rb);
2737      return;
2738    }
2739    for(i = 1; i < 5 ; i++) {
2740      resource[0]='a';
2741      resource[1]='0'+i;
2742      resource[2]=0;
2743      /* instead of sprintf(resource, "a%i", i); */
2744      a = ures_getByKeyWithFallback(alias, resource, a, &status);
2745      result = tres_getString(a, -1, NULL, &len, &status);
2746      u_charsToUChars(expects[i], expected, strlen(expects[i])+1);
2747      if(U_FAILURE(status) || !result || u_strcmp(result, expected)) {
2748        log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource, expects[i], result, myErrorName(status));
2749        status = U_ZERO_ERROR;
2750      }
2751    }
2752
2753  ures_close(a);
2754  ures_close(alias);
2755  ures_close(rb);
2756}
2757
2758static void TestFallbackCodes(void) {
2759  UErrorCode status = U_ZERO_ERROR;
2760  const char *testdatapath=loadTestData(&status);
2761
2762  UResourceBundle *res = ures_open(testdatapath, "te_IN", &status);
2763
2764  UResourceBundle *r = NULL, *fall = NULL;
2765
2766  r = ures_getByKey(res, "tagged_array_in_Root_te_te_IN", r, &status);
2767
2768  status = U_ZERO_ERROR;
2769  fall = ures_getByKeyWithFallback(r, "tag2", fall, &status);
2770
2771  if(status != U_ZERO_ERROR) {
2772    log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status));
2773    status = U_ZERO_ERROR;
2774  }
2775
2776  fall = ures_getByKeyWithFallback(r, "tag7", fall, &status);
2777
2778  if(status != U_USING_FALLBACK_WARNING) {
2779    log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status));
2780  }
2781  status = U_ZERO_ERROR;
2782
2783  fall = ures_getByKeyWithFallback(r, "tag1", fall, &status);
2784
2785  if(status != U_USING_DEFAULT_WARNING) {
2786    log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status));
2787  }
2788  status = U_ZERO_ERROR;
2789
2790  ures_close(fall);
2791  ures_close(r);
2792  ures_close(res);
2793}
2794
2795/* This test will crash if this doesn't work. Results don't need testing. */
2796static void TestStackReuse(void) {
2797    UResourceBundle table;
2798    UErrorCode errorCode = U_ZERO_ERROR;
2799    UResourceBundle *rb = ures_open(NULL, "en_US", &errorCode);
2800
2801    if(U_FAILURE(errorCode)) {
2802        log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode));
2803        return;
2804    }
2805    ures_initStackObject(&table);
2806    ures_getByKeyWithFallback(rb, "Types", &table, &errorCode);
2807    ures_getByKeyWithFallback(&table, "collation", &table, &errorCode);
2808    ures_close(rb);
2809    ures_close(&table);
2810}
2811
2812/* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2813
2814/*
2815 * Replace most ures_getStringXYZ() with this function which wraps the
2816 * desired call and also calls the UTF-8 variant and checks that it works.
2817 */
2818extern const UChar *
2819tres_getString(const UResourceBundle *resB,
2820               int32_t index, const char *key,
2821               int32_t *length,
2822               UErrorCode *status) {
2823    char buffer8[16];
2824    char *p8;
2825    const UChar *s16;
2826    const char *s8;
2827    UChar32 c16, c8;
2828    int32_t length16, length8, i16, i8;
2829    UBool forceCopy;
2830
2831    if(length == NULL) {
2832        length = &length16;
2833    }
2834    if(index >= 0) {
2835        s16 = ures_getStringByIndex(resB, index, length, status);
2836    } else if(key != NULL) {
2837        s16 = ures_getStringByKey(resB, key, length, status);
2838    } else {
2839        s16 = ures_getString(resB, length, status);
2840    }
2841    if(U_FAILURE(*status)) {
2842        return s16;
2843    }
2844    length16 = *length;
2845
2846    /* try the UTF-8 variant of ures_getStringXYZ() */
2847    for(forceCopy = FALSE; forceCopy <= TRUE; ++forceCopy) {
2848        p8 = buffer8;
2849        length8 = (int32_t)sizeof(buffer8);
2850        if(index >= 0) {
2851            s8 = ures_getUTF8StringByIndex(resB, index, p8, &length8, forceCopy, status);
2852        } else if(key != NULL) {
2853            s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2854        } else {
2855            s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2856        }
2857        if(*status == U_INVALID_CHAR_FOUND) {
2858            /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2859            return s16;
2860        }
2861        if(*status == U_BUFFER_OVERFLOW_ERROR) {
2862            *status = U_ZERO_ERROR;
2863            p8 = (char *)malloc(++length8);
2864            if(p8 == NULL) {
2865                return s16;
2866            }
2867            if(index >= 0) {
2868                s8 = ures_getUTF8StringByIndex(resB, index, p8, &length8, forceCopy, status);
2869            } else if(key != NULL) {
2870                s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2871            } else {
2872                s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2873            }
2874        }
2875        if(U_FAILURE(*status)) {
2876            /* something unexpected happened */
2877            if(p8 != buffer8) {
2878                free(p8);
2879            }
2880            return s16;
2881        }
2882
2883        if(forceCopy && s8 != p8) {
2884            log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
2885                    resB, (long)index, key);
2886        }
2887
2888        /* verify NUL-termination */
2889        if((p8 != buffer8 || length8 < sizeof(buffer8)) && s8[length8] != 0) {
2890            log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
2891                    resB, (long)index, key);
2892        }
2893        /* verify correct string */
2894        i16 = i8 = 0;
2895        while(i16 < length16 && i8 < length8) {
2896            U16_NEXT(s16, i16, length16, c16);
2897            U8_NEXT(s8, i8, length8, c8);
2898            if(c16 != c8) {
2899                log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
2900                        resB, (long)index, key, (long)c16, (long)c8, (long)i16);
2901            }
2902        }
2903        /* verify correct length */
2904        if(i16 < length16) {
2905            log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
2906                    resB, (long)index, key, (long)length8, (long)length16);
2907        }
2908        if(i8 < length8) {
2909            log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
2910                    resB, (long)index, key, (long)length8, (long)length16);
2911        }
2912
2913        /* clean up */
2914        if(p8 != buffer8) {
2915            free(p8);
2916        }
2917    }
2918    return s16;
2919}
2920
2921/*
2922 * API tests for ures_getUTF8String().
2923 * Most cases are handled by tres_getString(), which leaves argument checking
2924 * to be tested here.
2925 * Since the variants share most of their implementation, we only need to test
2926 * one of them.
2927 * We also need not test for checking arguments which will be checked by the
2928 * UTF-16 ures_getStringXYZ() that are called internally.
2929 */
2930static void
2931TestGetUTF8String() {
2932    UResourceBundle *res;
2933    const char *testdatapath;
2934    char buffer8[16];
2935    const char *s8;
2936    int32_t length8;
2937    UErrorCode status;
2938
2939    status = U_ZERO_ERROR;
2940    testdatapath = loadTestData(&status);
2941    if(U_FAILURE(status)) {
2942        log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
2943        return;
2944    }
2945
2946    res = ures_open(testdatapath, "", &status);
2947    if(U_FAILURE(status)) {
2948        log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status));
2949        return;
2950    }
2951
2952    /* one good call */
2953    status = U_ZERO_ERROR;
2954    length8 = (int32_t)sizeof(buffer8);
2955    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
2956    if(status != U_ZERO_ERROR) {
2957        log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status));
2958    }
2959
2960    /* negative capacity */
2961    status = U_ZERO_ERROR;
2962    length8 = -1;
2963    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
2964    if(status != U_ILLEGAL_ARGUMENT_ERROR) {
2965        log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status));
2966    }
2967
2968    /* capacity>0 but dest=NULL */
2969    status = U_ZERO_ERROR;
2970    length8 = (int32_t)sizeof(buffer8);
2971    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", NULL, &length8, FALSE, &status);
2972    if(status != U_ILLEGAL_ARGUMENT_ERROR) {
2973        log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status));
2974    }
2975
2976    ures_close(res);
2977}
2978
2979static void TestCLDRVersion(void) {
2980  UVersionInfo zeroVersion;
2981  UVersionInfo testExpect;
2982  UVersionInfo testCurrent;
2983  UVersionInfo cldrVersion;
2984  char tmp[200];
2985  UErrorCode status = U_ZERO_ERROR;
2986
2987  /* setup the constant value */
2988  u_versionFromString(zeroVersion, "0.0.0.0");
2989
2990  /* test CLDR value from API */
2991  ulocdata_getCLDRVersion(cldrVersion, &status);
2992  if(U_FAILURE(status)) {
2993    /* the show is pretty much over at this point */
2994    log_err_status(status, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status));
2995    return;
2996  } else {
2997    u_versionToString(cldrVersion, tmp);
2998    log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp);
2999  }
3000
3001
3002  /* setup from resource bundle */
3003  {
3004    UResourceBundle *res;
3005    const char *testdatapath;
3006
3007    status = U_ZERO_ERROR;
3008    testdatapath = loadTestData(&status);
3009    if(U_FAILURE(status)) {
3010        log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
3011        return;
3012    }
3013
3014    res = ures_openDirect(testdatapath, "root", &status);
3015    if(U_FAILURE(status)) {
3016        log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3017));
3018        return;
3019    }
3020    ures_getVersionByKey(res, "ExpectCLDRVersionAtLeast", testExpect, &status);
3021    ures_getVersionByKey(res, "CurrentCLDRVersion", testCurrent, &status);
3022    ures_close(res);
3023    if(U_FAILURE(status)) {
3024        log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status));
3025    }
3026  }
3027  if(U_FAILURE(status)) return;
3028
3029
3030  u_versionToString(testExpect,tmp);
3031  log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp);
3032  if(memcmp(cldrVersion, testExpect, sizeof(UVersionInfo)) < 0) {
3033    log_data_err("CLDR version is too old, expect at least %s.", tmp);
3034  }
3035  u_versionToString(testCurrent,tmp);
3036  log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp);
3037  switch(memcmp(cldrVersion, testCurrent, sizeof(UVersionInfo))) {
3038    case 0: break; /* OK- current. */
3039    case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3040    case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3041  }
3042
3043}
3044