]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2003, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #include "cstring.h" | |
10 | #include "unicode/unistr.h" | |
11 | #include "unicode/resbund.h" | |
12 | #include "restsnew.h" | |
13 | ||
14 | #include <stdlib.h> | |
15 | #include <time.h> | |
16 | #include <string.h> | |
17 | #include <limits.h> | |
18 | ||
19 | //*************************************************************************************** | |
20 | ||
21 | static const UChar kErrorUChars[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 }; | |
22 | static const int32_t kErrorLength = 5; | |
23 | static const int32_t kERROR_COUNT = -1234567; | |
24 | ||
25 | //*************************************************************************************** | |
26 | ||
27 | enum E_Where | |
28 | { | |
29 | e_Root, | |
30 | e_te, | |
31 | e_te_IN, | |
32 | e_Where_count | |
33 | }; | |
34 | ||
35 | //*************************************************************************************** | |
36 | ||
37 | #define CONFIRM_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + (UnicodeString)"\n"); } | |
38 | #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + (UnicodeString)"\n"); } | |
39 | #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + (UnicodeString)"\n"); } | |
40 | ||
41 | #define CONFIRM_UErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (UnicodeString)u_errorName(actual) + (UnicodeString)" instead of " + (UnicodeString)u_errorName(expected) + (UnicodeString)"\n"); } | |
42 | ||
43 | //*************************************************************************************** | |
44 | ||
45 | /** | |
46 | * Convert an integer, positive or negative, to a character string radix 10. | |
47 | */ | |
48 | static char* | |
49 | itoa(int32_t i, char* buf) | |
50 | { | |
51 | char* result = buf; | |
52 | ||
53 | // Handle negative | |
54 | if (i < 0) | |
55 | { | |
56 | *buf++ = '-'; | |
57 | i = -i; | |
58 | } | |
59 | ||
60 | // Output digits in reverse order | |
61 | char* p = buf; | |
62 | do | |
63 | { | |
64 | *p++ = (char)('0' + (i % 10)); | |
65 | i /= 10; | |
66 | } | |
67 | while (i); | |
68 | *p-- = 0; | |
69 | ||
70 | // Reverse the string | |
71 | while (buf < p) | |
72 | { | |
73 | char c = *buf; | |
74 | *buf++ = *p; | |
75 | *p-- = c; | |
76 | } | |
77 | ||
78 | return result; | |
79 | } | |
80 | ||
81 | ||
82 | ||
83 | //*************************************************************************************** | |
84 | ||
85 | // Array of our test objects | |
86 | ||
87 | static struct | |
88 | { | |
89 | const char* name; | |
90 | Locale *locale; | |
91 | UErrorCode expected_constructor_status; | |
92 | E_Where where; | |
93 | UBool like[e_Where_count]; | |
94 | UBool inherits[e_Where_count]; | |
95 | } | |
96 | param[] = | |
97 | { | |
98 | // "te" means test | |
99 | // "IN" means inherits | |
100 | // "NE" or "ne" means "does not exist" | |
101 | ||
102 | { "root", 0, U_ZERO_ERROR, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }, | |
103 | { "te", 0, U_ZERO_ERROR, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } }, | |
104 | { "te_IN", 0, U_ZERO_ERROR, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } }, | |
105 | { "te_NE", 0, U_USING_FALLBACK_WARNING, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } }, | |
106 | { "te_IN_NE", 0, U_USING_FALLBACK_WARNING, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } }, | |
107 | { "ne", 0, U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } } | |
108 | }; | |
109 | ||
110 | static int32_t bundles_count = sizeof(param) / sizeof(param[0]); | |
111 | ||
112 | //*************************************************************************************** | |
113 | ||
114 | /** | |
115 | * Return a random unsigned long l where 0N <= l <= ULONG_MAX. | |
116 | */ | |
117 | ||
118 | static uint32_t | |
119 | randul() | |
120 | { | |
121 | static UBool initialized = FALSE; | |
122 | if (!initialized) | |
123 | { | |
124 | srand((unsigned)time(NULL)); | |
125 | initialized = TRUE; | |
126 | } | |
127 | // Assume rand has at least 12 bits of precision | |
128 | uint32_t l = 0; | |
129 | for (uint32_t i=0; i<sizeof(l); ++i) | |
130 | ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4); | |
131 | return l; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Return a random double x where 0.0 <= x < 1.0. | |
136 | */ | |
137 | static double | |
138 | randd() | |
139 | { | |
140 | return (double)(randul() / ULONG_MAX); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Return a random integer i where 0 <= i < n. | |
145 | */ | |
146 | static int32_t randi(int32_t n) | |
147 | { | |
148 | return (int32_t)(randd() * n); | |
149 | } | |
150 | ||
151 | //*************************************************************************************** | |
152 | ||
153 | /* | |
154 | Don't use more than one of these at a time because of the Locale names | |
155 | */ | |
156 | NewResourceBundleTest::NewResourceBundleTest() | |
157 | : pass(0), | |
374ca955 | 158 | fail(0) |
b75a7d8f A |
159 | { |
160 | if (param[5].locale == NULL) { | |
161 | param[0].locale = new Locale("root"); | |
162 | param[1].locale = new Locale("te"); | |
163 | param[2].locale = new Locale("te", "IN"); | |
164 | param[3].locale = new Locale("te", "NE"); | |
165 | param[4].locale = new Locale("te", "IN", "NE"); | |
166 | param[5].locale = new Locale("ne"); | |
167 | } | |
168 | } | |
169 | ||
170 | NewResourceBundleTest::~NewResourceBundleTest() | |
171 | { | |
172 | if (param[5].locale) { | |
173 | int idx; | |
174 | for (idx = 0; idx < (int)(sizeof(param)/sizeof(param[0])); idx++) { | |
175 | delete param[idx].locale; | |
176 | param[idx].locale = NULL; | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | void NewResourceBundleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
182 | { | |
183 | if (exec) logln("TestSuite ResourceBundleTest: "); | |
184 | switch (index) { | |
185 | case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break; | |
186 | case 1: name = "TestConstruction"; if (exec) TestConstruction(); break; | |
187 | case 2: name = "TestIteration"; if (exec) TestIteration(); break; | |
188 | case 3: name = "TestOtherAPI"; if(exec) TestOtherAPI(); break; | |
189 | case 4: name = "TestNewTypes"; if(exec) TestNewTypes(); break; | |
190 | default: name = ""; break; //needed to end loop | |
191 | } | |
192 | } | |
193 | ||
194 | //*************************************************************************************** | |
195 | ||
196 | void | |
197 | NewResourceBundleTest::TestResourceBundles() | |
198 | { | |
199 | testTag("only_in_Root", TRUE, FALSE, FALSE); | |
200 | testTag("only_in_te", FALSE, TRUE, FALSE); | |
201 | testTag("only_in_te_IN", FALSE, FALSE, TRUE); | |
202 | testTag("in_Root_te", TRUE, TRUE, FALSE); | |
203 | testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE); | |
204 | testTag("in_Root_te_IN", TRUE, FALSE, TRUE); | |
205 | testTag("in_te_te_IN", FALSE, TRUE, TRUE); | |
206 | testTag("nonexistent", FALSE, FALSE, FALSE); | |
207 | logln("Passed: %d\nFailed: %d", pass, fail); | |
208 | } | |
209 | ||
210 | void | |
211 | NewResourceBundleTest::TestConstruction() | |
212 | { | |
213 | { | |
214 | UErrorCode err = U_ZERO_ERROR; | |
215 | const char* testdatapath; | |
216 | Locale locale("te", "IN"); | |
217 | testdatapath=loadTestData(err); | |
218 | if(U_FAILURE(err)) | |
219 | { | |
220 | errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err))); | |
221 | return; | |
222 | } | |
223 | ||
224 | ResourceBundle test1((UnicodeString)testdatapath, err); | |
225 | ResourceBundle test2(testdatapath, locale, err); | |
226 | ||
227 | UnicodeString result1; | |
228 | UnicodeString result2; | |
229 | ||
230 | result1 = test1.getStringEx("string_in_Root_te_te_IN", err); | |
231 | result2 = test2.getStringEx("string_in_Root_te_te_IN", err); | |
232 | if (U_FAILURE(err)) { | |
233 | errln("Something threw an error in TestConstruction()"); | |
234 | return; | |
235 | } | |
236 | ||
237 | logln("for string_in_Root_te_te_IN, root.txt had " + result1); | |
238 | logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2); | |
239 | ||
240 | if (result1 != "ROOT" || result2 != "TE_IN") | |
241 | errln("Construction test failed; run verbose for more information"); | |
242 | ||
243 | const char* version1; | |
244 | const char* version2; | |
245 | ||
246 | version1 = test1.getVersionNumber(); | |
247 | version2 = test2.getVersionNumber(); | |
248 | ||
249 | char *versionID1 = new char[1 + strlen(U_ICU_VERSION) + strlen(version1)]; // + 1 for zero byte | |
250 | char *versionID2 = new char[1 + strlen(U_ICU_VERSION) + strlen(version2)]; // + 1 for zero byte | |
251 | ||
252 | strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed. | |
253 | ||
254 | strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed. | |
255 | ||
256 | logln(UnicodeString("getVersionNumber on default.txt returned ") + version1 + UnicodeString(" Expect: " ) + versionID1); | |
257 | logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2 + UnicodeString(" Expect: " ) + versionID2); | |
258 | ||
259 | if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0) | |
260 | errln("getVersionNumber() failed"); | |
261 | delete[] versionID1; | |
262 | delete[] versionID2; | |
263 | } | |
264 | } | |
265 | ||
266 | void | |
267 | NewResourceBundleTest::TestIteration() | |
268 | { | |
269 | UErrorCode err = U_ZERO_ERROR; | |
270 | const char* testdatapath; | |
271 | const char* data[]={ | |
272 | "string_in_Root_te_te_IN", "1", | |
273 | "array_in_Root_te_te_IN", "5", | |
274 | "array_2d_in_Root_te_te_IN", "4", | |
275 | }; | |
276 | ||
277 | Locale *locale=new Locale("te_IN"); | |
278 | ||
279 | testdatapath=loadTestData(err); | |
280 | if(U_FAILURE(err)) | |
281 | { | |
282 | errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err))); | |
283 | return; | |
284 | } | |
285 | ||
286 | ResourceBundle test1(testdatapath, *locale, err); | |
287 | if(U_FAILURE(err)){ | |
288 | errln("Construction failed"); | |
289 | } | |
290 | uint32_t i; | |
291 | int32_t count, row=0, col=0; | |
292 | char buf[5]; | |
293 | UnicodeString expected; | |
294 | UnicodeString element("TE_IN"); | |
295 | UnicodeString action; | |
296 | ||
297 | ||
298 | for(i=0; i<sizeof(data)/sizeof(data[0]); i=i+2){ | |
299 | action = "te_IN"; | |
300 | action +=".get("; | |
301 | action += data[i]; | |
302 | action +=", err)"; | |
303 | err=U_ZERO_ERROR; | |
304 | ResourceBundle bundle = test1.get(data[i], err); | |
305 | if(!U_FAILURE(err)){ | |
306 | action = "te_IN"; | |
307 | action +=".getKey()"; | |
308 | ||
309 | CONFIRM_EQ((UnicodeString)bundle.getKey(), (UnicodeString)data[i]); | |
310 | ||
311 | count=0; | |
312 | row=0; | |
313 | while(bundle.hasNext()){ | |
314 | action = data[i]; | |
315 | action +=".getNextString(err)"; | |
316 | row=count; | |
317 | UnicodeString got=bundle.getNextString(err); | |
318 | if(U_SUCCESS(err)){ | |
319 | expected=element; | |
320 | if(bundle.getSize() > 1){ | |
321 | CONFIRM_EQ(bundle.getType(), URES_ARRAY); | |
322 | expected+=itoa(row, buf); | |
323 | ResourceBundle rowbundle=bundle.get(row, err); | |
324 | if(!U_FAILURE(err) && rowbundle.getSize()>1){ | |
325 | col=0; | |
326 | while(rowbundle.hasNext()){ | |
327 | expected=element; | |
328 | got=rowbundle.getNextString(err); | |
329 | if(!U_FAILURE(err)){ | |
330 | expected+=itoa(row, buf); | |
331 | expected+=itoa(col, buf); | |
332 | col++; | |
333 | CONFIRM_EQ(got, expected); | |
334 | } | |
335 | } | |
336 | CONFIRM_EQ(col, rowbundle.getSize()); | |
337 | } | |
338 | } | |
339 | else{ | |
340 | CONFIRM_EQ(bundle.getType(), (int32_t)URES_STRING); | |
341 | } | |
342 | } | |
343 | CONFIRM_EQ(got, expected); | |
344 | count++; | |
345 | } | |
346 | action = data[i]; | |
347 | action +=".getSize()"; | |
348 | CONFIRM_EQ(bundle.getSize(), count); | |
349 | CONFIRM_EQ(count, atoi(data[i+1])); | |
350 | //after reaching the end | |
351 | err=U_ZERO_ERROR; | |
352 | ResourceBundle errbundle=bundle.getNext(err); | |
353 | action = "After reaching the end of the Iterator:- "; | |
354 | action +=data[i]; | |
355 | action +=".getNext()"; | |
356 | CONFIRM_NE(err, (int32_t)U_ZERO_ERROR); | |
357 | CONFIRM_EQ(u_errorName(err), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR)); | |
358 | //reset the iterator | |
359 | err = U_ZERO_ERROR; | |
360 | bundle.resetIterator(); | |
361 | /* The following code is causing a crash | |
362 | ****CRASH****** | |
363 | */ | |
364 | ||
365 | bundle.getNext(err); | |
366 | if(U_FAILURE(err)){ | |
367 | errln("ERROR: getNext() throw an error"); | |
368 | }/**/ | |
369 | ||
370 | ||
371 | } | |
372 | ||
373 | ||
374 | ||
375 | } | |
376 | delete locale; | |
377 | } | |
378 | ||
374ca955 A |
379 | // TODO: add operator== and != to ResourceBundle |
380 | static UBool | |
381 | equalRB(ResourceBundle &a, ResourceBundle &b) { | |
382 | UResType type; | |
383 | UErrorCode status; | |
384 | ||
385 | type=a.getType(); | |
386 | status=U_ZERO_ERROR; | |
387 | return | |
388 | type==b.getType() && | |
389 | a.getLocale()==b.getLocale() && | |
390 | 0==strcmp(a.getName(), b.getName()) && | |
391 | type==URES_STRING ? | |
392 | a.getString(status)==b.getString(status) : | |
393 | type==URES_INT ? | |
394 | a.getInt(status)==b.getInt(status) : | |
395 | TRUE; | |
396 | } | |
397 | ||
b75a7d8f A |
398 | void |
399 | NewResourceBundleTest::TestOtherAPI(){ | |
400 | UErrorCode err = U_ZERO_ERROR; | |
401 | const char* testdatapath; | |
402 | testdatapath=loadTestData(err); | |
403 | UnicodeString tDataPathUS = UnicodeString(testdatapath, ""); | |
404 | ||
405 | if(U_FAILURE(err)) | |
406 | { | |
407 | errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err))); | |
408 | return; | |
409 | } | |
410 | Locale *locale=new Locale("te_IN"); | |
411 | ||
412 | ResourceBundle test0(tDataPathUS, *locale, err); | |
413 | if(U_FAILURE(err)){ | |
414 | errln("Construction failed"); | |
415 | return; | |
416 | } | |
417 | ||
418 | ResourceBundle test1(testdatapath, *locale, err); | |
419 | if(U_FAILURE(err)){ | |
420 | errln("Construction failed"); | |
421 | return; | |
422 | } | |
423 | ||
424 | logln("Testing getLocale()\n"); | |
425 | if(strcmp(test1.getLocale().getName(), locale->getName()) !=0 ){ | |
426 | errln("FAIL: ResourceBundle::getLocale() failed\n"); | |
427 | } | |
428 | ||
429 | delete locale; | |
430 | ||
431 | logln("Testing ResourceBundle(UErrorCode)\n"); | |
432 | ResourceBundle defaultresource(err); | |
433 | if(U_FAILURE(err)){ | |
434 | errln("Construction of default resourcebundle failed"); | |
435 | return; | |
436 | } | |
437 | if(strcmp(defaultresource.getLocale().getName(), Locale::getDefault().getName()) != 0){ | |
438 | errln("Construction of default resourcebundle didn't take the defaultlocale\n"); | |
439 | } | |
440 | ||
441 | ||
442 | ResourceBundle copyRes(defaultresource); | |
443 | if(strcmp(copyRes.getName(), defaultresource.getName() ) !=0 || | |
444 | strcmp(test1.getName(), defaultresource.getName() ) ==0 || | |
445 | strcmp(copyRes.getLocale().getName(), defaultresource.getLocale().getName() ) !=0 || | |
446 | strcmp(test1.getLocale().getName(), defaultresource.getLocale().getName() ) ==0 ){ | |
447 | errln("copy construction failed\n"); | |
448 | } | |
449 | ||
450 | ResourceBundle defaultSub = defaultresource.get(1, err); | |
451 | ResourceBundle defSubCopy(defaultSub); | |
452 | if(strcmp(defSubCopy.getName(), defaultSub.getName() ) !=0 || | |
453 | strcmp(defSubCopy.getLocale().getName(), defaultSub.getLocale().getName() ) !=0 ){ | |
454 | errln("copy construction for subresource failed\n"); | |
455 | } | |
456 | ||
374ca955 | 457 | ResourceBundle *p; |
b75a7d8f | 458 | |
374ca955 A |
459 | p = defaultresource.clone(); |
460 | if(p == &defaultresource || !equalRB(*p, defaultresource)) { | |
461 | errln("ResourceBundle.clone() failed"); | |
462 | } | |
463 | delete p; | |
464 | ||
465 | p = defaultSub.clone(); | |
466 | if(p == &defaultSub || !equalRB(*p, defaultSub)) { | |
467 | errln("2nd ResourceBundle.clone() failed"); | |
468 | } | |
469 | delete p; | |
b75a7d8f A |
470 | |
471 | UVersionInfo ver; | |
472 | copyRes.getVersion(ver); | |
473 | ||
474 | logln("Version returned: [%d.%d.%d.%d]\n", ver[0], ver[1], ver[2], ver[3]); | |
475 | ||
476 | logln("Testing C like UnicodeString APIs\n"); | |
477 | ||
478 | UResourceBundle *testCAPI = NULL, *bundle = NULL, *rowbundle = NULL, *temp = NULL; | |
479 | err = U_ZERO_ERROR; | |
480 | const char* data[]={ | |
481 | "string_in_Root_te_te_IN", "1", | |
482 | "array_in_Root_te_te_IN", "5", | |
483 | "array_2d_in_Root_te_te_IN", "4", | |
484 | }; | |
485 | ||
486 | ||
487 | ||
488 | testCAPI = ures_open(testdatapath, "te_IN", &err); | |
489 | ||
490 | if(U_SUCCESS(err)) { | |
491 | // Do the testing | |
492 | // first iteration | |
493 | ||
494 | uint32_t i; | |
495 | int32_t count, row=0, col=0; | |
496 | char buf[5]; | |
497 | UnicodeString expected; | |
498 | UnicodeString element("TE_IN"); | |
499 | UnicodeString action; | |
500 | ||
501 | ||
502 | for(i=0; i<sizeof(data)/sizeof(data[0]); i=i+2){ | |
503 | action = "te_IN"; | |
504 | action +=".get("; | |
505 | action += data[i]; | |
506 | action +=", err)"; | |
507 | err=U_ZERO_ERROR; | |
508 | bundle = ures_getByKey(testCAPI, data[i], bundle, &err); | |
509 | if(!U_FAILURE(err)){ | |
510 | const char* key = NULL; | |
511 | action = "te_IN"; | |
512 | action +=".getKey()"; | |
513 | ||
514 | CONFIRM_EQ((UnicodeString)ures_getKey(bundle), (UnicodeString)data[i]); | |
515 | ||
516 | count=0; | |
517 | row=0; | |
518 | while(ures_hasNext(bundle)){ | |
519 | action = data[i]; | |
520 | action +=".getNextString(err)"; | |
521 | row=count; | |
522 | UnicodeString got=ures_getNextUnicodeString(bundle, &key, &err); | |
523 | if(U_SUCCESS(err)){ | |
524 | expected=element; | |
525 | if(ures_getSize(bundle) > 1){ | |
526 | CONFIRM_EQ(ures_getType(bundle), URES_ARRAY); | |
527 | expected+=itoa(row, buf); | |
528 | rowbundle=ures_getByIndex(bundle, row, rowbundle, &err); | |
529 | if(!U_FAILURE(err) && ures_getSize(rowbundle)>1){ | |
530 | col=0; | |
531 | while(ures_hasNext(rowbundle)){ | |
532 | expected=element; | |
533 | got=ures_getNextUnicodeString(rowbundle, &key, &err); | |
534 | temp = ures_getByIndex(rowbundle, col, temp, &err); | |
535 | UnicodeString bla = ures_getUnicodeString(temp, &err); | |
536 | UnicodeString bla2 = ures_getUnicodeStringByIndex(rowbundle, col, &err); | |
537 | if(!U_FAILURE(err)){ | |
538 | expected+=itoa(row, buf); | |
539 | expected+=itoa(col, buf); | |
540 | col++; | |
541 | CONFIRM_EQ(got, expected); | |
542 | CONFIRM_EQ(bla, expected); | |
543 | CONFIRM_EQ(bla2, expected); | |
544 | } | |
545 | } | |
546 | CONFIRM_EQ(col, ures_getSize(rowbundle)); | |
547 | } | |
548 | } | |
549 | else{ | |
550 | CONFIRM_EQ(ures_getType(bundle), (int32_t)URES_STRING); | |
551 | } | |
552 | } | |
553 | CONFIRM_EQ(got, expected); | |
554 | count++; | |
555 | } | |
556 | } | |
557 | } | |
558 | ures_close(temp); | |
559 | ures_close(rowbundle); | |
560 | ures_close(bundle); | |
561 | ures_close(testCAPI); | |
562 | } else { | |
563 | errln("failed to open a resource bundle\n"); | |
564 | } | |
565 | ||
566 | ||
567 | ||
568 | ||
569 | ||
570 | ||
571 | ||
572 | ||
573 | } | |
574 | ||
575 | ||
576 | ||
577 | ||
578 | ||
579 | ||
580 | //*************************************************************************************** | |
581 | ||
582 | UBool | |
583 | NewResourceBundleTest::testTag(const char* frag, | |
584 | UBool in_Root, | |
585 | UBool in_te, | |
586 | UBool in_te_IN) | |
587 | { | |
588 | int32_t failOrig = fail; | |
589 | ||
590 | // Make array from input params | |
591 | ||
592 | UBool is_in[] = { in_Root, in_te, in_te_IN }; | |
593 | ||
594 | const char* NAME[] = { "ROOT", "TE", "TE_IN" }; | |
595 | ||
596 | // Now try to load the desired items | |
597 | ||
598 | char tag[100]; | |
599 | UnicodeString action; | |
600 | ||
601 | int32_t i,j,row,col, actual_bundle; | |
602 | int32_t index; | |
603 | const char* testdatapath; | |
604 | ||
605 | UErrorCode status = U_ZERO_ERROR; | |
606 | testdatapath=loadTestData(status); | |
607 | if(U_FAILURE(status)) | |
608 | { | |
609 | errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status))); | |
610 | return FALSE; | |
611 | } | |
612 | ||
613 | for (i=0; i<bundles_count; ++i) | |
614 | { | |
615 | action = "Constructor for "; | |
616 | action += param[i].name; | |
617 | ||
618 | status = U_ZERO_ERROR; | |
619 | ResourceBundle theBundle( testdatapath, *param[i].locale, status); | |
620 | //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status); | |
621 | CONFIRM_UErrorCode(status,param[i].expected_constructor_status); | |
622 | ||
623 | if(i == 5) | |
624 | actual_bundle = 0; /* ne -> default */ | |
625 | else if(i == 3) | |
626 | actual_bundle = 1; /* te_NE -> te */ | |
627 | else if(i == 4) | |
628 | actual_bundle = 2; /* te_IN_NE -> te_IN */ | |
629 | else | |
630 | actual_bundle = i; | |
631 | ||
632 | ||
633 | UErrorCode expected_resource_status = U_MISSING_RESOURCE_ERROR; | |
634 | for (j=e_te_IN; j>=e_Root; --j) | |
635 | { | |
636 | if (is_in[j] && param[i].inherits[j]) | |
637 | { | |
638 | if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */ | |
639 | expected_resource_status = U_ZERO_ERROR; | |
640 | else if(j == 0) | |
641 | expected_resource_status = U_USING_DEFAULT_WARNING; | |
642 | else | |
643 | expected_resource_status = U_USING_FALLBACK_WARNING; | |
644 | ||
645 | break; | |
646 | } | |
647 | } | |
648 | ||
649 | UErrorCode expected_status; | |
650 | ||
651 | UnicodeString base; | |
652 | for (j=param[i].where; j>=0; --j) | |
653 | { | |
654 | if (is_in[j]) | |
655 | { | |
656 | base = NAME[j]; | |
657 | break; | |
658 | } | |
659 | } | |
660 | ||
661 | //-------------------------------------------------------------------------- | |
662 | // string | |
663 | ||
664 | uprv_strcpy(tag, "string_"); | |
665 | uprv_strcat(tag, frag); | |
666 | ||
667 | action = param[i].name; | |
668 | action += ".getStringEx("; | |
669 | action += tag; | |
670 | action += ")"; | |
671 | ||
672 | ||
673 | status = U_ZERO_ERROR; | |
674 | UnicodeString string = theBundle.getStringEx(tag, status); | |
675 | if(U_FAILURE(status)) { | |
676 | string.setTo(TRUE, kErrorUChars, kErrorLength); | |
677 | } | |
678 | ||
679 | CONFIRM_UErrorCode(status, expected_resource_status); | |
680 | ||
681 | UnicodeString expected_string(kErrorUChars); | |
682 | if (U_SUCCESS(status)) { | |
683 | expected_string = base; | |
684 | } | |
685 | ||
686 | CONFIRM_EQ(string, expected_string); | |
687 | ||
688 | //-------------------------------------------------------------------------- | |
689 | // array ResourceBundle using the key | |
690 | ||
691 | uprv_strcpy(tag, "array_"); | |
692 | uprv_strcat(tag, frag); | |
693 | ||
694 | action = param[i].name; | |
695 | action += ".get("; | |
696 | action += tag; | |
697 | action += ")"; | |
698 | ||
699 | int32_t count = kERROR_COUNT; | |
700 | status = U_ZERO_ERROR; | |
701 | ResourceBundle array = theBundle.get(tag, status); | |
702 | CONFIRM_UErrorCode(status,expected_resource_status); | |
703 | ||
704 | ||
705 | if (U_SUCCESS(status)) | |
706 | { | |
707 | //confirm the resource type is an array | |
708 | UResType bundleType=array.getType(); | |
709 | CONFIRM_EQ(bundleType, URES_ARRAY); | |
710 | ||
711 | count=array.getSize(); | |
712 | CONFIRM_GE(count,1); | |
713 | ||
714 | for (j=0; j<count; ++j) | |
715 | { | |
716 | char buf[32]; | |
717 | expected_string = base; | |
718 | expected_string += itoa(j,buf); | |
719 | CONFIRM_EQ(array.getNextString(status),expected_string); | |
720 | } | |
721 | ||
722 | } | |
723 | else | |
724 | { | |
725 | CONFIRM_EQ(count,kERROR_COUNT); | |
726 | // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0); | |
727 | count = 0; | |
728 | } | |
729 | ||
730 | //-------------------------------------------------------------------------- | |
731 | // arrayItem ResourceBundle using the index | |
732 | ||
733 | ||
734 | for (j=0; j<100; ++j) | |
735 | { | |
736 | index = count ? (randi(count * 3) - count) : (randi(200) - 100); | |
737 | status = U_ZERO_ERROR; | |
738 | string = kErrorUChars; | |
739 | ResourceBundle array = theBundle.get(tag, status); | |
740 | if(!U_FAILURE(status)){ | |
741 | UnicodeString t = array.getStringEx(index, status); | |
742 | if(!U_FAILURE(status)) { | |
743 | string=t; | |
744 | } | |
745 | } | |
746 | ||
747 | expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR; | |
748 | CONFIRM_UErrorCode(status,expected_status); | |
749 | ||
750 | if (U_SUCCESS(status)){ | |
751 | char buf[32]; | |
752 | expected_string = base; | |
753 | expected_string += itoa(index,buf); | |
754 | } else { | |
755 | expected_string = kErrorUChars; | |
756 | } | |
757 | CONFIRM_EQ(string,expected_string); | |
758 | ||
759 | } | |
760 | ||
761 | //-------------------------------------------------------------------------- | |
762 | // 2dArray | |
763 | ||
764 | uprv_strcpy(tag, "array_2d_"); | |
765 | uprv_strcat(tag, frag); | |
766 | ||
767 | action = param[i].name; | |
768 | action += ".get("; | |
769 | action += tag; | |
770 | action += ")"; | |
771 | ||
772 | ||
773 | int32_t row_count = kERROR_COUNT, column_count = kERROR_COUNT; | |
774 | status = U_ZERO_ERROR; | |
775 | ResourceBundle array2d=theBundle.get(tag, status); | |
776 | ||
777 | //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status); | |
778 | CONFIRM_UErrorCode(status,expected_resource_status); | |
779 | ||
780 | if (U_SUCCESS(status)) | |
781 | { | |
782 | //confirm the resource type is an 2darray | |
783 | UResType bundleType=array2d.getType(); | |
784 | CONFIRM_EQ(bundleType, URES_ARRAY); | |
785 | ||
786 | row_count=array2d.getSize(); | |
787 | CONFIRM_GE(row_count,1); | |
788 | ||
789 | for(row=0; row<row_count; ++row){ | |
790 | ResourceBundle tablerow=array2d.get(row, status); | |
791 | CONFIRM_UErrorCode(status, expected_resource_status); | |
792 | if(U_SUCCESS(status)){ | |
793 | //confirm the resourcetype of each table row is an array | |
794 | UResType rowType=tablerow.getType(); | |
795 | CONFIRM_EQ(rowType, URES_ARRAY); | |
796 | ||
797 | column_count=tablerow.getSize(); | |
798 | CONFIRM_GE(column_count,1); | |
799 | ||
800 | for (col=0; j<column_count; ++j) { | |
801 | char buf[32]; | |
802 | expected_string = base; | |
803 | expected_string += itoa(row,buf); | |
804 | expected_string += itoa(col,buf); | |
805 | CONFIRM_EQ(tablerow.getNextString(status),expected_string); | |
806 | } | |
807 | } | |
808 | } | |
809 | }else{ | |
810 | CONFIRM_EQ(row_count,kERROR_COUNT); | |
811 | CONFIRM_EQ(column_count,kERROR_COUNT); | |
812 | row_count=column_count=0; | |
813 | } | |
814 | ||
815 | ||
816 | ||
817 | ||
818 | //-------------------------------------------------------------------------- | |
819 | // 2dArrayItem | |
820 | for (j=0; j<200; ++j) | |
821 | { | |
822 | row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100); | |
823 | col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100); | |
824 | status = U_ZERO_ERROR; | |
825 | string = kErrorUChars; | |
826 | ResourceBundle array2d=theBundle.get(tag, status); | |
827 | if(U_SUCCESS(status)){ | |
828 | ResourceBundle tablerow=array2d.get(row, status); | |
829 | if(U_SUCCESS(status)) { | |
830 | UnicodeString t=tablerow.getStringEx(col, status); | |
831 | if(U_SUCCESS(status)){ | |
832 | string=t; | |
833 | } | |
834 | } | |
835 | } | |
836 | expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ? | |
837 | expected_resource_status: U_MISSING_RESOURCE_ERROR; | |
838 | CONFIRM_UErrorCode(status,expected_status); | |
839 | ||
840 | if (U_SUCCESS(status)){ | |
841 | char buf[32]; | |
842 | expected_string = base; | |
843 | expected_string += itoa(row,buf); | |
844 | expected_string += itoa(col,buf); | |
845 | } else { | |
846 | expected_string = kErrorUChars; | |
847 | } | |
848 | CONFIRM_EQ(string,expected_string); | |
849 | ||
850 | } | |
851 | ||
852 | //-------------------------------------------------------------------------- | |
853 | // taggedArray | |
854 | ||
855 | uprv_strcpy(tag, "tagged_array_"); | |
856 | uprv_strcat(tag, frag); | |
857 | ||
858 | action = param[i].name; | |
859 | action += ".get("; | |
860 | action += tag; | |
861 | action += ")"; | |
862 | ||
863 | int32_t tag_count; | |
864 | status = U_ZERO_ERROR; | |
865 | ||
866 | ResourceBundle tags=theBundle.get(tag, status); | |
867 | CONFIRM_UErrorCode(status, expected_resource_status); | |
868 | ||
869 | if (U_SUCCESS(status)) { | |
870 | UResType bundleType=tags.getType(); | |
871 | CONFIRM_EQ(bundleType, URES_TABLE); | |
872 | ||
873 | tag_count=tags.getSize(); | |
874 | CONFIRM_GE((int32_t)tag_count, (int32_t)0); | |
875 | ||
876 | for(index=0; index <tag_count; index++){ | |
877 | ResourceBundle tagelement=tags.get(index, status); | |
878 | UnicodeString key=tagelement.getKey(); | |
879 | UnicodeString value=tagelement.getNextString(status); | |
880 | logln("tag = " + key + ", value = " + value ); | |
881 | if(key.startsWith("tag") && value.startsWith(base)){ | |
882 | record_pass(); | |
883 | }else{ | |
884 | record_fail(); | |
885 | } | |
886 | ||
887 | } | |
888 | ||
889 | for(index=0; index <tag_count; index++){ | |
890 | ResourceBundle tagelement=tags.get(index, status); | |
891 | const char *tkey=NULL; | |
892 | UnicodeString value=tagelement.getNextString(&tkey, status); | |
893 | UnicodeString key(tkey); | |
894 | logln("tag = " + key + ", value = " + value ); | |
895 | if(value.startsWith(base)){ | |
896 | record_pass(); | |
897 | }else{ | |
898 | record_fail(); | |
899 | } | |
900 | } | |
901 | ||
902 | }else{ | |
903 | tag_count=0; | |
904 | } | |
905 | ||
906 | ||
907 | ||
908 | ||
909 | //-------------------------------------------------------------------------- | |
910 | // taggedArrayItem | |
911 | ||
912 | action = param[i].name; | |
913 | action += ".get("; | |
914 | action += tag; | |
915 | action += ")"; | |
916 | ||
917 | count = 0; | |
918 | for (index=-20; index<20; ++index) | |
919 | { | |
920 | char buf[32]; | |
921 | status = U_ZERO_ERROR; | |
922 | string = kErrorUChars; | |
923 | char item_tag[8]; | |
924 | uprv_strcpy(item_tag, "tag"); | |
925 | uprv_strcat(item_tag, itoa(index,buf)); | |
926 | ResourceBundle tags=theBundle.get(tag, status); | |
927 | if(U_SUCCESS(status)){ | |
928 | ResourceBundle tagelement=tags.get(item_tag, status); | |
929 | if(!U_FAILURE(status)){ | |
930 | UResType elementType=tagelement.getType(); | |
931 | CONFIRM_EQ(elementType, (int32_t)URES_STRING); | |
932 | const char* key=tagelement.getKey(); | |
933 | CONFIRM_EQ((UnicodeString)key, (UnicodeString)item_tag); | |
934 | UnicodeString t=tagelement.getString(status); | |
935 | if(!U_FAILURE(status)){ | |
936 | string=t; | |
937 | } | |
938 | } | |
939 | if (index < 0) { | |
940 | CONFIRM_UErrorCode(status,U_MISSING_RESOURCE_ERROR); | |
941 | } | |
942 | else{ | |
943 | if (status != U_MISSING_RESOURCE_ERROR) { | |
944 | count++; | |
945 | expected_string = base; | |
946 | expected_string += buf; | |
947 | CONFIRM_EQ(string,expected_string); | |
948 | } | |
949 | } | |
950 | } | |
951 | ||
952 | } | |
953 | CONFIRM_EQ(count, tag_count); | |
954 | ||
955 | } | |
956 | return (UBool)(failOrig == fail); | |
957 | } | |
958 | ||
959 | void | |
960 | NewResourceBundleTest::record_pass() | |
961 | { | |
962 | ++pass; | |
963 | } | |
964 | void | |
965 | NewResourceBundleTest::record_fail() | |
966 | { | |
967 | err(); | |
968 | ++fail; | |
969 | } | |
970 | ||
971 | ||
972 | void | |
973 | NewResourceBundleTest::TestNewTypes() { | |
974 | char action[256]; | |
975 | const char* testdatapath; | |
976 | UErrorCode status = U_ZERO_ERROR; | |
977 | uint8_t *binResult = NULL; | |
978 | int32_t len = 0; | |
979 | int32_t i = 0; | |
980 | int32_t intResult = 0; | |
981 | uint32_t uintResult = 0; | |
982 | UChar expected[] = { 'a','b','c','\0','d','e','f' }; | |
983 | const char* expect ="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''"; | |
984 | UChar uExpect[200]; | |
985 | ||
986 | testdatapath=loadTestData(status); | |
987 | ||
988 | if(U_FAILURE(status)) | |
989 | { | |
990 | logln("Could not load testdata.dat %s \n",u_errorName(status)); | |
991 | return; | |
992 | } | |
993 | ||
994 | ResourceBundle theBundle(testdatapath, "testtypes", status); | |
374ca955 | 995 | ResourceBundle bundle(testdatapath, Locale("te_IN"),status); |
b75a7d8f A |
996 | |
997 | UnicodeString emptyStr = theBundle.getStringEx("emptystring", status); | |
998 | if(!emptyStr.length()==0) { | |
999 | logln("Empty string returned invalid value\n"); | |
1000 | } | |
1001 | ||
1002 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1003 | ||
1004 | /* This test reads the string "abc\u0000def" from the bundle */ | |
1005 | /* if everything is working correctly, the size of this string */ | |
1006 | /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/ | |
1007 | ||
1008 | strcpy(action, "getting and testing of string with embeded zero"); | |
1009 | ResourceBundle res = theBundle.get("zerotest", status); | |
1010 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1011 | CONFIRM_EQ(res.getType(), URES_STRING); | |
1012 | UnicodeString zeroString=res.getString(status); | |
374ca955 | 1013 | len = zeroString.length(); |
b75a7d8f A |
1014 | if(U_SUCCESS(status)){ |
1015 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1016 | CONFIRM_EQ(len, 7); | |
1017 | CONFIRM_NE(len, 3); | |
1018 | } | |
1019 | for(i=0;i<len;i++){ | |
1020 | if(zeroString[i]!= expected[i]){ | |
1021 | logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected[i], zeroString[i]); | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | strcpy(action, "getting and testing of binary type"); | |
1026 | res = theBundle.get("binarytest", status); | |
1027 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1028 | CONFIRM_EQ(res.getType(), URES_BINARY); | |
1029 | binResult=(uint8_t*)res.getBinary(len, status); | |
1030 | if(U_SUCCESS(status)){ | |
1031 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1032 | CONFIRM_EQ(len, 15); | |
1033 | for(i = 0; i<15; i++) { | |
1034 | CONFIRM_EQ(binResult[i], i); | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | strcpy(action, "getting and testing of imported binary type"); | |
1039 | res = theBundle.get("importtest",status); | |
1040 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1041 | CONFIRM_EQ(res.getType(), URES_BINARY); | |
1042 | binResult=(uint8_t*)res.getBinary(len, status); | |
1043 | if(U_SUCCESS(status)){ | |
1044 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1045 | CONFIRM_EQ(len, 15); | |
1046 | for(i = 0; i<15; i++) { | |
1047 | CONFIRM_EQ(binResult[i], i); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | strcpy(action, "getting and testing of integer types"); | |
1052 | res = theBundle.get("one", status); | |
1053 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1054 | CONFIRM_EQ(res.getType(), URES_INT); | |
1055 | intResult=res.getInt(status); | |
1056 | uintResult = res.getUInt(status); | |
1057 | if(U_SUCCESS(status)){ | |
1058 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1059 | CONFIRM_EQ(uintResult, (uint32_t)intResult); | |
1060 | CONFIRM_EQ(intResult, 1); | |
1061 | } | |
1062 | ||
1063 | strcpy(action, "getting minusone"); | |
1064 | res = theBundle.get((const char*)"minusone", status); | |
1065 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1066 | CONFIRM_EQ(res.getType(), URES_INT); | |
1067 | intResult=res.getInt(status); | |
1068 | uintResult = res.getUInt(status); | |
1069 | if(U_SUCCESS(status)){ | |
1070 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1071 | CONFIRM_EQ(uintResult, 0x0FFFFFFF); /* a 28 bit integer */ | |
1072 | CONFIRM_EQ(intResult, -1); | |
1073 | CONFIRM_NE(uintResult, (uint32_t)intResult); | |
1074 | } | |
1075 | ||
1076 | strcpy(action, "getting plusone"); | |
1077 | res = theBundle.get("plusone",status); | |
1078 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1079 | CONFIRM_EQ(res.getType(), URES_INT); | |
1080 | intResult=res.getInt(status); | |
1081 | uintResult = res.getUInt(status); | |
1082 | if(U_SUCCESS(status)){ | |
1083 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1084 | CONFIRM_EQ(uintResult, (uint32_t)intResult); | |
1085 | CONFIRM_EQ(intResult, 1); | |
1086 | } | |
1087 | ||
1088 | res = theBundle.get("onehundredtwentythree",status); | |
1089 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1090 | CONFIRM_EQ(res.getType(), URES_INT); | |
1091 | intResult=res.getInt(status); | |
1092 | if(U_SUCCESS(status)){ | |
1093 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1094 | CONFIRM_EQ(intResult, 123); | |
1095 | } | |
1096 | ||
1097 | /* this tests if escapes are preserved or not */ | |
1098 | { | |
1099 | UnicodeString str = theBundle.getStringEx("testescape",status); | |
1100 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
1101 | if(U_SUCCESS(status)){ | |
374ca955 | 1102 | u_charsToUChars(expect,uExpect,(int32_t)uprv_strlen(expect)+1); |
b75a7d8f A |
1103 | if(str.compare(uExpect)!=0){ |
1104 | errln("Did not get the expected string for testescape expected. Expected : " | |
374ca955 | 1105 | +UnicodeString(uExpect )+ " Got: " + str); |
b75a7d8f A |
1106 | } |
1107 | } | |
1108 | } | |
1109 | /* test for jitterbug#1435 */ | |
1110 | { | |
1111 | UnicodeString str = theBundle.getStringEx("test_underscores",status); | |
1112 | expect ="test message ...."; | |
1113 | CONFIRM_UErrorCode(status, U_ZERO_ERROR); | |
374ca955 | 1114 | u_charsToUChars(expect,uExpect,(int32_t)uprv_strlen(expect)+1); |
b75a7d8f A |
1115 | if(str.compare(uExpect)!=0){ |
1116 | errln("Did not get the expected string for test_underscores.\n"); | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | ||
1121 | } | |
1122 | //eof | |
1123 |