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