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