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 *
10 * File CRESTST.C
11 *
12 * Modification History:
13 * Name Description
14 * Madhu Katragadda Ported for C API
15 * 06/14/99 stephen Updated for RB API changes (no suffix).
16 ********************************************************************************
17 */
18
19
20 #include "unicode/utypes.h"
21 #include "cintltst.h"
22 #include "unicode/ustring.h"
23 #include "unicode/utf16.h"
24 #include "cmemory.h"
25 #include "cstring.h"
26 #include "filestrm.h"
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <stdio.h> // for sprintf()
30
31 #define RESTEST_HEAP_CHECK 0
32
33 #include "unicode/ures.h"
34 #include "crestst.h"
35 #include "unicode/ctest.h"
36 #include "uresimp.h"
37
38 static void TestOpenDirect(void);
39 static void TestFallback(void);
40 static void TestTable32(void);
41 static void TestFileStream(void);
42 static void TestAlgorithmicParentFallback(void);
43
44 /*****************************************************************************/
45
46 const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
47 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
48
49 /*****************************************************************************/
50
51 enum E_Where
52 {
53 e_Root,
54 e_te,
55 e_te_IN,
56 e_Where_count
57 };
58 typedef enum E_Where E_Where;
59 /*****************************************************************************/
60
61 #define CONFIRM_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
62 if (u_strcmp(expected,actual)==0) { \
63 record_pass(); \
64 } else { \
65 record_fail(); \
66 log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); \
67 } \
68 } UPRV_BLOCK_MACRO_END
69
70 #define CONFIRM_ErrorCode(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
71 if ((expected)==(actual)) { \
72 record_pass(); \
73 } else { \
74 record_fail(); \
75 log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); \
76 } \
77 } UPRV_BLOCK_MACRO_END
78
79
80 /* Array of our test objects */
81
82 static struct
83 {
84 const char* name;
85 UErrorCode expected_constructor_status;
86 E_Where where;
87 UBool like[e_Where_count];
88 UBool inherits[e_Where_count];
89 } param[] =
90 {
91 /* "te" means test */
92 /* "IN" means inherits */
93 /* "NE" or "ne" means "does not exist" */
94
95 { "root", U_ZERO_ERROR, e_Root, { true, false, false }, { true, false, false } },
96 { "te", U_ZERO_ERROR, e_te, { false, true, false }, { true, true, false } },
97 { "te_IN", U_ZERO_ERROR, e_te_IN, { false, false, true }, { true, true, true } },
98 { "te_NE", U_USING_FALLBACK_WARNING, e_te, { false, true, false }, { true, true, false } },
99 { "te_IN_NE", U_USING_FALLBACK_WARNING, e_te_IN, { false, false, true }, { true, true, true } },
100 { "ne", U_USING_DEFAULT_WARNING, e_Root, { true, false, false }, { true, false, false } }
101 };
102
103 static int32_t bundles_count = UPRV_LENGTHOF(param);
104
105
106
107 /***************************************************************************************/
108
109 /* Array of our test objects */
110
111 void addResourceBundleTest(TestNode** root);
112
addResourceBundleTest(TestNode ** root)113 void addResourceBundleTest(TestNode** root)
114 {
115 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
116 addTest(root, &TestConstruction1, "tsutil/crestst/TestConstruction1");
117 addTest(root, &TestOpenDirect, "tsutil/crestst/TestOpenDirect");
118 addTest(root, &TestResourceBundles, "tsutil/crestst/TestResourceBundles");
119 addTest(root, &TestTable32, "tsutil/crestst/TestTable32");
120 addTest(root, &TestFileStream, "tsutil/crestst/TestFileStream");
121 addTest(root, &TestGetSize, "tsutil/crestst/TestGetSize");
122 addTest(root, &TestGetLocaleByType, "tsutil/crestst/TestGetLocaleByType");
123 #endif
124 addTest(root, &TestFallback, "tsutil/crestst/TestFallback");
125 addTest(root, &TestAliasConflict, "tsutil/crestst/TestAliasConflict");
126 addTest(root, &TestAlgorithmicParentFallback, "tsutil/crestst/TestAlgorithmicParentFallback");
127
128 }
129
130
131 /***************************************************************************************/
TestAliasConflict(void)132 void TestAliasConflict(void) {
133 UErrorCode status = U_ZERO_ERROR;
134 UResourceBundle *he = NULL;
135 UResourceBundle *iw = NULL;
136 const UChar *result = NULL;
137 int32_t resultLen;
138
139 he = ures_open(NULL, "he", &status);
140 iw = ures_open(NULL, "iw", &status);
141 if(U_FAILURE(status)) {
142 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
143 }
144 ures_close(iw);
145 result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
146 if(U_FAILURE(status) || result == NULL) {
147 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
148 }
149 ures_close(he);
150 }
151
152
TestResourceBundles()153 void TestResourceBundles()
154 {
155 // The test expectation only works if the default locale is not one of the
156 // locale bundle in the testdata which have those info. Therefore, we skip
157 // the test if the default locale is te, sh, mc, or them with subtags.
158 if ( uprv_strncmp(uloc_getDefault(), "te", 2) == 0 ||
159 uprv_strncmp(uloc_getDefault(), "sh", 2) == 0 ||
160 uprv_strncmp(uloc_getDefault(), "mc", 2) == 0) {
161 return;
162 }
163
164 UErrorCode status = U_ZERO_ERROR;
165 loadTestData(&status);
166 if(U_FAILURE(status)) {
167 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
168 return;
169 }
170
171 testTag("only_in_Root", true, false, false);
172 testTag("in_Root_te", true, true, false);
173 testTag("in_Root_te_te_IN", true, true, true);
174 testTag("in_Root_te_IN", true, false, true);
175 testTag("only_in_te", false, true, false);
176 testTag("only_in_te_IN", false, false, true);
177 testTag("in_te_te_IN", false, true, true);
178 testTag("nonexistent", false, false, false);
179
180 log_verbose("Passed:= %d Failed= %d \n", pass, fail);
181 }
182
TestConstruction1()183 void TestConstruction1()
184 {
185 UResourceBundle *test1 = 0, *test2 = 0;
186 const UChar *result1, *result2;
187 int32_t resultLen;
188 UChar temp[7];
189
190 UErrorCode err = U_ZERO_ERROR;
191 const char* testdatapath ;
192 const char* locale="te_IN";
193
194 log_verbose("Testing ures_open()......\n");
195
196
197 testdatapath=loadTestData(&err);
198 if(U_FAILURE(err))
199 {
200 log_data_err("Could not load testdata.dat %s \n",myErrorName(err));
201 return;
202 }
203
204 test1=ures_open(testdatapath, NULL, &err);
205 if(U_FAILURE(err))
206 {
207 log_err("construction of %s did not succeed : %s \n",NULL, myErrorName(err));
208 return;
209 }
210
211
212 test2=ures_open(testdatapath, locale, &err);
213 if(U_FAILURE(err))
214 {
215 log_err("construction of %s did not succeed : %s \n",locale, myErrorName(err));
216 return;
217 }
218 result1= ures_getStringByKey(test1, "string_in_Root_te_te_IN", &resultLen, &err);
219 result2= ures_getStringByKey(test2, "string_in_Root_te_te_IN", &resultLen, &err);
220
221
222 if (U_FAILURE(err)) {
223 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(err));
224 return;
225 }
226
227 u_uastrcpy(temp, "TE_IN");
228
229 if(u_strcmp(result2, temp)!=0)
230 {
231 int n;
232
233 log_err("Construction test failed for ures_open();\n");
234 if(!getTestOption(VERBOSITY_OPTION))
235 log_info("(run verbose for more information)\n");
236
237 log_verbose("\nGot->");
238 for(n=0;result2[n];n++)
239 {
240 log_verbose("%04X ",result2[n]);
241 }
242 log_verbose("<\n");
243
244 log_verbose("\nWant>");
245 for(n=0;temp[n];n++)
246 {
247 log_verbose("%04X ",temp[n]);
248 }
249 log_verbose("<\n");
250
251 }
252
253 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", austrdup(result1));
254 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", austrdup(result2));
255
256 /* Test getVersionNumber*/
257 log_verbose("Testing version number\n");
258 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1));
259
260 ures_close(test1);
261 ures_close(test2);
262 }
263
264 /*****************************************************************************/
265 /*****************************************************************************/
266
testTag(const char * frag,UBool in_Root,UBool in_te,UBool in_te_IN)267 UBool testTag(const char* frag,
268 UBool in_Root,
269 UBool in_te,
270 UBool in_te_IN)
271 {
272 int32_t passNum=pass;
273
274 /* Make array from input params */
275
276 UBool is_in[3];
277 const char *NAME[] = { "ROOT", "TE", "TE_IN" };
278
279 /* Now try to load the desired items */
280 UResourceBundle* theBundle = NULL;
281 char tag[99];
282 char action[256];
283 UErrorCode status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
284 UChar* base = NULL;
285 UChar* expected_string = NULL;
286 const UChar* string = NULL;
287 char item_tag[10];
288 int32_t i,j;
289 int32_t actual_bundle;
290 int32_t resultLen;
291 const char *testdatapath = loadTestData(&status);
292
293 is_in[0] = in_Root;
294 is_in[1] = in_te;
295 is_in[2] = in_te_IN;
296
297 strcpy(item_tag, "tag");
298
299 status = U_ZERO_ERROR;
300 theBundle = ures_open(testdatapath, "root", &status);
301 if(U_FAILURE(status))
302 {
303 ures_close(theBundle);
304 log_err("Couldn't open root bundle in %s", testdatapath);
305 return false;
306 }
307 ures_close(theBundle);
308 theBundle = NULL;
309
310
311 for (i=0; i<bundles_count; ++i)
312 {
313 strcpy(action,"construction for");
314 strcat(action, param[i].name);
315
316
317 status = U_ZERO_ERROR;
318
319 theBundle = ures_open(testdatapath, param[i].name, &status);
320 /*theBundle = ures_open("c:\\icu\\icu\\source\\test\\testdata\\testdata", param[i].name, &status);*/
321
322 CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
323
324
325
326 if(i == 5)
327 actual_bundle = 0; /* ne -> default */
328 else if(i == 3)
329 actual_bundle = 1; /* te_NE -> te */
330 else if(i == 4)
331 actual_bundle = 2; /* te_IN_NE -> te_IN */
332 else
333 actual_bundle = i;
334
335 expected_resource_status = U_MISSING_RESOURCE_ERROR;
336 for (j=e_te_IN; j>=e_Root; --j)
337 {
338 if (is_in[j] && param[i].inherits[j])
339 {
340
341 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
342 expected_resource_status = U_ZERO_ERROR;
343 else if(j == 0)
344 expected_resource_status = U_USING_DEFAULT_WARNING;
345 else
346 expected_resource_status = U_USING_FALLBACK_WARNING;
347
348 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
349 param[i].name,
350 i,
351 frag,
352 j,
353 is_in[j]?"Yes":"No",
354 j,
355 param[i].inherits[j]?"Yes":"No",
356 param[actual_bundle].name);
357
358 break;
359 }
360 }
361
362 for (j=param[i].where; j>=0; --j)
363 {
364 if (is_in[j])
365 {
366 if(base != NULL) {
367 free(base);
368 base = NULL;
369 }
370
371 base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
372 u_uastrcpy(base,NAME[j]);
373
374 break;
375 }
376 else {
377 if(base != NULL) {
378 free(base);
379 base = NULL;
380 }
381 base = (UChar*) malloc(sizeof(UChar) * 1);
382 *base = 0x0000;
383 }
384 }
385
386 /*-------------------------------------------------------------------- */
387 /* string */
388
389 strcpy(tag,"string_");
390 strcat(tag,frag);
391
392 strcpy(action,param[i].name);
393 strcat(action, ".ures_get(" );
394 strcat(action,tag);
395 strcat(action, ")");
396
397 string= kERROR;
398
399 status = U_ZERO_ERROR;
400
401 ures_getStringByKey(theBundle, tag, &resultLen, &status);
402 if(U_SUCCESS(status))
403 {
404 status = U_ZERO_ERROR;
405 string=ures_getStringByKey(theBundle, tag, &resultLen, &status);
406 }
407
408 log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
409
410 CONFIRM_ErrorCode(status, expected_resource_status);
411
412
413 if(U_SUCCESS(status)){
414 expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 3));
415 u_strcpy(expected_string,base);
416
417 }
418 else
419 {
420 expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
421 u_strcpy(expected_string,kERROR);
422
423 }
424
425 CONFIRM_EQ(string, expected_string);
426
427 free(expected_string);
428 ures_close(theBundle);
429 }
430 free(base);
431 return (UBool)(passNum == pass);
432 }
433
record_pass()434 void record_pass()
435 {
436 ++pass;
437 }
438
record_fail()439 void record_fail()
440 {
441 ++fail;
442 }
443
444 /**
445 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
446 * are set correctly
447 */
448
TestFallback()449 static void TestFallback()
450 {
451 UErrorCode status = U_ZERO_ERROR;
452 UResourceBundle *fr_FR = NULL;
453 UResourceBundle *subResource = NULL;
454 const UChar *junk; /* ignored */
455 int32_t resultLen;
456
457 log_verbose("Opening fr_FR..");
458 fr_FR = ures_open(NULL, "fr_FR", &status);
459 if(U_FAILURE(status))
460 {
461 log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
462 return;
463 }
464
465 status = U_ZERO_ERROR;
466
467
468 /* clear it out.. just do some calls to get the gears turning */
469 junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
470 (void)junk; /* Suppress set but not used warning. */
471 status = U_ZERO_ERROR;
472 junk = ures_getStringByKey(fr_FR, "LocaleString", &resultLen, &status);
473 status = U_ZERO_ERROR;
474 junk = ures_getStringByKey(fr_FR, "LocaleID", &resultLen, &status);
475 status = U_ZERO_ERROR;
476
477 /* OK first one. This should be a Default value. */
478 subResource = ures_getByKey(fr_FR, "layout", NULL, &status);
479 if(status != U_USING_DEFAULT_WARNING)
480 {
481 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
482 u_errorName(status));
483 }
484 ures_close(subResource);
485 status = U_ZERO_ERROR;
486
487 /* and this is a Fallback, to fr */
488 junk = ures_getStringByKey(fr_FR, "ExemplarCharacters", &resultLen, &status);
489 if(status != U_USING_FALLBACK_WARNING)
490 {
491 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %s\n",
492 u_errorName(status));
493 }
494
495 status = U_ZERO_ERROR;
496
497 ures_close(fr_FR);
498 }
499
500 static void
TestOpenDirect(void)501 TestOpenDirect(void) {
502 UResourceBundle *idna_rules, *casing, *te_IN, *ne, *item, *defaultLocale;
503 UErrorCode errorCode;
504
505 /*
506 * test that ures_openDirect() opens a resource bundle
507 * where one can look up its own items but not fallback items
508 * from root or similar
509 */
510 errorCode=U_ZERO_ERROR;
511 idna_rules=ures_openDirect(loadTestData(&errorCode), "idna_rules", &errorCode);
512 if(U_FAILURE(errorCode)) {
513 log_data_err("ures_openDirect(\"idna_rules\") failed: %s\n", u_errorName(errorCode));
514 return;
515 }
516
517 if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules, &errorCode))) {
518 log_err("ures_openDirect(\"idna_rules\").getLocale()!=idna_rules\n");
519 }
520 errorCode=U_ZERO_ERROR;
521
522 /* try an item in idna_rules, must work */
523 item=ures_getByKey(idna_rules, "UnassignedSet", NULL, &errorCode);
524 if(U_FAILURE(errorCode)) {
525 log_err("translit_index.getByKey(local key) failed: %s\n", u_errorName(errorCode));
526 errorCode=U_ZERO_ERROR;
527 } else {
528 ures_close(item);
529 }
530
531 /* try an item in root, must fail */
532 item=ures_getByKey(idna_rules, "ShortLanguage", NULL, &errorCode);
533 if(U_FAILURE(errorCode)) {
534 errorCode=U_ZERO_ERROR;
535 } else {
536 log_err("idna_rules.getByKey(root key) succeeded!\n");
537 ures_close(item);
538 }
539 ures_close(idna_rules);
540
541 /* now make sure that "idna_rules" will not work with ures_open() */
542 errorCode=U_ZERO_ERROR;
543 idna_rules=ures_open("testdata", "idna_rules", &errorCode);
544 if(U_FAILURE(errorCode) || errorCode==U_USING_DEFAULT_WARNING || errorCode==U_USING_FALLBACK_WARNING) {
545 /* falling back to default or root is ok */
546 errorCode=U_ZERO_ERROR;
547 } else if(0!=uprv_strcmp("idna_rules", ures_getLocale(idna_rules, &errorCode))) {
548 /* Opening this file will work in "files mode" on Windows and the Mac,
549 which have case insensitive file systems */
550 log_err("ures_open(\"idna_rules\") succeeded, should fail! Got: %s\n", u_errorName(errorCode));
551 }
552 ures_close(idna_rules);
553
554 /* ures_openDirect("translit_index_WronG") must fail */
555 idna_rules=ures_openDirect(NULL, "idna_rules_WronG", &errorCode);
556 if(U_FAILURE(errorCode)) {
557 errorCode=U_ZERO_ERROR;
558 } else {
559 log_err("ures_openDirect(\"idna_rules_WronG\") succeeded, should fail!\n");
560 }
561 ures_close(idna_rules);
562
563 errorCode = U_USING_FALLBACK_WARNING;
564 idna_rules=ures_openDirect("testdata", "idna_rules", &errorCode);
565 if(U_FAILURE(errorCode)) {
566 log_data_err("ures_openDirect(\"idna_rules\") failed when U_USING_FALLBACK_WARNING was set prior to call: %s\n", u_errorName(errorCode));
567 return;
568 }
569 ures_close(idna_rules);
570
571 /*
572 * ICU 3.6 has new resource bundle syntax and data for bundles that do not
573 * participate in locale fallback. Now,
574 * - ures_open() works like ures_openDirect() on a bundle with a top-level
575 * type of ":table(nofallback)" _if_ the bundle exists
576 * - ures_open() will continue to find a root bundle if the requested one
577 * does not exist, unlike ures_openDirect()
578 *
579 * Test with a different bundle than above to avoid confusion in the cache.
580 */
581
582 /*
583 * verify that ures_open("casing"), which now has a nofallback declaration,
584 * does not enable fallbacks
585 */
586 errorCode=U_ZERO_ERROR;
587 casing=ures_open("testdata", "casing", &errorCode);
588 if(U_FAILURE(errorCode)) {
589 log_data_err("ures_open(\"casing\") failed: %s\n", u_errorName(errorCode));
590 return;
591 }
592
593 errorCode=U_ZERO_ERROR;
594 item=ures_getByKey(casing, "Info", NULL, &errorCode);
595 if(U_FAILURE(errorCode)) {
596 log_err("casing.getByKey(Info) failed - %s\n", u_errorName(errorCode));
597 } else {
598 ures_close(item);
599 }
600
601 errorCode=U_ZERO_ERROR;
602 item=ures_getByKey(casing, "ShortLanguage", NULL, &errorCode);
603 if(U_SUCCESS(errorCode)) {
604 log_err("casing.getByKey(root key) succeeded despite nofallback declaration - %s\n", u_errorName(errorCode));
605 ures_close(item);
606 }
607 ures_close(casing);
608
609 /*
610 * verify that ures_open("ne") finds the root bundle or default locale
611 * bundle but ures_openDirect("ne") does not.
612 */
613 errorCode=U_ZERO_ERROR;
614 ne=ures_open("testdata", "ne", &errorCode);
615 if(U_FAILURE(errorCode)) {
616 log_data_err("ures_open(\"ne\") failed (expected to get root): %s\n", u_errorName(errorCode));
617 }
618 if( errorCode!=U_USING_DEFAULT_WARNING ||
619 (0!=uprv_strcmp("root", ures_getLocale(ne, &errorCode)) &&
620 0!=uprv_strcmp(uloc_getDefault(), ures_getLocale(ne, &errorCode)))) {
621 log_err("ures_open(\"ne\") found something other than \"root\" "
622 "or default locale \"%s\" - %s\n", uloc_getDefault(), u_errorName(errorCode));
623 }
624 ures_close(ne);
625
626 errorCode=U_ZERO_ERROR;
627 ne=ures_openDirect("testdata", "ne", &errorCode);
628 if(U_SUCCESS(errorCode)) {
629 log_data_err("ures_openDirect(\"ne\") succeeded unexpectedly\n");
630 ures_close(ne);
631 }
632
633 /* verify that ures_openDirect("te_IN") does not enable fallbacks */
634 errorCode=U_ZERO_ERROR;
635 te_IN=ures_openDirect("testdata", "te_IN", &errorCode);
636 if(U_FAILURE(errorCode)) {
637 log_data_err("ures_open(\"te_IN\") failed: %s\n", u_errorName(errorCode));
638 return;
639 }
640 errorCode=U_ZERO_ERROR;
641 item=ures_getByKey(te_IN, "ShortLanguage", NULL, &errorCode);
642 if(U_SUCCESS(errorCode)) {
643 log_err("te_IN.getByKey(root key) succeeded despite use of ures_openDirect() - %s\n", u_errorName(errorCode));
644 ures_close(item);
645 }
646 ures_close(te_IN);
647
648 // ICU-21705
649 // Verify that calling ures_openDirect() with a NULL localeID doesn't crash or assert.
650 errorCode = U_ZERO_ERROR;
651 defaultLocale = ures_openDirect(NULL, NULL, &errorCode);
652 ures_close(defaultLocale);
653 }
654
655 static int32_t
parseTable32Key(const char * key)656 parseTable32Key(const char *key) {
657 int32_t number;
658 char c;
659
660 number=0;
661 while((c=*key++)!=0) {
662 number<<=1;
663 if(c=='1') {
664 number|=1;
665 }
666 }
667 return number;
668 }
669
670 static void
TestTable32(void)671 TestTable32(void) {
672 static const struct {
673 const char *key;
674 int32_t number;
675 } testcases[]={
676 { "ooooooooooooooooo", 0 },
677 { "oooooooooooooooo1", 1 },
678 { "ooooooooooooooo1o", 2 },
679 { "oo11ooo1ooo11111o", 25150 },
680 { "oo11ooo1ooo111111", 25151 },
681 { "o1111111111111111", 65535 },
682 { "1oooooooooooooooo", 65536 },
683 { "1ooooooo11o11ooo1", 65969 },
684 { "1ooooooo11o11oo1o", 65970 },
685 { "1ooooooo111oo1111", 65999 }
686 };
687
688 /* ### TODO UResourceBundle staticItem={ 0 }; - need to know the size */
689 UResourceBundle *res, *item;
690 const UChar *s;
691 const char *key;
692 UErrorCode errorCode;
693 int32_t i, j, number, parsedNumber, length, count;
694
695 errorCode=U_ZERO_ERROR;
696 res=ures_open(loadTestData(&errorCode), "testtable32", &errorCode);
697 if(U_FAILURE(errorCode)) {
698 log_data_err("unable to open testdata/testtable32.res - %s\n", u_errorName(errorCode));
699 return;
700 }
701 if(ures_getType(res)!=URES_TABLE) {
702 log_data_err("testdata/testtable32.res has type %d instead of URES_TABLE\n", ures_getType(res));
703 }
704
705 count=ures_getSize(res);
706 if(count!=66000) {
707 log_err("testdata/testtable32.res should have 66000 entries but has %d\n", count);
708 }
709
710 /* get the items by index */
711 item=NULL;
712 for(i=0; i<count; ++i) {
713 item=ures_getByIndex(res, i, item, &errorCode);
714 if(U_FAILURE(errorCode)) {
715 log_err("unable to get item %d of %d in testdata/testtable32.res - %s\n",
716 i, count, u_errorName(errorCode));
717 break;
718 }
719
720 key=ures_getKey(item);
721 parsedNumber=parseTable32Key(key);
722
723 switch(ures_getType(item)) {
724 case URES_STRING:
725 s=ures_getString(item, &length, &errorCode);
726 if(U_FAILURE(errorCode) || s==NULL) {
727 log_err("unable to access the string \"%s\" at %d in testdata/testtable32.res - %s\n",
728 key, i, u_errorName(errorCode));
729 number=-1;
730 } else {
731 j=0;
732 U16_NEXT(s, j, length, number);
733 }
734 break;
735 case URES_INT:
736 number=ures_getInt(item, &errorCode);
737 if(U_FAILURE(errorCode)) {
738 log_err("unable to access the integer \"%s\" at %d in testdata/testtable32.res - %s\n",
739 key, i, u_errorName(errorCode));
740 number=-1;
741 }
742 break;
743 default:
744 log_err("unexpected resource type %d for \"%s\" at %d in testdata/testtable32.res - %s\n",
745 ures_getType(item), key, i, u_errorName(errorCode));
746 number=-1;
747 break;
748 }
749
750 if(number>=0 && number!=parsedNumber) {
751 log_err("\"%s\" at %d in testdata/testtable32.res has a string/int value of %d, expected %d\n",
752 key, i, number, parsedNumber);
753 }
754 }
755
756 /* search for some items by key */
757 for(i=0; i<UPRV_LENGTHOF(testcases); ++i) {
758 item=ures_getByKey(res, testcases[i].key, item, &errorCode);
759 if(U_FAILURE(errorCode)) {
760 log_err("unable to find the key \"%s\" in testdata/testtable32.res - %s\n",
761 testcases[i].key, u_errorName(errorCode));
762 continue;
763 }
764
765 switch(ures_getType(item)) {
766 case URES_STRING:
767 s=ures_getString(item, &length, &errorCode);
768 if(U_FAILURE(errorCode) || s==NULL) {
769 log_err("unable to access the string \"%s\" in testdata/testtable32.res - %s\n",
770 testcases[i].key, u_errorName(errorCode));
771 number=-1;
772 } else {
773 j=0;
774 U16_NEXT(s, j, length, number);
775 }
776 break;
777 case URES_INT:
778 number=ures_getInt(item, &errorCode);
779 if(U_FAILURE(errorCode)) {
780 log_err("unable to access the integer \"%s\" in testdata/testtable32.res - %s\n",
781 testcases[i].key, u_errorName(errorCode));
782 number=-1;
783 }
784 break;
785 default:
786 log_err("unexpected resource type %d for \"%s\" in testdata/testtable32.res - %s\n",
787 ures_getType(item), testcases[i].key, u_errorName(errorCode));
788 number=-1;
789 break;
790 }
791
792 if(number>=0 && number!=testcases[i].number) {
793 log_err("\"%s\" in testdata/testtable32.res has a string/int value of %d, expected %d\n",
794 testcases[i].key, number, testcases[i].number);
795 }
796
797 key=ures_getKey(item);
798 if(0!=uprv_strcmp(key, testcases[i].key)) {
799 log_err("\"%s\" in testdata/testtable32.res claims to have the key \"%s\"\n",
800 testcases[i].key, key);
801 }
802 }
803
804 ures_close(item);
805 ures_close(res);
806 }
807
TestFileStream(void)808 static void TestFileStream(void){
809 int32_t c = 0;
810 int32_t c1=0;
811 UErrorCode status = U_ZERO_ERROR;
812 const char* testdatapath = loadTestData(&status);
813 char* fileName = (char*) malloc(uprv_strlen(testdatapath) +10);
814 FileStream* stream = NULL;
815 /* these should not be closed */
816 FileStream* pStdin = T_FileStream_stdin();
817 FileStream* pStdout = T_FileStream_stdout();
818 FileStream* pStderr = T_FileStream_stderr();
819
820 const char* testline = "This is a test line";
821 int32_t bufLen = (int32_t)strlen(testline)+10;
822 char* buf = (char*) malloc(bufLen);
823 int32_t retLen = 0;
824
825 if(pStdin==NULL){
826 log_err("failed to get T_FileStream_stdin()");
827 }
828 if(pStdout==NULL){
829 log_err("failed to get T_FileStream_stdout()");
830 }
831 if(pStderr==NULL){
832 log_err("failed to get T_FileStream_stderr()");
833 }
834
835 uprv_strcpy(fileName,testdatapath);
836 uprv_strcat(fileName,".dat");
837 stream = T_FileStream_open(fileName, "r");
838 if(stream==NULL){
839 log_data_err("T_FileStream_open failed to open %s\n",fileName);
840 } else {
841 if(!T_FileStream_file_exists(fileName)){
842 log_data_err("T_FileStream_file_exists failed to verify existence of %s \n",fileName);
843 }
844
845 retLen=T_FileStream_read(stream,&c,1);
846 if(retLen==0){
847 log_data_err("T_FileStream_read failed to read from %s \n",fileName);
848 }
849 retLen=0;
850 T_FileStream_rewind(stream);
851 T_FileStream_read(stream,&c1,1);
852 if(c!=c1){
853 log_data_err("T_FileStream_rewind failed to rewind %s \n",fileName);
854 }
855 T_FileStream_rewind(stream);
856 c1 = T_FileStream_peek(stream);
857 if(c!=c1){
858 log_data_err("T_FileStream_peek failed to peekd %s \n",fileName);
859 }
860 c = T_FileStream_getc(stream);
861 T_FileStream_ungetc(c,stream);
862 if(c!= T_FileStream_getc(stream)){
863 log_data_err("T_FileStream_ungetc failed to d %s \n",fileName);
864 }
865
866 if(T_FileStream_size(stream)<=0){
867 log_data_err("T_FileStream_size failed to d %s \n",fileName);
868 }
869 if(T_FileStream_error(stream)){
870 log_data_err("T_FileStream_error shouldn't have an error %s\n",fileName);
871 }
872 if(!T_FileStream_error(NULL)){
873 log_err("T_FileStream_error didn't get an error %s\n",fileName);
874 }
875 T_FileStream_putc(stream, 0x20);
876 if(!T_FileStream_error(stream)){
877 /*
878 Warning
879 writing to a read-only file may not consistently fail on all platforms
880 (e.g. HP-UX, FreeBSD, MacOSX)
881 */
882 log_verbose("T_FileStream_error didn't get an error when writing to a readonly file %s\n",fileName);
883 }
884
885 T_FileStream_close(stream);
886 }
887 /* test writing function */
888 stream=NULL;
889 uprv_strcpy(fileName,testdatapath);
890 uprv_strcat(fileName,".tmp");
891 stream = T_FileStream_open(fileName,"w+");
892
893 if(stream == NULL){
894 log_data_err("Could not open %s for writing\n",fileName);
895 } else {
896 c= '$';
897 T_FileStream_putc(stream,c);
898 T_FileStream_rewind(stream);
899 if(c != T_FileStream_getc(stream)){
900 log_data_err("T_FileStream_putc failed %s\n",fileName);
901 }
902
903 T_FileStream_rewind(stream);
904 T_FileStream_writeLine(stream,testline);
905 T_FileStream_rewind(stream);
906 T_FileStream_readLine(stream,buf,bufLen);
907 if(uprv_strncmp(testline, buf,uprv_strlen(buf))!=0){
908 log_data_err("T_FileStream_writeLine failed %s\n",fileName);
909 }
910
911 T_FileStream_rewind(stream);
912 T_FileStream_write(stream,testline,(int32_t)strlen(testline));
913 T_FileStream_rewind(stream);
914 retLen = T_FileStream_read(stream, buf, bufLen);
915 if(uprv_strncmp(testline, buf,retLen)!=0){
916 log_data_err("T_FileStream_write failed %s\n",fileName);
917 }
918
919 T_FileStream_close(stream);
920 }
921 if(!T_FileStream_remove(fileName)){
922 log_data_err("T_FileStream_remove failed to delete %s\n",fileName);
923 }
924
925
926 free(fileName);
927 free(buf);
928
929 }
930
TestGetSize(void)931 static void TestGetSize(void) {
932 const struct {
933 const char* key;
934 int32_t size;
935 } test[] = {
936 { "zerotest", 1},
937 { "one", 1},
938 { "importtest", 1},
939 { "integerarray", 1},
940 { "emptyarray", 0},
941 { "emptytable", 0},
942 { "emptystring", 1}, /* empty string is still a string */
943 { "emptyint", 1},
944 { "emptybin", 1},
945 { "testinclude", 1},
946 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
947 };
948
949 UErrorCode status = U_ZERO_ERROR;
950
951 UResourceBundle *rb = NULL;
952 UResourceBundle *res = NULL;
953 UResourceBundle *helper = NULL;
954 const char* testdatapath = loadTestData(&status);
955 int32_t i = 0, j = 0;
956 int32_t size = 0;
957
958 if(U_FAILURE(status))
959 {
960 log_data_err("Could not load testdata.dat %s\n", u_errorName(status));
961 return;
962 }
963
964 rb = ures_open(testdatapath, "testtypes", &status);
965 if(U_FAILURE(status))
966 {
967 log_err("Could not testtypes resource bundle %s\n", u_errorName(status));
968 return;
969 }
970
971 for(i = 0; i < UPRV_LENGTHOF(test); i++) {
972 res = ures_getByKey(rb, test[i].key, res, &status);
973 if(U_FAILURE(status))
974 {
975 log_err("Couldn't find the key %s. Error: %s\n", test[i].key, u_errorName(status));
976 ures_close(rb);
977 return;
978 }
979 size = ures_getSize(res);
980 if(size != test[i].size) {
981 log_err("Expected size %i, got size %i for key %s\n", test[i].size, size, test[i].key);
982 for(j = 0; j < size; j++) {
983 helper = ures_getByIndex(res, j, helper, &status);
984 log_err("%s\n", ures_getKey(helper));
985 }
986 }
987 }
988 ures_close(helper);
989 ures_close(res);
990 ures_close(rb);
991 }
992
TestGetLocaleByType(void)993 static void TestGetLocaleByType(void) {
994 static const struct {
995 const char *requestedLocale;
996 const char *resourceKey;
997 const char *validLocale;
998 const char *actualLocale;
999 } test[] = {
1000 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
1001 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
1002 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
1003 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
1004 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
1005 { "te_IN_BLAH@collation=phonebook;calendar=thai", "array_2d_only_in_te", "te_IN", "te" }
1006 };
1007
1008 UErrorCode status = U_ZERO_ERROR;
1009
1010 UResourceBundle *rb = NULL;
1011 UResourceBundle *res = NULL;
1012 const char* testdatapath = loadTestData(&status);
1013 int32_t i = 0;
1014 const char *locale = NULL;
1015
1016 if(U_FAILURE(status))
1017 {
1018 log_data_err("Could not load testdata.dat %s\n", u_errorName(status));
1019 return;
1020 }
1021
1022 for(i = 0; i < UPRV_LENGTHOF(test); i++) {
1023 rb = ures_open(testdatapath, test[i].requestedLocale, &status);
1024 if(U_FAILURE(status))
1025 {
1026 log_err("Could not open resource bundle %s (error %s)\n", test[i].requestedLocale, u_errorName(status));
1027 status = U_ZERO_ERROR;
1028 continue;
1029 }
1030
1031 res = ures_getByKey(rb, test[i].resourceKey, res, &status);
1032 if(U_FAILURE(status))
1033 {
1034 log_err("Couldn't find the key %s. Error: %s\n", test[i].resourceKey, u_errorName(status));
1035 ures_close(rb);
1036 status = U_ZERO_ERROR;
1037 continue;
1038 }
1039
1040 locale = ures_getLocaleByType(res, ULOC_REQUESTED_LOCALE, &status);
1041 if(U_SUCCESS(status) && locale != NULL) {
1042 log_err("Requested locale should return NULL\n");
1043 }
1044 status = U_ZERO_ERROR;
1045 locale = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &status);
1046 if(!locale || strcmp(locale, test[i].validLocale) != 0) {
1047 log_err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1048 }
1049 locale = ures_getLocaleByType(res, ULOC_ACTUAL_LOCALE, &status);
1050 if(!locale || strcmp(locale, test[i].actualLocale) != 0) {
1051 log_err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale);
1052 }
1053 ures_close(rb);
1054 }
1055 ures_close(res);
1056 }
1057
TestAlgorithmicParentFallback(void)1058 static void TestAlgorithmicParentFallback(void) {
1059 // Test for ICU-21125 and ICU-21126 -- cases where resource fallback isn't determined by lopping fields off
1060 // the end of the locale ID (or following a %%Parent directive in a resource bundle)
1061 // first column is input locale, second column is expected output locale
1062 const char* testCases[] = {
1063 "de_Latn_LI", "de_LI", "de_LI",
1064 // "en_VA", "en_150", "en",// TODO: put this back in after https://unicode-org.atlassian.net/browse/CLDR-15893 is fixed
1065 "yi_Latn_DE", "root", "yi",
1066 "yi_Hebr_DE", "yi", "yi",
1067 "zh_Hant_SG", "zh_Hant", "zh_Hant"
1068 // would be nice to test that sr_Latn_ME falls back to sr_Latn, or sr_ME to sr_Latn_ME,
1069 // or sr_Latn to root, but all of these resource bundle files actually exist in the project
1070 };
1071
1072 // fallbacks to "root" in the table above actually fall back to the system default locale.
1073 // Trying to compare the locale ID from the resource bundle we get back to uloc_getDefault()
1074 // has all kinds of problems, so instead we open a resource bundle with a locale ID that we know
1075 // will fall back to the default, save THAT resource bundle's locale ID, and just compare anything
1076 // that's supposed to fall back to "root" to THAT. (If trying to determine the default locale
1077 // in this way fails for some reason, we dump out and don't do the rest of the test.)
1078 UErrorCode err = U_ZERO_ERROR;
1079 UResourceBundle* defaultLocaleRB = ures_open(NULL, "xx_YY", &err);
1080 const char* defaultLocaleID = ures_getLocaleByType(defaultLocaleRB, ULOC_ACTUAL_LOCALE, &err);
1081 if (U_FAILURE(err)) {
1082 log_err("Couldn't create resource bundle for default locale: %s\n", u_errorName(err));
1083 return;
1084 }
1085 // (can't close defaultLocaleRB here because then defaultLocaleID would go bad)
1086
1087 for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i += 3) {
1088 const char* testLocale = testCases[i];
1089 const char* regularExpected = testCases[i + 1];
1090 const char* noDefaultExpected = testCases[i + 2];
1091
1092 err = U_ZERO_ERROR;
1093 UResourceBundle* regularRB = ures_open(NULL, testLocale, &err);
1094 char errorMessage[200];
1095
1096 sprintf(errorMessage, "Error %s opening resource bundle for locale %s and URES_OPEN_LOCALE_DEFAULT_ROOT", u_errorName(err), testLocale);
1097 if (assertSuccess(errorMessage, &err)) {
1098 const char* resourceLocale = ures_getLocaleByType(regularRB, ULOC_ACTUAL_LOCALE, &err);
1099
1100 sprintf(errorMessage, "Error %s getting resource locale for locale %s and URES_OPEN_LOCALE_DEFAULT_ROOT", u_errorName(err), testLocale);
1101 if (assertSuccess(errorMessage, &err)) {
1102 sprintf(errorMessage, "Mismatch for locale %s and URES_OPEN_LOCALE_DEFAULT_ROOT", testLocale);
1103 if (uprv_strcmp(regularExpected, "root") == 0) {
1104 assertEquals(errorMessage, defaultLocaleID, resourceLocale);
1105 } else {
1106 assertEquals(errorMessage, regularExpected, resourceLocale);
1107 }
1108 }
1109 }
1110 ures_close(regularRB);
1111
1112 err = U_ZERO_ERROR;
1113 UResourceBundle* noDefaultRB = ures_openNoDefault(NULL, testLocale, &err);
1114
1115 sprintf(errorMessage, "Error %s opening resource bundle for locale %s and URES_OPEN_LOCALE_ROOT", u_errorName(err), testLocale);
1116 if (assertSuccess(errorMessage, &err)) {
1117 const char* resourceLocale = ures_getLocaleByType(noDefaultRB, ULOC_ACTUAL_LOCALE, &err);
1118
1119 sprintf(errorMessage, "Error %s getting resource locale for locale %s and URES_OPEN_LOCALE_ROOT", u_errorName(err), testLocale);
1120 if (assertSuccess(errorMessage, &err)) {
1121 sprintf(errorMessage, "Mismatch for locale %s and URES_OPEN_LOCALE_ROOT", testLocale);
1122 assertEquals(errorMessage, noDefaultExpected, resourceLocale);
1123 }
1124 }
1125 ures_close(noDefaultRB);
1126 }
1127 ures_close(defaultLocaleRB);
1128 }
1129