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