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