1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2013, 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, &TestTicket9804,            "tsutil/creststn/TestTicket9804");
206    addTest(root, &TestXPath,                 "tsutil/creststn/TestXPath");
207    addTest(root, &TestCLDRStyleAliases,      "tsutil/creststn/TestCLDRStyleAliases");
208    addTest(root, &TestFallbackCodes,         "tsutil/creststn/TestFallbackCodes");
209    addTest(root, &TestGetUTF8String,         "tsutil/creststn/TestGetUTF8String");
210    addTest(root, &TestCLDRVersion,           "tsutil/creststn/TestCLDRVersion");
211#endif
212    addTest(root, &TestFallback,              "tsutil/creststn/TestFallback");
213    addTest(root, &TestGetVersion,            "tsutil/creststn/TestGetVersion");
214    addTest(root, &TestGetVersionColl,        "tsutil/creststn/TestGetVersionColl");
215    addTest(root, &TestAliasConflict,         "tsutil/creststn/TestAliasConflict");
216    addTest(root, &TestGetKeywordValues,      "tsutil/creststn/TestGetKeywordValues");
217    addTest(root, &TestGetFunctionalEquivalent,"tsutil/creststn/TestGetFunctionalEquivalent");
218    addTest(root, &TestJB3763,                "tsutil/creststn/TestJB3763");
219    addTest(root, &TestStackReuse,            "tsutil/creststn/TestStackReuse");
220}
221
222
223/***************************************************************************************/
224static const char* norwayNames[] = {
225    "no_NO_NY",
226    "no_NO",
227    "no",
228    "nn_NO",
229    "nn",
230    "nb_NO",
231    "nb"
232};
233
234static const char* norwayLocales[] = {
235    "nn_NO",
236    "nb_NO",
237    "nb",
238    "nn_NO",
239    "nn",
240    "nb_NO",
241    "nb"
242};
243
244static void checkStatus(int32_t line, UErrorCode expected, UErrorCode status) {
245  if(U_FAILURE(status)) {
246    log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__, line);
247  }
248  if(status != expected) {
249    log_err_status(status, "%s:%d: Expected error code %s, got error code %s\n", __FILE__, line, u_errorName(expected), u_errorName(status));
250  }
251}
252
253static void TestErrorCodes(void) {
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 - but CldrBug:3058 is WONTFIX */
285  if(U_SUCCESS(status) && r != NULL && isICUVersionAtLeast(52, 0, 1)) {
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          log_data_err("Could not load UCARules for locale %s\n", locName);
1444          continue;
1445        }
1446        if(u_strlen(rules) != len){
1447            log_err("UCARules string not nul terminated! \n");
1448        }
1449        ures_getVersion(resB, versionArray);
1450        for (i=0; i<4; ++i) {
1451            if (versionArray[i] < minVersionArray[i] ||
1452                versionArray[i] > maxVersionArray[i])
1453            {
1454                log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1455                    locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
1456                break;
1457            }
1458        }
1459        ures_close(resB);
1460    } while((locName = uenum_next(locs,&locLen,&status))&&U_SUCCESS(status));
1461
1462    if(U_FAILURE(status)) {
1463        log_err("Err %s testing Collation locales.\n", u_errorName(status));
1464    }
1465    uenum_close(locs);
1466}
1467
1468static void TestResourceBundles()
1469{
1470    UErrorCode status = U_ZERO_ERROR;
1471    loadTestData(&status);
1472    if(U_FAILURE(status)) {
1473        log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
1474        return;
1475    }
1476
1477    testTag("only_in_Root", TRUE, FALSE, FALSE);
1478    testTag("in_Root_te", TRUE, TRUE, FALSE);
1479    testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
1480    testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
1481    testTag("only_in_te", FALSE, TRUE, FALSE);
1482    testTag("only_in_te_IN", FALSE, FALSE, TRUE);
1483    testTag("in_te_te_IN", FALSE, TRUE, TRUE);
1484    testTag("nonexistent", FALSE, FALSE, FALSE);
1485
1486    log_verbose("Passed:=  %d   Failed=   %d \n", pass, fail);
1487
1488}
1489
1490
1491static void TestConstruction1()
1492{
1493    UResourceBundle *test1 = 0, *test2 = 0,*empty = 0;
1494    const UChar *result1, *result2;
1495    UErrorCode status= U_ZERO_ERROR;
1496    UErrorCode   err = U_ZERO_ERROR;
1497    const char*      locale="te_IN";
1498    const char* testdatapath;
1499
1500    int32_t len1=0;
1501    int32_t len2=0;
1502    UVersionInfo versionInfo;
1503    char versionString[256];
1504    char verboseOutput[256];
1505
1506    U_STRING_DECL(rootVal, "ROOT", 4);
1507    U_STRING_DECL(te_inVal, "TE_IN", 5);
1508
1509    U_STRING_INIT(rootVal, "ROOT", 4);
1510    U_STRING_INIT(te_inVal, "TE_IN", 5);
1511
1512    testdatapath=loadTestData(&status);
1513    if(U_FAILURE(status))
1514    {
1515        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1516        return;
1517    }
1518
1519    log_verbose("Testing ures_open()......\n");
1520
1521    empty = ures_open(testdatapath, "testempty", &status);
1522    if(empty == NULL || U_FAILURE(status)) {
1523        log_err("opening empty failed!\n");
1524    }
1525    ures_close(empty);
1526
1527    test1=ures_open(testdatapath, NULL, &err);
1528
1529    if(U_FAILURE(err))
1530    {
1531        log_err("construction of NULL did not succeed :  %s \n", myErrorName(status));
1532        return;
1533    }
1534    test2=ures_open(testdatapath, locale, &err);
1535    if(U_FAILURE(err))
1536    {
1537        log_err("construction of %s did not succeed :  %s \n", locale, myErrorName(status));
1538        return;
1539    }
1540    result1= tres_getString(test1, -1, "string_in_Root_te_te_IN", &len1, &err);
1541    result2= tres_getString(test2, -1, "string_in_Root_te_te_IN", &len2, &err);
1542    if (U_FAILURE(err) || len1==0 || len2==0) {
1543        log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status));
1544        return;
1545    }
1546    log_verbose("for string_in_Root_te_te_IN, default.txt had  %s\n", u_austrcpy(verboseOutput, result1));
1547    log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput, result2));
1548    if(u_strcmp(result1, rootVal) !=0  || u_strcmp(result2, te_inVal) !=0 ){
1549        log_err("construction test failed. Run Verbose for more information");
1550    }
1551
1552
1553    /* Test getVersionNumber*/
1554    log_verbose("Testing version number\n");
1555    log_verbose("for getVersionNumber :  %s\n", ures_getVersionNumber(test1));
1556
1557    log_verbose("Testing version \n");
1558    ures_getVersion(test1, versionInfo);
1559    u_versionToString(versionInfo, versionString);
1560
1561    log_verbose("for getVersion :  %s\n", versionString);
1562
1563    if(strcmp(versionString, ures_getVersionNumber(test1)) != 0) {
1564        log_err("Versions differ: %s vs %s\n", versionString, ures_getVersionNumber(test1));
1565    }
1566
1567    ures_close(test1);
1568    ures_close(test2);
1569
1570}
1571
1572/*****************************************************************************/
1573/*****************************************************************************/
1574
1575static UBool testTag(const char* frag,
1576           UBool in_Root,
1577           UBool in_te,
1578           UBool in_te_IN)
1579{
1580    int32_t failNum = fail;
1581
1582    /* Make array from input params */
1583
1584    UBool is_in[3];
1585    const char *NAME[] = { "ROOT", "TE", "TE_IN" };
1586
1587    /* Now try to load the desired items */
1588    UResourceBundle* theBundle = NULL;
1589    char tag[99];
1590    char action[256];
1591    UErrorCode expected_status,status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
1592    UChar* base = NULL;
1593    UChar* expected_string = NULL;
1594    const UChar* string = NULL;
1595    char buf[5];
1596    char item_tag[10];
1597    int32_t i,j,row,col, len;
1598    int32_t actual_bundle;
1599    int32_t count = 0;
1600    int32_t row_count=0;
1601    int32_t column_count=0;
1602    int32_t idx = 0;
1603    int32_t tag_count= 0;
1604    const char* testdatapath;
1605    char verboseOutput[256];
1606    UResourceBundle* array=NULL;
1607    UResourceBundle* array2d=NULL;
1608    UResourceBundle* tags=NULL;
1609    UResourceBundle* arrayItem1=NULL;
1610
1611    testdatapath = loadTestData(&status);
1612    if(U_FAILURE(status))
1613    {
1614        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
1615        return FALSE;
1616    }
1617
1618    is_in[0] = in_Root;
1619    is_in[1] = in_te;
1620    is_in[2] = in_te_IN;
1621
1622    strcpy(item_tag, "tag");
1623
1624    for (i=0; i<bundles_count; ++i)
1625    {
1626        strcpy(action,"construction for ");
1627        strcat(action, param[i].name);
1628
1629
1630        status = U_ZERO_ERROR;
1631
1632        theBundle = ures_open(testdatapath, param[i].name, &status);
1633        CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
1634
1635        if(i == 5)
1636            actual_bundle = 0; /* ne -> default */
1637        else if(i == 3)
1638            actual_bundle = 1; /* te_NE -> te */
1639        else if(i == 4)
1640            actual_bundle = 2; /* te_IN_NE -> te_IN */
1641        else
1642            actual_bundle = i;
1643
1644        expected_resource_status = U_MISSING_RESOURCE_ERROR;
1645        for (j=e_te_IN; j>=e_Root; --j)
1646        {
1647            if (is_in[j] && param[i].inherits[j])
1648            {
1649
1650                if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1651                    expected_resource_status = U_ZERO_ERROR;
1652                else if(j == 0)
1653                    expected_resource_status = U_USING_DEFAULT_WARNING;
1654                else
1655                    expected_resource_status = U_USING_FALLBACK_WARNING;
1656
1657                log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>.  actual_bundle=%s\n",
1658                            param[i].name,
1659                            i,
1660                            frag,
1661                            j,
1662                            is_in[j]?"Yes":"No",
1663                            j,
1664                            param[i].inherits[j]?"Yes":"No",
1665                            param[actual_bundle].name);
1666
1667                break;
1668            }
1669        }
1670
1671        for (j=param[i].where; j>=0; --j)
1672        {
1673            if (is_in[j])
1674            {
1675                if(base != NULL) {
1676                    free(base);
1677                    base = NULL;
1678                }
1679                base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
1680                u_uastrcpy(base,NAME[j]);
1681
1682                break;
1683            }
1684            else {
1685                if(base != NULL) {
1686                    free(base);
1687                    base = NULL;
1688                }
1689                base = (UChar*) malloc(sizeof(UChar) * 1);
1690                *base = 0x0000;
1691            }
1692        }
1693
1694        /*----string---------------------------------------------------------------- */
1695
1696        strcpy(tag,"string_");
1697        strcat(tag,frag);
1698
1699        strcpy(action,param[i].name);
1700        strcat(action, ".ures_getStringByKey(" );
1701        strcat(action,tag);
1702        strcat(action, ")");
1703
1704
1705        status = U_ZERO_ERROR;
1706        len=0;
1707
1708        string=tres_getString(theBundle, -1, tag, &len, &status);
1709        if(U_SUCCESS(status)) {
1710            expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 4));
1711            u_strcpy(expected_string,base);
1712            CONFIRM_INT_EQ(len, u_strlen(expected_string));
1713        }else{
1714            expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
1715            u_strcpy(expected_string,kERROR);
1716            string=kERROR;
1717        }
1718        log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
1719
1720        CONFIRM_ErrorCode(status, expected_resource_status);
1721        CONFIRM_EQ(string, expected_string);
1722
1723
1724
1725        /*--------------array------------------------------------------------- */
1726
1727        strcpy(tag,"array_");
1728        strcat(tag,frag);
1729
1730        strcpy(action,param[i].name);
1731        strcat(action, ".ures_getByKey(" );
1732        strcat(action,tag);
1733        strcat(action, ")");
1734
1735        len=0;
1736
1737        count = kERROR_COUNT;
1738        status = U_ZERO_ERROR;
1739        array=ures_getByKey(theBundle, tag, array, &status);
1740        CONFIRM_ErrorCode(status,expected_resource_status);
1741        if (U_SUCCESS(status)) {
1742            /*confirm the resource type is an array*/
1743            CONFIRM_INT_EQ(ures_getType(array), URES_ARRAY);
1744            /*confirm the size*/
1745            count=ures_getSize(array);
1746            CONFIRM_INT_GE(count,1);
1747            for (j=0; j<count; ++j) {
1748                UChar element[3];
1749                u_strcpy(expected_string, base);
1750                u_uastrcpy(element, itoa1(j,buf));
1751                u_strcat(expected_string, element);
1752                arrayItem1=ures_getNextResource(array, arrayItem1, &status);
1753                if(U_SUCCESS(status)){
1754                    CONFIRM_EQ(tres_getString(arrayItem1, -1, NULL, &len, &status),expected_string);
1755                }
1756            }
1757
1758        }
1759        else {
1760            CONFIRM_INT_EQ(count,kERROR_COUNT);
1761            CONFIRM_ErrorCode(status, U_MISSING_RESOURCE_ERROR);
1762            /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1763            count = 0;
1764        }
1765
1766        /*--------------arrayItem------------------------------------------------- */
1767
1768        strcpy(tag,"array_");
1769        strcat(tag,frag);
1770
1771        strcpy(action,param[i].name);
1772        strcat(action, ".ures_getStringByIndex(");
1773        strcat(action, tag);
1774        strcat(action, ")");
1775
1776
1777        for (j=0; j<10; ++j){
1778            idx = count ? (randi(count * 3) - count) : (randi(200) - 100);
1779            status = U_ZERO_ERROR;
1780            string=kERROR;
1781            array=ures_getByKey(theBundle, tag, array, &status);
1782            if(!U_FAILURE(status)){
1783                UChar *t=NULL;
1784                t=(UChar*)ures_getStringByIndex(array, idx, &len, &status);
1785                if(!U_FAILURE(status)){
1786                    UChar element[3];
1787                    string=t;
1788                    u_strcpy(expected_string, base);
1789                    u_uastrcpy(element, itoa1(idx,buf));
1790                    u_strcat(expected_string, element);
1791                } else {
1792                    u_strcpy(expected_string, kERROR);
1793                }
1794
1795            }
1796            expected_status = (idx >= 0 && idx < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
1797            CONFIRM_ErrorCode(status,expected_status);
1798            CONFIRM_EQ(string,expected_string);
1799
1800        }
1801
1802
1803        /*--------------2dArray------------------------------------------------- */
1804
1805        strcpy(tag,"array_2d_");
1806        strcat(tag,frag);
1807
1808        strcpy(action,param[i].name);
1809        strcat(action, ".ures_getByKey(" );
1810        strcat(action,tag);
1811        strcat(action, ")");
1812
1813
1814
1815        row_count = kERROR_COUNT, column_count = kERROR_COUNT;
1816        status = U_ZERO_ERROR;
1817        array2d=ures_getByKey(theBundle, tag, array2d, &status);
1818
1819        CONFIRM_ErrorCode(status,expected_resource_status);
1820        if (U_SUCCESS(status))
1821        {
1822            /*confirm the resource type is an 2darray*/
1823            CONFIRM_INT_EQ(ures_getType(array2d), URES_ARRAY);
1824            row_count=ures_getSize(array2d);
1825            CONFIRM_INT_GE(row_count,1);
1826
1827            for(row=0; row<row_count; ++row){
1828                UResourceBundle *tableRow=NULL;
1829                tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1830                CONFIRM_ErrorCode(status, expected_resource_status);
1831                if(U_SUCCESS(status)){
1832                    /*confirm the resourcetype of each table row is an array*/
1833                    CONFIRM_INT_EQ(ures_getType(tableRow), URES_ARRAY);
1834                    column_count=ures_getSize(tableRow);
1835                    CONFIRM_INT_GE(column_count,1);
1836
1837                    for (col=0; j<column_count; ++j) {
1838                        UChar element[3];
1839                        u_strcpy(expected_string, base);
1840                        u_uastrcpy(element, itoa1(row, buf));
1841                        u_strcat(expected_string, element);
1842                        u_uastrcpy(element, itoa1(col, buf));
1843                        u_strcat(expected_string, element);
1844                        arrayItem1=ures_getNextResource(tableRow, arrayItem1, &status);
1845                        if(U_SUCCESS(status)){
1846                            const UChar *stringValue=tres_getString(arrayItem1, -1, NULL, &len, &status);
1847                            CONFIRM_EQ(stringValue, expected_string);
1848                        }
1849                    }
1850                }
1851                ures_close(tableRow);
1852            }
1853        }else{
1854            CONFIRM_INT_EQ(row_count,kERROR_COUNT);
1855            CONFIRM_INT_EQ(column_count,kERROR_COUNT);
1856            row_count=column_count=0;
1857        }
1858
1859
1860        /*------2dArrayItem-------------------------------------------------------------- */
1861        /* 2dArrayItem*/
1862        for (j=0; j<10; ++j)
1863        {
1864            row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100);
1865            col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100);
1866            status = U_ZERO_ERROR;
1867            string = kERROR;
1868            len=0;
1869            array2d=ures_getByKey(theBundle, tag, array2d, &status);
1870            if(U_SUCCESS(status)){
1871                UResourceBundle *tableRow=NULL;
1872                tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1873                if(U_SUCCESS(status)) {
1874                    UChar *t=NULL;
1875                    t=(UChar*)ures_getStringByIndex(tableRow, col, &len, &status);
1876                    if(U_SUCCESS(status)){
1877                        string=t;
1878                    }
1879                }
1880                ures_close(tableRow);
1881            }
1882            expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
1883                                   expected_resource_status: U_MISSING_RESOURCE_ERROR;
1884            CONFIRM_ErrorCode(status,expected_status);
1885
1886            if (U_SUCCESS(status)){
1887                UChar element[3];
1888                u_strcpy(expected_string, base);
1889                u_uastrcpy(element, itoa1(row, buf));
1890                u_strcat(expected_string, element);
1891                u_uastrcpy(element, itoa1(col, buf));
1892                u_strcat(expected_string, element);
1893            } else {
1894                u_strcpy(expected_string,kERROR);
1895            }
1896            CONFIRM_EQ(string,expected_string);
1897
1898        }
1899
1900
1901        /*--------------taggedArray----------------------------------------------- */
1902        strcpy(tag,"tagged_array_");
1903        strcat(tag,frag);
1904
1905        strcpy(action,param[i].name);
1906        strcat(action,".ures_getByKey(");
1907        strcat(action, tag);
1908        strcat(action,")");
1909
1910
1911        status = U_ZERO_ERROR;
1912        tag_count=0;
1913        tags=ures_getByKey(theBundle, tag, tags, &status);
1914        CONFIRM_ErrorCode(status, expected_resource_status);
1915        if (U_SUCCESS(status)) {
1916            UResType bundleType=ures_getType(tags);
1917            CONFIRM_INT_EQ(bundleType, URES_TABLE);
1918
1919            tag_count=ures_getSize(tags);
1920            CONFIRM_INT_GE((int32_t)tag_count, (int32_t)0);
1921
1922            for(idx=0; idx <tag_count; idx++){
1923                UResourceBundle *tagelement=NULL;
1924                const char *key=NULL;
1925                UChar* value=NULL;
1926                tagelement=ures_getByIndex(tags, idx, tagelement, &status);
1927                key=ures_getKey(tagelement);
1928                value=(UChar*)ures_getNextString(tagelement, &len, &key, &status);
1929                log_verbose("tag = %s, value = %s\n", key, u_austrcpy(verboseOutput, value));
1930                if(strncmp(key, "tag", 3) == 0 && u_strncmp(value, base, u_strlen(base)) == 0){
1931                    record_pass();
1932                }else{
1933                    record_fail();
1934                }
1935                ures_close(tagelement);
1936            }
1937        }else{
1938            tag_count=0;
1939        }
1940
1941        /*---------taggedArrayItem----------------------------------------------*/
1942        count = 0;
1943        for (idx=-20; idx<20; ++idx)
1944        {
1945
1946            status = U_ZERO_ERROR;
1947            string = kERROR;
1948            strcpy(item_tag, "tag");
1949            strcat(item_tag, itoa1(idx,buf));
1950            tags=ures_getByKey(theBundle, tag, tags, &status);
1951            if(U_SUCCESS(status)){
1952                UResourceBundle *tagelement=NULL;
1953                UChar *t=NULL;
1954                tagelement=ures_getByKey(tags, item_tag, tagelement, &status);
1955                if(!U_FAILURE(status)){
1956                    UResType elementType=ures_getType(tagelement);
1957                    CONFIRM_INT_EQ(elementType, URES_STRING);
1958                    if(strcmp(ures_getKey(tagelement), item_tag) == 0){
1959                        record_pass();
1960                    }else{
1961                        record_fail();
1962                    }
1963                    t=(UChar*)tres_getString(tagelement, -1, NULL, &len, &status);
1964                    if(!U_FAILURE(status)){
1965                        string=t;
1966                    }
1967                }
1968                if (idx < 0) {
1969                    CONFIRM_ErrorCode(status,U_MISSING_RESOURCE_ERROR);
1970                }
1971                else{
1972                    if (status != U_MISSING_RESOURCE_ERROR) {
1973                        UChar element[3];
1974                        u_strcpy(expected_string, base);
1975                        u_uastrcpy(element, itoa1(idx,buf));
1976                        u_strcat(expected_string, element);
1977                        CONFIRM_EQ(string,expected_string);
1978                        count++;
1979                    }
1980                }
1981                ures_close(tagelement);
1982            }
1983        }
1984        CONFIRM_INT_EQ(count, tag_count);
1985
1986        free(expected_string);
1987        ures_close(theBundle);
1988    }
1989    ures_close(array);
1990    ures_close(array2d);
1991    ures_close(tags);
1992    ures_close(arrayItem1);
1993    free(base);
1994    return (UBool)(failNum == fail);
1995}
1996
1997static void record_pass()
1998{
1999    ++pass;
2000}
2001
2002static void record_fail()
2003{
2004    ++fail;
2005}
2006
2007/**
2008 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2009 * are set correctly
2010 */
2011
2012static void TestFallback()
2013{
2014    UErrorCode status = U_ZERO_ERROR;
2015    UResourceBundle *fr_FR = NULL;
2016    UResourceBundle *subResource = NULL;
2017    const UChar *junk; /* ignored */
2018    int32_t resultLen;
2019
2020    log_verbose("Opening fr_FR..");
2021    fr_FR = ures_open(NULL, "fr_FR", &status);
2022    if(U_FAILURE(status))
2023    {
2024        log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
2025        return;
2026    }
2027
2028    status = U_ZERO_ERROR;
2029
2030
2031    /* clear it out..  just do some calls to get the gears turning */
2032    junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
2033    status = U_ZERO_ERROR;
2034    junk = tres_getString(fr_FR, -1, "LocaleString", &resultLen, &status);
2035    status = U_ZERO_ERROR;
2036    junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
2037    status = U_ZERO_ERROR;
2038
2039    /* OK first one. This should be a Default value. */
2040    subResource = ures_getByKey(fr_FR, "MeasurementSystem", NULL, &status);
2041    if(status != U_USING_DEFAULT_WARNING)
2042    {
2043        log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get CurrencyMap from fr_FR, got %s\n",
2044            u_errorName(status));
2045    }
2046
2047    status = U_ZERO_ERROR;
2048    ures_close(subResource);
2049
2050    /* and this is a Fallback, to fr */
2051    junk = tres_getString(fr_FR, -1, "ExemplarCharacters", &resultLen, &status);
2052    if(status != U_USING_FALLBACK_WARNING)
2053    {
2054        log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
2055            status);
2056    }
2057
2058    status = U_ZERO_ERROR;
2059
2060    ures_close(fr_FR);
2061    /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2062    /* Test Jitterbug 552 fallback mechanism of aliased data */
2063    {
2064        UErrorCode err =U_ZERO_ERROR;
2065        UResourceBundle* myResB = ures_open(NULL,"no_NO_NY",&err);
2066        UResourceBundle* resLocID = ures_getByKey(myResB, "Version", NULL, &err);
2067        UResourceBundle* tResB;
2068        UResourceBundle* zoneResource;
2069        const UChar* version = NULL;
2070        static const UChar versionStr[] = { 0x0032, 0x002E, 0x0030, 0x002E, 0x0038, 0x0032, 0x002E, 0x0034, 0x0035, 0x0000};
2071
2072        if(err != U_ZERO_ERROR){
2073            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));
2074            return;
2075        }
2076        version = tres_getString(resLocID, -1, NULL, &resultLen, &err);
2077        if(u_strcmp(version, versionStr) != 0){
2078            char x[100];
2079            char g[100];
2080            u_austrcpy(x, versionStr);
2081            u_austrcpy(g, version);
2082            log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2083                    x, g);
2084        }
2085        zoneResource = ures_open(U_ICUDATA_ZONE, "no_NO_NY", &err);
2086        tResB = ures_getByKey(zoneResource, "zoneStrings", NULL, &err);
2087        if(err != U_USING_FALLBACK_WARNING){
2088            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));
2089        }
2090        ures_close(tResB);
2091        ures_close(zoneResource);
2092        ures_close(resLocID);
2093        ures_close(myResB);
2094    }
2095
2096}
2097
2098/* static void printUChars(UChar* uchars){
2099/    int16_t i=0;
2100/    for(i=0; i<u_strlen(uchars); i++){
2101/        log_err("%04X ", *(uchars+i));
2102/    }
2103/ } */
2104
2105static void TestResourceLevelAliasing(void) {
2106    UErrorCode status = U_ZERO_ERROR;
2107    UResourceBundle *aliasB = NULL, *tb = NULL;
2108    UResourceBundle *en = NULL, *uk = NULL, *testtypes = NULL;
2109    const char* testdatapath = NULL;
2110    const UChar *string = NULL, *sequence = NULL;
2111    /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2112    int32_t strLen = 0, seqLen = 0;/*, binLen = 0, binSeqLen = 0;*/
2113    char buffer[100];
2114    char *s;
2115
2116    testdatapath=loadTestData(&status);
2117    if(U_FAILURE(status))
2118    {
2119        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2120        return;
2121    }
2122
2123    aliasB = ures_open(testdatapath, "testaliases", &status);
2124
2125    if(U_FAILURE(status))
2126    {
2127        log_data_err("Could not load testaliases.res %s \n",myErrorName(status));
2128        return;
2129    }
2130    /* this should fail - circular alias */
2131    tb = ures_getByKey(aliasB, "aaa", tb, &status);
2132    if(status != U_TOO_MANY_ALIASES_ERROR) {
2133        log_err("Failed to detect circular alias\n");
2134    }
2135    else {
2136        status = U_ZERO_ERROR;
2137    }
2138    tb = ures_getByKey(aliasB, "aab", tb, &status);
2139    if(status != U_TOO_MANY_ALIASES_ERROR) {
2140        log_err("Failed to detect circular alias\n");
2141    } else {
2142        status = U_ZERO_ERROR;
2143    }
2144    if(U_FAILURE(status) ) {
2145        log_data_err("err loading tb resource\n");
2146    }  else {
2147      /* testing aliasing to a non existing resource */
2148      tb = ures_getByKey(aliasB, "nonexisting", tb, &status);
2149      if(status != U_MISSING_RESOURCE_ERROR) {
2150        log_err("Managed to find an alias to non-existing resource\n");
2151      } else {
2152        status = U_ZERO_ERROR;
2153      }
2154      /* testing referencing/composed alias */
2155      uk = ures_findResource("ja/LocaleScript/2", uk, &status);
2156      if((uk == NULL) || U_FAILURE(status)) {
2157        log_err_status(status, "Couldn't findResource('ja/LocaleScript/2') err %s\n", u_errorName(status));
2158        goto cleanup;
2159      }
2160
2161      sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
2162
2163      tb = ures_getByKey(aliasB, "referencingalias", tb, &status);
2164      string = tres_getString(tb, -1, NULL, &strLen, &status);
2165
2166      if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2167        log_err("Referencing alias didn't get the right string (1)\n");
2168      }
2169
2170      string = tres_getString(aliasB, -1, "referencingalias", &strLen, &status);
2171      if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2172        log_err("Referencing alias didn't get the right string (2)\n");
2173      }
2174
2175      checkStatus(__LINE__, U_ZERO_ERROR, status);
2176      tb = ures_getByKey(aliasB, "LocaleScript", tb, &status);
2177      checkStatus(__LINE__, U_ZERO_ERROR, status);
2178      tb = ures_getByIndex(tb, 2, tb, &status);
2179      checkStatus(__LINE__, U_ZERO_ERROR, status);
2180      string = tres_getString(tb, -1, NULL, &strLen, &status);
2181      checkStatus(__LINE__, U_ZERO_ERROR, status);
2182
2183      if(U_FAILURE(status)) {
2184        log_err("%s trying to get string via separate getters\n", u_errorName(status));
2185      } else if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2186        log_err("Referencing alias didn't get the right string (3)\n");
2187      }
2188
2189      /* simple alias */
2190      testtypes = ures_open(testdatapath, "testtypes", &status);
2191      strcpy(buffer, "menu/file/open");
2192      s = buffer;
2193      uk = ures_findSubResource(testtypes, s, uk, &status);
2194      sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
2195
2196      tb = ures_getByKey(aliasB, "simplealias", tb, &status);
2197      string = tres_getString(tb, -1, NULL, &strLen, &status);
2198
2199      if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2200        log_err("Referencing alias didn't get the right string (4)\n");
2201      }
2202
2203      /* test indexed aliasing */
2204
2205      tb = ures_getByKey(aliasB, "zoneTests", tb, &status);
2206      tb = ures_getByKey(tb, "zoneAlias2", tb, &status);
2207      string = tres_getString(tb, -1, NULL, &strLen, &status);
2208
2209      en = ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en, &status);
2210      sequence = tres_getString(en, -1, NULL, &seqLen, &status);
2211
2212      if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
2213        log_err("Referencing alias didn't get the right string (5)\n");
2214      }
2215    }
2216    /* test getting aliased string by index */
2217    {
2218        const char* keys[] = {
2219                "KeyAlias0PST",
2220                "KeyAlias1PacificStandardTime",
2221                "KeyAlias2PDT",
2222                "KeyAlias3LosAngeles"
2223        };
2224
2225        const char* strings[] = {
2226                "America/Los_Angeles",
2227                "Pacific Standard Time",
2228                "PDT",
2229                "Los Angeles",
2230        };
2231        UChar uBuffer[256];
2232        const UChar* result;
2233        int32_t uBufferLen = 0, resultLen = 0;
2234        int32_t i = 0;
2235        const char *key = NULL;
2236        tb = ures_getByKey(aliasB, "testGetStringByKeyAliasing", tb, &status);
2237        if(U_FAILURE(status)) {
2238          log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status));
2239        } else {
2240            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2241                result = tres_getString(tb, -1, keys[i], &resultLen, &status);
2242                if(U_FAILURE(status)){
2243                    log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2244                    continue;
2245                }
2246                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2247                if(resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2248                  log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys[i]);
2249                }
2250            }
2251            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2252                result = tres_getString(tb, i, NULL, &resultLen, &status);
2253                if(U_FAILURE(status)){
2254                    log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2255                    continue;
2256                }
2257                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2258                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2259                  log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings[i]);
2260                }
2261            }
2262            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2263                result = ures_getNextString(tb, &resultLen, &key, &status);
2264                if(U_FAILURE(status)){
2265                    log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2266                    continue;
2267                }
2268                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2269                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2270                  log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings[i]);
2271                }
2272            }
2273        }
2274        tb = ures_getByKey(aliasB, "testGetStringByIndexAliasing", tb, &status);
2275        if(U_FAILURE(status)) {
2276          log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status));
2277        } else {
2278            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2279                result = tres_getString(tb, i, NULL, &resultLen, &status);
2280                if(U_FAILURE(status)){
2281                    log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2282                    continue;
2283                }
2284                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2285                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2286                  log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings[i]);
2287                }
2288            }
2289            for(i = 0; i < sizeof(strings)/sizeof(strings[0]); i++) {
2290                result = ures_getNextString(tb, &resultLen, &key, &status);
2291                if(U_FAILURE(status)){
2292                    log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2293                    continue;
2294                }
2295                uBufferLen = u_unescape(strings[i], uBuffer, 256);
2296                if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2297                  log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings[i]);
2298                }
2299            }
2300        }
2301    }
2302    tb = ures_getByKey(aliasB, "testAliasToTree", tb, &status);
2303    if(U_FAILURE(status)){
2304        log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status));
2305        goto cleanup;
2306    }
2307    if (strcmp(ures_getKey(tb), "collations") != 0) {
2308        log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb));
2309    }
2310cleanup:
2311    ures_close(aliasB);
2312    ures_close(tb);
2313    ures_close(en);
2314    ures_close(uk);
2315    ures_close(testtypes);
2316}
2317
2318static void TestDirectAccess(void) {
2319    UErrorCode status = U_ZERO_ERROR;
2320    UResourceBundle *t = NULL, *t2 = NULL;
2321    const char* key = NULL;
2322
2323    char buffer[100];
2324    char *s;
2325    /*const char* testdatapath=loadTestData(&status);
2326    if(U_FAILURE(status)){
2327        log_err("Could not load testdata.dat %s \n",myErrorName(status));
2328        return;
2329    }*/
2330
2331    t = ures_findResource("/testdata/te/zoneStrings/3/2", t, &status);
2332    if(U_FAILURE(status)) {
2333        log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
2334        status = U_ZERO_ERROR;
2335    } else {
2336        key = ures_getKey(t);
2337        if(key != NULL) {
2338            log_err("Got a strange key, expected NULL, got %s\n", key);
2339        }
2340    }
2341    t = ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t, &status);
2342    if(U_FAILURE(status)) {
2343        log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
2344        status = U_ZERO_ERROR;
2345    } else {
2346        key = ures_getKey(t);
2347        if(key != NULL) {
2348            log_err("Got a strange key, expected NULL, got %s\n", key);
2349        }
2350    }
2351
2352    t = ures_findResource("ja/LocaleScript", t, &status);
2353    if(U_FAILURE(status)) {
2354        log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
2355        status = U_ZERO_ERROR;
2356    } else {
2357        key = ures_getKey(t);
2358        if(strcmp(key, "LocaleScript")!=0) {
2359            log_err("Got a strange key, expected 'LocaleScript', got %s\n", key);
2360        }
2361    }
2362
2363    t2 = ures_open(U_ICUDATA_LANG, "sr", &status);
2364    if(U_FAILURE(status)) {
2365        log_err_status(status, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status));
2366        log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2367                     "You probably have no data! Aborting this test\n");
2368    }
2369
2370    if(U_SUCCESS(status)) {
2371        strcpy(buffer, "Languages/hr");
2372        s = buffer;
2373        t = ures_findSubResource(t2, s, t, &status);
2374        if(U_FAILURE(status)) {
2375            log_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
2376            status = U_ZERO_ERROR;
2377        } else {
2378            key = ures_getKey(t);
2379            if(strcmp(key, "hr")!=0) {
2380                log_err("Got a strange key, expected 'hr', got %s\n", key);
2381            }
2382        }
2383    }
2384
2385    t = ures_findResource("root/calendar/islamic-civil/DateTime", t, &status);
2386    if(U_SUCCESS(status)) {
2387        log_data_err("This resource does not exist. How did it get here?\n");
2388    }
2389    status = U_ZERO_ERROR;
2390
2391    /* this one will freeze */
2392    t = ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t, &status);
2393    if(U_SUCCESS(status)) {
2394        log_data_err("Second resource does not exist. How did it get here?\n");
2395    }
2396    status = U_ZERO_ERROR;
2397
2398    ures_close(t2);
2399    t2 = ures_open(NULL, "he", &status);
2400    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2401    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2402    t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
2403    if(U_SUCCESS(status)) {
2404        log_err("This resource does not exist. How did it get here?\n");
2405    }
2406    status = U_ZERO_ERROR;
2407
2408    ures_close(t2);
2409    t2 = ures_open(NULL, "he", &status);
2410    /* George's fix */
2411    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2412    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2413    t2 = ures_getByKeyWithFallback(t2, "eras", t2, &status);
2414    if(U_FAILURE(status)) {
2415        log_err_status(status, "Didn't get Eras. I know they are there!\n");
2416    }
2417    status = U_ZERO_ERROR;
2418
2419    ures_close(t2);
2420    t2 = ures_open(NULL, "root", &status);
2421    t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2422    t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2423    t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
2424    if(U_SUCCESS(status)) {
2425        log_err("This resource does not exist. How did it get here?\n");
2426    }
2427    status = U_ZERO_ERROR;
2428
2429    ures_close(t2);
2430    ures_close(t);
2431}
2432
2433static void TestTicket9804(void) {
2434    UErrorCode status = U_ZERO_ERROR;
2435    UResourceBundle *t = NULL;
2436    t = ures_open(NULL, "he", &status);
2437    t = ures_getByKeyWithFallback(t, "calendar/islamic-civil/DateTime", t, &status);
2438    if(U_SUCCESS(status)) {
2439        log_err("This resource does not exist. How did it get here?\n");
2440    }
2441    status = U_ZERO_ERROR;
2442    ures_close(t);
2443    t = ures_open(NULL, "he", &status);
2444    t = ures_getByKeyWithFallback(t, "calendar/islamic-civil/eras", t, &status);
2445    if(U_FAILURE(status)) {
2446        log_err_status(status, "Didn't get Eras. I know they are there!\n");
2447    } else {
2448        const char *locale = ures_getLocaleByType(t, ULOC_ACTUAL_LOCALE, &status);
2449        if (uprv_strcmp("he", locale) != 0) {
2450            log_err("Eras should be in the 'he' locale, but was in: %s", locale);
2451        }
2452    }
2453    status = U_ZERO_ERROR;
2454    ures_close(t);
2455}
2456
2457static void TestJB3763(void) {
2458    /* Nasty bug prevented using parent as fill-in, since it would
2459     * stomp the path information.
2460     */
2461    UResourceBundle *t = NULL;
2462    UErrorCode status = U_ZERO_ERROR;
2463    t = ures_open(NULL, "sr_Latn", &status);
2464    t = ures_getByKeyWithFallback(t, "calendar", t, &status);
2465    t = ures_getByKeyWithFallback(t, "gregorian", t, &status);
2466    t = ures_getByKeyWithFallback(t, "AmPmMarkers", t, &status);
2467    if(U_FAILURE(status)) {
2468        log_err_status(status, "This resource should be available?\n");
2469    }
2470    status = U_ZERO_ERROR;
2471
2472    ures_close(t);
2473
2474}
2475
2476static void TestGetKeywordValues(void) {
2477    UEnumeration *kwVals;
2478    UBool foundStandard = FALSE;
2479    UErrorCode status = U_ZERO_ERROR;
2480    const char *kw;
2481#if !UCONFIG_NO_COLLATION
2482    kwVals = ures_getKeywordValues( U_ICUDATA_COLL, "collations", &status);
2483
2484    log_verbose("Testing getting collation keyword values:\n");
2485
2486    while((kw=uenum_next(kwVals, NULL, &status))) {
2487        log_verbose("  %s\n", kw);
2488        if(!strcmp(kw,"standard")) {
2489            if(foundStandard == FALSE) {
2490                foundStandard = TRUE;
2491            } else {
2492                log_err("'standard' was found twice in the keyword list.\n");
2493            }
2494        }
2495    }
2496    if(foundStandard == FALSE) {
2497        log_err_status(status, "'standard' was not found in the keyword list.\n");
2498    }
2499    uenum_close(kwVals);
2500    if(U_FAILURE(status)) {
2501        log_err_status(status, "err %s getting collation values\n", u_errorName(status));
2502    }
2503    status = U_ZERO_ERROR;
2504#endif
2505    foundStandard = FALSE;
2506    kwVals = ures_getKeywordValues( "ICUDATA", "calendar", &status);
2507
2508    log_verbose("Testing getting calendar keyword values:\n");
2509
2510    while((kw=uenum_next(kwVals, NULL, &status))) {
2511        log_verbose("  %s\n", kw);
2512        if(!strcmp(kw,"japanese")) {
2513            if(foundStandard == FALSE) {
2514                foundStandard = TRUE;
2515            } else {
2516                log_err("'japanese' was found twice in the calendar keyword list.\n");
2517            }
2518        }
2519    }
2520    if(foundStandard == FALSE) {
2521        log_err_status(status, "'japanese' was not found in the calendar keyword list.\n");
2522    }
2523    uenum_close(kwVals);
2524    if(U_FAILURE(status)) {
2525        log_err_status(status, "err %s getting calendar values\n", u_errorName(status));
2526    }
2527}
2528
2529static void TestGetFunctionalEquivalentOf(const char *path, const char *resName, const char *keyword, UBool truncate, const char * const testCases[]) {
2530    int32_t i;
2531    for(i=0;testCases[i];i+=3) {
2532        UBool expectAvail = (testCases[i][0]=='t')?TRUE:FALSE;
2533        UBool gotAvail = FALSE;
2534        const char *inLocale = testCases[i+1];
2535        const char *expectLocale = testCases[i+2];
2536        char equivLocale[256];
2537        int32_t len;
2538        UErrorCode status = U_ZERO_ERROR;
2539        log_verbose("%d:   %c      %s\texpect %s\n",i/3,  expectAvail?'t':'f', inLocale, expectLocale);
2540        len = ures_getFunctionalEquivalent(equivLocale, 255, path,
2541            resName, keyword, inLocale,
2542            &gotAvail, truncate, &status);
2543        if(U_FAILURE(status) || (len <= 0)) {
2544            log_err_status(status, "FAIL: got len %d, err %s  on #%d: %c\t%s\t%s\n",
2545                len, u_errorName(status),
2546                i/3,expectAvail?'t':'f', inLocale, expectLocale);
2547        } else {
2548            log_verbose("got:  %c   %s\n", expectAvail?'t':'f',equivLocale);
2549
2550            if((gotAvail != expectAvail) || strcmp(equivLocale, expectLocale)) {
2551                log_err("FAIL: got avail=%c, loc=%s but  expected #%d: %c\t%s\t-> loc=%s\n",
2552                    gotAvail?'t':'f', equivLocale,
2553                    i/3,
2554                    expectAvail?'t':'f', inLocale, expectLocale);
2555
2556            }
2557        }
2558    }
2559}
2560
2561static void TestGetFunctionalEquivalent(void) {
2562    static const char * const collCases[] = {
2563        /*   avail   locale          equiv   */
2564        "f",    "de_US_CALIFORNIA",               "de",
2565        "f",    "zh_TW@collation=stroke",         "zh@collation=stroke", /* alias of zh_Hant_TW */
2566        "t",    "zh_Hant_TW@collation=stroke",    "zh@collation=stroke",
2567        "f",    "de_CN@collation=pinyin",         "de",
2568        "t",    "zh@collation=pinyin",            "zh",
2569        "f",    "zh_CN@collation=pinyin",         "zh", /* alias of zh_Hans_CN */
2570        "t",    "zh_Hans_CN@collation=pinyin",    "zh",
2571        "f",    "zh_HK@collation=pinyin",         "zh", /* alias of zh_Hant_HK */
2572        "t",    "zh_Hant_HK@collation=pinyin",    "zh",
2573        "f",    "zh_HK@collation=stroke",         "zh@collation=stroke", /* alias of zh_Hant_HK */
2574        "t",    "zh_Hant_HK@collation=stroke",    "zh@collation=stroke",
2575        "f",    "zh_HK",                          "zh@collation=stroke", /* alias of zh_Hant_HK */
2576        "t",    "zh_Hant_HK",                     "zh@collation=stroke",
2577        "f",    "zh_MO",                          "zh@collation=stroke", /* alias of zh_Hant_MO */
2578        "t",    "zh_Hant_MO",                     "zh@collation=stroke",
2579        "f",    "zh_TW_STROKE",                   "zh@collation=stroke",
2580        "f",    "zh_TW_STROKE@collation=big5han", "zh@collation=big5han",
2581        "f",    "de_CN@calendar=japanese",        "de",
2582        "t",    "de@calendar=japanese",           "de",
2583        "f",    "zh_TW@collation=big5han",        "zh@collation=big5han", /* alias of zh_Hant_TW */
2584        "t",    "zh_Hant_TW@collation=big5han",   "zh@collation=big5han",
2585        "f",    "zh_TW@collation=gb2312han",      "zh@collation=gb2312han", /* alias of zh_Hant_TW */
2586        "t",    "zh_Hant_TW@collation=gb2312han", "zh@collation=gb2312han",
2587        "f",    "zh_CN@collation=big5han",        "zh@collation=big5han", /* alias of zh_Hans_CN */
2588        "t",    "zh_Hans_CN@collation=big5han",   "zh@collation=big5han",
2589        "f",    "zh_CN@collation=gb2312han",      "zh@collation=gb2312han", /* alias of zh_Hans_CN */
2590        "t",    "zh_Hans_CN@collation=gb2312han", "zh@collation=gb2312han",
2591        "t",    "zh@collation=big5han",           "zh@collation=big5han",
2592        "t",    "zh@collation=gb2312han",         "zh@collation=gb2312han",
2593        "t",    "hi@collation=standard",          "hi",
2594        "f",    "hi_AU@collation=standard;currency=CHF;calendar=buddhist",    "hi",
2595        "t",    "de_DE@collation=pinyin",         "de", /* bug 4582 tests */
2596        "f",    "de_DE_BONN@collation=pinyin",    "de",
2597        "t",    "nl",                             "root",
2598        "t",    "nl_NL",                          "root",
2599        "f",    "nl_NL_EEXT",                     "root",
2600        "t",    "nl@collation=stroke",            "root",
2601        "t",    "nl_NL@collation=stroke",         "root",
2602        "f",    "nl_NL_EEXT@collation=stroke",    "root",
2603        NULL
2604    };
2605
2606    static const char *calCases[] = {
2607        /*   avail   locale                       equiv   */
2608        "t",    "en_US_POSIX",                   "en@calendar=gregorian",
2609        "f",    "ja_JP_TOKYO",                   "ja@calendar=gregorian",
2610        "f",    "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2611        "t",    "sr@calendar=gregorian", "sr@calendar=gregorian",
2612        "t",    "en", "en@calendar=gregorian",
2613        NULL
2614    };
2615
2616#if !UCONFIG_NO_COLLATION
2617    TestGetFunctionalEquivalentOf(U_ICUDATA_COLL, "collations", "collation", TRUE, collCases);
2618#endif
2619    TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE, calCases);
2620
2621#if !UCONFIG_NO_COLLATION
2622    log_verbose("Testing error conditions:\n");
2623    {
2624        char equivLocale[256] = "???";
2625        int32_t len;
2626        UErrorCode status = U_ZERO_ERROR;
2627        UBool gotAvail = FALSE;
2628
2629        len = ures_getFunctionalEquivalent(equivLocale, 255, U_ICUDATA_COLL,
2630            "calendar", "calendar", "ar_EG@calendar=islamic",
2631            &gotAvail, FALSE, &status);
2632
2633        if(status == U_MISSING_RESOURCE_ERROR) {
2634            log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2635        } else {
2636            log_err("ures_getFunctionalEquivalent  returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2637                equivLocale, gotAvail?'t':'f', u_errorName(status));
2638        }
2639    }
2640#endif
2641}
2642
2643static void TestXPath(void) {
2644    UErrorCode status = U_ZERO_ERROR;
2645    UResourceBundle *rb = NULL, *alias = NULL;
2646    int32_t len = 0;
2647    const UChar* result = NULL;
2648    const UChar expResult[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2649    /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2650
2651    const char *testdatapath=loadTestData(&status);
2652    if(U_FAILURE(status))
2653    {
2654        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2655        return;
2656    }
2657
2658    log_verbose("Testing ures_open()......\n");
2659
2660    rb = ures_open(testdatapath, "te_IN", &status);
2661    if(U_FAILURE(status)) {
2662      log_err("Could not open te_IN (%s)\n", myErrorName(status));
2663      return;
2664    }
2665    alias = ures_getByKey(rb, "rootAliasClient", alias, &status);
2666    if(U_FAILURE(status)) {
2667      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2668      ures_close(rb);
2669      return;
2670    }
2671
2672    result = tres_getString(alias, -1, NULL, &len, &status);
2673    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2674      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2675    }
2676
2677    alias = ures_getByKey(rb, "aliasClient", alias, &status);
2678    if(U_FAILURE(status)) {
2679      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2680      ures_close(rb);
2681      return;
2682    }
2683
2684    result = tres_getString(alias, -1, NULL, &len, &status);
2685    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2686      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2687    }
2688
2689    alias = ures_getByKey(rb, "nestedRootAliasClient", alias, &status);
2690    if(U_FAILURE(status)) {
2691      log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2692      ures_close(rb);
2693      return;
2694    }
2695
2696    result = tres_getString(alias, -1, NULL, &len, &status);
2697    if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2698      log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2699    }
2700
2701    ures_close(alias);
2702    ures_close(rb);
2703}
2704static void TestCLDRStyleAliases(void) {
2705    UErrorCode status = U_ZERO_ERROR;
2706    UResourceBundle *rb = NULL, *alias = NULL, *a=NULL;
2707    int32_t i, len;
2708    char resource[256];
2709    const UChar *result = NULL;
2710    UChar expected[256];
2711    const char *expects[7] = { "", "a41", "a12", "a03", "ar4" };
2712    const char *testdatapath=loadTestData(&status);
2713    if(U_FAILURE(status)) {
2714        log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2715        return;
2716    }
2717    log_verbose("Testing CLDR style aliases......\n");
2718
2719    rb = ures_open(testdatapath, "te_IN_REVISED", &status);
2720    if(U_FAILURE(status)) {
2721      log_err("Could not open te_IN (%s)\n", myErrorName(status));
2722      return;
2723    }
2724    alias = ures_getByKey(rb, "a", alias, &status);
2725    if(U_FAILURE(status)) {
2726      log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status));
2727      ures_close(rb);
2728      return;
2729    }
2730    for(i = 1; i < 5 ; i++) {
2731      resource[0]='a';
2732      resource[1]='0'+i;
2733      resource[2]=0;
2734      /* instead of sprintf(resource, "a%i", i); */
2735      a = ures_getByKeyWithFallback(alias, resource, a, &status);
2736      result = tres_getString(a, -1, NULL, &len, &status);
2737      u_charsToUChars(expects[i], expected, strlen(expects[i])+1);
2738      if(U_FAILURE(status) || !result || u_strcmp(result, expected)) {
2739        log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource, expects[i], result, myErrorName(status));
2740        status = U_ZERO_ERROR;
2741      }
2742    }
2743
2744  ures_close(a);
2745  ures_close(alias);
2746  ures_close(rb);
2747}
2748
2749static void TestFallbackCodes(void) {
2750  UErrorCode status = U_ZERO_ERROR;
2751  const char *testdatapath=loadTestData(&status);
2752
2753  UResourceBundle *res = ures_open(testdatapath, "te_IN", &status);
2754
2755  UResourceBundle *r = NULL, *fall = NULL;
2756
2757  r = ures_getByKey(res, "tagged_array_in_Root_te_te_IN", r, &status);
2758
2759  status = U_ZERO_ERROR;
2760  fall = ures_getByKeyWithFallback(r, "tag2", fall, &status);
2761
2762  if(status != U_ZERO_ERROR) {
2763    log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status));
2764    status = U_ZERO_ERROR;
2765  }
2766
2767  fall = ures_getByKeyWithFallback(r, "tag7", fall, &status);
2768
2769  if(status != U_USING_FALLBACK_WARNING) {
2770    log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status));
2771  }
2772  status = U_ZERO_ERROR;
2773
2774  fall = ures_getByKeyWithFallback(r, "tag1", fall, &status);
2775
2776  if(status != U_USING_DEFAULT_WARNING) {
2777    log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status));
2778  }
2779  status = U_ZERO_ERROR;
2780
2781  ures_close(fall);
2782  ures_close(r);
2783  ures_close(res);
2784}
2785
2786/* This test will crash if this doesn't work. Results don't need testing. */
2787static void TestStackReuse(void) {
2788    UResourceBundle table;
2789    UErrorCode errorCode = U_ZERO_ERROR;
2790    UResourceBundle *rb = ures_open(NULL, "en_US", &errorCode);
2791
2792    if(U_FAILURE(errorCode)) {
2793        log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode));
2794        return;
2795    }
2796    ures_initStackObject(&table);
2797    ures_getByKeyWithFallback(rb, "Types", &table, &errorCode);
2798    ures_getByKeyWithFallback(&table, "collation", &table, &errorCode);
2799    ures_close(rb);
2800    ures_close(&table);
2801}
2802
2803/* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2804
2805/*
2806 * Replace most ures_getStringXYZ() with this function which wraps the
2807 * desired call and also calls the UTF-8 variant and checks that it works.
2808 */
2809extern const UChar *
2810tres_getString(const UResourceBundle *resB,
2811               int32_t idx, const char *key,
2812               int32_t *length,
2813               UErrorCode *status) {
2814    char buffer8[16];
2815    char *p8;
2816    const UChar *s16;
2817    const char *s8;
2818    UChar32 c16, c8;
2819    int32_t length16, length8, i16, i8;
2820    UBool forceCopy;
2821
2822    if(length == NULL) {
2823        length = &length16;
2824    }
2825    if(idx >= 0) {
2826        s16 = ures_getStringByIndex(resB, idx, length, status);
2827    } else if(key != NULL) {
2828        s16 = ures_getStringByKey(resB, key, length, status);
2829    } else {
2830        s16 = ures_getString(resB, length, status);
2831    }
2832    if(U_FAILURE(*status)) {
2833        return s16;
2834    }
2835    length16 = *length;
2836
2837    /* try the UTF-8 variant of ures_getStringXYZ() */
2838    for(forceCopy = FALSE; forceCopy <= TRUE; ++forceCopy) {
2839        p8 = buffer8;
2840        length8 = (int32_t)sizeof(buffer8);
2841        if(idx >= 0) {
2842            s8 = ures_getUTF8StringByIndex(resB, idx, p8, &length8, forceCopy, status);
2843        } else if(key != NULL) {
2844            s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2845        } else {
2846            s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2847        }
2848        if(*status == U_INVALID_CHAR_FOUND) {
2849            /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2850            return s16;
2851        }
2852        if(*status == U_BUFFER_OVERFLOW_ERROR) {
2853            *status = U_ZERO_ERROR;
2854            p8 = (char *)malloc(++length8);
2855            if(p8 == NULL) {
2856                return s16;
2857            }
2858            if(idx >= 0) {
2859                s8 = ures_getUTF8StringByIndex(resB, idx, p8, &length8, forceCopy, status);
2860            } else if(key != NULL) {
2861                s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2862            } else {
2863                s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2864            }
2865        }
2866        if(U_FAILURE(*status)) {
2867            /* something unexpected happened */
2868            if(p8 != buffer8) {
2869                free(p8);
2870            }
2871            return s16;
2872        }
2873
2874        if(forceCopy && s8 != p8) {
2875            log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
2876                    resB, (long)idx, key);
2877        }
2878
2879        /* verify NUL-termination */
2880        if((p8 != buffer8 || length8 < sizeof(buffer8)) && s8[length8] != 0) {
2881            log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
2882                    resB, (long)idx, key);
2883        }
2884        /* verify correct string */
2885        i16 = i8 = 0;
2886        while(i16 < length16 && i8 < length8) {
2887            U16_NEXT(s16, i16, length16, c16);
2888            U8_NEXT(s8, i8, length8, c8);
2889            if(c16 != c8) {
2890                log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
2891                        resB, (long)idx, key, (long)c16, (long)c8, (long)i16);
2892            }
2893        }
2894        /* verify correct length */
2895        if(i16 < length16) {
2896            log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
2897                    resB, (long)idx, key, (long)length8, (long)length16);
2898        }
2899        if(i8 < length8) {
2900            log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
2901                    resB, (long)idx, key, (long)length8, (long)length16);
2902        }
2903
2904        /* clean up */
2905        if(p8 != buffer8) {
2906            free(p8);
2907        }
2908    }
2909    return s16;
2910}
2911
2912/*
2913 * API tests for ures_getUTF8String().
2914 * Most cases are handled by tres_getString(), which leaves argument checking
2915 * to be tested here.
2916 * Since the variants share most of their implementation, we only need to test
2917 * one of them.
2918 * We also need not test for checking arguments which will be checked by the
2919 * UTF-16 ures_getStringXYZ() that are called internally.
2920 */
2921static void
2922TestGetUTF8String() {
2923    UResourceBundle *res;
2924    const char *testdatapath;
2925    char buffer8[16];
2926    const char *s8;
2927    int32_t length8;
2928    UErrorCode status;
2929
2930    status = U_ZERO_ERROR;
2931    testdatapath = loadTestData(&status);
2932    if(U_FAILURE(status)) {
2933        log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
2934        return;
2935    }
2936
2937    res = ures_open(testdatapath, "", &status);
2938    if(U_FAILURE(status)) {
2939        log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status));
2940        return;
2941    }
2942
2943    /* one good call */
2944    status = U_ZERO_ERROR;
2945    length8 = (int32_t)sizeof(buffer8);
2946    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
2947    if(status != U_ZERO_ERROR) {
2948        log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status));
2949    }
2950
2951    /* negative capacity */
2952    status = U_ZERO_ERROR;
2953    length8 = -1;
2954    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
2955    if(status != U_ILLEGAL_ARGUMENT_ERROR) {
2956        log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status));
2957    }
2958
2959    /* capacity>0 but dest=NULL */
2960    status = U_ZERO_ERROR;
2961    length8 = (int32_t)sizeof(buffer8);
2962    s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", NULL, &length8, FALSE, &status);
2963    if(status != U_ILLEGAL_ARGUMENT_ERROR) {
2964        log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status));
2965    }
2966
2967    ures_close(res);
2968}
2969
2970static void TestCLDRVersion(void) {
2971  UVersionInfo zeroVersion;
2972  UVersionInfo testExpect;
2973  UVersionInfo testCurrent;
2974  UVersionInfo cldrVersion;
2975  char tmp[200];
2976  UErrorCode status = U_ZERO_ERROR;
2977
2978  /* setup the constant value */
2979  u_versionFromString(zeroVersion, "0.0.0.0");
2980
2981  /* test CLDR value from API */
2982  ulocdata_getCLDRVersion(cldrVersion, &status);
2983  if(U_FAILURE(status)) {
2984    /* the show is pretty much over at this point */
2985    log_err_status(status, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status));
2986    return;
2987  } else {
2988    u_versionToString(cldrVersion, tmp);
2989    log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp);
2990  }
2991
2992
2993  /* setup from resource bundle */
2994  {
2995    UResourceBundle *res;
2996    const char *testdatapath;
2997
2998    status = U_ZERO_ERROR;
2999    testdatapath = loadTestData(&status);
3000    if(U_FAILURE(status)) {
3001        log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
3002        return;
3003    }
3004
3005    res = ures_openDirect(testdatapath, "root", &status);
3006    if(U_FAILURE(status)) {
3007        log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3008));
3009        return;
3010    }
3011    ures_getVersionByKey(res, "ExpectCLDRVersionAtLeast", testExpect, &status);
3012    ures_getVersionByKey(res, "CurrentCLDRVersion", testCurrent, &status);
3013    ures_close(res);
3014    if(U_FAILURE(status)) {
3015        log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status));
3016    }
3017  }
3018  if(U_FAILURE(status)) return;
3019
3020
3021  u_versionToString(testExpect,tmp);
3022  log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp);
3023  if(memcmp(cldrVersion, testExpect, sizeof(UVersionInfo)) < 0) {
3024    log_data_err("CLDR version is too old, expect at least %s.", tmp);
3025  }
3026  u_versionToString(testCurrent,tmp);
3027  log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp);
3028  switch(memcmp(cldrVersion, testCurrent, sizeof(UVersionInfo))) {
3029    case 0: break; /* OK- current. */
3030    case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3031    case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3032  }
3033
3034}
3035