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