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