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