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