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