]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/restsnew.cpp
ICU-57165.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / restsnew.cpp
1 /********************************************************************
2 * Copyright (c) 1997-2016, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************/
5
6 #include "unicode/utypes.h"
7
8 #include "cmemory.h"
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)); }
38 #define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected)); }
39 #define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); errln(action + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected)); }
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)); }
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 = UPRV_LENGTHOF(param);
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),
158 fail(0)
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 < UPRV_LENGTHOF(param); 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 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
186 case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break;
187 case 1: name = "TestConstruction"; if (exec) TestConstruction(); break;
188 case 2: name = "TestIteration"; if (exec) TestIteration(); break;
189 case 3: name = "TestOtherAPI"; if(exec) TestOtherAPI(); break;
190 case 4: name = "TestNewTypes"; if(exec) TestNewTypes(); break;
191 #else
192 case 0: case 1: case 2: case 3: case 4: name = "skip"; break;
193 #endif
194
195 case 5: name = "TestGetByFallback"; if(exec) TestGetByFallback(); break;
196 default: name = ""; break; //needed to end loop
197 }
198 }
199
200 //***************************************************************************************
201
202 void
203 NewResourceBundleTest::TestResourceBundles()
204 {
205 UErrorCode status = U_ZERO_ERROR;
206 loadTestData(status);
207 if(U_FAILURE(status))
208 {
209 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
210 return;
211 }
212
213 /* Make sure that users using te_IN for the default locale don't get test failures. */
214 Locale originalDefault;
215 if (Locale::getDefault() == Locale("te_IN")) {
216 Locale::setDefault(Locale("en_US"), status);
217 }
218
219 testTag("only_in_Root", TRUE, FALSE, FALSE);
220 testTag("only_in_te", FALSE, TRUE, FALSE);
221 testTag("only_in_te_IN", FALSE, FALSE, TRUE);
222 testTag("in_Root_te", TRUE, TRUE, FALSE);
223 testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
224 testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
225 testTag("in_te_te_IN", FALSE, TRUE, TRUE);
226 testTag("nonexistent", FALSE, FALSE, FALSE);
227 logln("Passed: %d\nFailed: %d", pass, fail);
228
229 /* Restore the default locale for the other tests. */
230 Locale::setDefault(originalDefault, status);
231 }
232
233 void
234 NewResourceBundleTest::TestConstruction()
235 {
236 UErrorCode err = U_ZERO_ERROR;
237 Locale locale("te", "IN");
238
239 const char* testdatapath;
240 testdatapath=loadTestData(err);
241 if(U_FAILURE(err))
242 {
243 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
244 return;
245 }
246
247 /* Make sure that users using te_IN for the default locale don't get test failures. */
248 Locale originalDefault;
249 if (Locale::getDefault() == Locale("te_IN")) {
250 Locale::setDefault(Locale("en_US"), err);
251 }
252
253 ResourceBundle test1((UnicodeString)testdatapath, err);
254 ResourceBundle test2(testdatapath, locale, err);
255
256 UnicodeString result1;
257 UnicodeString result2;
258
259 result1 = test1.getStringEx("string_in_Root_te_te_IN", err);
260 result2 = test2.getStringEx("string_in_Root_te_te_IN", err);
261 if (U_FAILURE(err)) {
262 errln("Something threw an error in TestConstruction()");
263 return;
264 }
265
266 logln("for string_in_Root_te_te_IN, root.txt had " + result1);
267 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
268
269 if (result1 != "ROOT" || result2 != "TE_IN") {
270 errln("Construction test failed; run verbose for more information");
271 }
272
273 const char* version1;
274 const char* version2;
275
276 version1 = test1.getVersionNumber();
277 version2 = test2.getVersionNumber();
278
279 char *versionID1 = new char[1 + strlen(U_ICU_VERSION) + strlen(version1)]; // + 1 for zero byte
280 char *versionID2 = new char[1 + strlen(U_ICU_VERSION) + strlen(version2)]; // + 1 for zero byte
281
282 strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
283
284 strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
285
286 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1 + UnicodeString(" Expect: " ) + versionID1);
287 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2 + UnicodeString(" Expect: " ) + versionID2);
288
289 if (strcmp(version1, versionID1) != 0) {
290 errln("getVersionNumber(version1) failed. %s != %s", version1, versionID1);
291 }
292 if (strcmp(version2, versionID2) != 0) {
293 errln("getVersionNumber(version2) failed. %s != %s", version2, versionID2);
294 }
295 delete[] versionID1;
296 delete[] versionID2;
297
298 /* Restore the default locale for the other tests. */
299 Locale::setDefault(originalDefault, err);
300 }
301
302 void
303 NewResourceBundleTest::TestIteration()
304 {
305 UErrorCode err = U_ZERO_ERROR;
306 const char* testdatapath;
307 const char* data[]={
308 "string_in_Root_te_te_IN", "1",
309 "array_in_Root_te_te_IN", "5",
310 "array_2d_in_Root_te_te_IN", "4",
311 };
312
313 Locale *locale=new Locale("te_IN");
314
315 testdatapath=loadTestData(err);
316 if(U_FAILURE(err))
317 {
318 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
319 return;
320 }
321
322 ResourceBundle test1(testdatapath, *locale, err);
323 if(U_FAILURE(err)){
324 errln("Construction failed");
325 }
326 uint32_t i;
327 int32_t count, row=0, col=0;
328 char buf[5];
329 UnicodeString expected;
330 UnicodeString element("TE_IN");
331 UnicodeString action;
332
333
334 for(i=0; i<UPRV_LENGTHOF(data); i=i+2){
335 action = "te_IN";
336 action +=".get(";
337 action += data[i];
338 action +=", err)";
339 err=U_ZERO_ERROR;
340 ResourceBundle bundle = test1.get(data[i], err);
341 if(!U_FAILURE(err)){
342 action = "te_IN";
343 action +=".getKey()";
344
345 CONFIRM_EQ((UnicodeString)bundle.getKey(), (UnicodeString)data[i]);
346
347 count=0;
348 row=0;
349 while(bundle.hasNext()){
350 action = data[i];
351 action +=".getNextString(err)";
352 row=count;
353 UnicodeString got=bundle.getNextString(err);
354 if(U_SUCCESS(err)){
355 expected=element;
356 if(bundle.getSize() > 1){
357 CONFIRM_EQ(bundle.getType(), URES_ARRAY);
358 expected+=itoa(row, buf);
359 ResourceBundle rowbundle=bundle.get(row, err);
360 if(!U_FAILURE(err) && rowbundle.getSize()>1){
361 col=0;
362 while(rowbundle.hasNext()){
363 expected=element;
364 got=rowbundle.getNextString(err);
365 if(!U_FAILURE(err)){
366 expected+=itoa(row, buf);
367 expected+=itoa(col, buf);
368 col++;
369 CONFIRM_EQ(got, expected);
370 }
371 }
372 CONFIRM_EQ(col, rowbundle.getSize());
373 }
374 }
375 else{
376 CONFIRM_EQ(bundle.getType(), (int32_t)URES_STRING);
377 }
378 }
379 CONFIRM_EQ(got, expected);
380 count++;
381 }
382 action = data[i];
383 action +=".getSize()";
384 CONFIRM_EQ(bundle.getSize(), count);
385 CONFIRM_EQ(count, atoi(data[i+1]));
386 //after reaching the end
387 err=U_ZERO_ERROR;
388 ResourceBundle errbundle=bundle.getNext(err);
389 action = "After reaching the end of the Iterator:- ";
390 action +=data[i];
391 action +=".getNext()";
392 CONFIRM_NE(err, (int32_t)U_ZERO_ERROR);
393 CONFIRM_EQ(u_errorName(err), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR));
394 //reset the iterator
395 err = U_ZERO_ERROR;
396 bundle.resetIterator();
397 /* The following code is causing a crash
398 ****CRASH******
399 */
400
401 bundle.getNext(err);
402 if(U_FAILURE(err)){
403 errln("ERROR: getNext() throw an error");
404 }
405 }
406 }
407 delete locale;
408 }
409
410 // TODO: add operator== and != to ResourceBundle
411 static UBool
412 equalRB(ResourceBundle &a, ResourceBundle &b) {
413 UResType type;
414 UErrorCode status;
415
416 type=a.getType();
417 status=U_ZERO_ERROR;
418 return
419 type==b.getType() &&
420 a.getLocale()==b.getLocale() &&
421 0==strcmp(a.getName(), b.getName()) &&
422 type==URES_STRING ?
423 a.getString(status)==b.getString(status) :
424 type==URES_INT ?
425 a.getInt(status)==b.getInt(status) :
426 TRUE;
427 }
428
429 void
430 NewResourceBundleTest::TestOtherAPI(){
431 UErrorCode err = U_ZERO_ERROR;
432 const char* testdatapath=loadTestData(err);
433 UnicodeString tDataPathUS = UnicodeString(testdatapath, "");
434
435 if(U_FAILURE(err))
436 {
437 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
438 return;
439 }
440
441 /* Make sure that users using te_IN for the default locale don't get test failures. */
442 Locale originalDefault;
443 if (Locale::getDefault() == Locale("te_IN")) {
444 Locale::setDefault(Locale("en_US"), err);
445 }
446
447 Locale *locale=new Locale("te_IN");
448
449 ResourceBundle test0(tDataPathUS, *locale, err);
450 if(U_FAILURE(err)){
451 errln("Construction failed");
452 return;
453 }
454
455 ResourceBundle test1(testdatapath, *locale, err);
456 if(U_FAILURE(err)){
457 errln("Construction failed");
458 return;
459 }
460
461 logln("Testing getLocale()\n");
462 if(strcmp(test1.getLocale().getName(), locale->getName()) !=0 ){
463 errln("FAIL: ResourceBundle::getLocale() failed\n");
464 }
465
466 delete locale;
467
468 logln("Testing ResourceBundle(UErrorCode)\n");
469 ResourceBundle defaultresource(err);
470 ResourceBundle explicitdefaultresource(NULL, Locale::getDefault(), err);
471 if(U_FAILURE(err)){
472 errcheckln(err, "Construction of default resourcebundle failed - %s", u_errorName(err));
473 return;
474 }
475 // You can't compare the default locale to the resolved locale in the
476 // resource bundle due to aliasing, keywords in the default locale
477 // or the chance that the machine running these tests is using a locale
478 // that isn't available in ICU.
479 if(strcmp(defaultresource.getLocale().getName(), explicitdefaultresource.getLocale().getName()) != 0){
480 errln("Construction of default resourcebundle didn't take the defaultlocale. Expected %s Got %s err=%s\n",
481 explicitdefaultresource.getLocale().getName(), defaultresource.getLocale().getName(), u_errorName(err));
482 }
483
484
485 ResourceBundle copyRes(defaultresource);
486 if(strcmp(copyRes.getName(), defaultresource.getName() ) !=0 ||
487 strcmp(test1.getName(), defaultresource.getName() ) ==0 ||
488 strcmp(copyRes.getLocale().getName(), defaultresource.getLocale().getName() ) !=0 ||
489 strcmp(test1.getLocale().getName(), defaultresource.getLocale().getName() ) ==0 )
490 {
491 errln("copy construction failed\n");
492 }
493
494 ResourceBundle defaultSub = defaultresource.get((int32_t)0, err);
495 ResourceBundle defSubCopy(defaultSub);
496 if(strcmp(defSubCopy.getName(), defaultSub.getName() ) !=0 ||
497 strcmp(defSubCopy.getLocale().getName(), defaultSub.getLocale().getName() ) !=0 ){
498 errln("copy construction for subresource failed\n");
499 }
500
501 ResourceBundle *p;
502
503 p = defaultresource.clone();
504 if(p == &defaultresource || !equalRB(*p, defaultresource)) {
505 errln("ResourceBundle.clone() failed");
506 }
507 delete p;
508
509 p = defaultSub.clone();
510 if(p == &defaultSub || !equalRB(*p, defaultSub)) {
511 errln("2nd ResourceBundle.clone() failed");
512 }
513 delete p;
514
515 UVersionInfo ver;
516 copyRes.getVersion(ver);
517
518 logln("Version returned: [%d.%d.%d.%d]\n", ver[0], ver[1], ver[2], ver[3]);
519
520 logln("Testing C like UnicodeString APIs\n");
521
522 UResourceBundle *testCAPI = NULL, *bundle = NULL, *rowbundle = NULL, *temp = NULL;
523 err = U_ZERO_ERROR;
524 const char* data[]={
525 "string_in_Root_te_te_IN", "1",
526 "array_in_Root_te_te_IN", "5",
527 "array_2d_in_Root_te_te_IN", "4",
528 };
529
530
531 testCAPI = ures_open(testdatapath, "te_IN", &err);
532
533 if(U_SUCCESS(err)) {
534 // Do the testing
535 // first iteration
536
537 uint32_t i;
538 int32_t count, row=0, col=0;
539 char buf[5];
540 UnicodeString expected;
541 UnicodeString element("TE_IN");
542 UnicodeString action;
543
544
545 for(i=0; i<UPRV_LENGTHOF(data); i=i+2){
546 action = "te_IN";
547 action +=".get(";
548 action += data[i];
549 action +=", err)";
550 err=U_ZERO_ERROR;
551 bundle = ures_getByKey(testCAPI, data[i], bundle, &err);
552 if(!U_FAILURE(err)){
553 const char* key = NULL;
554 action = "te_IN";
555 action +=".getKey()";
556
557 CONFIRM_EQ((UnicodeString)ures_getKey(bundle), (UnicodeString)data[i]);
558
559 count=0;
560 row=0;
561 while(ures_hasNext(bundle)){
562 action = data[i];
563 action +=".getNextString(err)";
564 row=count;
565 UnicodeString got=ures_getNextUnicodeString(bundle, &key, &err);
566 if(U_SUCCESS(err)){
567 expected=element;
568 if(ures_getSize(bundle) > 1){
569 CONFIRM_EQ(ures_getType(bundle), URES_ARRAY);
570 expected+=itoa(row, buf);
571 rowbundle=ures_getByIndex(bundle, row, rowbundle, &err);
572 if(!U_FAILURE(err) && ures_getSize(rowbundle)>1){
573 col=0;
574 while(ures_hasNext(rowbundle)){
575 expected=element;
576 got=ures_getNextUnicodeString(rowbundle, &key, &err);
577 temp = ures_getByIndex(rowbundle, col, temp, &err);
578 UnicodeString bla = ures_getUnicodeString(temp, &err);
579 UnicodeString bla2 = ures_getUnicodeStringByIndex(rowbundle, col, &err);
580 if(!U_FAILURE(err)){
581 expected+=itoa(row, buf);
582 expected+=itoa(col, buf);
583 col++;
584 CONFIRM_EQ(got, expected);
585 CONFIRM_EQ(bla, expected);
586 CONFIRM_EQ(bla2, expected);
587 }
588 }
589 CONFIRM_EQ(col, ures_getSize(rowbundle));
590 }
591 }
592 else{
593 CONFIRM_EQ(ures_getType(bundle), (int32_t)URES_STRING);
594 }
595 }
596 CONFIRM_EQ(got, expected);
597 count++;
598 }
599 }
600 }
601
602 // Check that ures_getUnicodeString() & variants return a bogus string if failure.
603 // Same relevant code path whether the failure code is passed in
604 // or comes from a lookup error.
605 UErrorCode failure = U_INTERNAL_PROGRAM_ERROR;
606 assertTrue("ures_getUnicodeString(failure).isBogus()",
607 ures_getUnicodeString(testCAPI, &failure).isBogus());
608 assertTrue("ures_getNextUnicodeString(failure).isBogus()",
609 ures_getNextUnicodeString(testCAPI, NULL, &failure).isBogus());
610 assertTrue("ures_getUnicodeStringByIndex(failure).isBogus()",
611 ures_getUnicodeStringByIndex(testCAPI, 999, &failure).isBogus());
612 assertTrue("ures_getUnicodeStringByKey(failure).isBogus()",
613 ures_getUnicodeStringByKey(testCAPI, "bogus key", &failure).isBogus());
614
615 ures_close(temp);
616 ures_close(rowbundle);
617 ures_close(bundle);
618 ures_close(testCAPI);
619 } else {
620 errln("failed to open a resource bundle\n");
621 }
622
623 /* Restore the default locale for the other tests. */
624 Locale::setDefault(originalDefault, err);
625 }
626
627
628
629
630 //***************************************************************************************
631
632 UBool
633 NewResourceBundleTest::testTag(const char* frag,
634 UBool in_Root,
635 UBool in_te,
636 UBool in_te_IN)
637 {
638 int32_t failOrig = fail;
639
640 // Make array from input params
641
642 UBool is_in[] = { in_Root, in_te, in_te_IN };
643
644 const char* NAME[] = { "ROOT", "TE", "TE_IN" };
645
646 // Now try to load the desired items
647
648 char tag[100];
649 UnicodeString action;
650
651 int32_t i,j,row,col, actual_bundle;
652 int32_t index;
653 const char* testdatapath;
654
655 UErrorCode status = U_ZERO_ERROR;
656 testdatapath=loadTestData(status);
657 if(U_FAILURE(status))
658 {
659 dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
660 return FALSE;
661 }
662
663 for (i=0; i<bundles_count; ++i)
664 {
665 action = "Constructor for ";
666 action += param[i].name;
667
668 status = U_ZERO_ERROR;
669 ResourceBundle theBundle( testdatapath, *param[i].locale, status);
670 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
671 CONFIRM_UErrorCode(status,param[i].expected_constructor_status);
672
673 if(i == 5)
674 actual_bundle = 0; /* ne -> default */
675 else if(i == 3)
676 actual_bundle = 1; /* te_NE -> te */
677 else if(i == 4)
678 actual_bundle = 2; /* te_IN_NE -> te_IN */
679 else
680 actual_bundle = i;
681
682
683 UErrorCode expected_resource_status = U_MISSING_RESOURCE_ERROR;
684 for (j=e_te_IN; j>=e_Root; --j)
685 {
686 if (is_in[j] && param[i].inherits[j])
687 {
688 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
689 expected_resource_status = U_ZERO_ERROR;
690 else if(j == 0)
691 expected_resource_status = U_USING_DEFAULT_WARNING;
692 else
693 expected_resource_status = U_USING_FALLBACK_WARNING;
694
695 break;
696 }
697 }
698
699 UErrorCode expected_status;
700
701 UnicodeString base;
702 for (j=param[i].where; j>=0; --j)
703 {
704 if (is_in[j])
705 {
706 base = NAME[j];
707 break;
708 }
709 }
710
711 //--------------------------------------------------------------------------
712 // string
713
714 uprv_strcpy(tag, "string_");
715 uprv_strcat(tag, frag);
716
717 action = param[i].name;
718 action += ".getStringEx(";
719 action += tag;
720 action += ")";
721
722
723 status = U_ZERO_ERROR;
724 UnicodeString string = theBundle.getStringEx(tag, status);
725 if(U_FAILURE(status)) {
726 string.setTo(TRUE, kErrorUChars, kErrorLength);
727 }
728
729 CONFIRM_UErrorCode(status, expected_resource_status);
730
731 UnicodeString expected_string(kErrorUChars);
732 if (U_SUCCESS(status)) {
733 expected_string = base;
734 }
735
736 CONFIRM_EQ(string, expected_string);
737
738 //--------------------------------------------------------------------------
739 // array ResourceBundle using the key
740
741 uprv_strcpy(tag, "array_");
742 uprv_strcat(tag, frag);
743
744 action = param[i].name;
745 action += ".get(";
746 action += tag;
747 action += ")";
748
749 int32_t count = kERROR_COUNT;
750 status = U_ZERO_ERROR;
751 ResourceBundle array = theBundle.get(tag, status);
752 CONFIRM_UErrorCode(status,expected_resource_status);
753
754
755 if (U_SUCCESS(status))
756 {
757 //confirm the resource type is an array
758 UResType bundleType=array.getType();
759 CONFIRM_EQ(bundleType, URES_ARRAY);
760
761 count=array.getSize();
762 CONFIRM_GE(count,1);
763
764 for (j=0; j<count; ++j)
765 {
766 char buf[32];
767 expected_string = base;
768 expected_string += itoa(j,buf);
769 CONFIRM_EQ(array.getNextString(status),expected_string);
770 }
771
772 }
773 else
774 {
775 CONFIRM_EQ(count,kERROR_COUNT);
776 // CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
777 count = 0;
778 }
779
780 //--------------------------------------------------------------------------
781 // arrayItem ResourceBundle using the index
782
783
784 for (j=0; j<100; ++j)
785 {
786 index = count ? (randi(count * 3) - count) : (randi(200) - 100);
787 status = U_ZERO_ERROR;
788 string = kErrorUChars;
789 ResourceBundle array = theBundle.get(tag, status);
790 if(!U_FAILURE(status)){
791 UnicodeString t = array.getStringEx(index, status);
792 if(!U_FAILURE(status)) {
793 string=t;
794 }
795 }
796
797 expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
798 CONFIRM_UErrorCode(status,expected_status);
799
800 if (U_SUCCESS(status)){
801 char buf[32];
802 expected_string = base;
803 expected_string += itoa(index,buf);
804 } else {
805 expected_string = kErrorUChars;
806 }
807 CONFIRM_EQ(string,expected_string);
808
809 }
810
811 //--------------------------------------------------------------------------
812 // 2dArray
813
814 uprv_strcpy(tag, "array_2d_");
815 uprv_strcat(tag, frag);
816
817 action = param[i].name;
818 action += ".get(";
819 action += tag;
820 action += ")";
821
822
823 int32_t row_count = kERROR_COUNT, column_count = kERROR_COUNT;
824 status = U_ZERO_ERROR;
825 ResourceBundle array2d=theBundle.get(tag, status);
826
827 //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
828 CONFIRM_UErrorCode(status,expected_resource_status);
829
830 if (U_SUCCESS(status))
831 {
832 //confirm the resource type is an 2darray
833 UResType bundleType=array2d.getType();
834 CONFIRM_EQ(bundleType, URES_ARRAY);
835
836 row_count=array2d.getSize();
837 CONFIRM_GE(row_count,1);
838
839 for(row=0; row<row_count; ++row){
840 ResourceBundle tablerow=array2d.get(row, status);
841 CONFIRM_UErrorCode(status, expected_resource_status);
842 if(U_SUCCESS(status)){
843 //confirm the resourcetype of each table row is an array
844 UResType rowType=tablerow.getType();
845 CONFIRM_EQ(rowType, URES_ARRAY);
846
847 column_count=tablerow.getSize();
848 CONFIRM_GE(column_count,1);
849
850 for (col=0; j<column_count; ++j) {
851 char buf[32];
852 expected_string = base;
853 expected_string += itoa(row,buf);
854 expected_string += itoa(col,buf);
855 CONFIRM_EQ(tablerow.getNextString(status),expected_string);
856 }
857 }
858 }
859 }else{
860 CONFIRM_EQ(row_count,kERROR_COUNT);
861 CONFIRM_EQ(column_count,kERROR_COUNT);
862 row_count=column_count=0;
863 }
864
865
866
867
868 //--------------------------------------------------------------------------
869 // 2dArrayItem
870 for (j=0; j<200; ++j)
871 {
872 row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100);
873 col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100);
874 status = U_ZERO_ERROR;
875 string = kErrorUChars;
876 ResourceBundle array2d=theBundle.get(tag, status);
877 if(U_SUCCESS(status)){
878 ResourceBundle tablerow=array2d.get(row, status);
879 if(U_SUCCESS(status)) {
880 UnicodeString t=tablerow.getStringEx(col, status);
881 if(U_SUCCESS(status)){
882 string=t;
883 }
884 }
885 }
886 expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
887 expected_resource_status: U_MISSING_RESOURCE_ERROR;
888 CONFIRM_UErrorCode(status,expected_status);
889
890 if (U_SUCCESS(status)){
891 char buf[32];
892 expected_string = base;
893 expected_string += itoa(row,buf);
894 expected_string += itoa(col,buf);
895 } else {
896 expected_string = kErrorUChars;
897 }
898 CONFIRM_EQ(string,expected_string);
899
900 }
901
902 //--------------------------------------------------------------------------
903 // taggedArray
904
905 uprv_strcpy(tag, "tagged_array_");
906 uprv_strcat(tag, frag);
907
908 action = param[i].name;
909 action += ".get(";
910 action += tag;
911 action += ")";
912
913 int32_t tag_count;
914 status = U_ZERO_ERROR;
915
916 ResourceBundle tags=theBundle.get(tag, status);
917 CONFIRM_UErrorCode(status, expected_resource_status);
918
919 if (U_SUCCESS(status)) {
920 UResType bundleType=tags.getType();
921 CONFIRM_EQ(bundleType, URES_TABLE);
922
923 tag_count=tags.getSize();
924 CONFIRM_GE((int32_t)tag_count, (int32_t)0);
925
926 for(index=0; index <tag_count; index++){
927 ResourceBundle tagelement=tags.get(index, status);
928 UnicodeString key=tagelement.getKey();
929 UnicodeString value=tagelement.getNextString(status);
930 logln("tag = " + key + ", value = " + value );
931 if(key.startsWith("tag") && value.startsWith(base)){
932 record_pass();
933 }else{
934 record_fail();
935 }
936
937 }
938
939 for(index=0; index <tag_count; index++){
940 ResourceBundle tagelement=tags.get(index, status);
941 const char *tkey=NULL;
942 UnicodeString value=tagelement.getNextString(&tkey, status);
943 UnicodeString key(tkey);
944 logln("tag = " + key + ", value = " + value );
945 if(value.startsWith(base)){
946 record_pass();
947 }else{
948 record_fail();
949 }
950 }
951
952 }else{
953 tag_count=0;
954 }
955
956
957
958
959 //--------------------------------------------------------------------------
960 // taggedArrayItem
961
962 action = param[i].name;
963 action += ".get(";
964 action += tag;
965 action += ")";
966
967 count = 0;
968 for (index=-20; index<20; ++index)
969 {
970 char buf[32];
971 status = U_ZERO_ERROR;
972 string = kErrorUChars;
973 char item_tag[8];
974 uprv_strcpy(item_tag, "tag");
975 uprv_strcat(item_tag, itoa(index,buf));
976 ResourceBundle tags=theBundle.get(tag, status);
977 if(U_SUCCESS(status)){
978 ResourceBundle tagelement=tags.get(item_tag, status);
979 if(!U_FAILURE(status)){
980 UResType elementType=tagelement.getType();
981 CONFIRM_EQ(elementType, (int32_t)URES_STRING);
982 const char* key=tagelement.getKey();
983 CONFIRM_EQ((UnicodeString)key, (UnicodeString)item_tag);
984 UnicodeString t=tagelement.getString(status);
985 if(!U_FAILURE(status)){
986 string=t;
987 }
988 }
989 if (index < 0) {
990 CONFIRM_UErrorCode(status,U_MISSING_RESOURCE_ERROR);
991 }
992 else{
993 if (status != U_MISSING_RESOURCE_ERROR) {
994 count++;
995 expected_string = base;
996 expected_string += buf;
997 CONFIRM_EQ(string,expected_string);
998 }
999 }
1000 }
1001
1002 }
1003 CONFIRM_EQ(count, tag_count);
1004
1005 }
1006 return (UBool)(failOrig == fail);
1007 }
1008
1009 void
1010 NewResourceBundleTest::record_pass()
1011 {
1012 ++pass;
1013 }
1014 void
1015 NewResourceBundleTest::record_fail()
1016 {
1017 err();
1018 ++fail;
1019 }
1020
1021
1022 void
1023 NewResourceBundleTest::TestNewTypes() {
1024 char action[256];
1025 const char* testdatapath;
1026 UErrorCode status = U_ZERO_ERROR;
1027 uint8_t *binResult = NULL;
1028 int32_t len = 0;
1029 int32_t i = 0;
1030 int32_t intResult = 0;
1031 uint32_t uintResult = 0;
1032 UChar expected[] = { 'a','b','c','\0','d','e','f' };
1033 const char* expect ="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
1034 UChar uExpect[200];
1035
1036 testdatapath=loadTestData(status);
1037
1038 if(U_FAILURE(status))
1039 {
1040 dataerrln("Could not load testdata.dat %s \n",u_errorName(status));
1041 return;
1042 }
1043
1044 ResourceBundle theBundle(testdatapath, "testtypes", status);
1045 ResourceBundle bundle(testdatapath, Locale("te_IN"),status);
1046
1047 UnicodeString emptyStr = theBundle.getStringEx("emptystring", status);
1048 if(emptyStr.length() != 0) {
1049 logln("Empty string returned invalid value\n");
1050 }
1051
1052 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1053
1054 /* This test reads the string "abc\u0000def" from the bundle */
1055 /* if everything is working correctly, the size of this string */
1056 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
1057
1058 strcpy(action, "getting and testing of string with embeded zero");
1059 ResourceBundle res = theBundle.get("zerotest", status);
1060 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1061 CONFIRM_EQ(res.getType(), URES_STRING);
1062 UnicodeString zeroString=res.getString(status);
1063 len = zeroString.length();
1064 if(U_SUCCESS(status)){
1065 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1066 CONFIRM_EQ(len, 7);
1067 CONFIRM_NE(len, 3);
1068 }
1069 for(i=0;i<len;i++){
1070 if(zeroString[i]!= expected[i]){
1071 logln("Output didnot match Expected: \\u%4X Got: \\u%4X", expected[i], zeroString[i]);
1072 }
1073 }
1074
1075 strcpy(action, "getting and testing of binary type");
1076 res = theBundle.get("binarytest", status);
1077 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1078 CONFIRM_EQ(res.getType(), URES_BINARY);
1079 binResult=(uint8_t*)res.getBinary(len, status);
1080 if(U_SUCCESS(status)){
1081 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1082 CONFIRM_EQ(len, 15);
1083 for(i = 0; i<15; i++) {
1084 CONFIRM_EQ(binResult[i], i);
1085 }
1086 }
1087
1088 strcpy(action, "getting and testing of imported binary type");
1089 res = theBundle.get("importtest",status);
1090 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1091 CONFIRM_EQ(res.getType(), URES_BINARY);
1092 binResult=(uint8_t*)res.getBinary(len, status);
1093 if(U_SUCCESS(status)){
1094 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1095 CONFIRM_EQ(len, 15);
1096 for(i = 0; i<15; i++) {
1097 CONFIRM_EQ(binResult[i], i);
1098 }
1099 }
1100
1101 strcpy(action, "getting and testing of integer types");
1102 res = theBundle.get("one", status);
1103 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1104 CONFIRM_EQ(res.getType(), URES_INT);
1105 intResult=res.getInt(status);
1106 uintResult = res.getUInt(status);
1107 if(U_SUCCESS(status)){
1108 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1109 CONFIRM_EQ(uintResult, (uint32_t)intResult);
1110 CONFIRM_EQ(intResult, 1);
1111 }
1112
1113 strcpy(action, "getting minusone");
1114 res = theBundle.get((const char*)"minusone", status);
1115 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1116 CONFIRM_EQ(res.getType(), URES_INT);
1117 intResult=res.getInt(status);
1118 uintResult = res.getUInt(status);
1119 if(U_SUCCESS(status)){
1120 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1121 CONFIRM_EQ(uintResult, 0x0FFFFFFF); /* a 28 bit integer */
1122 CONFIRM_EQ(intResult, -1);
1123 CONFIRM_NE(uintResult, (uint32_t)intResult);
1124 }
1125
1126 strcpy(action, "getting plusone");
1127 res = theBundle.get("plusone",status);
1128 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1129 CONFIRM_EQ(res.getType(), URES_INT);
1130 intResult=res.getInt(status);
1131 uintResult = res.getUInt(status);
1132 if(U_SUCCESS(status)){
1133 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1134 CONFIRM_EQ(uintResult, (uint32_t)intResult);
1135 CONFIRM_EQ(intResult, 1);
1136 }
1137
1138 res = theBundle.get("onehundredtwentythree",status);
1139 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1140 CONFIRM_EQ(res.getType(), URES_INT);
1141 intResult=res.getInt(status);
1142 if(U_SUCCESS(status)){
1143 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1144 CONFIRM_EQ(intResult, 123);
1145 }
1146
1147 /* this tests if escapes are preserved or not */
1148 {
1149 UnicodeString str = theBundle.getStringEx("testescape",status);
1150 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1151 if(U_SUCCESS(status)){
1152 u_charsToUChars(expect,uExpect,(int32_t)uprv_strlen(expect)+1);
1153 if(str.compare(uExpect)!=0){
1154 errln("Did not get the expected string for testescape expected. Expected : "
1155 +UnicodeString(uExpect )+ " Got: " + str);
1156 }
1157 }
1158 }
1159 /* test for jitterbug#1435 */
1160 {
1161 UnicodeString str = theBundle.getStringEx("test_underscores",status);
1162 expect ="test message ....";
1163 CONFIRM_UErrorCode(status, U_ZERO_ERROR);
1164 u_charsToUChars(expect,uExpect,(int32_t)uprv_strlen(expect)+1);
1165 if(str.compare(uExpect)!=0){
1166 errln("Did not get the expected string for test_underscores.\n");
1167 }
1168 }
1169
1170
1171 }
1172
1173 void
1174 NewResourceBundleTest::TestGetByFallback() {
1175 UErrorCode status = U_ZERO_ERROR;
1176
1177 ResourceBundle heRes(NULL, "he", status);
1178
1179 heRes.getWithFallback("calendar", status).getWithFallback("islamic-civil", status).getWithFallback("DateTime", status);
1180 if(U_SUCCESS(status)) {
1181 errln("he locale's Islamic-civil DateTime resource exists. How did it get here?\n");
1182 }
1183 status = U_ZERO_ERROR;
1184
1185 heRes.getWithFallback("calendar", status).getWithFallback("islamic-civil", status).getWithFallback("eras", status);
1186 if(U_FAILURE(status)) {
1187 dataerrln("Didn't get Islamic Eras. I know they are there! - %s", u_errorName(status));
1188 }
1189 status = U_ZERO_ERROR;
1190
1191 ResourceBundle rootRes(NULL, "root", status);
1192 rootRes.getWithFallback("calendar", status).getWithFallback("islamic-civil", status).getWithFallback("DateTime", status);
1193 if(U_SUCCESS(status)) {
1194 errln("Root's Islamic-civil's DateTime resource exists. How did it get here?\n");
1195 }
1196 status = U_ZERO_ERROR;
1197
1198 }
1199 //eof
1200