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