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