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