• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2016, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 
9 #include "unicode/utypes.h"
10 
11 #include "cmemory.h"
12 #include "cstring.h"
13 #include "unicode/unistr.h"
14 #include "unicode/uniset.h"
15 #include "unicode/resbund.h"
16 #include "restest.h"
17 
18 #include <stdlib.h>
19 #include <time.h>
20 #include <string.h>
21 #include <limits.h>
22 
23 //***************************************************************************************
24 
25 static const UChar kErrorUChars[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
26 static const int32_t kErrorLength = 5;
27 
28 //***************************************************************************************
29 
30 enum E_Where
31 {
32     e_Root,
33     e_te,
34     e_te_IN,
35     e_Where_count
36 };
37 
38 //***************************************************************************************
39 
40 #define CONFIRM_EQ(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
41     if ((expected)==(actual)) { \
42         record_pass(myAction); \
43     } else { \
44         record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + "\n"); \
45     } \
46 } UPRV_BLOCK_MACRO_END
47 #define CONFIRM_GE(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
48     if ((actual)>=(expected)) { \
49         record_pass(myAction); \
50     } else { \
51         record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + "\n"); \
52     } \
53 } UPRV_BLOCK_MACRO_END
54 #define CONFIRM_NE(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
55     if ((expected)!=(actual)) { \
56         record_pass(myAction); \
57     } else { \
58         record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + "\n"); \
59     } \
60 } UPRV_BLOCK_MACRO_END
61 
62 #define CONFIRM_UErrorCode(actual, expected, myAction) UPRV_BLOCK_MACRO_BEGIN { \
63     if ((expected)==(actual)) { \
64         record_pass(myAction); \
65     } else { \
66         record_fail(myAction + (UnicodeString)" returned " + u_errorName(actual) + " instead of " + u_errorName(expected) + "\n"); \
67     } \
68 } UPRV_BLOCK_MACRO_END
69 
70 //***************************************************************************************
71 
72 /**
73  * Convert an integer, positive or negative, to a character string radix 10.
74  */
75 char*
itoa(int32_t i,char * buf)76 itoa(int32_t i, char* buf)
77 {
78     char* result = buf;
79 
80     // Handle negative
81     if (i < 0)
82     {
83         *buf++ = '-';
84         i = -i;
85     }
86 
87     // Output digits in reverse order
88     char* p = buf;
89     do
90     {
91         *p++ = (char)('0' + (i % 10));
92         i /= 10;
93     }
94     while (i);
95     *p-- = 0;
96 
97     // Reverse the string
98     while (buf < p)
99     {
100         char c = *buf;
101         *buf++ = *p;
102         *p-- = c;
103     }
104 
105     return result;
106 }
107 
108 
109 
110 //***************************************************************************************
111 
112 // Array of our test objects
113 
114 static struct
115 {
116     const char* name;
117     Locale *locale;
118     UErrorCode expected_constructor_status;
119     E_Where where;
120     UBool like[e_Where_count];
121     UBool inherits[e_Where_count];
122 }
123 param[] =
124 {
125     // "te" means test
126     // "IN" means inherits
127     // "NE" or "ne" means "does not exist"
128 
129     { "root",       NULL,   U_ZERO_ERROR,             e_Root,      { true, false, false }, { true, false, false } },
130     { "te",         NULL,   U_ZERO_ERROR,             e_te,        { false, true, false }, { true, true, false } },
131     { "te_IN",      NULL,   U_ZERO_ERROR,             e_te_IN,     { false, false, true }, { true, true, true } },
132     { "te_NE",      NULL,   U_USING_FALLBACK_WARNING, e_te,        { false, true, false }, { true, true, false } },
133     { "te_IN_NE",   NULL,   U_USING_FALLBACK_WARNING, e_te_IN,     { false, false, true }, { true, true, true } },
134     { "ne",         NULL,   U_USING_DEFAULT_WARNING,  e_Root,      { true, false, false }, { true, false, false } }
135 };
136 
137 static const int32_t bundles_count = UPRV_LENGTHOF(param);
138 
139 //***************************************************************************************
140 
141 /**
142  * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
143  */
144 
145 uint32_t
randul()146 randul()
147 {
148     static UBool initialized = false;
149     if (!initialized)
150     {
151         srand((unsigned)time(NULL));
152         initialized = true;
153     }
154     // Assume rand has at least 12 bits of precision
155     uint32_t l = 0;
156     for (uint32_t i=0; i<sizeof(l); ++i)
157         ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
158     return l;
159 }
160 
161 /**
162  * Return a random double x where 0.0 <= x < 1.0.
163  */
164 double
randd()165 randd()
166 {
167     return (double)(randul() / ULONG_MAX);
168 }
169 
170 /**
171  * Return a random integer i where 0 <= i < n.
172  */
randi(int32_t n)173 int32_t randi(int32_t n)
174 {
175     return (int32_t)(randd() * n);
176 }
177 
178 //***************************************************************************************
179 
180 /*
181  Don't use more than one of these at a time because of the Locale names
182 */
ResourceBundleTest()183 ResourceBundleTest::ResourceBundleTest()
184 : pass(0),
185   fail(0)
186 {
187     if (param[5].locale == NULL) {
188         param[0].locale = new Locale("root");
189         param[1].locale = new Locale("te");
190         param[2].locale = new Locale("te", "IN");
191         param[3].locale = new Locale("te", "NE");
192         param[4].locale = new Locale("te", "IN", "NE");
193         param[5].locale = new Locale("ne");
194     }
195 }
196 
~ResourceBundleTest()197 ResourceBundleTest::~ResourceBundleTest()
198 {
199     if (param[5].locale) {
200         int idx;
201         for (idx = 0; idx < UPRV_LENGTHOF(param); idx++) {
202             delete param[idx].locale;
203             param[idx].locale = NULL;
204         }
205     }
206 }
207 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)208 void ResourceBundleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
209 {
210     if (exec) logln("TestSuite ResourceBundleTest: ");
211     switch (index) {
212 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
213     case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break;
214     case 1: name = "TestConstruction"; if (exec) TestConstruction(); break;
215     case 2: name = "TestGetSize"; if (exec) TestGetSize(); break;
216     case 3: name = "TestGetLocaleByType"; if (exec) TestGetLocaleByType(); break;
217 #else
218     case 0: case 1: case 2: case 3: name = "skip"; break;
219 #endif
220 
221     case 4: name = "TestExemplar"; if (exec) TestExemplar(); break;
222         default: name = ""; break; //needed to end loop
223     }
224 }
225 
226 //***************************************************************************************
227 
228 void
TestResourceBundles()229 ResourceBundleTest::TestResourceBundles()
230 {
231     UErrorCode status = U_ZERO_ERROR;
232 
233     loadTestData(status);
234     if(U_FAILURE(status))
235     {
236         dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
237         return;
238     }
239 
240     /* Make sure that users using te_IN for the default locale don't get test failures. */
241     Locale originalDefault;
242     if (Locale::getDefault() == Locale("te_IN")) {
243         Locale::setDefault(Locale("en_US"), status);
244     }
245 
246     testTag("only_in_Root", true, false, false);
247     testTag("only_in_te", false, true, false);
248     testTag("only_in_te_IN", false, false, true);
249     testTag("in_Root_te", true, true, false);
250     testTag("in_Root_te_te_IN", true, true, true);
251     testTag("in_Root_te_IN", true, false, true);
252     testTag("in_te_te_IN", false, true, true);
253     testTag("nonexistent", false, false, false);
254     logln("Passed: %d\nFailed: %d", pass, fail);
255 
256     /* Restore the default locale for the other tests. */
257     Locale::setDefault(originalDefault, status);
258 }
259 
260 void
TestConstruction()261 ResourceBundleTest::TestConstruction()
262 {
263     UErrorCode   err = U_ZERO_ERROR;
264     Locale       locale("te", "IN");
265 
266     const char* testdatapath=loadTestData(err);
267     if(U_FAILURE(err))
268     {
269         dataerrln("Could not load testdata.dat " + UnicodeString(testdatapath) +  ", " + UnicodeString(u_errorName(err)));
270         return;
271     }
272 
273     /* Make sure that users using te_IN for the default locale don't get test failures. */
274     Locale originalDefault;
275     if (Locale::getDefault() == Locale("te_IN")) {
276         Locale::setDefault(Locale("en_US"), err);
277     }
278 
279     ResourceBundle  test1((UnicodeString)testdatapath, err);
280     ResourceBundle  test2(testdatapath, locale, err);
281     //ResourceBundle  test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
282     //ResourceBundle  test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
283 
284     UnicodeString   result1(test1.getStringEx("string_in_Root_te_te_IN", err));
285     UnicodeString   result2(test2.getStringEx("string_in_Root_te_te_IN", err));
286 
287     if (U_FAILURE(err)) {
288         errln("Something threw an error in TestConstruction()");
289         return;
290     }
291 
292     logln("for string_in_Root_te_te_IN, default.txt had " + result1);
293     logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
294 
295     if (result1 != "ROOT" || result2 != "TE_IN")
296         errln("Construction test failed; run verbose for more information");
297 
298     const char* version1;
299     const char* version2;
300 
301     version1 = test1.getVersionNumber();
302     version2 = test2.getVersionNumber();
303 
304     char *versionID1 = new char[1+strlen(version1)]; // + 1 for zero byte
305     char *versionID2 = new char[1+ strlen(version2)]; // + 1 for zero byte
306 
307     strcpy(versionID1, "44.0");  // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
308 
309     strcpy(versionID2, "55.0");  // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
310 
311     logln(UnicodeString("getVersionNumber on default.txt returned ") + version1);
312     logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2);
313 
314     if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0)
315         errln("getVersionNumber() failed");
316 
317     delete[] versionID1;
318     delete[] versionID2;
319 
320     /* Restore the default locale for the other tests. */
321     Locale::setDefault(originalDefault, err);
322 }
323 
324 //***************************************************************************************
325 
326 UBool
testTag(const char * frag,UBool in_Root,UBool in_te,UBool in_te_IN)327 ResourceBundleTest::testTag(const char* frag,
328                             UBool in_Root,
329                             UBool in_te,
330                             UBool in_te_IN)
331 {
332     int32_t failOrig = fail;
333 
334     // Make array from input params
335 
336     UBool is_in[] = { in_Root, in_te, in_te_IN };
337 
338     const char* NAME[] = { "ROOT", "TE", "TE_IN" };
339 
340     // Now try to load the desired items
341 
342     char tag[100];
343     UnicodeString action;
344 
345     int32_t i,j,actual_bundle;
346 //    int32_t row,col;
347     int32_t index;
348     UErrorCode status = U_ZERO_ERROR;
349     const char* testdatapath;
350     testdatapath=loadTestData(status);
351     if(U_FAILURE(status))
352     {
353         dataerrln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
354         return false;
355     }
356 
357     for (i=0; i<bundles_count; ++i)
358     {
359         action = "Constructor for ";
360         action += param[i].name;
361 
362         status = U_ZERO_ERROR;
363         ResourceBundle theBundle( testdatapath, *param[i].locale, status);
364         //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
365         CONFIRM_UErrorCode(status, param[i].expected_constructor_status, action);
366 
367         if(i == 5)
368           actual_bundle = 0; /* ne -> default */
369         else if(i == 3)
370           actual_bundle = 1; /* te_NE -> te */
371         else if(i == 4)
372           actual_bundle = 2; /* te_IN_NE -> te_IN */
373         else
374           actual_bundle = i;
375 
376 
377         UErrorCode expected_resource_status = U_MISSING_RESOURCE_ERROR;
378         for (j=e_te_IN; j>=e_Root; --j)
379         {
380             if (is_in[j] && param[i].inherits[j])
381             {
382                 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
383                   expected_resource_status = U_ZERO_ERROR;
384                 else if(j == 0)
385                   expected_resource_status = U_USING_DEFAULT_WARNING;
386                 else
387                   expected_resource_status = U_USING_FALLBACK_WARNING;
388 
389                 break;
390             }
391         }
392 
393         UErrorCode expected_status;
394 
395         UnicodeString base;
396         for (j=param[i].where; j>=0; --j)
397         {
398             if (is_in[j])
399             {
400                 base = NAME[j];
401                 break;
402             }
403         }
404 
405         //--------------------------------------------------------------------------
406         // string
407 
408         uprv_strcpy(tag, "string_");
409         uprv_strcat(tag, frag);
410 
411         action = param[i].name;
412         action += ".getString(";
413         action += tag;
414         action += ")";
415 
416 
417         status = U_ZERO_ERROR;
418 
419         UnicodeString string(theBundle.getStringEx(tag, status));
420 
421         if(U_FAILURE(status)) {
422             string.setTo(true, kErrorUChars, kErrorLength);
423         }
424 
425         CONFIRM_UErrorCode(status, expected_resource_status, action);
426 
427         UnicodeString expected_string(kErrorUChars);
428         if (U_SUCCESS(status)) {
429             expected_string = base;
430         }
431 
432         CONFIRM_EQ(string, expected_string, action);
433 
434         //--------------------------------------------------------------------------
435         // array
436 
437         uprv_strcpy(tag, "array_");
438         uprv_strcat(tag, frag);
439 
440         action = param[i].name;
441         action += ".get(";
442         action += tag;
443         action += ")";
444 
445         status = U_ZERO_ERROR;
446         ResourceBundle arrayBundle(theBundle.get(tag, status));
447         CONFIRM_UErrorCode(status, expected_resource_status, action);
448         int32_t count = arrayBundle.getSize();
449 
450         if (U_SUCCESS(status))
451         {
452             CONFIRM_GE(count, 1, action);
453 
454             for (j=0; j < count; ++j)
455             {
456                 char buf[32];
457                 UnicodeString value(arrayBundle.getStringEx(j, status));
458                 expected_string = base;
459                 expected_string += itoa(j,buf);
460                 CONFIRM_EQ(value, expected_string, action);
461             }
462 
463             action = param[i].name;
464             action += ".getStringEx(";
465             action += tag;
466             action += ")";
467 
468             for (j=0; j<100; ++j)
469             {
470                 index = count ? (randi(count * 3) - count) : (randi(200) - 100);
471                 status = U_ZERO_ERROR;
472                 string = kErrorUChars;
473                 UnicodeString t(arrayBundle.getStringEx(index, status));
474                 expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
475                 CONFIRM_UErrorCode(status, expected_status, action);
476 
477                 if (U_SUCCESS(status))
478                 {
479                     char buf[32];
480                     expected_string = base;
481                     expected_string += itoa(index,buf);
482                 }
483                 else
484                 {
485                     expected_string = kErrorUChars;
486                 }
487                 CONFIRM_EQ(string, expected_string, action);
488             }
489         }
490         else if (status != expected_resource_status)
491         {
492             record_fail("Error getting " + (UnicodeString)tag);
493             return (UBool)(failOrig != fail);
494         }
495 
496     }
497 
498     return (UBool)(failOrig != fail);
499 }
500 
501 void
record_pass(UnicodeString passMessage)502 ResourceBundleTest::record_pass(UnicodeString passMessage)
503 {
504     logln(passMessage);
505     ++pass;
506 }
507 void
record_fail(UnicodeString errMessage)508 ResourceBundleTest::record_fail(UnicodeString errMessage)
509 {
510     err(errMessage);
511     ++fail;
512 }
513 
514 void
TestExemplar()515 ResourceBundleTest::TestExemplar(){
516 
517     int32_t locCount = uloc_countAvailable();
518     int32_t locIndex=0;
519     int num=0;
520     UErrorCode status = U_ZERO_ERROR;
521     for(;locIndex<locCount;locIndex++){
522         const char* locale = uloc_getAvailable(locIndex);
523         UResourceBundle *resb =ures_open(NULL,locale,&status);
524         if(U_SUCCESS(status) && status!=U_USING_FALLBACK_WARNING && status!=U_USING_DEFAULT_WARNING){
525             int32_t len=0;
526             const UChar* strSet = ures_getStringByKey(resb,"ExemplarCharacters",&len,&status);
527             UnicodeSet set(strSet,status);
528             if(U_FAILURE(status)){
529                 errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale,u_errorName(status));
530                 status=U_ZERO_ERROR;
531             }
532             num++;
533         }
534         ures_close(resb);
535     }
536     logln("Number of installed locales with exemplar characters that could be tested: %d",num);
537 
538 }
539 
540 void
TestGetSize(void)541 ResourceBundleTest::TestGetSize(void)
542 {
543     const struct {
544         const char* key;
545         int32_t size;
546     } test[] = {
547         { "zerotest", 1},
548         { "one", 1},
549         { "importtest", 1},
550         { "integerarray", 1},
551         { "emptyarray", 0},
552         { "emptytable", 0},
553         { "emptystring", 1}, /* empty string is still a string */
554         { "emptyint", 1},
555         { "emptybin", 1},
556         { "testinclude", 1},
557         { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
558     };
559 
560     UErrorCode status = U_ZERO_ERROR;
561 
562     const char* testdatapath = loadTestData(status);
563     int32_t i = 0, j = 0;
564     int32_t size = 0;
565 
566     if(U_FAILURE(status))
567     {
568         dataerrln("Could not load testdata.dat %s\n", u_errorName(status));
569         return;
570     }
571 
572     ResourceBundle rb(testdatapath, "testtypes", status);
573     if(U_FAILURE(status))
574     {
575         err("Could not testtypes resource bundle %s\n", u_errorName(status));
576         return;
577     }
578 
579     for(i = 0; i < UPRV_LENGTHOF(test); i++) {
580         ResourceBundle res = rb.get(test[i].key, status);
581         if(U_FAILURE(status))
582         {
583             err("Couldn't find the key %s. Error: %s\n", u_errorName(status));
584             return;
585         }
586         size = res.getSize();
587         if(size != test[i].size) {
588             err("Expected size %i, got size %i for key %s\n", test[i].size, size, test[i].key);
589             for(j = 0; j < size; j++) {
590                 ResourceBundle helper = res.get(j, status);
591                 err("%s\n", helper.getKey());
592             }
593         }
594     }
595 }
596 
597 void
TestGetLocaleByType(void)598 ResourceBundleTest::TestGetLocaleByType(void)
599 {
600     const struct {
601         const char *requestedLocale;
602         const char *resourceKey;
603         const char *validLocale;
604         const char *actualLocale;
605     } test[] = {
606         { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
607         { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
608         { "te_IN_BLAH", "string_only_in_Root", "te_IN", "" },
609         { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "" },
610         { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
611         { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
612     };
613 
614     UErrorCode status = U_ZERO_ERROR;
615 
616     const char* testdatapath = loadTestData(status);
617     int32_t i = 0;
618     Locale locale;
619 
620     if(U_FAILURE(status))
621     {
622         dataerrln("Could not load testdata.dat %s\n", u_errorName(status));
623         return;
624     }
625 
626     for(i = 0; i < UPRV_LENGTHOF(test); i++) {
627         ResourceBundle rb(testdatapath, test[i].requestedLocale, status);
628         if(U_FAILURE(status))
629         {
630             err("Could not open resource bundle %s (error %s)\n", test[i].requestedLocale, u_errorName(status));
631             status = U_ZERO_ERROR;
632             continue;
633         }
634 
635         ResourceBundle res = rb.get(test[i].resourceKey, status);
636         if(U_FAILURE(status))
637         {
638             err("Couldn't find the key %s. Error: %s\n", test[i].resourceKey, u_errorName(status));
639             status = U_ZERO_ERROR;
640             continue;
641         }
642 
643         locale = res.getLocale(ULOC_REQUESTED_LOCALE, status);
644         if(U_SUCCESS(status) && locale != Locale::getDefault()) {
645             err("Expected requested locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
646         }
647         status = U_ZERO_ERROR;
648         locale = res.getLocale(ULOC_VALID_LOCALE, status);
649         if(strcmp(locale.getName(), test[i].validLocale) != 0) {
650             err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
651         }
652         locale = res.getLocale(ULOC_ACTUAL_LOCALE, status);
653         if(strcmp(locale.getName(), test[i].actualLocale) != 0) {
654             err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
655         }
656     }
657 }
658