1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /*******************************************************************************
12 * Modification History:
13 * Name Date Description
14 * Madhu Katragadda 05/09/2000 Ported Tests for New ResourceBundle API
15 * Madhu Katragadda 05/24/2000 Added new tests to test RES_BINARY for collationElements
16 ********************************************************************************
21 #include "unicode/utypes.h"
23 #include "unicode/putil.h"
24 #include "unicode/ustring.h"
25 #include "unicode/ucnv.h"
26 #include "unicode/utf8.h"
27 #include "unicode/utf16.h"
31 #include "unicode/uchar.h"
32 #include "ucol_imp.h" /* for U_ICUDATA_COLL */
33 #include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */
34 #define RESTEST_HEAP_CHECK 0
36 #include "unicode/uloc.h"
37 #include "unicode/ulocdata.h"
40 #include "unicode/ctest.h"
47 /*****************************************************************************/
49 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
57 static UBool initialized
= FALSE
;
60 srand((unsigned)time(NULL
));
63 /* Assume rand has at least 12 bits of precision */
65 for (i
=0; i
<sizeof(l
); ++i
)
66 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
71 * Return a random double x where 0.0 <= x < 1.0.
76 return ((double)randul()) / UINT32_MAX
;
80 * Return a random integer i where 0 <= i < n.
82 static int32_t randi(int32_t n
)
84 return (int32_t)(randd() * n
);
86 /***************************************************************************************/
88 * Convert an integer, positive or negative, to a character string radix 10.
91 itoa1(int32_t i
, char* buf
)
101 /* Output digits in reverse order */
104 *p
++ = (char)('0' + (i
% 10));
110 /* Reverse the string */
119 static const int32_t kERROR_COUNT
= -1234567;
120 static const UChar kERROR
[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
121 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
123 /*****************************************************************************/
132 typedef enum E_Where E_Where
;
133 /*****************************************************************************/
135 #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)); }
136 #define CONFIRM_INT_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of %d\n", action, actual, expected); }
137 #define CONFIRM_INT_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x >= %d\n", action, actual, expected); }
138 #define CONFIRM_INT_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); log_err("%s returned %d instead of x != %d\n", action, actual, expected); }
139 /*#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)); } */
141 CONFIRM_ErrorCode(UErrorCode actual
,UErrorCode expected
)
143 if ((expected
)==(actual
))
148 /*log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); */
149 log_err("returned %s instead of %s\n", myErrorName(actual
), myErrorName(expected
));
154 /* Array of our test objects */
159 UErrorCode expected_constructor_status
;
161 UBool like
[e_Where_count
];
162 UBool inherits
[e_Where_count
];
166 /* "te" means test */
167 /* "IN" means inherits */
168 /* "NE" or "ne" means "does not exist" */
170 { "root", U_ZERO_ERROR
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} },
171 { "te", U_ZERO_ERROR
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
172 { "te_IN", U_ZERO_ERROR
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
173 { "te_NE", U_USING_FALLBACK_WARNING
, e_te
, { FALSE
, TRUE
, FALSE
}, { TRUE
, TRUE
, FALSE
} },
174 { "te_IN_NE", U_USING_FALLBACK_WARNING
, e_te_IN
, { FALSE
, FALSE
, TRUE
}, { TRUE
, TRUE
, TRUE
} },
175 { "ne", U_USING_DEFAULT_WARNING
, e_Root
, { TRUE
, FALSE
, FALSE
}, { TRUE
, FALSE
, FALSE
} }
178 static int32_t bundles_count
= UPRV_LENGTHOF(param
);
182 /*static void printUChars(UChar*);*/
183 static void TestDecodedBundle(void);
184 static void TestGetKeywordValues(void);
185 static void TestGetFunctionalEquivalent(void);
186 static void TestCLDRStyleAliases(void);
187 static void TestFallbackCodes(void);
188 static void TestGetUTF8String(void);
189 static void TestCLDRVersion(void);
191 /***************************************************************************************/
193 /* Array of our test objects */
195 void addNEWResourceBundleTest(TestNode
** root
)
197 addTest(root
, &TestErrorCodes
, "tsutil/creststn/TestErrorCodes");
198 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
199 addTest(root
, &TestEmptyBundle
, "tsutil/creststn/TestEmptyBundle");
200 addTest(root
, &TestConstruction1
, "tsutil/creststn/TestConstruction1");
201 addTest(root
, &TestResourceBundles
, "tsutil/creststn/TestResourceBundles");
202 addTest(root
, &TestNewTypes
, "tsutil/creststn/TestNewTypes");
203 addTest(root
, &TestEmptyTypes
, "tsutil/creststn/TestEmptyTypes");
204 addTest(root
, &TestBinaryCollationData
, "tsutil/creststn/TestBinaryCollationData");
205 addTest(root
, &TestAPI
, "tsutil/creststn/TestAPI");
206 addTest(root
, &TestErrorConditions
, "tsutil/creststn/TestErrorConditions");
207 addTest(root
, &TestDecodedBundle
, "tsutil/creststn/TestDecodedBundle");
208 addTest(root
, &TestResourceLevelAliasing
, "tsutil/creststn/TestResourceLevelAliasing");
209 addTest(root
, &TestDirectAccess
, "tsutil/creststn/TestDirectAccess");
210 addTest(root
, &TestTicket9804
, "tsutil/creststn/TestTicket9804");
211 addTest(root
, &TestXPath
, "tsutil/creststn/TestXPath");
212 addTest(root
, &TestCLDRStyleAliases
, "tsutil/creststn/TestCLDRStyleAliases");
213 addTest(root
, &TestFallbackCodes
, "tsutil/creststn/TestFallbackCodes");
214 addTest(root
, &TestGetUTF8String
, "tsutil/creststn/TestGetUTF8String");
215 addTest(root
, &TestCLDRVersion
, "tsutil/creststn/TestCLDRVersion");
216 addTest(root
, &TestPreventFallback
, "tsutil/creststn/TestPreventFallback");
218 addTest(root
, &TestFallback
, "tsutil/creststn/TestFallback");
219 addTest(root
, &TestGetVersion
, "tsutil/creststn/TestGetVersion");
220 addTest(root
, &TestGetVersionColl
, "tsutil/creststn/TestGetVersionColl");
221 addTest(root
, &TestAliasConflict
, "tsutil/creststn/TestAliasConflict");
222 addTest(root
, &TestGetKeywordValues
, "tsutil/creststn/TestGetKeywordValues");
223 addTest(root
, &TestGetFunctionalEquivalent
,"tsutil/creststn/TestGetFunctionalEquivalent");
224 addTest(root
, &TestJB3763
, "tsutil/creststn/TestJB3763");
225 addTest(root
, &TestStackReuse
, "tsutil/creststn/TestStackReuse");
229 /***************************************************************************************/
230 static const char* norwayNames
[] = {
240 static const char* norwayLocales
[] = {
250 static void checkStatus(int32_t line
, UErrorCode expected
, UErrorCode status
) {
251 if(U_FAILURE(status
)) {
252 log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__
, line
);
254 if(status
!= expected
) {
255 log_err_status(status
, "%s:%d: Expected error code %s, got error code %s\n", __FILE__
, line
, u_errorName(expected
), u_errorName(status
));
259 static void TestErrorCodes(void) {
260 UErrorCode status
= U_USING_DEFAULT_WARNING
;
262 UResourceBundle
*r
= NULL
, *r2
= NULL
;
264 /* First check with ICUDATA */
265 /* first bundle should return fallback warning */
266 r
= ures_open(NULL
, "ti_ER_ASSAB", &status
);
267 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
270 /* this bundle should return zero error, so it shouldn't change the status */
271 status
= U_USING_DEFAULT_WARNING
;
272 r
= ures_open(NULL
, "ti_ER", &status
);
273 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
275 /* we look up the resource which is aliased, but it lives in fallback */
277 if(U_SUCCESS(status
) && r
!= NULL
) {
278 status
= U_USING_DEFAULT_WARNING
;
279 r2
= ures_getByKey(r
, "ExemplarCharacters", NULL
, &status
); /* ExemplarCharacters lives in ti */
280 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
284 /* this bundle should return zero error, so it shouldn't change the status */
285 status
= U_USING_DEFAULT_WARNING
;
286 r
= ures_open(U_ICUDATA_REGION
, "ti", &status
);
287 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
290 status
= U_USING_FALLBACK_WARNING
;
291 r
= ures_open(NULL
, "nolocale", &status
);
292 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
296 #if !UCONFIG_NO_COLLATION
297 /** Now, with the collation bundle **/
299 /* first bundle should return fallback warning */
300 r
= ures_open(U_ICUDATA_COLL
, "sr_YU_VOJVODINA", &status
);
301 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
304 /* this bundle should return zero error, so it shouldn't change the status */
305 status
= U_USING_FALLBACK_WARNING
;
306 r
= ures_open(U_ICUDATA_COLL
, "sr", &status
);
307 checkStatus(__LINE__
, U_USING_FALLBACK_WARNING
, status
);
309 /* we look up the resource which is aliased */
310 if(U_SUCCESS(status
) && r
!= NULL
) {
311 status
= U_USING_DEFAULT_WARNING
;
312 r2
= ures_getByKey(r
, "collations", NULL
, &status
);
313 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
317 /* this bundle should return zero error, so it shouldn't change the status */
318 status
= U_USING_DEFAULT_WARNING
;
319 r
= ures_open(U_ICUDATA_COLL
, "sr", &status
);
320 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
322 /* we look up the resource which is aliased and at our level */
323 if(U_SUCCESS(status
) && r
!= NULL
) {
324 status
= U_USING_DEFAULT_WARNING
;
325 r2
= ures_getByKey(r
, "collations", r2
, &status
);
326 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
330 status
= U_USING_FALLBACK_WARNING
;
331 r
= ures_open(U_ICUDATA_COLL
, "nolocale", &status
);
332 checkStatus(__LINE__
, U_USING_DEFAULT_WARNING
, status
);
335 #endif /* !UCONFIG_NO_COLLATION */
338 static void TestAliasConflict(void) {
339 UErrorCode status
= U_ZERO_ERROR
;
340 UResourceBundle
*he
= NULL
;
341 UResourceBundle
*iw
= NULL
;
342 UResourceBundle
*norway
= NULL
;
343 const UChar
*result
= NULL
;
347 const char *realName
= NULL
;
349 he
= ures_open(NULL
, "he", &status
);
350 iw
= ures_open(NULL
, "iw", &status
);
351 if(U_FAILURE(status
)) {
352 log_err_status(status
, "Failed to get resource with %s\n", myErrorName(status
));
355 result
= ures_getStringByKey(he
, "ExemplarCharacters", &resultLen
, &status
);
356 if(U_FAILURE(status
) || result
== NULL
) {
357 log_err_status(status
, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status
));
361 size
= UPRV_LENGTHOF(norwayNames
);
362 for(i
= 0; i
< size
; i
++) {
363 status
= U_ZERO_ERROR
;
364 norway
= ures_open(NULL
, norwayNames
[i
], &status
);
365 if(U_FAILURE(status
)) {
366 log_err_status(status
, "Failed to get resource with %s for %s\n", myErrorName(status
), norwayNames
[i
]);
369 realName
= ures_getLocale(norway
, &status
);
370 log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames
[i
], realName
);
371 if(realName
== NULL
|| strcmp(norwayLocales
[i
], realName
) != 0) {
372 log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames
[i
], norwayLocales
[i
], realName
);
378 static void TestDecodedBundle(){
380 UErrorCode error
= U_ZERO_ERROR
;
382 UResourceBundle
* resB
;
384 const UChar
* srcFromRes
;
386 static const UChar uSrc
[] = {
387 0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F,
388 0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928,
389 0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A,
390 0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938,
391 0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020,
392 0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938,
393 0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947,
394 0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020,
395 0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915,
396 0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E,
397 0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917,
398 0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D,
399 0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020,
400 0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915,
401 0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B,
402 0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E,
403 0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902,
404 0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932,
405 0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F,
406 0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938,
407 0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930,
408 0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020,
409 0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020,
410 0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F,
411 0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C,
412 0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941,
413 0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917,
414 0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E,
415 0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930,
416 0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E,
417 0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,
418 0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,
419 0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935,
420 0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915,
421 0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F,
422 0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033,
423 0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F,
424 0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915,
425 0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020,
431 const char *testdatapath
= loadTestData(&error
);
432 resB
= ures_open(testdatapath
, "encoded", &error
);
433 srcFromRes
=tres_getString(resB
,-1,"str",&len
,&error
);
434 if(U_FAILURE(error
)){
435 log_data_err("Could not find encoded.res from test data bundle. Error: %s\n", u_errorName(error
));
439 if(u_strncmp(srcFromRes
,uSrc
,len
)!=0){
440 log_err("Genrb produced res files after decoding failed\n");
443 if(uSrc
[num
]!=srcFromRes
[num
]){
444 log_verbose(" Expected: 0x%04X Got: 0x%04X \n", uSrc
[num
],srcFromRes
[num
]);
448 if (len
!= u_strlen(uSrc
)) {
449 log_err("Genrb produced a string larger than expected\n");
454 static void TestNewTypes() {
455 UResourceBundle
* theBundle
= NULL
;
457 const char* testdatapath
;
458 UErrorCode status
= U_ZERO_ERROR
;
459 UResourceBundle
* res
= NULL
;
460 uint8_t *binResult
= NULL
;
463 int32_t intResult
= 0;
464 uint32_t uintResult
= 0;
465 const UChar
*empty
= NULL
;
466 const UChar
*zeroString
;
467 UChar expected
[] = { 'a','b','c','\0','d','e','f' };
468 const char* expect
="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
471 testdatapath
=loadTestData(&status
);
473 if(U_FAILURE(status
))
475 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
479 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
481 empty
= tres_getString(theBundle
, -1, "emptystring", &len
, &status
);
482 if(empty
&& (*empty
!= 0 || len
!= 0)) {
483 log_err("Empty string returned invalid value\n");
486 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
488 CONFIRM_INT_NE(theBundle
, NULL
);
490 /* This test reads the string "abc\u0000def" from the bundle */
491 /* if everything is working correctly, the size of this string */
492 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
494 strcpy(action
, "getting and testing of string with embeded zero");
495 res
= ures_getByKey(theBundle
, "zerotest", res
, &status
);
496 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
497 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
498 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
499 if(U_SUCCESS(status
)){
500 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
501 CONFIRM_INT_EQ(len
, 7);
502 CONFIRM_INT_NE(len
, 3);
505 if(zeroString
[i
]!= expected
[i
]){
506 log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected
[i
], zeroString
[i
]);
510 strcpy(action
, "getting and testing of binary type");
511 res
= ures_getByKey(theBundle
, "binarytest", res
, &status
);
512 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
513 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
514 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
515 if(U_SUCCESS(status
)){
516 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
517 CONFIRM_INT_EQ(len
, 15);
518 for(i
= 0; i
<15; i
++) {
519 CONFIRM_INT_EQ(binResult
[i
], i
);
523 strcpy(action
, "getting and testing of imported binary type");
524 res
= ures_getByKey(theBundle
, "importtest", res
, &status
);
525 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
526 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
527 binResult
=(uint8_t*)ures_getBinary(res
, &len
, &status
);
528 if(U_SUCCESS(status
)){
529 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
530 CONFIRM_INT_EQ(len
, 15);
531 for(i
= 0; i
<15; i
++) {
532 CONFIRM_INT_EQ(binResult
[i
], i
);
536 strcpy(action
, "getting and testing of integer types");
537 res
= ures_getByKey(theBundle
, "one", res
, &status
);
538 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
539 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
540 intResult
=ures_getInt(res
, &status
);
541 uintResult
= ures_getUInt(res
, &status
);
542 if(U_SUCCESS(status
)){
543 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
544 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
545 CONFIRM_INT_EQ(intResult
, 1);
548 strcpy(action
, "getting minusone");
549 res
= ures_getByKey(theBundle
, "minusone", res
, &status
);
550 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
551 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
552 intResult
=ures_getInt(res
, &status
);
553 uintResult
= ures_getUInt(res
, &status
);
554 if(U_SUCCESS(status
)){
555 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
556 CONFIRM_INT_EQ(uintResult
, 0x0FFFFFFF); /* a 28 bit integer */
557 CONFIRM_INT_EQ(intResult
, -1);
558 CONFIRM_INT_NE(uintResult
, (uint32_t)intResult
);
561 strcpy(action
, "getting plusone");
562 res
= ures_getByKey(theBundle
, "plusone", res
, &status
);
563 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
564 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
565 intResult
=ures_getInt(res
, &status
);
566 uintResult
= ures_getUInt(res
, &status
);
567 if(U_SUCCESS(status
)){
568 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
569 CONFIRM_INT_EQ(uintResult
, (uint32_t)intResult
);
570 CONFIRM_INT_EQ(intResult
, 1);
573 res
= ures_getByKey(theBundle
, "onehundredtwentythree", res
, &status
);
574 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
575 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
576 intResult
=ures_getInt(res
, &status
);
577 if(U_SUCCESS(status
)){
578 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
579 CONFIRM_INT_EQ(intResult
, 123);
582 /* this tests if escapes are preserved or not */
584 const UChar
* str
= tres_getString(theBundle
,-1,"testescape",&len
,&status
);
585 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
586 if(U_SUCCESS(status
)){
587 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
588 if(u_strcmp(uExpect
,str
)){
589 log_err("Did not get the expected string for testescape\n");
593 /* this tests if unescaping works are expected */
596 char pattern
[2048] = "";
598 UChar
* expectedEscaped
;
602 /* This strcpy fixes compiler warnings about long strings */
603 strcpy(pattern
, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 "
604 "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F "
605 "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 "
606 "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD "
607 "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF "
610 "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF "
611 "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF "
612 "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF "
613 "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB "
614 "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C "
617 "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F "
621 patternLen
= (int32_t)uprv_strlen(pattern
);
622 expectedEscaped
= (UChar
*)malloc(U_SIZEOF_UCHAR
* patternLen
);
623 got
= tres_getString(theBundle
,-1,"test_unescaping",&len
,&status
);
624 expectedLen
= u_unescape(pattern
,expectedEscaped
,patternLen
);
625 if(got
==NULL
|| u_strncmp(expectedEscaped
,got
,expectedLen
)!=0 || expectedLen
!= len
){
626 log_err("genrb failed to unescape string\n");
629 for(i
=0;i
<expectedLen
;i
++){
630 if(expectedEscaped
[i
] != got
[i
]){
631 log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped
[i
], got
[i
]);
635 free(expectedEscaped
);
636 status
= U_ZERO_ERROR
;
638 /* test for jitterbug#1435 */
640 const UChar
* str
= tres_getString(theBundle
,-1,"test_underscores",&len
,&status
);
641 expect
="test message ....";
642 u_charsToUChars(expect
,uExpect
,(int32_t)strlen(expect
)+1);
643 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
644 if(str
== NULL
|| u_strcmp(uExpect
,str
)){
645 log_err("Did not get the expected string for test_underscores.\n");
648 /* test for jitterbug#2626 */
649 #if !UCONFIG_NO_COLLATION
651 UResourceBundle
* resB
= NULL
;
652 const UChar
* str
= NULL
;
653 int32_t strLength
= 0;
654 const UChar my
[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */
655 status
= U_ZERO_ERROR
;
656 resB
= ures_getByKey(theBundle
, "collations", resB
, &status
);
657 resB
= ures_getByKey(resB
, "standard", resB
, &status
);
658 str
= tres_getString(resB
,-1,"Sequence",&strLength
,&status
);
659 if(!str
|| U_FAILURE(status
)) {
660 log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status
));
661 } else if(u_strcmp(my
,str
) != 0){
662 log_err("Did not get the expected string for escaped \\u0075\n");
668 const char *sourcePath
= ctest_dataSrcDir();
669 int32_t srcPathLen
= (int32_t)strlen(sourcePath
);
670 const char *deltaPath
= ".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
;
671 int32_t deltaPathLen
= (int32_t)strlen(deltaPath
);
672 char *testDataFileName
= (char *) malloc( srcPathLen
+ deltaPathLen
+ 50 );
673 char *path
= testDataFileName
;
675 strcpy(path
, sourcePath
);
677 strcpy(path
, deltaPath
);
678 path
+= deltaPathLen
;
679 status
= U_ZERO_ERROR
;
682 const UChar
* str
= tres_getString(theBundle
, -1, "testincludeUTF",&strLen
,&status
);
683 strcpy(path
, "riwords.txt");
684 path
[strlen("riwords.txt")]=0;
685 if(U_FAILURE(status
)){
686 log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status
));
689 const char* cp
= NULL
;
690 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
692 if(U_SUCCESS(status
)){
693 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
694 if(U_SUCCESS(status
)){
695 /* verify the contents */
697 log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len
,strLen
);
699 /* test string termination */
700 if(u_strlen(str
) != strLen
|| str
[strLen
]!= 0 ){
701 log_err("testinclude not null terminated!\n");
703 if(u_strncmp(str
, buffer
,strLen
)!=0){
704 log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n");
707 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
712 log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
716 status
= U_ZERO_ERROR
;
719 const UChar
* str
= tres_getString(theBundle
, -1, "testinclude",&strLen
,&status
);
720 strcpy(path
, "translit_rules.txt");
721 path
[strlen("translit_rules.txt")]=0;
723 if(U_FAILURE(status
)){
724 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status
));
728 UCHARBUF
* ucbuf
= ucbuf_open(testDataFileName
,&cp
,FALSE
,FALSE
,&status
);
730 if(U_SUCCESS(status
)){
731 const UChar
* buffer
= ucbuf_getBuffer(ucbuf
,&len
,&status
);
732 if(U_SUCCESS(status
)){
733 /* verify the contents */
735 log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len
,strLen
);
737 if(u_strncmp(str
, buffer
,strLen
)!=0){
738 log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n");
741 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName
, u_errorName(status
));
745 log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName
,u_errorName(status
));
749 free(testDataFileName
);
752 ures_close(theBundle
);
756 static void TestEmptyTypes() {
757 UResourceBundle
* theBundle
= NULL
;
759 const char* testdatapath
;
760 UErrorCode status
= U_ZERO_ERROR
;
761 UResourceBundle
* res
= NULL
;
762 UResourceBundle
* resArray
= NULL
;
763 const uint8_t *binResult
= NULL
;
765 int32_t intResult
= 0;
766 const UChar
*zeroString
;
767 const int32_t *zeroIntVect
= NULL
;
769 strcpy(action
, "Construction of testtypes bundle");
770 testdatapath
=loadTestData(&status
);
771 if(U_FAILURE(status
))
773 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
777 theBundle
= ures_open(testdatapath
, "testtypes", &status
);
779 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
781 CONFIRM_INT_NE(theBundle
, NULL
);
783 /* This test reads the string "abc\u0000def" from the bundle */
784 /* if everything is working correctly, the size of this string */
785 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
787 status
= U_ZERO_ERROR
;
788 strcpy(action
, "getting and testing of explicit string of zero length string");
789 res
= ures_getByKey(theBundle
, "emptyexplicitstring", res
, &status
);
790 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
791 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
792 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
793 if(U_SUCCESS(status
)){
794 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
795 CONFIRM_INT_EQ(len
, 0);
796 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
799 log_err("Couldn't get emptyexplicitstring\n");
802 status
= U_ZERO_ERROR
;
803 strcpy(action
, "getting and testing of normal string of zero length string");
804 res
= ures_getByKey(theBundle
, "emptystring", res
, &status
);
805 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
806 CONFIRM_INT_EQ(ures_getType(res
), URES_STRING
);
807 zeroString
=tres_getString(res
, -1, NULL
, &len
, &status
);
808 if(U_SUCCESS(status
)){
809 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
810 CONFIRM_INT_EQ(len
, 0);
811 CONFIRM_INT_EQ(u_strlen(zeroString
), 0);
814 log_err("Couldn't get emptystring\n");
817 status
= U_ZERO_ERROR
;
818 strcpy(action
, "getting and testing of empty int");
819 res
= ures_getByKey(theBundle
, "emptyint", res
, &status
);
820 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
821 CONFIRM_INT_EQ(ures_getType(res
), URES_INT
);
822 intResult
=ures_getInt(res
, &status
);
823 if(U_SUCCESS(status
)){
824 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
825 CONFIRM_INT_EQ(intResult
, 0);
828 log_err("Couldn't get emptystring\n");
831 status
= U_ZERO_ERROR
;
832 strcpy(action
, "getting and testing of zero length intvector");
833 res
= ures_getByKey(theBundle
, "emptyintv", res
, &status
);
834 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
835 CONFIRM_INT_EQ(ures_getType(res
), URES_INT_VECTOR
);
837 if(U_FAILURE(status
)){
838 log_err("Couldn't get emptyintv key %s\n", u_errorName(status
));
841 zeroIntVect
=ures_getIntVector(res
, &len
, &status
);
842 (void)zeroIntVect
; /* Suppress set but not used warning. */
843 if(!U_SUCCESS(status
) || resArray
!= NULL
|| len
!= 0) {
844 log_err("Shouldn't get emptyintv\n");
848 status
= U_ZERO_ERROR
;
849 strcpy(action
, "getting and testing of zero length emptybin");
850 res
= ures_getByKey(theBundle
, "emptybin", res
, &status
);
851 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
852 CONFIRM_INT_EQ(ures_getType(res
), URES_BINARY
);
854 if(U_FAILURE(status
)){
855 log_err("Couldn't get emptybin key %s\n", u_errorName(status
));
858 binResult
=ures_getBinary(res
, &len
, &status
);
859 (void)binResult
; /* Suppress set but not used warning. */
860 if(!U_SUCCESS(status
) || len
!= 0) {
861 log_err("Couldn't get emptybin, or it's not empty\n");
865 status
= U_ZERO_ERROR
;
866 strcpy(action
, "getting and testing of zero length emptyarray");
867 res
= ures_getByKey(theBundle
, "emptyarray", res
, &status
);
868 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
869 CONFIRM_INT_EQ(ures_getType(res
), URES_ARRAY
);
871 if(U_FAILURE(status
)){
872 log_err("Couldn't get emptyarray key %s\n", u_errorName(status
));
875 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
876 if(U_SUCCESS(status
) || resArray
!= NULL
){
877 log_err("Shouldn't get emptyarray[0]\n");
881 status
= U_ZERO_ERROR
;
882 strcpy(action
, "getting and testing of zero length emptytable");
883 res
= ures_getByKey(theBundle
, "emptytable", res
, &status
);
884 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
885 CONFIRM_INT_EQ(ures_getType(res
), URES_TABLE
);
887 if(U_FAILURE(status
)){
888 log_err("Couldn't get emptytable key %s\n", u_errorName(status
));
891 resArray
=ures_getByIndex(res
, 0, resArray
, &status
);
892 if(U_SUCCESS(status
) || resArray
!= NULL
){
893 log_err("Shouldn't get emptytable[0]\n");
898 ures_close(theBundle
);
901 static void TestEmptyBundle(){
902 UErrorCode status
= U_ZERO_ERROR
;
903 const char* testdatapath
=NULL
;
904 UResourceBundle
*resb
=0, *dResB
=0;
906 testdatapath
=loadTestData(&status
);
907 if(U_FAILURE(status
))
909 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
912 resb
= ures_open(testdatapath
, "testempty", &status
);
914 if(U_SUCCESS(status
)){
915 dResB
= ures_getByKey(resb
,"test",dResB
,&status
);
916 if(status
!= U_MISSING_RESOURCE_ERROR
){
917 log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n",
918 u_errorName(U_MISSING_RESOURCE_ERROR
),u_errorName(status
));
925 static void TestBinaryCollationData(){
926 #if !UCONFIG_NO_COLLATION
927 UErrorCode status
=U_ZERO_ERROR
;
928 const char* locale
="te";
929 const char* testdatapath
;
930 UResourceBundle
*teRes
= NULL
;
931 UResourceBundle
*coll
=NULL
;
932 UResourceBundle
*binColl
= NULL
;
933 uint8_t *binResult
= NULL
;
935 const char* action
="testing the binary collaton data";
937 log_verbose("Testing binary collation data resource......\n");
939 testdatapath
=loadTestData(&status
);
940 if(U_FAILURE(status
))
942 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
947 teRes
=ures_open(testdatapath
, locale
, &status
);
948 if(U_FAILURE(status
)){
949 log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status
));
953 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
954 coll
= ures_getByKey(coll
, "standard", coll
, &status
);
955 if(U_SUCCESS(status
)){
956 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
957 CONFIRM_INT_EQ(ures_getType(coll
), URES_TABLE
);
958 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
959 if(U_SUCCESS(status
)){
960 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
961 CONFIRM_INT_EQ(ures_getType(binColl
), URES_BINARY
);
962 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
963 (void)binResult
; /* Suppress set but not used warning. */
964 if(U_SUCCESS(status
)){
965 CONFIRM_ErrorCode(status
, U_ZERO_ERROR
);
966 CONFIRM_INT_GE(len
, 1);
970 log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n");
974 log_err("ERROR: ures_getByKey(locale(te), collations) failed\n");
983 static void TestAPI() {
984 UErrorCode status
=U_ZERO_ERROR
;
986 const char* key
=NULL
;
987 const UChar
* value
=NULL
;
988 const char* testdatapath
;
989 UChar
* utestdatapath
=NULL
;
990 char convOutput
[256];
991 UChar largeBuffer
[1025];
992 UResourceBundle
*teRes
= NULL
;
993 UResourceBundle
*teFillin
=NULL
;
994 UResourceBundle
*teFillin2
=NULL
;
996 log_verbose("Testing ures_openU()......\n");
998 testdatapath
=loadTestData(&status
);
999 if(U_FAILURE(status
))
1001 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1004 len
=(int32_t)strlen(testdatapath
);
1005 utestdatapath
= (UChar
*) malloc((len
+10)*sizeof(UChar
));
1007 u_charsToUChars(testdatapath
, utestdatapath
, (int32_t)strlen(testdatapath
)+1);
1008 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\'
1010 /* Convert all backslashes to forward slashes so that we can make sure that ures_openU
1011 can handle invariant characters. */
1013 while ((backslash
= u_strchr(utestdatapath
, 0x005C))) {
1014 *backslash
= 0x002F;
1019 u_memset(largeBuffer
, 0x0030, UPRV_LENGTHOF(largeBuffer
));
1020 largeBuffer
[UPRV_LENGTHOF(largeBuffer
)-1] = 0;
1022 /*Test ures_openU */
1024 status
= U_ZERO_ERROR
;
1025 ures_close(ures_openU(largeBuffer
, "root", &status
));
1026 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1027 log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status
));
1030 status
= U_ZERO_ERROR
;
1031 ures_close(ures_openU(NULL
, "root", &status
));
1032 if(U_FAILURE(status
)){
1033 log_err_status(status
, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status
));
1036 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1037 if(ures_openU(NULL
, "root", &status
) != NULL
){
1038 log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status
));
1041 status
= U_ZERO_ERROR
;
1042 teRes
=ures_openU(utestdatapath
, "te", &status
);
1043 if(U_FAILURE(status
)){
1044 log_err_status(status
, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1047 /*Test ures_getLocale() */
1048 log_verbose("Testing ures_getLocale() .....\n");
1049 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1050 log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes
, &status
));
1052 /*Test ures_getNextString() */
1053 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1054 key
=ures_getKey(teFillin
);
1055 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1056 (void)value
; /* Suppress set but not used warning. */
1057 ures_resetIterator(NULL
);
1058 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1059 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1060 log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1061 myErrorName(status
));
1063 ures_resetIterator(teRes
);
1064 /*Test ures_getNextResource() where resource is table*/
1065 status
=U_ZERO_ERROR
;
1066 #if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1067 /* The next key varies depending on the charset. */
1068 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1069 if(U_FAILURE(status
)){
1070 log_err("ERROR: ures_getNextResource() failed \n");
1072 key
=ures_getKey(teFillin
);
1073 /*if(strcmp(key, "%%CollationBin") != 0){*/
1074 /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */
1075 if(strcmp(key
, "a") != 0){
1076 log_err("ERROR: ures_getNextResource() failed\n");
1080 /*Test ures_getByIndex on string Resource*/
1081 teFillin
=ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1082 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1083 if(U_FAILURE(status
)){
1084 log_err("ERROR: ures_getByIndex on string resource failed\n");
1086 if(strcmp(u_austrcpy(convOutput
, tres_getString(teFillin2
, -1, NULL
, &len
, &status
)), "TE") != 0){
1087 status
=U_ZERO_ERROR
;
1088 log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2
, &len
, &status
)));
1091 /*ures_close(teRes);*/
1093 /*Test ures_openFillIn*/
1094 log_verbose("Testing ures_openFillIn......\n");
1095 status
=U_ZERO_ERROR
;
1096 ures_openFillIn(teRes
, testdatapath
, "te", &status
);
1097 if(U_FAILURE(status
)){
1098 log_err("ERROR: ures_openFillIn failed\n");
1101 if(strcmp(ures_getLocale(teRes
, &status
), "te") != 0){
1102 log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n");
1104 ures_getByKey(teRes
, "string_only_in_te", teFillin
, &status
);
1105 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1106 if(ures_getType(teFillin2
) != URES_STRING
){
1107 log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n");
1109 teFillin2
=ures_getNextResource(teFillin
, teFillin2
, &status
);
1110 if(status
!=U_INDEX_OUTOFBOUNDS_ERROR
){
1111 log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1112 myErrorName(status
));
1115 ures_close(teFillin
);
1116 ures_close(teFillin2
);
1119 /* Test that ures_getLocale() returns the "real" locale ID */
1120 status
=U_ZERO_ERROR
;
1121 teRes
=ures_open(NULL
, "dE_At_NOWHERE_TO_BE_FOUND", &status
);
1122 if(U_FAILURE(status
)) {
1123 log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status
));
1125 if(0!=strcmp("de_AT", ures_getLocale(teRes
, &status
))) {
1126 log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes
, &status
));
1131 /* same test, but with an aliased locale resource bundle */
1132 status
=U_ZERO_ERROR
;
1133 teRes
=ures_open(NULL
, "iW_Il_depRecaTed_HebreW", &status
);
1134 if(U_FAILURE(status
)) {
1135 log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status
));
1137 if(0!=strcmp("he_IL", ures_getLocale(teRes
, &status
))) {
1138 log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes
, &status
));
1142 free(utestdatapath
);
1145 static void TestErrorConditions(){
1146 UErrorCode status
=U_ZERO_ERROR
;
1147 const char *key
=NULL
;
1148 const UChar
*value
=NULL
;
1149 const char* testdatapath
;
1150 UChar
* utestdatapath
;
1152 UResourceBundle
*teRes
= NULL
;
1153 UResourceBundle
*coll
=NULL
;
1154 UResourceBundle
*binColl
= NULL
;
1155 UResourceBundle
*teFillin
=NULL
;
1156 UResourceBundle
*teFillin2
=NULL
;
1157 uint8_t *binResult
= NULL
;
1161 testdatapath
= loadTestData(&status
);
1162 if(U_FAILURE(status
))
1164 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1167 len
= (int32_t)strlen(testdatapath
);
1168 utestdatapath
= (UChar
*) malloc(sizeof(UChar
) *(len
+10));
1169 u_uastrcpy(utestdatapath
, testdatapath
);
1171 /*Test ures_openU with status != U_ZERO_ERROR*/
1172 log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n");
1173 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1174 teRes
=ures_openU(utestdatapath
, "te", &status
);
1175 if(U_FAILURE(status
)){
1176 log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath
);
1178 log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath
));
1181 /*Test ures_openFillIn fails when input UResourceBundle parameter is NULL*/
1182 log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n");
1183 status
=U_ZERO_ERROR
;
1184 ures_openFillIn(NULL
, testdatapath
, "te", &status
);
1185 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1186 log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1187 myErrorName(status
));
1189 /*Test ures_openDirectFillIn fails when input UResourceBundle parameter is NULL*/
1190 log_verbose("Testing ures_openDirectFillIn with UResourceBundle = NULL.....\n");
1191 status
=U_ZERO_ERROR
;
1192 ures_openDirectFillIn(NULL
, testdatapath
, "te", &status
);
1193 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1194 log_err("ERROR: ures_openDirectFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1195 myErrorName(status
));
1197 /*Test ures_getLocale() with status != U_ZERO_ERROR*/
1198 status
=U_ZERO_ERROR
;
1199 teRes
=ures_openU(utestdatapath
, "te", &status
);
1200 if(U_FAILURE(status
)){
1201 log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath
), myErrorName(status
));
1204 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1205 if(ures_getLocale(teRes
, &status
) != NULL
){
1206 log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n");
1208 /*Test ures_getLocale() with UResourceBundle = NULL*/
1209 status
=U_ZERO_ERROR
;
1210 if(ures_getLocale(NULL
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1211 log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1212 myErrorName(status
));
1214 /*Test ures_getSize() with UResourceBundle = NULL */
1215 status
=U_ZERO_ERROR
;
1216 if(ures_getSize(NULL
) != 0){
1217 log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL. Got =%d\n", ures_getSize(NULL
));
1219 /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/
1220 status
=U_ZERO_ERROR
;
1221 if(ures_getType(NULL
) != URES_NONE
){
1222 log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL. Got =%d\n", ures_getType(NULL
));
1224 /*Test ures_getKey() with UResourceBundle = NULL*/
1225 status
=U_ZERO_ERROR
;
1226 if(ures_getKey(NULL
) != NULL
){
1227 log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL. Got =%d\n", ures_getKey(NULL
));
1229 /*Test ures_hasNext() with UResourceBundle = NULL*/
1230 status
=U_ZERO_ERROR
;
1231 if(ures_hasNext(NULL
) != FALSE
){
1232 log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL. Got =%d\n", ures_hasNext(NULL
));
1234 /*Test ures_get() with UResourceBundle = NULL*/
1235 status
=U_ZERO_ERROR
;
1236 if(ures_getStringByKey(NULL
, "string_only_in_te", &resultLen
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1237 log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1238 myErrorName(status
));
1240 /*Test ures_getByKey() with UResourceBundle = NULL*/
1241 status
=U_ZERO_ERROR
;
1242 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1243 if( teFillin
!= NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1244 log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1245 myErrorName(status
));
1247 /*Test ures_getByKey() with status != U_ZERO_ERROR*/
1248 teFillin
=ures_getByKey(NULL
, "string_only_in_te", teFillin
, &status
);
1249 if(teFillin
!= NULL
){
1250 log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n");
1252 /*Test ures_getStringByKey() with UResourceBundle = NULL*/
1253 status
=U_ZERO_ERROR
;
1254 if(ures_getStringByKey(NULL
, "string_only_in_te", &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1255 log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1256 myErrorName(status
));
1258 /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/
1259 if(ures_getStringByKey(teRes
, "string_only_in_te", &len
, &status
) != NULL
){
1260 log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1261 myErrorName(status
));
1263 /*Test ures_getString() with UResourceBundle = NULL*/
1264 status
=U_ZERO_ERROR
;
1265 if(ures_getString(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1266 log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1267 myErrorName(status
));
1269 /*Test ures_getString() with status != U_ZERO_ERROR*/
1270 if(ures_getString(teRes
, &len
, &status
) != NULL
){
1271 log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1272 myErrorName(status
));
1274 /*Test ures_getBinary() with UResourceBundle = NULL*/
1275 status
=U_ZERO_ERROR
;
1276 if(ures_getBinary(NULL
, &len
, &status
) != NULL
&& status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1277 log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1278 myErrorName(status
));
1280 /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/
1281 status
=U_ZERO_ERROR
;
1282 coll
= ures_getByKey(teRes
, "collations", coll
, &status
);
1283 coll
= ures_getByKey(teRes
, "standard", coll
, &status
);
1284 binColl
=ures_getByKey(coll
, "%%CollationBin", binColl
, &status
);
1286 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1287 binResult
=(uint8_t*)ures_getBinary(binColl
, &len
, &status
);
1288 if(binResult
!= NULL
){
1289 log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n");
1292 /*Test ures_getNextResource() with status != U_ZERO_ERROR*/
1293 teFillin
=ures_getNextResource(teRes
, teFillin
, &status
);
1294 if(teFillin
!= NULL
){
1295 log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1297 /*Test ures_getNextResource() with UResourceBundle = NULL*/
1298 status
=U_ZERO_ERROR
;
1299 teFillin
=ures_getNextResource(NULL
, teFillin
, &status
);
1300 if(teFillin
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1301 log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail. Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n",
1302 myErrorName(status
));
1304 /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/
1305 teFillin
=ures_getByKey(teRes
, "tagged_array_in_te_te_IN", teFillin
, &status
);
1306 key
=ures_getKey(teFillin
);
1307 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1308 value
=(UChar
*)ures_getNextString(teFillin
, &len
, &key
, &status
);
1310 log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1312 /*Test ures_getNextString with UResourceBundle = NULL*/
1313 status
=U_ZERO_ERROR
;
1314 value
=(UChar
*)ures_getNextString(NULL
, &len
, &key
, &status
);
1315 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1316 log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1317 myErrorName(status
));
1319 /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/
1320 status
=U_ZERO_ERROR
;
1321 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1322 if(ures_countArrayItems(teRes
, "array_only_in_te", &status
) != 4) {
1323 log_err("ERROR: Wrong number of items in an array!\n");
1325 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1326 teFillin2
=ures_getByIndex(teFillin
, 0, teFillin2
, &status
);
1327 if(teFillin2
!= NULL
){
1328 log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1330 /*Test ures_getByIndex with UResourceBundle = NULL */
1331 status
=U_ZERO_ERROR
;
1332 teFillin2
=ures_getByIndex(NULL
, 0, teFillin2
, &status
);
1333 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1334 log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1335 myErrorName(status
));
1337 /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/
1338 status
=U_ZERO_ERROR
;
1339 teFillin
=ures_getByKey(teRes
, "array_only_in_te", teFillin
, &status
);
1340 status
=U_ILLEGAL_ARGUMENT_ERROR
;
1341 value
=(UChar
*)ures_getStringByIndex(teFillin
, 0, &len
, &status
);
1343 log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1345 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1346 status
=U_ZERO_ERROR
;
1347 value
=(UChar
*)ures_getStringByIndex(NULL
, 0, &len
, &status
);
1348 if(value
!= NULL
|| status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1349 log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1350 myErrorName(status
));
1352 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1353 status
=U_ZERO_ERROR
;
1354 value
=(UChar
*)ures_getStringByIndex(teFillin
, 9999, &len
, &status
);
1355 if(value
!= NULL
|| status
!= U_MISSING_RESOURCE_ERROR
){
1356 log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n",
1357 myErrorName(status
));
1359 /*Test ures_getInt() where UResourceBundle = NULL */
1360 status
=U_ZERO_ERROR
;
1361 if(ures_getInt(NULL
, &status
) != -1 && status
!= U_ILLEGAL_ARGUMENT_ERROR
){
1362 log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n",
1363 myErrorName(status
));
1365 /*Test ures_getInt() where status != U_ZERO_ERROR */
1366 if(ures_getInt(teRes
, &status
) != -1){
1367 log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n");
1370 ures_close(teFillin
);
1371 ures_close(teFillin2
);
1373 ures_close(binColl
);
1375 free(utestdatapath
);
1380 static void TestGetVersion(){
1381 UVersionInfo minVersionArray
= {0x01, 0x00, 0x00, 0x00};
1382 UVersionInfo maxVersionArray
= {0x50, 0xff, 0xcf, 0xcf};
1383 UVersionInfo versionArray
;
1384 UErrorCode status
= U_ZERO_ERROR
;
1385 UResourceBundle
* resB
= NULL
;
1387 int locCount
= uloc_countAvailable();
1388 const char *locName
= "root";
1390 log_verbose("The ures_getVersion tests begin : \n");
1392 for(j
= -1; j
< locCount
; j
++) {
1394 locName
= uloc_getAvailable(j
);
1396 log_verbose("Testing version number for locale %s\n", locName
);
1397 resB
= ures_open(NULL
,locName
, &status
);
1398 if (U_FAILURE(status
)) {
1399 log_err_status(status
, "Resource bundle creation for locale %s failed.: %s\n", locName
, myErrorName(status
));
1403 ures_getVersion(resB
, versionArray
);
1404 for (i
=0; i
<4; ++i
) {
1405 if (versionArray
[i
] < minVersionArray
[i
] ||
1406 versionArray
[i
] > maxVersionArray
[i
])
1408 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1409 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1418 static void TestGetVersionColl(){
1419 #if !UCONFIG_NO_COLLATION
1420 UVersionInfo minVersionArray
= {0x00, 0x00, 0x00, 0x00};
1421 UVersionInfo maxVersionArray
= {0x50, 0x80, 0xcf, 0xcf};
1422 UVersionInfo versionArray
;
1423 UErrorCode status
= U_ZERO_ERROR
;
1424 UResourceBundle
* resB
= NULL
;
1425 UEnumeration
*locs
= NULL
;
1427 const char *locName
= "root";
1429 const UChar
* rules
=NULL
;
1432 /* test NUL termination of UCARules */
1433 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1434 rules
= tres_getString(resB
,-1,"UCARules",&len
, &status
);
1435 if(!rules
|| U_FAILURE(status
)) {
1436 log_data_err("Could not load UCARules for locale %s\n", locName
);
1437 status
= U_ZERO_ERROR
;
1438 } else if(u_strlen(rules
) != len
){
1439 log_err("UCARules string not nul terminated! \n");
1443 log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL
);
1444 locs
= ures_openAvailableLocales(U_ICUDATA_COLL
, &status
);
1445 if (U_FAILURE(status
)) {
1446 log_err_status(status
, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL
, myErrorName(status
));
1451 log_verbose("Testing version number for locale %s\n", locName
);
1452 resB
= ures_open(U_ICUDATA_COLL
,locName
, &status
);
1453 if (U_FAILURE(status
)) {
1454 log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL
, locName
, myErrorName(status
));
1458 ures_getVersion(resB
, versionArray
);
1459 for (i
=0; i
<4; ++i
) {
1460 if (versionArray
[i
] < minVersionArray
[i
] ||
1461 versionArray
[i
] > maxVersionArray
[i
])
1463 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1464 locName
, versionArray
[0], versionArray
[1], versionArray
[2], versionArray
[3]);
1469 locName
= uenum_next(locs
, &locLen
, &status
);
1470 if(U_FAILURE(status
)) {
1471 log_err("uenum_next(locs) error %s\n", u_errorName(status
));
1474 if(locName
== NULL
) {
1479 #endif /* !UCONFIG_NO_COLLATION */
1482 static void TestResourceBundles()
1484 UErrorCode status
= U_ZERO_ERROR
;
1485 loadTestData(&status
);
1486 if(U_FAILURE(status
)) {
1487 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status
));
1491 testTag("only_in_Root", TRUE
, FALSE
, FALSE
);
1492 testTag("in_Root_te", TRUE
, TRUE
, FALSE
);
1493 testTag("in_Root_te_te_IN", TRUE
, TRUE
, TRUE
);
1494 testTag("in_Root_te_IN", TRUE
, FALSE
, TRUE
);
1495 testTag("only_in_te", FALSE
, TRUE
, FALSE
);
1496 testTag("only_in_te_IN", FALSE
, FALSE
, TRUE
);
1497 testTag("in_te_te_IN", FALSE
, TRUE
, TRUE
);
1498 testTag("nonexistent", FALSE
, FALSE
, FALSE
);
1500 log_verbose("Passed:= %d Failed= %d \n", pass
, fail
);
1505 static void TestConstruction1()
1507 UResourceBundle
*test1
= 0, *test2
= 0,*empty
= 0;
1508 const UChar
*result1
, *result2
;
1509 UErrorCode status
= U_ZERO_ERROR
;
1510 UErrorCode err
= U_ZERO_ERROR
;
1511 const char* locale
="te_IN";
1512 const char* testdatapath
;
1516 UVersionInfo versionInfo
;
1517 char versionString
[256];
1518 char verboseOutput
[256];
1520 U_STRING_DECL(rootVal
, "ROOT", 4);
1521 U_STRING_DECL(te_inVal
, "TE_IN", 5);
1523 U_STRING_INIT(rootVal
, "ROOT", 4);
1524 U_STRING_INIT(te_inVal
, "TE_IN", 5);
1526 testdatapath
=loadTestData(&status
);
1527 if(U_FAILURE(status
))
1529 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1533 log_verbose("Testing ures_open()......\n");
1535 empty
= ures_open(testdatapath
, "testempty", &status
);
1536 if(empty
== NULL
|| U_FAILURE(status
)) {
1537 log_err("opening empty failed!\n");
1541 test1
=ures_open(testdatapath
, NULL
, &err
);
1545 log_err("construction of NULL did not succeed : %s \n", myErrorName(status
));
1548 test2
=ures_open(testdatapath
, locale
, &err
);
1551 log_err("construction of %s did not succeed : %s \n", locale
, myErrorName(status
));
1554 result1
= tres_getString(test1
, -1, "string_in_Root_te_te_IN", &len1
, &err
);
1555 result2
= tres_getString(test2
, -1, "string_in_Root_te_te_IN", &len2
, &err
);
1556 if (U_FAILURE(err
) || len1
==0 || len2
==0) {
1557 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status
));
1560 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", u_austrcpy(verboseOutput
, result1
));
1561 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput
, result2
));
1562 if(u_strcmp(result1
, rootVal
) !=0 || u_strcmp(result2
, te_inVal
) !=0 ){
1563 log_err("construction test failed. Run Verbose for more information");
1567 /* Test getVersionNumber*/
1568 log_verbose("Testing version number\n");
1569 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1
));
1571 log_verbose("Testing version \n");
1572 ures_getVersion(test1
, versionInfo
);
1573 u_versionToString(versionInfo
, versionString
);
1575 log_verbose("for getVersion : %s\n", versionString
);
1577 if(strcmp(versionString
, ures_getVersionNumber(test1
)) != 0) {
1578 log_err("Versions differ: %s vs %s\n", versionString
, ures_getVersionNumber(test1
));
1586 /*****************************************************************************/
1587 /*****************************************************************************/
1589 static UBool
testTag(const char* frag
,
1594 int32_t failNum
= fail
;
1596 /* Make array from input params */
1599 const char *NAME
[] = { "ROOT", "TE", "TE_IN" };
1601 /* Now try to load the desired items */
1602 UResourceBundle
* theBundle
= NULL
;
1605 UErrorCode expected_status
,status
= U_ZERO_ERROR
,expected_resource_status
= U_ZERO_ERROR
;
1607 UChar
* expected_string
= NULL
;
1608 const UChar
* string
= NULL
;
1611 int32_t i
,j
,row
,col
, len
;
1612 int32_t actual_bundle
;
1614 int32_t row_count
=0;
1615 int32_t column_count
=0;
1617 int32_t tag_count
= 0;
1618 const char* testdatapath
;
1619 char verboseOutput
[256];
1620 UResourceBundle
* array
=NULL
;
1621 UResourceBundle
* array2d
=NULL
;
1622 UResourceBundle
* tags
=NULL
;
1623 UResourceBundle
* arrayItem1
=NULL
;
1625 testdatapath
= loadTestData(&status
);
1626 if(U_FAILURE(status
))
1628 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
1634 is_in
[2] = in_te_IN
;
1636 strcpy(item_tag
, "tag");
1638 for (i
=0; i
<bundles_count
; ++i
)
1640 strcpy(action
,"construction for ");
1641 strcat(action
, param
[i
].name
);
1644 status
= U_ZERO_ERROR
;
1646 theBundle
= ures_open(testdatapath
, param
[i
].name
, &status
);
1647 CONFIRM_ErrorCode(status
,param
[i
].expected_constructor_status
);
1650 actual_bundle
= 0; /* ne -> default */
1652 actual_bundle
= 1; /* te_NE -> te */
1654 actual_bundle
= 2; /* te_IN_NE -> te_IN */
1658 expected_resource_status
= U_MISSING_RESOURCE_ERROR
;
1659 for (j
=e_te_IN
; j
>=e_Root
; --j
)
1661 if (is_in
[j
] && param
[i
].inherits
[j
])
1664 if(j
== actual_bundle
) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1665 expected_resource_status
= U_ZERO_ERROR
;
1667 expected_resource_status
= U_USING_DEFAULT_WARNING
;
1669 expected_resource_status
= U_USING_FALLBACK_WARNING
;
1671 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
1676 is_in
[j
]?"Yes":"No",
1678 param
[i
].inherits
[j
]?"Yes":"No",
1679 param
[actual_bundle
].name
);
1685 for (j
=param
[i
].where
; j
>=0; --j
)
1693 base
=(UChar
*)malloc(sizeof(UChar
)*(strlen(NAME
[j
]) + 1));
1694 u_uastrcpy(base
,NAME
[j
]);
1703 base
= (UChar
*) malloc(sizeof(UChar
) * 1);
1708 /*----string---------------------------------------------------------------- */
1710 strcpy(tag
,"string_");
1713 strcpy(action
,param
[i
].name
);
1714 strcat(action
, ".ures_getStringByKey(" );
1716 strcat(action
, ")");
1719 status
= U_ZERO_ERROR
;
1722 string
=tres_getString(theBundle
, -1, tag
, &len
, &status
);
1723 if(U_SUCCESS(status
)) {
1724 expected_string
=(UChar
*)malloc(sizeof(UChar
)*(u_strlen(base
) + 4));
1725 u_strcpy(expected_string
,base
);
1726 CONFIRM_INT_EQ(len
, u_strlen(expected_string
));
1728 expected_string
= (UChar
*)malloc(sizeof(UChar
)*(u_strlen(kERROR
) + 1));
1729 u_strcpy(expected_string
,kERROR
);
1732 log_verbose("%s got %d, expected %d\n", action
, status
, expected_resource_status
);
1734 CONFIRM_ErrorCode(status
, expected_resource_status
);
1735 CONFIRM_EQ(string
, expected_string
);
1739 /*--------------array------------------------------------------------- */
1741 strcpy(tag
,"array_");
1744 strcpy(action
,param
[i
].name
);
1745 strcat(action
, ".ures_getByKey(" );
1747 strcat(action
, ")");
1751 count
= kERROR_COUNT
;
1752 status
= U_ZERO_ERROR
;
1753 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1754 CONFIRM_ErrorCode(status
,expected_resource_status
);
1755 if (U_SUCCESS(status
)) {
1756 /*confirm the resource type is an array*/
1757 CONFIRM_INT_EQ(ures_getType(array
), URES_ARRAY
);
1758 /*confirm the size*/
1759 count
=ures_getSize(array
);
1760 CONFIRM_INT_GE(count
,1);
1761 for (j
=0; j
<count
; ++j
) {
1763 u_strcpy(expected_string
, base
);
1764 u_uastrcpy(element
, itoa1(j
,buf
));
1765 u_strcat(expected_string
, element
);
1766 arrayItem1
=ures_getNextResource(array
, arrayItem1
, &status
);
1767 if(U_SUCCESS(status
)){
1768 CONFIRM_EQ(tres_getString(arrayItem1
, -1, NULL
, &len
, &status
),expected_string
);
1774 CONFIRM_INT_EQ(count
,kERROR_COUNT
);
1775 CONFIRM_ErrorCode(status
, U_MISSING_RESOURCE_ERROR
);
1776 /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1780 /*--------------arrayItem------------------------------------------------- */
1782 strcpy(tag
,"array_");
1785 strcpy(action
,param
[i
].name
);
1786 strcat(action
, ".ures_getStringByIndex(");
1787 strcat(action
, tag
);
1788 strcat(action
, ")");
1791 for (j
=0; j
<10; ++j
){
1792 idx
= count
? (randi(count
* 3) - count
) : (randi(200) - 100);
1793 status
= U_ZERO_ERROR
;
1795 array
=ures_getByKey(theBundle
, tag
, array
, &status
);
1796 if(!U_FAILURE(status
)){
1798 t
=(UChar
*)ures_getStringByIndex(array
, idx
, &len
, &status
);
1799 if(!U_FAILURE(status
)){
1802 u_strcpy(expected_string
, base
);
1803 u_uastrcpy(element
, itoa1(idx
,buf
));
1804 u_strcat(expected_string
, element
);
1806 u_strcpy(expected_string
, kERROR
);
1810 expected_status
= (idx
>= 0 && idx
< count
) ? expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1811 CONFIRM_ErrorCode(status
,expected_status
);
1812 CONFIRM_EQ(string
,expected_string
);
1817 /*--------------2dArray------------------------------------------------- */
1819 strcpy(tag
,"array_2d_");
1822 strcpy(action
,param
[i
].name
);
1823 strcat(action
, ".ures_getByKey(" );
1825 strcat(action
, ")");
1829 row_count
= kERROR_COUNT
, column_count
= kERROR_COUNT
;
1830 status
= U_ZERO_ERROR
;
1831 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1833 CONFIRM_ErrorCode(status
,expected_resource_status
);
1834 if (U_SUCCESS(status
))
1836 /*confirm the resource type is an 2darray*/
1837 CONFIRM_INT_EQ(ures_getType(array2d
), URES_ARRAY
);
1838 row_count
=ures_getSize(array2d
);
1839 CONFIRM_INT_GE(row_count
,1);
1841 for(row
=0; row
<row_count
; ++row
){
1842 UResourceBundle
*tableRow
=NULL
;
1843 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1844 CONFIRM_ErrorCode(status
, expected_resource_status
);
1845 if(U_SUCCESS(status
)){
1846 /*confirm the resourcetype of each table row is an array*/
1847 CONFIRM_INT_EQ(ures_getType(tableRow
), URES_ARRAY
);
1848 column_count
=ures_getSize(tableRow
);
1849 CONFIRM_INT_GE(column_count
,1);
1851 for (col
=0; j
<column_count
; ++j
) {
1853 u_strcpy(expected_string
, base
);
1854 u_uastrcpy(element
, itoa1(row
, buf
));
1855 u_strcat(expected_string
, element
);
1856 u_uastrcpy(element
, itoa1(col
, buf
));
1857 u_strcat(expected_string
, element
);
1858 arrayItem1
=ures_getNextResource(tableRow
, arrayItem1
, &status
);
1859 if(U_SUCCESS(status
)){
1860 const UChar
*stringValue
=tres_getString(arrayItem1
, -1, NULL
, &len
, &status
);
1861 CONFIRM_EQ(stringValue
, expected_string
);
1865 ures_close(tableRow
);
1868 CONFIRM_INT_EQ(row_count
,kERROR_COUNT
);
1869 CONFIRM_INT_EQ(column_count
,kERROR_COUNT
);
1870 row_count
=column_count
=0;
1874 /*------2dArrayItem-------------------------------------------------------------- */
1876 for (j
=0; j
<10; ++j
)
1878 row
= row_count
? (randi(row_count
* 3) - row_count
) : (randi(200) - 100);
1879 col
= column_count
? (randi(column_count
* 3) - column_count
) : (randi(200) - 100);
1880 status
= U_ZERO_ERROR
;
1883 array2d
=ures_getByKey(theBundle
, tag
, array2d
, &status
);
1884 if(U_SUCCESS(status
)){
1885 UResourceBundle
*tableRow
=NULL
;
1886 tableRow
=ures_getByIndex(array2d
, row
, tableRow
, &status
);
1887 if(U_SUCCESS(status
)) {
1889 t
=(UChar
*)ures_getStringByIndex(tableRow
, col
, &len
, &status
);
1890 if(U_SUCCESS(status
)){
1894 ures_close(tableRow
);
1896 expected_status
= (row
>= 0 && row
< row_count
&& col
>= 0 && col
< column_count
) ?
1897 expected_resource_status
: U_MISSING_RESOURCE_ERROR
;
1898 CONFIRM_ErrorCode(status
,expected_status
);
1900 if (U_SUCCESS(status
)){
1902 u_strcpy(expected_string
, base
);
1903 u_uastrcpy(element
, itoa1(row
, buf
));
1904 u_strcat(expected_string
, element
);
1905 u_uastrcpy(element
, itoa1(col
, buf
));
1906 u_strcat(expected_string
, element
);
1908 u_strcpy(expected_string
,kERROR
);
1910 CONFIRM_EQ(string
,expected_string
);
1915 /*--------------taggedArray----------------------------------------------- */
1916 strcpy(tag
,"tagged_array_");
1919 strcpy(action
,param
[i
].name
);
1920 strcat(action
,".ures_getByKey(");
1921 strcat(action
, tag
);
1925 status
= U_ZERO_ERROR
;
1927 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1928 CONFIRM_ErrorCode(status
, expected_resource_status
);
1929 if (U_SUCCESS(status
)) {
1930 UResType bundleType
=ures_getType(tags
);
1931 CONFIRM_INT_EQ(bundleType
, URES_TABLE
);
1933 tag_count
=ures_getSize(tags
);
1934 CONFIRM_INT_GE((int32_t)tag_count
, (int32_t)0);
1936 for(idx
=0; idx
<tag_count
; idx
++){
1937 UResourceBundle
*tagelement
=NULL
;
1938 const char *key
=NULL
;
1940 tagelement
=ures_getByIndex(tags
, idx
, tagelement
, &status
);
1941 key
=ures_getKey(tagelement
);
1942 value
=(UChar
*)ures_getNextString(tagelement
, &len
, &key
, &status
);
1943 log_verbose("tag = %s, value = %s\n", key
, u_austrcpy(verboseOutput
, value
));
1944 if(strncmp(key
, "tag", 3) == 0 && u_strncmp(value
, base
, u_strlen(base
)) == 0){
1949 ures_close(tagelement
);
1955 /*---------taggedArrayItem----------------------------------------------*/
1957 for (idx
=-20; idx
<20; ++idx
)
1960 status
= U_ZERO_ERROR
;
1962 strcpy(item_tag
, "tag");
1963 strcat(item_tag
, itoa1(idx
,buf
));
1964 tags
=ures_getByKey(theBundle
, tag
, tags
, &status
);
1965 if(U_SUCCESS(status
)){
1966 UResourceBundle
*tagelement
=NULL
;
1968 tagelement
=ures_getByKey(tags
, item_tag
, tagelement
, &status
);
1969 if(!U_FAILURE(status
)){
1970 UResType elementType
=ures_getType(tagelement
);
1971 CONFIRM_INT_EQ(elementType
, URES_STRING
);
1972 if(strcmp(ures_getKey(tagelement
), item_tag
) == 0){
1977 t
=(UChar
*)tres_getString(tagelement
, -1, NULL
, &len
, &status
);
1978 if(!U_FAILURE(status
)){
1983 CONFIRM_ErrorCode(status
,U_MISSING_RESOURCE_ERROR
);
1986 if (status
!= U_MISSING_RESOURCE_ERROR
) {
1988 u_strcpy(expected_string
, base
);
1989 u_uastrcpy(element
, itoa1(idx
,buf
));
1990 u_strcat(expected_string
, element
);
1991 CONFIRM_EQ(string
,expected_string
);
1995 ures_close(tagelement
);
1998 CONFIRM_INT_EQ(count
, tag_count
);
2000 free(expected_string
);
2001 ures_close(theBundle
);
2004 ures_close(array2d
);
2006 ures_close(arrayItem1
);
2008 return (UBool
)(failNum
== fail
);
2011 static void record_pass()
2016 static void record_fail()
2021 static void TestPreventFallback() {
2022 UResourceBundle
* theBundle
= NULL
;
2023 const char* testdatapath
;
2024 UErrorCode status
= U_ZERO_ERROR
;
2025 int32_t unused_len
= 0;
2027 testdatapath
=loadTestData(&status
);
2028 if(U_FAILURE(status
))
2030 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2034 // In te_IN locale, fallback of string_in_te_no_te_IN_fallback is blocked
2035 // with the three empty-set (U+2205) chars.
2036 theBundle
= ures_open(testdatapath
, "te_IN_NE", &status
);
2037 if(U_FAILURE(status
))
2039 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2043 // Fallback is blocked
2044 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2045 if (status
!= U_MISSING_RESOURCE_ERROR
)
2047 log_err("Expected missing resource error for string_in_te_no_te_IN_fallback.");
2049 status
= U_ZERO_ERROR
;
2051 // This fallback should succeed
2052 ures_getStringByKeyWithFallback(theBundle
, "string_only_in_te", &unused_len
, &status
);
2053 if(U_FAILURE(status
))
2055 log_err("Expected to find string_only_in_te %s \n",myErrorName(status
));
2057 status
= U_ZERO_ERROR
;
2058 ures_close(theBundle
);
2060 // From te locale, we should be able to fetch string_in_te_no_te_IN_fallback.
2061 theBundle
= ures_open(testdatapath
, "te", &status
);
2062 if(U_FAILURE(status
))
2064 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status
));
2067 ures_getStringByKeyWithFallback(theBundle
, "string_in_te_no_te_IN_fallback", &unused_len
, &status
);
2068 if(U_FAILURE(status
))
2070 log_err("Expected to find string_in_te_no_te_IN_fallback %s \n",myErrorName(status
));
2072 status
= U_ZERO_ERROR
;
2073 ures_close(theBundle
);
2077 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2081 static void TestFallback()
2083 UErrorCode status
= U_ZERO_ERROR
;
2084 UResourceBundle
*fr_FR
= NULL
;
2085 UResourceBundle
*subResource
= NULL
;
2086 const UChar
*junk
; /* ignored */
2089 log_verbose("Opening fr_FR..");
2090 fr_FR
= ures_open(NULL
, "fr_FR", &status
);
2091 if(U_FAILURE(status
))
2093 log_err_status(status
, "Couldn't open fr_FR - %s\n", u_errorName(status
));
2097 status
= U_ZERO_ERROR
;
2100 /* clear it out.. just do some calls to get the gears turning */
2101 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2102 status
= U_ZERO_ERROR
;
2103 junk
= tres_getString(fr_FR
, -1, "LocaleString", &resultLen
, &status
);
2104 status
= U_ZERO_ERROR
;
2105 junk
= tres_getString(fr_FR
, -1, "LocaleID", &resultLen
, &status
);
2106 status
= U_ZERO_ERROR
;
2107 (void)junk
; /* Suppress set but not used warning. */
2109 /* OK first one. This should be a Default value. */
2110 subResource
= ures_getByKey(fr_FR
, "layout", NULL
, &status
);
2111 if(status
!= U_USING_DEFAULT_WARNING
)
2113 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
2114 u_errorName(status
));
2117 status
= U_ZERO_ERROR
;
2118 ures_close(subResource
);
2120 /* and this is a Fallback, to fr */
2121 junk
= tres_getString(fr_FR
, -1, "ExemplarCharacters", &resultLen
, &status
);
2122 if(status
!= U_USING_FALLBACK_WARNING
)
2124 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
2128 status
= U_ZERO_ERROR
;
2131 /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2132 /* Test Jitterbug 552 fallback mechanism of aliased data */
2134 UErrorCode err
=U_ZERO_ERROR
;
2135 UResourceBundle
* myResB
= ures_open(NULL
,"no_NO_NY",&err
);
2136 UResourceBundle
* resLocID
= ures_getByKey(myResB
, "Version", NULL
, &err
);
2137 UResourceBundle
* tResB
;
2138 UResourceBundle
* zoneResource
;
2139 const UChar
* version
= NULL
;
2140 static const UChar versionStr
[] = { 0x0032, 0x002E, 0x0031, 0x002E, 0x0034, 0x0037, 0x002E, 0x0038, 0x0032, 0x0000}; // 2.1.47.82 in nn_NO
2142 if(err
!= U_ZERO_ERROR
){
2143 log_data_err("Expected U_ZERO_ERROR when trying to test no_NO_NY aliased to nn_NO for Version err=%s\n",u_errorName(err
));
2146 version
= tres_getString(resLocID
, -1, NULL
, &resultLen
, &err
);
2147 if(u_strcmp(version
, versionStr
) != 0){
2150 u_austrcpy(x
, versionStr
);
2151 u_austrcpy(g
, version
);
2152 log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2155 zoneResource
= ures_open(U_ICUDATA_ZONE
, "no_NO_NY", &err
);
2156 tResB
= ures_getByKey(zoneResource
, "zoneStrings", NULL
, &err
);
2157 if(err
!= U_USING_FALLBACK_WARNING
){
2158 log_err("Expected U_USING_FALLBACK_ERROR when trying to test no_NO_NY aliased with nn_NO_NY for zoneStrings err=%s\n",u_errorName(err
));
2161 ures_close(zoneResource
);
2162 ures_close(resLocID
);
2168 /* static void printUChars(UChar* uchars){
2170 / for(i=0; i<u_strlen(uchars); i++){
2171 / log_err("%04X ", *(uchars+i));
2175 static void TestResourceLevelAliasing(void) {
2176 UErrorCode status
= U_ZERO_ERROR
;
2177 UResourceBundle
*aliasB
= NULL
, *tb
= NULL
;
2178 UResourceBundle
*en
= NULL
, *uk
= NULL
, *testtypes
= NULL
;
2179 const char* testdatapath
= NULL
;
2180 const UChar
*string
= NULL
, *sequence
= NULL
;
2181 /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2182 int32_t strLen
= 0, seqLen
= 0;/*, binLen = 0, binSeqLen = 0;*/
2186 testdatapath
=loadTestData(&status
);
2187 if(U_FAILURE(status
))
2189 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2193 aliasB
= ures_open(testdatapath
, "testaliases", &status
);
2195 if(U_FAILURE(status
))
2197 log_data_err("Could not load testaliases.res %s \n",myErrorName(status
));
2200 /* this should fail - circular alias */
2201 tb
= ures_getByKey(aliasB
, "aaa", tb
, &status
);
2202 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2203 log_err("Failed to detect circular alias\n");
2206 status
= U_ZERO_ERROR
;
2208 tb
= ures_getByKey(aliasB
, "aab", tb
, &status
);
2209 if(status
!= U_TOO_MANY_ALIASES_ERROR
) {
2210 log_err("Failed to detect circular alias\n");
2212 status
= U_ZERO_ERROR
;
2214 if(U_FAILURE(status
) ) {
2215 log_data_err("err loading tb resource\n");
2217 /* testing aliasing to a non existing resource */
2218 tb
= ures_getByKey(aliasB
, "nonexisting", tb
, &status
);
2219 if(status
!= U_MISSING_RESOURCE_ERROR
) {
2220 log_err("Managed to find an alias to non-existing resource\n");
2222 status
= U_ZERO_ERROR
;
2224 /* testing referencing/composed alias */
2225 uk
= ures_findResource("ja/calendar/gregorian/DateTimePatterns/2", uk
, &status
);
2226 if((uk
== NULL
) || U_FAILURE(status
)) {
2227 log_err_status(status
, "Couldn't findResource('ja/calendar/gregorian/DateTimePatterns/2') err %s\n", u_errorName(status
));
2231 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2233 tb
= ures_getByKey(aliasB
, "referencingalias", tb
, &status
);
2234 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2236 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2237 log_err("Referencing alias didn't get the right string (1)\n");
2240 string
= tres_getString(aliasB
, -1, "referencingalias", &strLen
, &status
);
2241 if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2242 log_err("Referencing alias didn't get the right string (2)\n");
2245 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2246 tb
= ures_getByKey(aliasB
, "DateTimePatterns", tb
, &status
);
2247 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2248 tb
= ures_getByIndex(tb
, 2, tb
, &status
);
2249 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2250 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2251 checkStatus(__LINE__
, U_ZERO_ERROR
, status
);
2253 if(U_FAILURE(status
)) {
2254 log_err("%s trying to get string via separate getters\n", u_errorName(status
));
2255 } else if(seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2256 log_err("Referencing alias didn't get the right string (3)\n");
2260 testtypes
= ures_open(testdatapath
, "testtypes", &status
);
2261 strcpy(buffer
, "menu/file/open");
2263 uk
= ures_findSubResource(testtypes
, s
, uk
, &status
);
2264 sequence
= tres_getString(uk
, -1, NULL
, &seqLen
, &status
);
2266 tb
= ures_getByKey(aliasB
, "simplealias", tb
, &status
);
2267 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2269 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2270 log_err("Referencing alias didn't get the right string (4)\n");
2273 /* test indexed aliasing */
2275 tb
= ures_getByKey(aliasB
, "zoneTests", tb
, &status
);
2276 tb
= ures_getByKey(tb
, "zoneAlias2", tb
, &status
);
2277 string
= tres_getString(tb
, -1, NULL
, &strLen
, &status
);
2279 en
= ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en
, &status
);
2280 sequence
= tres_getString(en
, -1, NULL
, &seqLen
, &status
);
2282 if(U_FAILURE(status
) || seqLen
!= strLen
|| u_strncmp(sequence
, string
, seqLen
) != 0) {
2283 log_err("Referencing alias didn't get the right string (5)\n");
2286 /* test getting aliased string by index */
2288 const char* keys
[] = {
2290 "KeyAlias1PacificStandardTime",
2292 "KeyAlias3LosAngeles"
2295 const char* strings
[] = {
2296 "America/Los_Angeles",
2297 "Pacific Standard Time",
2302 const UChar
* result
;
2303 int32_t uBufferLen
= 0, resultLen
= 0;
2305 const char *key
= NULL
;
2306 tb
= ures_getByKey(aliasB
, "testGetStringByKeyAliasing", tb
, &status
);
2307 if(U_FAILURE(status
)) {
2308 log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status
));
2310 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2311 result
= tres_getString(tb
, -1, keys
[i
], &resultLen
, &status
);
2312 if(U_FAILURE(status
)){
2313 log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2316 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2317 if(resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2318 log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys
[i
]);
2321 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2322 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2323 if(U_FAILURE(status
)){
2324 log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2327 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2328 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2329 log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings
[i
]);
2332 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2333 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2334 if(U_FAILURE(status
)){
2335 log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2338 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2339 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2340 log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings
[i
]);
2344 tb
= ures_getByKey(aliasB
, "testGetStringByIndexAliasing", tb
, &status
);
2345 if(U_FAILURE(status
)) {
2346 log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status
));
2348 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2349 result
= tres_getString(tb
, i
, NULL
, &resultLen
, &status
);
2350 if(U_FAILURE(status
)){
2351 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2354 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2355 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2356 log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings
[i
]);
2359 for(i
= 0; i
< UPRV_LENGTHOF(strings
); i
++) {
2360 result
= ures_getNextString(tb
, &resultLen
, &key
, &status
);
2361 if(U_FAILURE(status
)){
2362 log_err("Fetching the resource with key %s failed. Error: %s\n", keys
[i
], u_errorName(status
));
2365 uBufferLen
= u_unescape(strings
[i
], uBuffer
, 256);
2366 if(result
==NULL
|| resultLen
!= uBufferLen
|| u_strncmp(result
, uBuffer
, resultLen
) != 0) {
2367 log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings
[i
]);
2372 tb
= ures_getByKey(aliasB
, "testAliasToTree", tb
, &status
);
2373 if(U_FAILURE(status
)){
2374 log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status
));
2377 if (strcmp(ures_getKey(tb
), "collations") != 0) {
2378 log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb
));
2385 ures_close(testtypes
);
2388 static void TestDirectAccess(void) {
2389 UErrorCode status
= U_ZERO_ERROR
;
2390 UResourceBundle
*t
= NULL
, *t2
= NULL
;
2391 const char* key
= NULL
;
2395 /*const char* testdatapath=loadTestData(&status);
2396 if(U_FAILURE(status)){
2397 log_err("Could not load testdata.dat %s \n",myErrorName(status));
2401 t
= ures_findResource("/testdata/te/zoneStrings/3/2", t
, &status
);
2402 if(U_FAILURE(status
)) {
2403 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2404 status
= U_ZERO_ERROR
;
2406 key
= ures_getKey(t
);
2408 log_err("Got a strange key, expected NULL, got %s\n", key
);
2411 t
= ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t
, &status
);
2412 if(U_FAILURE(status
)) {
2413 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status
));
2414 status
= U_ZERO_ERROR
;
2416 key
= ures_getKey(t
);
2418 log_err("Got a strange key, expected NULL, got %s\n", key
);
2422 t
= ures_findResource("ja/ExemplarCharacters", t
, &status
);
2423 if(U_FAILURE(status
)) {
2424 log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2425 status
= U_ZERO_ERROR
;
2427 key
= ures_getKey(t
);
2428 if(strcmp(key
, "ExemplarCharacters")!=0) {
2429 log_err("Got a strange key, expected 'ExemplarCharacters', got %s\n", key
);
2433 t2
= ures_open(U_ICUDATA_LANG
, "sr", &status
);
2434 if(U_FAILURE(status
)) {
2435 log_err_status(status
, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status
));
2436 log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2437 "You probably have no data! Aborting this test\n");
2440 if(U_SUCCESS(status
)) {
2441 strcpy(buffer
, "Languages/hr");
2443 t
= ures_findSubResource(t2
, s
, t
, &status
);
2444 if(U_FAILURE(status
)) {
2445 log_err("Couldn't access keyed resource, error %s\n", u_errorName(status
));
2446 status
= U_ZERO_ERROR
;
2448 key
= ures_getKey(t
);
2449 if(strcmp(key
, "hr")!=0) {
2450 log_err("Got a strange key, expected 'hr', got %s\n", key
);
2455 t
= ures_findResource("root/calendar/islamic-civil/DateTime", t
, &status
);
2456 if(U_SUCCESS(status
)) {
2457 log_data_err("This resource does not exist. How did it get here?\n");
2459 status
= U_ZERO_ERROR
;
2461 /* this one will freeze */
2462 t
= ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t
, &status
);
2463 if(U_SUCCESS(status
)) {
2464 log_data_err("Second resource does not exist. How did it get here?\n");
2466 status
= U_ZERO_ERROR
;
2469 t2
= ures_open(NULL
, "he", &status
);
2470 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2471 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2472 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2473 if(U_SUCCESS(status
)) {
2474 log_err("This resource does not exist. How did it get here?\n");
2476 status
= U_ZERO_ERROR
;
2479 t2
= ures_open(NULL
, "he", &status
);
2481 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2482 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2483 t2
= ures_getByKeyWithFallback(t2
, "eras", t2
, &status
);
2484 if(U_FAILURE(status
)) {
2485 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2487 status
= U_ZERO_ERROR
;
2490 t2
= ures_open(NULL
, "root", &status
);
2491 t2
= ures_getByKeyWithFallback(t2
, "calendar", t2
, &status
);
2492 t2
= ures_getByKeyWithFallback(t2
, "islamic-civil", t2
, &status
);
2493 t2
= ures_getByKeyWithFallback(t2
, "DateTime", t2
, &status
);
2494 if(U_SUCCESS(status
)) {
2495 log_err("This resource does not exist. How did it get here?\n");
2497 status
= U_ZERO_ERROR
;
2503 static void TestTicket9804(void) {
2504 UErrorCode status
= U_ZERO_ERROR
;
2505 UResourceBundle
*t
= NULL
;
2506 t
= ures_open(NULL
, "he", &status
);
2507 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/DateTime", t
, &status
);
2508 if(U_SUCCESS(status
)) {
2509 log_err("This resource does not exist. How did it get here?\n");
2511 status
= U_ZERO_ERROR
;
2513 t
= ures_open(NULL
, "he", &status
);
2514 t
= ures_getByKeyWithFallback(t
, "calendar/islamic-civil/eras", t
, &status
);
2515 if(U_FAILURE(status
)) {
2516 log_err_status(status
, "Didn't get Eras. I know they are there!\n");
2518 const char *locale
= ures_getLocaleByType(t
, ULOC_ACTUAL_LOCALE
, &status
);
2519 if (uprv_strcmp("he", locale
) != 0) {
2520 log_err("Eras should be in the 'he' locale, but was in: %s", locale
);
2523 status
= U_ZERO_ERROR
;
2527 static void TestJB3763(void) {
2528 /* Nasty bug prevented using parent as fill-in, since it would
2529 * stomp the path information.
2531 UResourceBundle
*t
= NULL
;
2532 UErrorCode status
= U_ZERO_ERROR
;
2533 t
= ures_open(NULL
, "sr_Latn", &status
);
2534 t
= ures_getByKeyWithFallback(t
, "calendar", t
, &status
);
2535 t
= ures_getByKeyWithFallback(t
, "gregorian", t
, &status
);
2536 t
= ures_getByKeyWithFallback(t
, "AmPmMarkers", t
, &status
);
2537 if(U_FAILURE(status
)) {
2538 log_err_status(status
, "This resource should be available?\n");
2540 status
= U_ZERO_ERROR
;
2546 static void TestGetKeywordValues(void) {
2547 UEnumeration
*kwVals
;
2548 UBool foundStandard
= FALSE
;
2549 UErrorCode status
= U_ZERO_ERROR
;
2551 #if !UCONFIG_NO_COLLATION
2552 kwVals
= ures_getKeywordValues( U_ICUDATA_COLL
, "collations", &status
);
2554 log_verbose("Testing getting collation keyword values:\n");
2556 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2557 log_verbose(" %s\n", kw
);
2558 if(!strcmp(kw
,"standard")) {
2559 if(foundStandard
== FALSE
) {
2560 foundStandard
= TRUE
;
2562 log_err("'standard' was found twice in the keyword list.\n");
2566 if(foundStandard
== FALSE
) {
2567 log_err_status(status
, "'standard' was not found in the keyword list.\n");
2569 uenum_close(kwVals
);
2570 if(U_FAILURE(status
)) {
2571 log_err_status(status
, "err %s getting collation values\n", u_errorName(status
));
2573 status
= U_ZERO_ERROR
;
2575 foundStandard
= FALSE
;
2576 kwVals
= ures_getKeywordValues( "ICUDATA", "calendar", &status
);
2578 log_verbose("Testing getting calendar keyword values:\n");
2580 while((kw
=uenum_next(kwVals
, NULL
, &status
))) {
2581 log_verbose(" %s\n", kw
);
2582 if(!strcmp(kw
,"japanese")) {
2583 if(foundStandard
== FALSE
) {
2584 foundStandard
= TRUE
;
2586 log_err("'japanese' was found twice in the calendar keyword list.\n");
2590 if(foundStandard
== FALSE
) {
2591 log_err_status(status
, "'japanese' was not found in the calendar keyword list.\n");
2593 uenum_close(kwVals
);
2594 if(U_FAILURE(status
)) {
2595 log_err_status(status
, "err %s getting calendar values\n", u_errorName(status
));
2599 static void TestGetFunctionalEquivalentOf(const char *path
, const char *resName
, const char *keyword
, UBool truncate
, const char * const testCases
[]) {
2601 for(i
=0;testCases
[i
];i
+=3) {
2602 UBool expectAvail
= (testCases
[i
][0]=='t')?TRUE
:FALSE
;
2603 UBool gotAvail
= FALSE
;
2604 const char *inLocale
= testCases
[i
+1];
2605 const char *expectLocale
= testCases
[i
+2];
2606 char equivLocale
[256];
2608 UErrorCode status
= U_ZERO_ERROR
;
2609 log_verbose("%d: %c %s\texpect %s\n",i
/3, expectAvail
?'t':'f', inLocale
, expectLocale
);
2610 len
= ures_getFunctionalEquivalent(equivLocale
, 255, path
,
2611 resName
, keyword
, inLocale
,
2612 &gotAvail
, truncate
, &status
);
2613 if(U_FAILURE(status
) || (len
<= 0)) {
2614 log_err_status(status
, "FAIL: got len %d, err %s on #%d: %c\t%s\t%s\n",
2615 len
, u_errorName(status
),
2616 i
/3,expectAvail
?'t':'f', inLocale
, expectLocale
);
2618 log_verbose("got: %c %s\n", expectAvail
?'t':'f',equivLocale
);
2620 if((gotAvail
!= expectAvail
) || strcmp(equivLocale
, expectLocale
)) {
2621 log_err("FAIL: #%d: %s -> expect avail=%c, loc=%s but get %c, loc=%s\n",
2623 expectAvail
?'t':'f', expectLocale
,
2624 gotAvail
?'t':'f', equivLocale
);
2631 static void TestGetFunctionalEquivalent(void) {
2632 #if !UCONFIG_NO_COLLATION
2633 static const char * const collCases
[] = {
2634 /* avail locale equiv */
2635 /* note: in ICU 64, empty locales are shown as available for collation */
2636 "f", "sv_US_CALIFORNIA", "sv",
2637 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
2638 "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
2639 "f", "sv_CN@collation=pinyin", "sv",
2640 "t", "zh@collation=pinyin", "zh",
2641 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
2642 "t", "zh_Hans_CN@collation=pinyin", "zh",
2643 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
2644 "t", "zh_Hant_HK@collation=pinyin", "zh",
2645 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
2646 "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
2647 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
2648 "t", "zh_Hant_HK", "zh@collation=stroke",
2649 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
2650 "t", "zh_Hant_MO", "zh@collation=stroke",
2651 "f", "zh_TW_STROKE", "zh@collation=stroke",
2652 "f", "zh_TW_STROKE@collation=pinyin", "zh",
2653 "f", "sv_CN@calendar=japanese", "sv",
2654 "t", "sv@calendar=japanese", "sv",
2655 "f", "zh_TW@collation=pinyin", "zh", /* alias of zh_Hant_TW */
2656 "t", "zh_Hant_TW@collation=pinyin", "zh",
2657 "f", "zh_CN@collation=stroke", "zh@collation=stroke", /* alias of zh_Hans_CN */
2658 "t", "zh_Hans_CN@collation=stroke", "zh@collation=stroke",
2659 "t", "de@collation=phonebook", "de@collation=phonebook",
2660 "t", "hi@collation=standard", "hi",
2661 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
2662 "f", "sv_SE@collation=pinyin", "sv", /* bug 4582 tests */
2663 "f", "sv_SE_BONN@collation=pinyin", "sv",
2665 "f", "nl_NL", "root",
2666 "f", "nl_NL_EEXT", "root",
2667 "t", "nl@collation=stroke", "root",
2668 "f", "nl_NL@collation=stroke", "root",
2669 "f", "nl_NL_EEXT@collation=stroke", "root",
2670 /* Additions to test aliased locales */
2671 "f", "yue_HK", "zh@collation=stroke",
2672 "f", "yue_Hant", "zh@collation=stroke",
2673 "f", "yue_Hant_HK", "zh@collation=stroke",
2674 "f", "yue@collation=stroke", "zh@collation=stroke",
2675 "f", "yue@collation=pinyin", "zh",
2676 "f", "yue_CN", "zh",
2677 "f", "yue_Hans", "zh",
2678 "f", "yue_Hans_CN", "zh",
2679 "f", "yue_Hans@collation=pinyin", "zh",
2680 "f", "yue_Hans@collation=stroke", "zh@collation=stroke",
2681 "f", "mo", "mo", /* ? */
2682 "f", "no", "no", /* ? */
2683 "f", "ars", "ars", /* ? */
2684 "t", "wuu", "wuu", /* ? */
2685 /* Additions to test locales without resources */
2686 "f", "en_CN", "root",
2687 "f", "zh_Hant_CN", "zh@collation=stroke",
2688 "f", "zh_Hant_US", "zh@collation=stroke",
2689 "f", "zh_Hans_US", "zh",
2690 "f", "yue_TW", "zh@collation=stroke",
2691 "f", "yue_US", "zh@collation=stroke",
2696 #endif /* !UCONFIG_NO_COLLATION */
2698 static const char *calCases
[] = {
2699 /* avail locale equiv */
2700 "t", "en_US_POSIX", "en@calendar=gregorian",
2701 "f", "ja_JP_TOKYO", "ja@calendar=gregorian",
2702 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2703 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
2704 "t", "en", "en@calendar=gregorian",
2708 #if !UCONFIG_NO_COLLATION
2709 TestGetFunctionalEquivalentOf(U_ICUDATA_COLL
, "collations", "collation", TRUE
, collCases
);
2711 TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE
, calCases
);
2713 #if !UCONFIG_NO_COLLATION
2714 log_verbose("Testing error conditions:\n");
2716 char equivLocale
[256] = "???";
2718 UErrorCode status
= U_ZERO_ERROR
;
2719 UBool gotAvail
= FALSE
;
2721 len
= ures_getFunctionalEquivalent(equivLocale
, 255, U_ICUDATA_COLL
,
2722 "calendar", "calendar", "ar_EG@calendar=islamic",
2723 &gotAvail
, FALSE
, &status
);
2724 (void)len
; /* Suppress set but not used warning. */
2726 if(status
== U_MISSING_RESOURCE_ERROR
) {
2727 log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2729 log_err("ures_getFunctionalEquivalent returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2730 equivLocale
, gotAvail
?'t':'f', u_errorName(status
));
2736 static void TestXPath(void) {
2737 UErrorCode status
= U_ZERO_ERROR
;
2738 UResourceBundle
*rb
= NULL
, *alias
= NULL
;
2740 const UChar
* result
= NULL
;
2741 const UChar expResult
[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2742 /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2744 const char *testdatapath
=loadTestData(&status
);
2745 if(U_FAILURE(status
))
2747 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2751 log_verbose("Testing ures_open()......\n");
2753 rb
= ures_open(testdatapath
, "te_IN", &status
);
2754 if(U_FAILURE(status
)) {
2755 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2758 alias
= ures_getByKey(rb
, "rootAliasClient", alias
, &status
);
2759 if(U_FAILURE(status
)) {
2760 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2765 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2766 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2767 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2770 alias
= ures_getByKey(rb
, "aliasClient", alias
, &status
);
2771 if(U_FAILURE(status
)) {
2772 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2777 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2778 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2779 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2782 alias
= ures_getByKey(rb
, "nestedRootAliasClient", alias
, &status
);
2783 if(U_FAILURE(status
)) {
2784 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status
));
2789 result
= tres_getString(alias
, -1, NULL
, &len
, &status
);
2790 if(U_FAILURE(status
) || result
== NULL
|| u_strcmp(result
, expResult
)) {
2791 log_err("Couldn't get correct string value (%s)\n", myErrorName(status
));
2797 static void TestCLDRStyleAliases(void) {
2798 UErrorCode status
= U_ZERO_ERROR
;
2799 UResourceBundle
*rb
= NULL
, *alias
= NULL
, *a
=NULL
;
2802 const UChar
*result
= NULL
;
2803 UChar expected
[256];
2804 const char *expects
[7] = { "", "a41", "a12", "a03", "ar4" };
2805 const char *testdatapath
=loadTestData(&status
);
2806 if(U_FAILURE(status
)) {
2807 log_data_err("Could not load testdata.dat %s \n",myErrorName(status
));
2810 log_verbose("Testing CLDR style aliases......\n");
2812 rb
= ures_open(testdatapath
, "te_IN_REVISED", &status
);
2813 if(U_FAILURE(status
)) {
2814 log_err("Could not open te_IN (%s)\n", myErrorName(status
));
2817 alias
= ures_getByKey(rb
, "a", alias
, &status
);
2818 if(U_FAILURE(status
)) {
2819 log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status
));
2823 for(i
= 1; i
< 5 ; i
++) {
2827 /* instead of sprintf(resource, "a%i", i); */
2828 a
= ures_getByKeyWithFallback(alias
, resource
, a
, &status
);
2829 result
= tres_getString(a
, -1, NULL
, &len
, &status
);
2830 u_charsToUChars(expects
[i
], expected
, (int32_t)strlen(expects
[i
])+1);
2831 if(U_FAILURE(status
) || !result
|| u_strcmp(result
, expected
)) {
2832 log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource
, expects
[i
], result
, myErrorName(status
));
2833 status
= U_ZERO_ERROR
;
2842 static void TestFallbackCodes(void) {
2843 UErrorCode status
= U_ZERO_ERROR
;
2844 const char *testdatapath
=loadTestData(&status
);
2846 UResourceBundle
*res
= ures_open(testdatapath
, "te_IN", &status
);
2848 UResourceBundle
*r
= NULL
, *fall
= NULL
;
2850 r
= ures_getByKey(res
, "tagged_array_in_Root_te_te_IN", r
, &status
);
2852 status
= U_ZERO_ERROR
;
2853 fall
= ures_getByKeyWithFallback(r
, "tag2", fall
, &status
);
2855 if(status
!= U_ZERO_ERROR
) {
2856 log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status
));
2857 status
= U_ZERO_ERROR
;
2860 fall
= ures_getByKeyWithFallback(r
, "tag7", fall
, &status
);
2862 if(status
!= U_USING_FALLBACK_WARNING
) {
2863 log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status
));
2865 status
= U_ZERO_ERROR
;
2867 fall
= ures_getByKeyWithFallback(r
, "tag1", fall
, &status
);
2869 if(status
!= U_USING_DEFAULT_WARNING
) {
2870 log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status
));
2872 status
= U_ZERO_ERROR
;
2879 /* This test will crash if this doesn't work. Results don't need testing. */
2880 static void TestStackReuse(void) {
2881 UResourceBundle table
;
2882 UErrorCode errorCode
= U_ZERO_ERROR
;
2883 UResourceBundle
*rb
= ures_open(NULL
, "en_US", &errorCode
);
2885 if(U_FAILURE(errorCode
)) {
2886 log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode
));
2889 ures_initStackObject(&table
);
2890 ures_getByKeyWithFallback(rb
, "Types", &table
, &errorCode
);
2891 ures_getByKeyWithFallback(&table
, "collation", &table
, &errorCode
);
2896 /* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2899 * Replace most ures_getStringXYZ() with this function which wraps the
2900 * desired call and also calls the UTF-8 variant and checks that it works.
2902 extern const UChar
*
2903 tres_getString(const UResourceBundle
*resB
,
2904 int32_t idx
, const char *key
,
2906 UErrorCode
*status
) {
2912 int32_t length16
, length8
, i16
, i8
;
2915 if(length
== NULL
) {
2919 s16
= ures_getStringByIndex(resB
, idx
, length
, status
);
2920 } else if(key
!= NULL
) {
2921 s16
= ures_getStringByKey(resB
, key
, length
, status
);
2923 s16
= ures_getString(resB
, length
, status
);
2925 if(U_FAILURE(*status
)) {
2930 /* try the UTF-8 variant of ures_getStringXYZ() */
2931 for(forceCopy
= FALSE
; forceCopy
<= TRUE
; ++forceCopy
) {
2933 length8
= (int32_t)sizeof(buffer8
);
2935 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2936 } else if(key
!= NULL
) {
2937 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2939 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2941 if(*status
== U_INVALID_CHAR_FOUND
) {
2942 /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2945 if(*status
== U_BUFFER_OVERFLOW_ERROR
) {
2946 *status
= U_ZERO_ERROR
;
2947 p8
= (char *)malloc(++length8
);
2952 s8
= ures_getUTF8StringByIndex(resB
, idx
, p8
, &length8
, forceCopy
, status
);
2953 } else if(key
!= NULL
) {
2954 s8
= ures_getUTF8StringByKey(resB
, key
, p8
, &length8
, forceCopy
, status
);
2956 s8
= ures_getUTF8String(resB
, p8
, &length8
, forceCopy
, status
);
2959 if(U_FAILURE(*status
)) {
2960 /* something unexpected happened */
2967 if(forceCopy
&& s8
!= p8
) {
2968 log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
2969 resB
, (long)idx
, key
);
2972 /* verify NUL-termination */
2973 if((p8
!= buffer8
|| length8
< sizeof(buffer8
)) && s8
[length8
] != 0) {
2974 log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
2975 resB
, (long)idx
, key
);
2977 /* verify correct string */
2979 while(i16
< length16
&& i8
< length8
) {
2980 U16_NEXT(s16
, i16
, length16
, c16
);
2981 U8_NEXT(s8
, i8
, length8
, c8
);
2983 log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
2984 resB
, (long)idx
, key
, (long)c16
, (long)c8
, (long)i16
);
2987 /* verify correct length */
2988 if(i16
< length16
) {
2989 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
2990 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
2993 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
2994 resB
, (long)idx
, key
, (long)length8
, (long)length16
);
3006 * API tests for ures_getUTF8String().
3007 * Most cases are handled by tres_getString(), which leaves argument checking
3008 * to be tested here.
3009 * Since the variants share most of their implementation, we only need to test
3011 * We also need not test for checking arguments which will be checked by the
3012 * UTF-16 ures_getStringXYZ() that are called internally.
3015 TestGetUTF8String() {
3016 UResourceBundle
*res
;
3017 const char *testdatapath
;
3023 status
= U_ZERO_ERROR
;
3024 testdatapath
= loadTestData(&status
);
3025 if(U_FAILURE(status
)) {
3026 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
3030 res
= ures_open(testdatapath
, "", &status
);
3031 if(U_FAILURE(status
)) {
3032 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
));
3037 status
= U_ZERO_ERROR
;
3038 length8
= (int32_t)sizeof(buffer8
);
3039 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3040 (void)s8
; /* Suppress set but not used warning. */
3041 if(status
!= U_ZERO_ERROR
) {
3042 log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status
));
3045 /* negative capacity */
3046 status
= U_ZERO_ERROR
;
3048 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", buffer8
, &length8
, FALSE
, &status
);
3049 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3050 log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status
));
3053 /* capacity>0 but dest=NULL */
3054 status
= U_ZERO_ERROR
;
3055 length8
= (int32_t)sizeof(buffer8
);
3056 s8
= ures_getUTF8StringByKey(res
, "string_only_in_Root", NULL
, &length8
, FALSE
, &status
);
3057 if(status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
3058 log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status
));
3064 static void TestCLDRVersion(void) {
3065 UVersionInfo zeroVersion
;
3066 UVersionInfo testExpect
;
3067 UVersionInfo testCurrent
;
3068 UVersionInfo cldrVersion
;
3070 UErrorCode status
= U_ZERO_ERROR
;
3072 /* setup the constant value */
3073 u_versionFromString(zeroVersion
, "0.0.0.0");
3075 /* test CLDR value from API */
3076 ulocdata_getCLDRVersion(cldrVersion
, &status
);
3077 if(U_FAILURE(status
)) {
3078 /* the show is pretty much over at this point */
3079 log_err_status(status
, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status
));
3082 u_versionToString(cldrVersion
, tmp
);
3083 log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp
);
3087 /* setup from resource bundle */
3089 UResourceBundle
*res
;
3090 const char *testdatapath
;
3092 status
= U_ZERO_ERROR
;
3093 testdatapath
= loadTestData(&status
);
3094 if(U_FAILURE(status
)) {
3095 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status
));
3099 res
= ures_openDirect(testdatapath
, "root", &status
);
3100 if(U_FAILURE(status
)) {
3101 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3105 ures_getVersionByKey(res
, "ExpectCLDRVersionAtLeast", testExpect
, &status
);
3106 ures_getVersionByKey(res
, "CurrentCLDRVersion", testCurrent
, &status
);
3108 if(U_FAILURE(status
)) {
3109 log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status
));
3112 if(U_FAILURE(status
)) return;
3115 u_versionToString(testExpect
,tmp
);
3116 log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp
);
3117 if(memcmp(cldrVersion
, testExpect
, sizeof(UVersionInfo
)) < 0) {
3118 log_data_err("CLDR version is too old, expect at least %s.", tmp
);
3120 u_versionToString(testCurrent
,tmp
);
3121 log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp
);
3122 switch(memcmp(cldrVersion
, testCurrent
, sizeof(UVersionInfo
))) {
3123 case 0: break; /* OK- current. */
3124 case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;
3125 case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp
); break;