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