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