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