]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/creststn.c
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / cintltst / creststn.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/********************************************************************
4 * COPYRIGHT:
2ca993e8 5 * Copyright (c) 1997-2016, International Business Machines Corporation and
b75a7d8f
A
6 * others. All Rights Reserved.
7 ********************************************************************/
46f4442e 8/*******************************************************************************
b75a7d8f 9*
57a6839d 10* File creststn.c
b75a7d8f
A
11*
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
46f4442e 16********************************************************************************
b75a7d8f
A
17*/
18
19
20#include <time.h>
21#include "unicode/utypes.h"
22#include "cintltst.h"
374ca955 23#include "unicode/putil.h"
b75a7d8f
A
24#include "unicode/ustring.h"
25#include "unicode/ucnv.h"
0f5d89e8
A
26#include "unicode/utf8.h"
27#include "unicode/utf16.h"
b75a7d8f 28#include "string.h"
2ca993e8 29#include "cmemory.h"
b75a7d8f
A
30#include "cstring.h"
31#include "unicode/uchar.h"
374ca955 32#include "ucol_imp.h" /* for U_ICUDATA_COLL */
73c04bcf 33#include "ubrkimpl.h" /* for U_ICUDATA_BRKITR */
b75a7d8f
A
34#define RESTEST_HEAP_CHECK 0
35
36#include "unicode/uloc.h"
729e4ab9 37#include "unicode/ulocdata.h"
b75a7d8f
A
38#include "uresimp.h"
39#include "creststn.h"
40#include "unicode/ctest.h"
41#include "ucbuf.h"
729e4ab9
A
42#include "ureslocs.h"
43
b75a7d8f
A
44static int32_t pass;
45static int32_t fail;
46
47/*****************************************************************************/
48/**
49 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
50 */
51
52static uint32_t
53randul()
54{
55 uint32_t l=0;
56 int32_t i;
57 static UBool initialized = FALSE;
58 if (!initialized)
59 {
60 srand((unsigned)time(NULL));
61 initialized = TRUE;
62 }
63 /* Assume rand has at least 12 bits of precision */
64
340931cb 65 for (i=0; i<(int32_t)sizeof(l); ++i)
b75a7d8f
A
66 ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
67 return l;
68}
69
70/**
71 * Return a random double x where 0.0 <= x < 1.0.
72 */
73static double
74randd()
75{
76 return ((double)randul()) / UINT32_MAX;
77}
78
79/**
80 * Return a random integer i where 0 <= i < n.
81 */
82static int32_t randi(int32_t n)
83{
84 return (int32_t)(randd() * n);
85}
86/***************************************************************************************/
87/**
88 * Convert an integer, positive or negative, to a character string radix 10.
89 */
90static char*
91itoa1(int32_t i, char* buf)
92{
93 char *p = 0;
94 char* result = buf;
95 /* Handle negative */
96 if(i < 0) {
97 *buf++ = '-';
98 i = -i;
99 }
100
101 /* Output digits in reverse order */
102 p = buf;
103 do {
104 *p++ = (char)('0' + (i % 10));
105 i /= 10;
106 }
107 while(i);
108 *p-- = 0;
109
110 /* Reverse the string */
111 while(buf < p) {
112 char c = *buf;
113 *buf++ = *p;
114 *p-- = c;
115 }
116
117 return result;
118}
119static const int32_t kERROR_COUNT = -1234567;
120static const UChar kERROR[] = { 0x0045 /*E*/, 0x0052 /*'R'*/, 0x0052 /*'R'*/,
121 0x004F /*'O'*/, 0x0052/*'R'*/, 0x0000 /*'\0'*/};
122
123/*****************************************************************************/
124
125enum E_Where
126{
127 e_Root,
128 e_te,
129 e_te_IN,
130 e_Where_count
131};
132typedef enum E_Where E_Where;
133/*****************************************************************************/
134
340931cb
A
135#define CONFIRM_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
136 if (u_strcmp(expected,actual)==0) { \
137 record_pass(); \
138 } else { \
139 record_fail(); \
140 log_err("%s returned %s instead of %s\n", action, austrdup(actual), austrdup(expected)); \
141 } \
142} UPRV_BLOCK_MACRO_END
143#define CONFIRM_INT_EQ(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
144 if ((expected)==(actual)) { \
145 record_pass(); \
146 } else { \
147 record_fail(); \
148 log_err("%s returned %d instead of %d\n", action, actual, expected); \
149 } \
150} UPRV_BLOCK_MACRO_END
151#define CONFIRM_INT_GE(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
152 if ((actual)>=(expected)) { \
153 record_pass(); \
154 } else { \
155 record_fail(); \
156 log_err("%s returned %d instead of x >= %d\n", action, actual, expected); \
157 } \
158} UPRV_BLOCK_MACRO_END
159#define CONFIRM_INT_NE(actual,expected) UPRV_BLOCK_MACRO_BEGIN { \
160 if ((expected)!=(actual)) { \
161 record_pass(); \
162 } else { \
163 record_fail(); \
164 log_err("%s returned %d instead of x != %d\n", action, actual, expected); \
165 } \
166} UPRV_BLOCK_MACRO_END
73c04bcf
A
167/*#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)); } */
168static void
169CONFIRM_ErrorCode(UErrorCode actual,UErrorCode expected)
170{
171 if ((expected)==(actual))
172 {
173 record_pass();
174 } else {
175 record_fail();
176 /*log_err("%s returned %s instead of %s\n", action, myErrorName(actual), myErrorName(expected)); */
177 log_err("returned %s instead of %s\n", myErrorName(actual), myErrorName(expected));
178 }
179}
b75a7d8f
A
180
181
182/* Array of our test objects */
183
184static struct
185{
186 const char* name;
187 UErrorCode expected_constructor_status;
188 E_Where where;
189 UBool like[e_Where_count];
190 UBool inherits[e_Where_count];
191}
192param[] =
193{
194 /* "te" means test */
195 /* "IN" means inherits */
196 /* "NE" or "ne" means "does not exist" */
197
198 { "root", U_ZERO_ERROR, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
199 { "te", U_ZERO_ERROR, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
200 { "te_IN", U_ZERO_ERROR, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
201 { "te_NE", U_USING_FALLBACK_WARNING, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
202 { "te_IN_NE", U_USING_FALLBACK_WARNING, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
203 { "ne", U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
204};
205
2ca993e8 206static int32_t bundles_count = UPRV_LENGTHOF(param);
b75a7d8f
A
207
208
729e4ab9 209
374ca955 210/*static void printUChars(UChar*);*/
b75a7d8f 211static void TestDecodedBundle(void);
374ca955
A
212static void TestGetKeywordValues(void);
213static void TestGetFunctionalEquivalent(void);
73c04bcf
A
214static void TestCLDRStyleAliases(void);
215static void TestFallbackCodes(void);
216static void TestGetUTF8String(void);
729e4ab9 217static void TestCLDRVersion(void);
b75a7d8f
A
218
219/***************************************************************************************/
220
221/* Array of our test objects */
222
223void addNEWResourceBundleTest(TestNode** root)
224{
225 addTest(root, &TestErrorCodes, "tsutil/creststn/TestErrorCodes");
729e4ab9 226#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
b75a7d8f
A
227 addTest(root, &TestEmptyBundle, "tsutil/creststn/TestEmptyBundle");
228 addTest(root, &TestConstruction1, "tsutil/creststn/TestConstruction1");
73c04bcf 229 addTest(root, &TestResourceBundles, "tsutil/creststn/TestResourceBundles");
b75a7d8f
A
230 addTest(root, &TestNewTypes, "tsutil/creststn/TestNewTypes");
231 addTest(root, &TestEmptyTypes, "tsutil/creststn/TestEmptyTypes");
232 addTest(root, &TestBinaryCollationData, "tsutil/creststn/TestBinaryCollationData");
233 addTest(root, &TestAPI, "tsutil/creststn/TestAPI");
234 addTest(root, &TestErrorConditions, "tsutil/creststn/TestErrorConditions");
235 addTest(root, &TestDecodedBundle, "tsutil/creststn/TestDecodedBundle");
236 addTest(root, &TestResourceLevelAliasing, "tsutil/creststn/TestResourceLevelAliasing");
237 addTest(root, &TestDirectAccess, "tsutil/creststn/TestDirectAccess");
51004dcb 238 addTest(root, &TestTicket9804, "tsutil/creststn/TestTicket9804");
729e4ab9
A
239 addTest(root, &TestXPath, "tsutil/creststn/TestXPath");
240 addTest(root, &TestCLDRStyleAliases, "tsutil/creststn/TestCLDRStyleAliases");
241 addTest(root, &TestFallbackCodes, "tsutil/creststn/TestFallbackCodes");
242 addTest(root, &TestGetUTF8String, "tsutil/creststn/TestGetUTF8String");
243 addTest(root, &TestCLDRVersion, "tsutil/creststn/TestCLDRVersion");
57a6839d 244 addTest(root, &TestPreventFallback, "tsutil/creststn/TestPreventFallback");
729e4ab9
A
245#endif
246 addTest(root, &TestFallback, "tsutil/creststn/TestFallback");
247 addTest(root, &TestGetVersion, "tsutil/creststn/TestGetVersion");
248 addTest(root, &TestGetVersionColl, "tsutil/creststn/TestGetVersionColl");
249 addTest(root, &TestAliasConflict, "tsutil/creststn/TestAliasConflict");
374ca955
A
250 addTest(root, &TestGetKeywordValues, "tsutil/creststn/TestGetKeywordValues");
251 addTest(root, &TestGetFunctionalEquivalent,"tsutil/creststn/TestGetFunctionalEquivalent");
252 addTest(root, &TestJB3763, "tsutil/creststn/TestJB3763");
73c04bcf 253 addTest(root, &TestStackReuse, "tsutil/creststn/TestStackReuse");
b75a7d8f
A
254}
255
256
257/***************************************************************************************/
258static const char* norwayNames[] = {
259 "no_NO_NY",
260 "no_NO",
261 "no",
262 "nn_NO",
263 "nn",
264 "nb_NO",
265 "nb"
266};
267
268static const char* norwayLocales[] = {
269 "nn_NO",
270 "nb_NO",
271 "nb",
272 "nn_NO",
273 "nn",
274 "nb_NO",
275 "nb"
276};
277
374ca955 278static void checkStatus(int32_t line, UErrorCode expected, UErrorCode status) {
b75a7d8f 279 if(U_FAILURE(status)) {
374ca955 280 log_data_err("Resource not present, cannot test (%s:%d)\n", __FILE__, line);
b75a7d8f
A
281 }
282 if(status != expected) {
729e4ab9 283 log_err_status(status, "%s:%d: Expected error code %s, got error code %s\n", __FILE__, line, u_errorName(expected), u_errorName(status));
b75a7d8f
A
284 }
285}
286
287static void TestErrorCodes(void) {
288 UErrorCode status = U_USING_DEFAULT_WARNING;
289
290 UResourceBundle *r = NULL, *r2 = NULL;
291
374ca955 292 /* First check with ICUDATA */
b75a7d8f 293 /* first bundle should return fallback warning */
374ca955
A
294 r = ures_open(NULL, "ti_ER_ASSAB", &status);
295 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
b75a7d8f
A
296 ures_close(r);
297
729e4ab9 298 /* this bundle should return zero error, so it shouldn't change the status */
b75a7d8f 299 status = U_USING_DEFAULT_WARNING;
374ca955
A
300 r = ures_open(NULL, "ti_ER", &status);
301 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
b75a7d8f
A
302
303 /* we look up the resource which is aliased, but it lives in fallback */
729e4ab9 304
b75a7d8f 305 if(U_SUCCESS(status) && r != NULL) {
729e4ab9 306 status = U_USING_DEFAULT_WARNING;
b331163b 307 r2 = ures_getByKey(r, "ExemplarCharacters", NULL, &status); /* ExemplarCharacters lives in ti */
374ca955 308 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
729e4ab9 309 }
b75a7d8f
A
310 ures_close(r);
311
729e4ab9 312 /* this bundle should return zero error, so it shouldn't change the status */
b75a7d8f 313 status = U_USING_DEFAULT_WARNING;
729e4ab9 314 r = ures_open(U_ICUDATA_REGION, "ti", &status);
374ca955 315 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
b75a7d8f
A
316 ures_close(r);
317
318 status = U_USING_FALLBACK_WARNING;
319 r = ures_open(NULL, "nolocale", &status);
374ca955
A
320 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
321 ures_close(r);
322 ures_close(r2);
323
57a6839d 324#if !UCONFIG_NO_COLLATION
374ca955 325 /** Now, with the collation bundle **/
729e4ab9 326
374ca955
A
327 /* first bundle should return fallback warning */
328 r = ures_open(U_ICUDATA_COLL, "sr_YU_VOJVODINA", &status);
329 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
330 ures_close(r);
331
729e4ab9 332 /* this bundle should return zero error, so it shouldn't change the status */
374ca955
A
333 status = U_USING_FALLBACK_WARNING;
334 r = ures_open(U_ICUDATA_COLL, "sr", &status);
335 checkStatus(__LINE__, U_USING_FALLBACK_WARNING, status);
336
337 /* we look up the resource which is aliased */
338 if(U_SUCCESS(status) && r != NULL) {
729e4ab9 339 status = U_USING_DEFAULT_WARNING;
374ca955
A
340 r2 = ures_getByKey(r, "collations", NULL, &status);
341 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
729e4ab9 342 }
374ca955
A
343 ures_close(r);
344
729e4ab9 345 /* this bundle should return zero error, so it shouldn't change the status */
374ca955
A
346 status = U_USING_DEFAULT_WARNING;
347 r = ures_open(U_ICUDATA_COLL, "sr", &status);
348 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
349
350 /* we look up the resource which is aliased and at our level */
351 if(U_SUCCESS(status) && r != NULL) {
729e4ab9 352 status = U_USING_DEFAULT_WARNING;
374ca955
A
353 r2 = ures_getByKey(r, "collations", r2, &status);
354 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
355 }
356 ures_close(r);
357
358 status = U_USING_FALLBACK_WARNING;
359 r = ures_open(U_ICUDATA_COLL, "nolocale", &status);
360 checkStatus(__LINE__, U_USING_DEFAULT_WARNING, status);
b75a7d8f
A
361 ures_close(r);
362 ures_close(r2);
57a6839d 363#endif /* !UCONFIG_NO_COLLATION */
b75a7d8f
A
364}
365
366static void TestAliasConflict(void) {
367 UErrorCode status = U_ZERO_ERROR;
368 UResourceBundle *he = NULL;
369 UResourceBundle *iw = NULL;
370 UResourceBundle *norway = NULL;
371 const UChar *result = NULL;
372 int32_t resultLen;
373 uint32_t size = 0;
374 uint32_t i = 0;
375 const char *realName = NULL;
376
377 he = ures_open(NULL, "he", &status);
378 iw = ures_open(NULL, "iw", &status);
379 if(U_FAILURE(status)) {
729e4ab9 380 log_err_status(status, "Failed to get resource with %s\n", myErrorName(status));
b75a7d8f
A
381 }
382 ures_close(iw);
46f4442e 383 result = ures_getStringByKey(he, "ExemplarCharacters", &resultLen, &status);
b75a7d8f 384 if(U_FAILURE(status) || result == NULL) {
729e4ab9 385 log_err_status(status, "Failed to get resource ExemplarCharacters with %s\n", myErrorName(status));
b75a7d8f
A
386 }
387 ures_close(he);
388
2ca993e8 389 size = UPRV_LENGTHOF(norwayNames);
b75a7d8f
A
390 for(i = 0; i < size; i++) {
391 status = U_ZERO_ERROR;
392 norway = ures_open(NULL, norwayNames[i], &status);
393 if(U_FAILURE(status)) {
729e4ab9 394 log_err_status(status, "Failed to get resource with %s for %s\n", myErrorName(status), norwayNames[i]);
b75a7d8f
A
395 continue;
396 }
397 realName = ures_getLocale(norway, &status);
398 log_verbose("ures_getLocale(\"%s\")=%s\n", norwayNames[i], realName);
399 if(realName == NULL || strcmp(norwayLocales[i], realName) != 0) {
400 log_data_err("Wrong locale name for %s, expected %s, got %s\n", norwayNames[i], norwayLocales[i], realName);
401 }
402 ures_close(norway);
403 }
404}
405
406static void TestDecodedBundle(){
407
408 UErrorCode error = U_ZERO_ERROR;
409
410 UResourceBundle* resB;
411
412 const UChar* srcFromRes;
413 int32_t len;
414 static const UChar uSrc[] = {
415 0x0009,0x092F,0x0941,0x0928,0x0947,0x0938,0x094D,0x0915,0x094B,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x090F,
416 0x0915,0x0020,0x002E,0x0905,0x0927,0x094D,0x092F,0x092F,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x0905,0x0928,
417 0x0941,0x0938,0x093E,0x0930,0x0020,0x0031,0x0039,0x0039,0x0030,0x0020,0x0924,0x0915,0x0020,0x0915,0x0902,0x092A,
418 0x094D,0x092F,0x0942,0x091F,0x0930,0x002D,0x092A,0x094D,0x0930,0x092C,0x0902,0x0927,0x093F,0x0924,0x0020,0x0938,
419 0x0942,0x091A,0x0928,0x093E,0x092A,0x094D,0x0930,0x0923,0x093E,0x0932,0x0940,0x0020,0x002E,0x0915,0x0947,0x0020,
420 0x002E,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0915,0x0947,0x0020,0x002E,0x092B,0x0932,0x0938,
421 0x094D,0x0935,0x0930,0x0942,0x092A,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,0x0020,0x002E,0x092E,0x0947,
422 0x0902,0x0020,0x002E,0x0938,0x093E,0x0932,0x093E,0x0928,0x093E,0x0020,0x002E,0x0032,0x0032,0x0030,0x0030,0x0020,
423 0x0905,0x0930,0x092C,0x0020,0x0930,0x0941,0x092A,0x092F,0x0947,0x0020,0x092E,0x0942,0x0932,0x094D,0x092F,0x0915,
424 0x0940,0x0020,0x002E,0x0034,0x0935,0x0938,0x094D,0x0924,0x0941,0x0913,0x0902,0x0020,0x002E,0x0034,0x0915,0x093E,
425 0x0020,0x002E,0x0034,0x0909,0x0924,0x094D,0x092A,0x093E,0x0926,0x0928,0x0020,0x002E,0x0034,0x0939,0x094B,0x0917,
426 0x093E,0x002C,0x0020,0x002E,0x0033,0x091C,0x092C,0x0915,0x093F,0x0020,0x002E,0x0033,0x0915,0x0902,0x092A,0x094D,
427 0x092F,0x0942,0x091F,0x0930,0x0020,0x002E,0x0033,0x0915,0x093E,0x0020,0x002E,0x0033,0x0915,0x0941,0x0932,0x0020,
428 0x002E,0x0033,0x092F,0x094B,0x0917,0x0926,0x093E,0x0928,0x0020,0x002E,0x0033,0x0907,0x0938,0x0938,0x0947,0x0915,
429 0x0939,0x093F,0x0020,0x002E,0x002F,0x091C,0x094D,0x092F,0x093E,0x0926,0x093E,0x0020,0x002E,0x002F,0x0939,0x094B,
430 0x0917,0x093E,0x0964,0x0020,0x002E,0x002F,0x0905,0x0928,0x0941,0x0938,0x0902,0x0927,0x093E,0x0928,0x0020,0x002E,
431 0x002F,0x0915,0x0940,0x0020,0x002E,0x002F,0x091A,0x0930,0x092E,0x0020,0x0938,0x0940,0x092E,0x093E,0x0913,0x0902,
432 0x0020,0x092A,0x0930,0x0020,0x092A,0x0939,0x0941,0x0902,0x091A,0x0928,0x0947,0x0020,0x0915,0x0947,0x0020,0x0932,
433 0x093F,0x090F,0x0020,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x090F,0x0915,0x0020,0x002E,0x002F,
434 0x0906,0x092E,0x0020,0x002E,0x002F,0x091C,0x0930,0x0942,0x0930,0x0924,0x0020,0x002E,0x002F,0x091C,0x0948,0x0938,
435 0x093E,0x0020,0x092C,0x0928,0x0020,0x0917,0x092F,0x093E,0x0020,0x0939,0x0948,0x0964,0x0020,0x092D,0x093E,0x0930,
436 0x0924,0x0020,0x092E,0x0947,0x0902,0x0020,0x092D,0x0940,0x002C,0x0020,0x0916,0x093E,0x0938,0x0915,0x0930,0x0020,
437 0x092E,0x094C,0x091C,0x0942,0x0926,0x093E,0x0020,0x0938,0x0930,0x0915,0x093E,0x0930,0x0928,0x0947,0x002C,0x0020,
438 0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,0x0915,0x0947,0x0020,0x092A,0x094D,0x0930,0x092F,
439 0x094B,0x0917,0x0020,0x092A,0x0930,0x0020,0x091C,0x092C,0x0930,0x0926,0x0938,0x094D,0x0924,0x0020,0x090F,0x095C,
440 0x0020,0x0932,0x0917,0x093E,0x092F,0x0940,0x0020,0x0939,0x0948,0x002C,0x0020,0x0915,0x093F,0x0902,0x0924,0x0941,
441 0x0020,0x0907,0x0938,0x0915,0x0947,0x0020,0x0938,0x0930,0x092A,0x091F,0x0020,0x0926,0x094C,0x095C,0x0932,0x0917,
442 0x093E,0x0928,0x0947,0x0020,0x002E,0x0032,0x0915,0x0947,0x0020,0x002E,0x0032,0x0932,0x093F,0x090F,0x0020,0x002E,
443 0x0032,0x0915,0x094D,0x092F,0x093E,0x0020,0x002E,0x0032,0x0938,0x092A,0x093E,0x091F,0x0020,0x002E,0x0032,0x0930,
444 0x093E,0x0938,0x094D,0x0924,0x093E,0x0020,0x002E,0x0032,0x0909,0x092A,0x0932,0x092C,0x094D,0x0927,0x0020,0x002E,
445 0x0939,0x0948,0x002C,0x0020,0x002E,0x0905,0x0925,0x0935,0x093E,0x0020,0x002E,0x0935,0x093F,0x0936,0x094D,0x0935,
446 0x0020,0x002E,0x092E,0x0947,0x0902,0x0020,0x002E,0x0915,0x0902,0x092A,0x094D,0x092F,0x0942,0x091F,0x0930,0x0020,
447 0x002E,0x0915,0x0940,0x0938,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0935,0x0020,0x002E,0x0033,0x0935,
448 0x093F,0x092B,0x0932,0x0924,0x093E,0x0020,0x002E,0x0033,0x0938,0x0947,0x0020,0x002E,0x0033,0x0938,0x092C,0x0915,
449 0x0020,0x002E,0x0033,0x0932,0x0947,0x0020,0x002E,0x0033,0x0915,0x0930,0x0020,0x002E,0x0033,0x0915,0x094D,0x092F,
450 0x093E,0x0020,0x002E,0x0033,0x0939,0x092E,0x0020,0x002E,0x0033,0x0907,0x0938,0x0915,0x093E,0x0020,0x002E,0x0033,
451 0x092F,0x0941,0x0915,0x094D,0x0924,0x093F,0x092A,0x0942,0x0930,0x094D,0x0923,0x0020,0x002E,0x0032,0x0935,0x093F,
452 0x0938,0x094D,0x0924,0x093E,0x0930,0x0020,0x0905,0x092A,0x0947,0x0915,0x094D,0x0937,0x093F,0x0924,0x0020,0x0915,
453 0x0930,0x0020,0x0938,0x0915,0x0947,0x0902,0x0917,0x0947,0x0020,0x003F,0x0020,
454 0
455 };
456
457 /* pre-flight */
458 int32_t num =0;
459 const char *testdatapath = loadTestData(&error);
b331163b 460 resB = ures_open(testdatapath, "encoded", &error);
73c04bcf 461 srcFromRes=tres_getString(resB,-1,"str",&len,&error);
b75a7d8f 462 if(U_FAILURE(error)){
b331163b 463 log_data_err("Could not find encoded.res from test data bundle. Error: %s\n", u_errorName(error));
b75a7d8f
A
464 ures_close(resB);
465 return;
466 }
467 if(u_strncmp(srcFromRes,uSrc,len)!=0){
468 log_err("Genrb produced res files after decoding failed\n");
469 }
470 while(num<len){
471 if(uSrc[num]!=srcFromRes[num]){
472 log_verbose(" Expected: 0x%04X Got: 0x%04X \n", uSrc[num],srcFromRes[num]);
473 }
474 num++;
475 }
476 if (len != u_strlen(uSrc)) {
477 log_err("Genrb produced a string larger than expected\n");
478 }
479 ures_close(resB);
480}
481
482static void TestNewTypes() {
483 UResourceBundle* theBundle = NULL;
484 char action[256];
485 const char* testdatapath;
486 UErrorCode status = U_ZERO_ERROR;
487 UResourceBundle* res = NULL;
488 uint8_t *binResult = NULL;
489 int32_t len = 0;
490 int32_t i = 0;
491 int32_t intResult = 0;
492 uint32_t uintResult = 0;
493 const UChar *empty = NULL;
494 const UChar *zeroString;
495 UChar expected[] = { 'a','b','c','\0','d','e','f' };
496 const char* expect ="tab:\t cr:\r ff:\f newline:\n backslash:\\\\ quote=\\\' doubleQuote=\\\" singlequoutes=''";
497 UChar uExpect[200];
498
499 testdatapath=loadTestData(&status);
500
501 if(U_FAILURE(status))
502 {
46f4442e 503 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
504 return;
505 }
506
507 theBundle = ures_open(testdatapath, "testtypes", &status);
508
73c04bcf 509 empty = tres_getString(theBundle, -1, "emptystring", &len, &status);
b75a7d8f
A
510 if(empty && (*empty != 0 || len != 0)) {
511 log_err("Empty string returned invalid value\n");
512 }
513
514 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
515
516 CONFIRM_INT_NE(theBundle, NULL);
517
518 /* This test reads the string "abc\u0000def" from the bundle */
519 /* if everything is working correctly, the size of this string */
520 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
521
522 strcpy(action, "getting and testing of string with embeded zero");
523 res = ures_getByKey(theBundle, "zerotest", res, &status);
524 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
525 CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
73c04bcf 526 zeroString=tres_getString(res, -1, NULL, &len, &status);
b75a7d8f
A
527 if(U_SUCCESS(status)){
528 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
529 CONFIRM_INT_EQ(len, 7);
530 CONFIRM_INT_NE(len, 3);
531 }
532 for(i=0;i<len;i++){
533 if(zeroString[i]!= expected[i]){
534 log_verbose("Output did not match Expected: \\u%4X Got: \\u%4X", expected[i], zeroString[i]);
535 }
536 }
537
538 strcpy(action, "getting and testing of binary type");
539 res = ures_getByKey(theBundle, "binarytest", res, &status);
540 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
541 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
542 binResult=(uint8_t*)ures_getBinary(res, &len, &status);
543 if(U_SUCCESS(status)){
544 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
545 CONFIRM_INT_EQ(len, 15);
546 for(i = 0; i<15; i++) {
547 CONFIRM_INT_EQ(binResult[i], i);
548 }
549 }
550
551 strcpy(action, "getting and testing of imported binary type");
552 res = ures_getByKey(theBundle, "importtest", res, &status);
553 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
554 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
555 binResult=(uint8_t*)ures_getBinary(res, &len, &status);
556 if(U_SUCCESS(status)){
557 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
558 CONFIRM_INT_EQ(len, 15);
559 for(i = 0; i<15; i++) {
560 CONFIRM_INT_EQ(binResult[i], i);
561 }
562 }
563
564 strcpy(action, "getting and testing of integer types");
565 res = ures_getByKey(theBundle, "one", res, &status);
566 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
567 CONFIRM_INT_EQ(ures_getType(res), URES_INT);
568 intResult=ures_getInt(res, &status);
569 uintResult = ures_getUInt(res, &status);
570 if(U_SUCCESS(status)){
571 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
572 CONFIRM_INT_EQ(uintResult, (uint32_t)intResult);
573 CONFIRM_INT_EQ(intResult, 1);
574 }
575
576 strcpy(action, "getting minusone");
577 res = ures_getByKey(theBundle, "minusone", res, &status);
578 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
579 CONFIRM_INT_EQ(ures_getType(res), URES_INT);
580 intResult=ures_getInt(res, &status);
581 uintResult = ures_getUInt(res, &status);
582 if(U_SUCCESS(status)){
583 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
584 CONFIRM_INT_EQ(uintResult, 0x0FFFFFFF); /* a 28 bit integer */
585 CONFIRM_INT_EQ(intResult, -1);
586 CONFIRM_INT_NE(uintResult, (uint32_t)intResult);
587 }
588
589 strcpy(action, "getting plusone");
590 res = ures_getByKey(theBundle, "plusone", res, &status);
591 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
592 CONFIRM_INT_EQ(ures_getType(res), URES_INT);
593 intResult=ures_getInt(res, &status);
594 uintResult = ures_getUInt(res, &status);
595 if(U_SUCCESS(status)){
596 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
597 CONFIRM_INT_EQ(uintResult, (uint32_t)intResult);
598 CONFIRM_INT_EQ(intResult, 1);
599 }
600
601 res = ures_getByKey(theBundle, "onehundredtwentythree", res, &status);
602 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
603 CONFIRM_INT_EQ(ures_getType(res), URES_INT);
604 intResult=ures_getInt(res, &status);
605 if(U_SUCCESS(status)){
606 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
607 CONFIRM_INT_EQ(intResult, 123);
608 }
609
610 /* this tests if escapes are preserved or not */
611 {
73c04bcf 612 const UChar* str = tres_getString(theBundle,-1,"testescape",&len,&status);
b75a7d8f
A
613 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
614 if(U_SUCCESS(status)){
374ca955 615 u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1);
b75a7d8f
A
616 if(u_strcmp(uExpect,str)){
617 log_err("Did not get the expected string for testescape\n");
618 }
619 }
620 }
621 /* this tests if unescaping works are expected */
622 len=0;
623 {
374ca955
A
624 char pattern[2048] = "";
625 int32_t patternLen;
626 UChar* expectedEscaped;
627 const UChar* got;
628 int32_t expectedLen;
629
630 /* This strcpy fixes compiler warnings about long strings */
631 strcpy(pattern, "[ \\\\u0020 \\\\u00A0 \\\\u1680 \\\\u2000 \\\\u2001 \\\\u2002 \\\\u2003 \\\\u2004 \\\\u2005 \\\\u2006 \\\\u2007 "
b75a7d8f
A
632 "\\\\u2008 \\\\u2009 \\\\u200A \\u200B \\\\u202F \\u205F \\\\u3000 \\u0000-\\u001F \\u007F \\u0080-\\u009F "
633 "\\\\u06DD \\\\u070F \\\\u180E \\\\u200C \\\\u200D \\\\u2028 \\\\u2029 \\\\u2060 \\\\u2061 \\\\u2062 \\\\u2063 "
634 "\\\\u206A-\\\\u206F \\\\uFEFF \\\\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD "
635 "\\U00100000-\\U0010FFFD \\uFDD0-\\uFDEF \\uFFFE-\\uFFFF \\U0001FFFE-\\U0001FFFF \\U0002FFFE-\\U0002FFFF "
374ca955
A
636 );
637 strcat(pattern,
b75a7d8f
A
638 "\\U0003FFFE-\\U0003FFFF \\U0004FFFE-\\U0004FFFF \\U0005FFFE-\\U0005FFFF \\U0006FFFE-\\U0006FFFF "
639 "\\U0007FFFE-\\U0007FFFF \\U0008FFFE-\\U0008FFFF \\U0009FFFE-\\U0009FFFF \\U000AFFFE-\\U000AFFFF "
640 "\\U000BFFFE-\\U000BFFFF \\U000CFFFE-\\U000CFFFF \\U000DFFFE-\\U000DFFFF \\U000EFFFE-\\U000EFFFF "
641 "\\U000FFFFE-\\U000FFFFF \\U0010FFFE-\\U0010FFFF \\uD800-\\uDFFF \\\\uFFF9 \\\\uFFFA \\\\uFFFB "
642 "\\uFFFC \\uFFFD \\u2FF0-\\u2FFB \\u0340 \\u0341 \\\\u200E \\\\u200F \\\\u202A \\\\u202B \\\\u202C "
374ca955
A
643 );
644 strcat(pattern,
b75a7d8f 645 "\\\\u202D \\\\u202E \\\\u206A \\\\u206B \\\\u206C \\\\u206D \\\\u206E \\\\u206F \\U000E0001 \\U000E0020-\\U000E007F "
374ca955
A
646 "]"
647 );
648
649 patternLen = (int32_t)uprv_strlen(pattern);
650 expectedEscaped = (UChar*)malloc(U_SIZEOF_UCHAR * patternLen);
73c04bcf 651 got = tres_getString(theBundle,-1,"test_unescaping",&len,&status);
374ca955
A
652 expectedLen = u_unescape(pattern,expectedEscaped,patternLen);
653 if(got==NULL || u_strncmp(expectedEscaped,got,expectedLen)!=0 || expectedLen != len){
b75a7d8f
A
654 log_err("genrb failed to unescape string\n");
655 }
374ca955
A
656 if(got != NULL){
657 for(i=0;i<expectedLen;i++){
658 if(expectedEscaped[i] != got[i]){
659 log_verbose("Expected: 0x%04X Got: 0x%04X \n",expectedEscaped[i], got[i]);
660 }
b75a7d8f
A
661 }
662 }
663 free(expectedEscaped);
664 status = U_ZERO_ERROR;
665 }
666 /* test for jitterbug#1435 */
667 {
73c04bcf 668 const UChar* str = tres_getString(theBundle,-1,"test_underscores",&len,&status);
b75a7d8f 669 expect ="test message ....";
374ca955 670 u_charsToUChars(expect,uExpect,(int32_t)strlen(expect)+1);
b75a7d8f 671 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
46f4442e 672 if(str == NULL || u_strcmp(uExpect,str)){
b75a7d8f
A
673 log_err("Did not get the expected string for test_underscores.\n");
674 }
675 }
676 /* test for jitterbug#2626 */
73c04bcf 677#if !UCONFIG_NO_COLLATION
b75a7d8f
A
678 {
679 UResourceBundle* resB = NULL;
680 const UChar* str = NULL;
681 int32_t strLength = 0;
682 const UChar my[] = {0x0026,0x0027,0x0075,0x0027,0x0020,0x003d,0x0020,0x0027,0xff55,0x0027,0x0000}; /* &'\u0075' = '\uFF55' */
683 status = U_ZERO_ERROR;
374ca955
A
684 resB = ures_getByKey(theBundle, "collations", resB, &status);
685 resB = ures_getByKey(resB, "standard", resB, &status);
73c04bcf 686 str = tres_getString(resB,-1,"Sequence",&strLength,&status);
b75a7d8f 687 if(!str || U_FAILURE(status)) {
374ca955 688 log_data_err("Could not load collations from theBundle: %s\n", u_errorName(status));
b75a7d8f
A
689 } else if(u_strcmp(my,str) != 0){
690 log_err("Did not get the expected string for escaped \\u0075\n");
691 }
692 ures_close(resB);
693 }
73c04bcf 694#endif
b75a7d8f
A
695 {
696 const char *sourcePath = ctest_dataSrcDir();
374ca955 697 int32_t srcPathLen = (int32_t)strlen(sourcePath);
b75a7d8f 698 const char *deltaPath = ".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
374ca955 699 int32_t deltaPathLen = (int32_t)strlen(deltaPath);
b75a7d8f
A
700 char *testDataFileName = (char *) malloc( srcPathLen+ deltaPathLen + 50 );
701 char *path = testDataFileName;
702
703 strcpy(path, sourcePath);
704 path += srcPathLen;
705 strcpy(path, deltaPath);
706 path += deltaPathLen;
707 status = U_ZERO_ERROR;
708 {
709 int32_t strLen =0;
73c04bcf 710 const UChar* str = tres_getString(theBundle, -1, "testincludeUTF",&strLen,&status);
374ca955
A
711 strcpy(path, "riwords.txt");
712 path[strlen("riwords.txt")]=0;
b75a7d8f
A
713 if(U_FAILURE(status)){
714 log_err("Could not get testincludeUTF resource from testtypes bundle. Error: %s\n",u_errorName(status));
715 }else{
716 /* open the file */
717 const char* cp = NULL;
718 UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status);
719 len = 0;
720 if(U_SUCCESS(status)){
721 const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status);
722 if(U_SUCCESS(status)){
723 /* verify the contents */
724 if(strLen != len ){
374ca955
A
725 log_err("Did not get the expected len for riwords. Expected: %i , Got: %i\n", len ,strLen);
726 }
727 /* test string termination */
728 if(u_strlen(str) != strLen || str[strLen]!= 0 ){
729 log_err("testinclude not null terminated!\n");
b75a7d8f
A
730 }
731 if(u_strncmp(str, buffer,strLen)!=0){
374ca955 732 log_err("Did not get the expected string from riwords. Include functionality failed for genrb.\n");
b75a7d8f
A
733 }
734 }else{
735 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status));
736 }
737
738 ucbuf_close(ucbuf);
739 }else{
374ca955 740 log_err("Could not get riwords.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status));
b75a7d8f
A
741 }
742 }
743 }
744 status = U_ZERO_ERROR;
745 {
746 int32_t strLen =0;
73c04bcf 747 const UChar* str = tres_getString(theBundle, -1, "testinclude",&strLen,&status);
b75a7d8f
A
748 strcpy(path, "translit_rules.txt");
749 path[strlen("translit_rules.txt")]=0;
750
751 if(U_FAILURE(status)){
752 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status));
753 }else{
754 /* open the file */
755 const char* cp=NULL;
756 UCHARBUF* ucbuf = ucbuf_open(testDataFileName,&cp,FALSE,FALSE,&status);
757 len = 0;
758 if(U_SUCCESS(status)){
759 const UChar* buffer = ucbuf_getBuffer(ucbuf,&len,&status);
760 if(U_SUCCESS(status)){
761 /* verify the contents */
762 if(strLen != len ){
763 log_err("Did not get the expected len for translit_rules. Expected: %i , Got: %i\n", len ,strLen);
764 }
765 if(u_strncmp(str, buffer,strLen)!=0){
766 log_err("Did not get the expected string from translit_rules. Include functionality failed for genrb.\n");
767 }
768 }else{
769 log_err("ucbuf failed to open %s. Error: %s\n", testDataFileName, u_errorName(status));
770 }
771 ucbuf_close(ucbuf);
772 }else{
773 log_err("Could not get translit_rules.txt (path : %s). Error: %s\n",testDataFileName,u_errorName(status));
774 }
775 }
776 }
777 free(testDataFileName);
778 }
779 ures_close(res);
780 ures_close(theBundle);
781
782}
783
784static void TestEmptyTypes() {
785 UResourceBundle* theBundle = NULL;
786 char action[256];
787 const char* testdatapath;
788 UErrorCode status = U_ZERO_ERROR;
789 UResourceBundle* res = NULL;
790 UResourceBundle* resArray = NULL;
791 const uint8_t *binResult = NULL;
792 int32_t len = 0;
793 int32_t intResult = 0;
794 const UChar *zeroString;
57a6839d 795 const int32_t *zeroIntVect = NULL;
b75a7d8f
A
796
797 strcpy(action, "Construction of testtypes bundle");
798 testdatapath=loadTestData(&status);
799 if(U_FAILURE(status))
800 {
46f4442e 801 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
802 return;
803 }
804
805 theBundle = ures_open(testdatapath, "testtypes", &status);
806
807 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
808
809 CONFIRM_INT_NE(theBundle, NULL);
810
811 /* This test reads the string "abc\u0000def" from the bundle */
812 /* if everything is working correctly, the size of this string */
813 /* should be 7. Everything else is a wrong answer, esp. 3 and 6*/
814
815 status = U_ZERO_ERROR;
816 strcpy(action, "getting and testing of explicit string of zero length string");
817 res = ures_getByKey(theBundle, "emptyexplicitstring", res, &status);
818 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
819 CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
73c04bcf 820 zeroString=tres_getString(res, -1, NULL, &len, &status);
b75a7d8f
A
821 if(U_SUCCESS(status)){
822 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
823 CONFIRM_INT_EQ(len, 0);
824 CONFIRM_INT_EQ(u_strlen(zeroString), 0);
825 }
826 else {
827 log_err("Couldn't get emptyexplicitstring\n");
828 }
829
830 status = U_ZERO_ERROR;
831 strcpy(action, "getting and testing of normal string of zero length string");
832 res = ures_getByKey(theBundle, "emptystring", res, &status);
833 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
834 CONFIRM_INT_EQ(ures_getType(res), URES_STRING);
73c04bcf 835 zeroString=tres_getString(res, -1, NULL, &len, &status);
b75a7d8f
A
836 if(U_SUCCESS(status)){
837 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
838 CONFIRM_INT_EQ(len, 0);
839 CONFIRM_INT_EQ(u_strlen(zeroString), 0);
840 }
841 else {
842 log_err("Couldn't get emptystring\n");
843 }
844
845 status = U_ZERO_ERROR;
846 strcpy(action, "getting and testing of empty int");
847 res = ures_getByKey(theBundle, "emptyint", res, &status);
848 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
849 CONFIRM_INT_EQ(ures_getType(res), URES_INT);
850 intResult=ures_getInt(res, &status);
851 if(U_SUCCESS(status)){
852 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
853 CONFIRM_INT_EQ(intResult, 0);
854 }
855 else {
856 log_err("Couldn't get emptystring\n");
857 }
858
859 status = U_ZERO_ERROR;
860 strcpy(action, "getting and testing of zero length intvector");
861 res = ures_getByKey(theBundle, "emptyintv", res, &status);
862 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
863 CONFIRM_INT_EQ(ures_getType(res), URES_INT_VECTOR);
864
865 if(U_FAILURE(status)){
866 log_err("Couldn't get emptyintv key %s\n", u_errorName(status));
867 }
868 else {
869 zeroIntVect=ures_getIntVector(res, &len, &status);
57a6839d 870 (void)zeroIntVect; /* Suppress set but not used warning. */
b75a7d8f
A
871 if(!U_SUCCESS(status) || resArray != NULL || len != 0) {
872 log_err("Shouldn't get emptyintv\n");
873 }
874 }
875
876 status = U_ZERO_ERROR;
877 strcpy(action, "getting and testing of zero length emptybin");
878 res = ures_getByKey(theBundle, "emptybin", res, &status);
879 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
880 CONFIRM_INT_EQ(ures_getType(res), URES_BINARY);
881
882 if(U_FAILURE(status)){
883 log_err("Couldn't get emptybin key %s\n", u_errorName(status));
884 }
885 else {
886 binResult=ures_getBinary(res, &len, &status);
57a6839d 887 (void)binResult; /* Suppress set but not used warning. */
729e4ab9
A
888 if(!U_SUCCESS(status) || len != 0) {
889 log_err("Couldn't get emptybin, or it's not empty\n");
b75a7d8f
A
890 }
891 }
892
893 status = U_ZERO_ERROR;
894 strcpy(action, "getting and testing of zero length emptyarray");
895 res = ures_getByKey(theBundle, "emptyarray", res, &status);
896 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
897 CONFIRM_INT_EQ(ures_getType(res), URES_ARRAY);
898
899 if(U_FAILURE(status)){
900 log_err("Couldn't get emptyarray key %s\n", u_errorName(status));
901 }
902 else {
903 resArray=ures_getByIndex(res, 0, resArray, &status);
904 if(U_SUCCESS(status) || resArray != NULL){
729e4ab9 905 log_err("Shouldn't get emptyarray[0]\n");
b75a7d8f
A
906 }
907 }
908
909 status = U_ZERO_ERROR;
910 strcpy(action, "getting and testing of zero length emptytable");
911 res = ures_getByKey(theBundle, "emptytable", res, &status);
912 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
913 CONFIRM_INT_EQ(ures_getType(res), URES_TABLE);
914
915 if(U_FAILURE(status)){
916 log_err("Couldn't get emptytable key %s\n", u_errorName(status));
917 }
918 else {
919 resArray=ures_getByIndex(res, 0, resArray, &status);
920 if(U_SUCCESS(status) || resArray != NULL){
729e4ab9 921 log_err("Shouldn't get emptytable[0]\n");
b75a7d8f
A
922 }
923 }
924
925 ures_close(res);
926 ures_close(theBundle);
927}
928
929static void TestEmptyBundle(){
930 UErrorCode status = U_ZERO_ERROR;
931 const char* testdatapath=NULL;
932 UResourceBundle *resb=0, *dResB=0;
933
934 testdatapath=loadTestData(&status);
935 if(U_FAILURE(status))
936 {
46f4442e 937 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
938 return;
939 }
940 resb = ures_open(testdatapath, "testempty", &status);
941
942 if(U_SUCCESS(status)){
943 dResB = ures_getByKey(resb,"test",dResB,&status);
944 if(status!= U_MISSING_RESOURCE_ERROR){
945 log_err("Did not get the expected error from an empty resource bundle. Expected : %s Got: %s\n",
946 u_errorName(U_MISSING_RESOURCE_ERROR),u_errorName(status));
947 }
948 }
949 ures_close(dResB);
950 ures_close(resb);
951}
952
953static void TestBinaryCollationData(){
57a6839d 954#if !UCONFIG_NO_COLLATION
b75a7d8f
A
955 UErrorCode status=U_ZERO_ERROR;
956 const char* locale="te";
957 const char* testdatapath;
958 UResourceBundle *teRes = NULL;
959 UResourceBundle *coll=NULL;
960 UResourceBundle *binColl = NULL;
961 uint8_t *binResult = NULL;
962 int32_t len=0;
963 const char* action="testing the binary collaton data";
964
b75a7d8f
A
965 log_verbose("Testing binary collation data resource......\n");
966
967 testdatapath=loadTestData(&status);
968 if(U_FAILURE(status))
969 {
46f4442e 970 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
971 return;
972 }
973
974
975 teRes=ures_open(testdatapath, locale, &status);
976 if(U_FAILURE(status)){
977 log_err("ERROR: Failed to get resource for \"te\" with %s", myErrorName(status));
978 return;
979 }
980 status=U_ZERO_ERROR;
374ca955
A
981 coll = ures_getByKey(teRes, "collations", coll, &status);
982 coll = ures_getByKey(coll, "standard", coll, &status);
b75a7d8f
A
983 if(U_SUCCESS(status)){
984 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
985 CONFIRM_INT_EQ(ures_getType(coll), URES_TABLE);
986 binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status);
987 if(U_SUCCESS(status)){
988 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
989 CONFIRM_INT_EQ(ures_getType(binColl), URES_BINARY);
990 binResult=(uint8_t*)ures_getBinary(binColl, &len, &status);
57a6839d 991 (void)binResult; /* Suppress set but not used warning. */
b75a7d8f
A
992 if(U_SUCCESS(status)){
993 CONFIRM_ErrorCode(status, U_ZERO_ERROR);
994 CONFIRM_INT_GE(len, 1);
995 }
996
997 }else{
998 log_err("ERROR: ures_getByKey(locale(te), %%CollationBin) failed\n");
999 }
1000 }
1001 else{
374ca955 1002 log_err("ERROR: ures_getByKey(locale(te), collations) failed\n");
b75a7d8f
A
1003 return;
1004 }
1005 ures_close(binColl);
1006 ures_close(coll);
1007 ures_close(teRes);
1008#endif
1009}
1010
1011static void TestAPI() {
1012 UErrorCode status=U_ZERO_ERROR;
1013 int32_t len=0;
1014 const char* key=NULL;
1015 const UChar* value=NULL;
1016 const char* testdatapath;
1017 UChar* utestdatapath=NULL;
1018 char convOutput[256];
73c04bcf 1019 UChar largeBuffer[1025];
b75a7d8f
A
1020 UResourceBundle *teRes = NULL;
1021 UResourceBundle *teFillin=NULL;
1022 UResourceBundle *teFillin2=NULL;
1023
1024 log_verbose("Testing ures_openU()......\n");
1025
1026 testdatapath=loadTestData(&status);
1027 if(U_FAILURE(status))
1028 {
46f4442e 1029 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
1030 return;
1031 }
374ca955 1032 len =(int32_t)strlen(testdatapath);
b75a7d8f
A
1033 utestdatapath = (UChar*) malloc((len+10)*sizeof(UChar));
1034
374ca955 1035 u_charsToUChars(testdatapath, utestdatapath, (int32_t)strlen(testdatapath)+1);
73c04bcf
A
1036#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) && U_FILE_SEP_CHAR == '\\'
1037 {
1038 /* Convert all backslashes to forward slashes so that we can make sure that ures_openU
1039 can handle invariant characters. */
1040 UChar *backslash;
1041 while ((backslash = u_strchr(utestdatapath, 0x005C))) {
1042 *backslash = 0x002F;
1043 }
1044 }
1045#endif
1046
2ca993e8
A
1047 u_memset(largeBuffer, 0x0030, UPRV_LENGTHOF(largeBuffer));
1048 largeBuffer[UPRV_LENGTHOF(largeBuffer)-1] = 0;
b75a7d8f
A
1049
1050 /*Test ures_openU */
1051
73c04bcf
A
1052 status = U_ZERO_ERROR;
1053 ures_close(ures_openU(largeBuffer, "root", &status));
1054 if(status != U_ILLEGAL_ARGUMENT_ERROR){
1055 log_err("ERROR: ures_openU() worked when the path is very large. It returned %s\n", myErrorName(status));
1056 }
1057
1058 status = U_ZERO_ERROR;
1059 ures_close(ures_openU(NULL, "root", &status));
1060 if(U_FAILURE(status)){
729e4ab9 1061 log_err_status(status, "ERROR: ures_openU() failed path = NULL with %s\n", myErrorName(status));
73c04bcf
A
1062 }
1063
1064 status = U_ILLEGAL_ARGUMENT_ERROR;
1065 if(ures_openU(NULL, "root", &status) != NULL){
1066 log_err("ERROR: ures_openU() worked with error status with %s\n", myErrorName(status));
1067 }
1068
1069 status = U_ZERO_ERROR;
b75a7d8f
A
1070 teRes=ures_openU(utestdatapath, "te", &status);
1071 if(U_FAILURE(status)){
729e4ab9 1072 log_err_status(status, "ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status));
b75a7d8f
A
1073 return;
1074 }
1075 /*Test ures_getLocale() */
1076 log_verbose("Testing ures_getLocale() .....\n");
1077 if(strcmp(ures_getLocale(teRes, &status), "te") != 0){
1078 log_err("ERROR: ures_getLocale() failed. Expected = te_TE Got = %s\n", ures_getLocale(teRes, &status));
1079 }
1080 /*Test ures_getNextString() */
1081 teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status);
1082 key=ures_getKey(teFillin);
1083 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
57a6839d 1084 (void)value; /* Suppress set but not used warning. */
b75a7d8f
A
1085 ures_resetIterator(NULL);
1086 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
1087 if(status !=U_INDEX_OUTOFBOUNDS_ERROR){
1088 log_err("ERROR: calling getNextString where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1089 myErrorName(status));
1090 }
1091 ures_resetIterator(teRes);
1092 /*Test ures_getNextResource() where resource is table*/
1093 status=U_ZERO_ERROR;
1094#if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
1095 /* The next key varies depending on the charset. */
1096 teFillin=ures_getNextResource(teRes, teFillin, &status);
1097 if(U_FAILURE(status)){
1098 log_err("ERROR: ures_getNextResource() failed \n");
1099 }
1100 key=ures_getKey(teFillin);
1101 /*if(strcmp(key, "%%CollationBin") != 0){*/
73c04bcf
A
1102 /*if(strcmp(key, "array_2d_in_Root_te") != 0){*/ /* added "aliasClient" that goes first */
1103 if(strcmp(key, "a") != 0){
b75a7d8f
A
1104 log_err("ERROR: ures_getNextResource() failed\n");
1105 }
1106#endif
1107
1108 /*Test ures_getByIndex on string Resource*/
1109 teFillin=ures_getByKey(teRes, "string_only_in_te", teFillin, &status);
1110 teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status);
1111 if(U_FAILURE(status)){
1112 log_err("ERROR: ures_getByIndex on string resource failed\n");
1113 }
73c04bcf 1114 if(strcmp(u_austrcpy(convOutput, tres_getString(teFillin2, -1, NULL, &len, &status)), "TE") != 0){
b75a7d8f
A
1115 status=U_ZERO_ERROR;
1116 log_err("ERROR: ures_getByIndex on string resource fetched the key=%s, expected \"TE\" \n", austrdup(ures_getString(teFillin2, &len, &status)));
1117 }
1118
1119 /*ures_close(teRes);*/
1120
1121 /*Test ures_openFillIn*/
1122 log_verbose("Testing ures_openFillIn......\n");
1123 status=U_ZERO_ERROR;
1124 ures_openFillIn(teRes, testdatapath, "te", &status);
1125 if(U_FAILURE(status)){
1126 log_err("ERROR: ures_openFillIn failed\n");
1127 return;
1128 }
1129 if(strcmp(ures_getLocale(teRes, &status), "te") != 0){
1130 log_err("ERROR: ures_openFillIn did not open the ResourceBundle correctly\n");
1131 }
1132 ures_getByKey(teRes, "string_only_in_te", teFillin, &status);
1133 teFillin2=ures_getNextResource(teFillin, teFillin2, &status);
1134 if(ures_getType(teFillin2) != URES_STRING){
1135 log_err("ERROR: getType for getNextResource after ures_openFillIn failed\n");
1136 }
1137 teFillin2=ures_getNextResource(teFillin, teFillin2, &status);
1138 if(status !=U_INDEX_OUTOFBOUNDS_ERROR){
1139 log_err("ERROR: calling getNextResource where index out of bounds should return U_INDEX_OUTOFBOUNDS_ERROR, Got : %s\n",
1140 myErrorName(status));
1141 }
1142
1143 ures_close(teFillin);
1144 ures_close(teFillin2);
1145 ures_close(teRes);
1146
1147 /* Test that ures_getLocale() returns the "real" locale ID */
1148 status=U_ZERO_ERROR;
1149 teRes=ures_open(NULL, "dE_At_NOWHERE_TO_BE_FOUND", &status);
1150 if(U_FAILURE(status)) {
1151 log_data_err("unable to open a locale resource bundle from \"dE_At_NOWHERE_TO_BE_FOUND\"(%s)\n", u_errorName(status));
1152 } else {
1153 if(0!=strcmp("de_AT", ures_getLocale(teRes, &status))) {
1154 log_data_err("ures_getLocale(\"dE_At_NOWHERE_TO_BE_FOUND\")=%s but must be de_AT\n", ures_getLocale(teRes, &status));
1155 }
1156 ures_close(teRes);
1157 }
1158
1159 /* same test, but with an aliased locale resource bundle */
1160 status=U_ZERO_ERROR;
1161 teRes=ures_open(NULL, "iW_Il_depRecaTed_HebreW", &status);
1162 if(U_FAILURE(status)) {
1163 log_data_err("unable to open a locale resource bundle from \"iW_Il_depRecaTed_HebreW\"(%s)\n", u_errorName(status));
1164 } else {
1165 if(0!=strcmp("he_IL", ures_getLocale(teRes, &status))) {
1166 log_data_err("ures_getLocale(\"iW_Il_depRecaTed_HebreW\")=%s but must be he_IL\n", ures_getLocale(teRes, &status));
1167 }
1168 ures_close(teRes);
1169 }
1170 free(utestdatapath);
1171}
1172
1173static void TestErrorConditions(){
1174 UErrorCode status=U_ZERO_ERROR;
1175 const char *key=NULL;
1176 const UChar *value=NULL;
1177 const char* testdatapath;
1178 UChar* utestdatapath;
1179 int32_t len=0;
1180 UResourceBundle *teRes = NULL;
1181 UResourceBundle *coll=NULL;
1182 UResourceBundle *binColl = NULL;
1183 UResourceBundle *teFillin=NULL;
1184 UResourceBundle *teFillin2=NULL;
1185 uint8_t *binResult = NULL;
1186 int32_t resultLen;
1187
1188
1189 testdatapath = loadTestData(&status);
1190 if(U_FAILURE(status))
1191 {
46f4442e 1192 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
1193 return;
1194 }
374ca955 1195 len = (int32_t)strlen(testdatapath);
b75a7d8f
A
1196 utestdatapath = (UChar*) malloc(sizeof(UChar) *(len+10));
1197 u_uastrcpy(utestdatapath, testdatapath);
1198
1199 /*Test ures_openU with status != U_ZERO_ERROR*/
1200 log_verbose("Testing ures_openU() with status != U_ZERO_ERROR.....\n");
1201 status=U_ILLEGAL_ARGUMENT_ERROR;
1202 teRes=ures_openU(utestdatapath, "te", &status);
1203 if(U_FAILURE(status)){
1204 log_verbose("ures_openU() failed as expected path =%s with status != U_ZERO_ERROR\n", testdatapath);
1205 }else{
1206 log_err("ERROR: ures_openU() is supposed to fail path =%s with status != U_ZERO_ERROR\n", austrdup(utestdatapath));
1207 ures_close(teRes);
1208 }
3d1f044b 1209 /*Test ures_openFillIn fails when input UResourceBundle parameter is NULL*/
b75a7d8f
A
1210 log_verbose("Testing ures_openFillIn with UResourceBundle = NULL.....\n");
1211 status=U_ZERO_ERROR;
1212 ures_openFillIn(NULL, testdatapath, "te", &status);
73c04bcf
A
1213 if(status != U_ILLEGAL_ARGUMENT_ERROR){
1214 log_err("ERROR: ures_openFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
b75a7d8f
A
1215 myErrorName(status));
1216 }
3d1f044b
A
1217 /*Test ures_openDirectFillIn fails when input UResourceBundle parameter is NULL*/
1218 log_verbose("Testing ures_openDirectFillIn with UResourceBundle = NULL.....\n");
1219 status=U_ZERO_ERROR;
1220 ures_openDirectFillIn(NULL, testdatapath, "te", &status);
1221 if(status != U_ILLEGAL_ARGUMENT_ERROR){
1222 log_err("ERROR: ures_openDirectFillIn with UResourceBundle= NULL should fail. Expected U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1223 myErrorName(status));
1224 }
b75a7d8f
A
1225 /*Test ures_getLocale() with status != U_ZERO_ERROR*/
1226 status=U_ZERO_ERROR;
1227 teRes=ures_openU(utestdatapath, "te", &status);
1228 if(U_FAILURE(status)){
73c04bcf 1229 log_err("ERROR: ures_openU() failed path =%s with %s\n", austrdup(utestdatapath), myErrorName(status));
b75a7d8f
A
1230 return;
1231 }
1232 status=U_ILLEGAL_ARGUMENT_ERROR;
1233 if(ures_getLocale(teRes, &status) != NULL){
1234 log_err("ERROR: ures_getLocale is supposed to fail with errorCode != U_ZERO_ERROR\n");
1235 }
1236 /*Test ures_getLocale() with UResourceBundle = NULL*/
1237 status=U_ZERO_ERROR;
1238 if(ures_getLocale(NULL, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1239 log_err("ERROR: ures_getLocale is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1240 myErrorName(status));
1241 }
1242 /*Test ures_getSize() with UResourceBundle = NULL */
1243 status=U_ZERO_ERROR;
1244 if(ures_getSize(NULL) != 0){
1245 log_err("ERROR: ures_getSize() should return 0 when UResourceBundle=NULL. Got =%d\n", ures_getSize(NULL));
1246 }
1247 /*Test ures_getType() with UResourceBundle = NULL should return URES_NONE==-1*/
1248 status=U_ZERO_ERROR;
1249 if(ures_getType(NULL) != URES_NONE){
1250 log_err("ERROR: ures_getType() should return URES_NONE when UResourceBundle=NULL. Got =%d\n", ures_getType(NULL));
1251 }
1252 /*Test ures_getKey() with UResourceBundle = NULL*/
1253 status=U_ZERO_ERROR;
1254 if(ures_getKey(NULL) != NULL){
1255 log_err("ERROR: ures_getKey() should return NULL when UResourceBundle=NULL. Got =%d\n", ures_getKey(NULL));
1256 }
1257 /*Test ures_hasNext() with UResourceBundle = NULL*/
1258 status=U_ZERO_ERROR;
1259 if(ures_hasNext(NULL) != FALSE){
1260 log_err("ERROR: ures_hasNext() should return FALSE when UResourceBundle=NULL. Got =%d\n", ures_hasNext(NULL));
1261 }
1262 /*Test ures_get() with UResourceBundle = NULL*/
1263 status=U_ZERO_ERROR;
1264 if(ures_getStringByKey(NULL, "string_only_in_te", &resultLen, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1265 log_err("ERROR: ures_get is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1266 myErrorName(status));
1267 }
1268 /*Test ures_getByKey() with UResourceBundle = NULL*/
1269 status=U_ZERO_ERROR;
1270 teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status);
1271 if( teFillin != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1272 log_err("ERROR: ures_getByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1273 myErrorName(status));
1274 }
1275 /*Test ures_getByKey() with status != U_ZERO_ERROR*/
1276 teFillin=ures_getByKey(NULL, "string_only_in_te", teFillin, &status);
1277 if(teFillin != NULL ){
1278 log_err("ERROR: ures_getByKey is supposed to fail when errorCode != U_ZERO_ERROR\n");
1279 }
1280 /*Test ures_getStringByKey() with UResourceBundle = NULL*/
1281 status=U_ZERO_ERROR;
1282 if(ures_getStringByKey(NULL, "string_only_in_te", &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1283 log_err("ERROR: ures_getStringByKey is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1284 myErrorName(status));
1285 }
1286 /*Test ures_getStringByKey() with status != U_ZERO_ERROR*/
1287 if(ures_getStringByKey(teRes, "string_only_in_te", &len, &status) != NULL){
1288 log_err("ERROR: ures_getStringByKey is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1289 myErrorName(status));
1290 }
1291 /*Test ures_getString() with UResourceBundle = NULL*/
1292 status=U_ZERO_ERROR;
1293 if(ures_getString(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1294 log_err("ERROR: ures_getString is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1295 myErrorName(status));
1296 }
1297 /*Test ures_getString() with status != U_ZERO_ERROR*/
1298 if(ures_getString(teRes, &len, &status) != NULL){
1299 log_err("ERROR: ures_getString is supposed to fail when status != U_ZERO_ERROR. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1300 myErrorName(status));
1301 }
1302 /*Test ures_getBinary() with UResourceBundle = NULL*/
1303 status=U_ZERO_ERROR;
1304 if(ures_getBinary(NULL, &len, &status) != NULL && status != U_ILLEGAL_ARGUMENT_ERROR){
1305 log_err("ERROR: ures_getBinary is supposed to fail when UResourceBundle = NULL. Expected: errorCode = U_ILLEGAL_ARGUMENT_ERROR, Got: errorCode=%s\n",
1306 myErrorName(status));
1307 }
1308 /*Test ures_getBinary(0 status != U_ILLEGAL_ARGUMENT_ERROR*/
1309 status=U_ZERO_ERROR;
374ca955
A
1310 coll = ures_getByKey(teRes, "collations", coll, &status);
1311 coll = ures_getByKey(teRes, "standard", coll, &status);
b75a7d8f
A
1312 binColl=ures_getByKey(coll, "%%CollationBin", binColl, &status);
1313
1314 status=U_ILLEGAL_ARGUMENT_ERROR;
1315 binResult=(uint8_t*)ures_getBinary(binColl, &len, &status);
1316 if(binResult != NULL){
1317 log_err("ERROR: ures_getBinary() with status != U_ZERO_ERROR is supposed to fail\n");
1318 }
1319
1320 /*Test ures_getNextResource() with status != U_ZERO_ERROR*/
1321 teFillin=ures_getNextResource(teRes, teFillin, &status);
1322 if(teFillin != NULL){
1323 log_err("ERROR: ures_getNextResource() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1324 }
1325 /*Test ures_getNextResource() with UResourceBundle = NULL*/
1326 status=U_ZERO_ERROR;
1327 teFillin=ures_getNextResource(NULL, teFillin, &status);
1328 if(teFillin != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1329 log_err("ERROR: ures_getNextResource() with UResourceBundle = NULL is supposed to fail. Expected : U_IILEGAL_ARGUMENT_ERROR, Got : %s\n",
1330 myErrorName(status));
1331 }
1332 /*Test ures_getNextString with errorCode != U_ZERO_ERROR*/
1333 teFillin=ures_getByKey(teRes, "tagged_array_in_te_te_IN", teFillin, &status);
1334 key=ures_getKey(teFillin);
1335 status = U_ILLEGAL_ARGUMENT_ERROR;
1336 value=(UChar*)ures_getNextString(teFillin, &len, &key, &status);
1337 if(value != NULL){
1338 log_err("ERROR: ures_getNextString() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1339 }
1340 /*Test ures_getNextString with UResourceBundle = NULL*/
1341 status=U_ZERO_ERROR;
1342 value=(UChar*)ures_getNextString(NULL, &len, &key, &status);
1343 if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1344 log_err("ERROR: ures_getNextString() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1345 myErrorName(status));
1346 }
1347 /*Test ures_getByIndex with errorCode != U_ZERO_ERROR*/
1348 status=U_ZERO_ERROR;
1349 teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status);
1350 if(ures_countArrayItems(teRes, "array_only_in_te", &status) != 4) {
1351 log_err("ERROR: Wrong number of items in an array!\n");
1352 }
1353 status=U_ILLEGAL_ARGUMENT_ERROR;
1354 teFillin2=ures_getByIndex(teFillin, 0, teFillin2, &status);
1355 if(teFillin2 != NULL){
1356 log_err("ERROR: ures_getByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1357 }
1358 /*Test ures_getByIndex with UResourceBundle = NULL */
1359 status=U_ZERO_ERROR;
1360 teFillin2=ures_getByIndex(NULL, 0, teFillin2, &status);
1361 if(status != U_ILLEGAL_ARGUMENT_ERROR){
1362 log_err("ERROR: ures_getByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1363 myErrorName(status));
1364 }
1365 /*Test ures_getStringByIndex with errorCode != U_ZERO_ERROR*/
1366 status=U_ZERO_ERROR;
1367 teFillin=ures_getByKey(teRes, "array_only_in_te", teFillin, &status);
1368 status=U_ILLEGAL_ARGUMENT_ERROR;
1369 value=(UChar*)ures_getStringByIndex(teFillin, 0, &len, &status);
1370 if( value != NULL){
1371 log_err("ERROR: ures_getSringByIndex() with errorCode != U_ZERO_ERROR is supposed to fail\n");
1372 }
1373 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1374 status=U_ZERO_ERROR;
1375 value=(UChar*)ures_getStringByIndex(NULL, 0, &len, &status);
1376 if(value != NULL || status != U_ILLEGAL_ARGUMENT_ERROR){
1377 log_err("ERROR: ures_getStringByIndex() with UResourceBundle=NULL is supposed to fail\n Expected: U_ILLEGAL_ARGUMENT_ERROR, Got: %s\n",
1378 myErrorName(status));
1379 }
1380 /*Test ures_getStringByIndex with UResourceBundle = NULL */
1381 status=U_ZERO_ERROR;
1382 value=(UChar*)ures_getStringByIndex(teFillin, 9999, &len, &status);
1383 if(value != NULL || status != U_MISSING_RESOURCE_ERROR){
1384 log_err("ERROR: ures_getStringByIndex() with index that is too big is supposed to fail\n Expected: U_MISSING_RESOURCE_ERROR, Got: %s\n",
1385 myErrorName(status));
1386 }
1387 /*Test ures_getInt() where UResourceBundle = NULL */
1388 status=U_ZERO_ERROR;
1389 if(ures_getInt(NULL, &status) != -1 && status != U_ILLEGAL_ARGUMENT_ERROR){
1390 log_err("ERROR: ures_getInt() with UResourceBundle = NULL should fail. Expected: U_IILEGAL_ARGUMENT_ERROR, Got: %s\n",
1391 myErrorName(status));
1392 }
1393 /*Test ures_getInt() where status != U_ZERO_ERROR */
1394 if(ures_getInt(teRes, &status) != -1){
1395 log_err("ERROR: ures_getInt() with errorCode != U_ZERO_ERROR should fail\n");
1396 }
1397
1398 ures_close(teFillin);
1399 ures_close(teFillin2);
1400 ures_close(coll);
1401 ures_close(binColl);
1402 ures_close(teRes);
1403 free(utestdatapath);
1404
1405
1406}
1407
1408static void TestGetVersion(){
1409 UVersionInfo minVersionArray = {0x01, 0x00, 0x00, 0x00};
46f4442e 1410 UVersionInfo maxVersionArray = {0x50, 0xff, 0xcf, 0xcf};
b75a7d8f
A
1411 UVersionInfo versionArray;
1412 UErrorCode status= U_ZERO_ERROR;
1413 UResourceBundle* resB = NULL;
1414 int i=0, j = 0;
1415 int locCount = uloc_countAvailable();
1416 const char *locName = "root";
374ca955 1417
b75a7d8f 1418 log_verbose("The ures_getVersion tests begin : \n");
374ca955 1419
b75a7d8f 1420 for(j = -1; j < locCount; j++) {
374ca955
A
1421 if(j >= 0) {
1422 locName = uloc_getAvailable(j);
1423 }
1424 log_verbose("Testing version number for locale %s\n", locName);
1425 resB = ures_open(NULL,locName, &status);
1426 if (U_FAILURE(status)) {
729e4ab9 1427 log_err_status(status, "Resource bundle creation for locale %s failed.: %s\n", locName, myErrorName(status));
374ca955
A
1428 ures_close(resB);
1429 return;
1430 }
1431 ures_getVersion(resB, versionArray);
1432 for (i=0; i<4; ++i) {
1433 if (versionArray[i] < minVersionArray[i] ||
1434 versionArray[i] > maxVersionArray[i])
1435 {
1436 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1437 locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
1438 break;
1439 }
1440 }
1441 ures_close(resB);
b75a7d8f
A
1442 }
1443}
1444
374ca955
A
1445
1446static void TestGetVersionColl(){
57a6839d 1447#if !UCONFIG_NO_COLLATION
374ca955
A
1448 UVersionInfo minVersionArray = {0x00, 0x00, 0x00, 0x00};
1449 UVersionInfo maxVersionArray = {0x50, 0x80, 0xcf, 0xcf};
1450 UVersionInfo versionArray;
1451 UErrorCode status= U_ZERO_ERROR;
1452 UResourceBundle* resB = NULL;
1453 UEnumeration *locs= NULL;
1454 int i=0;
1455 const char *locName = "root";
1456 int32_t locLen;
1457 const UChar* rules =NULL;
1458 int32_t len = 0;
b331163b
A
1459
1460 /* test NUL termination of UCARules */
1461 resB = ures_open(U_ICUDATA_COLL,locName, &status);
1462 rules = tres_getString(resB,-1,"UCARules",&len, &status);
1463 if(!rules || U_FAILURE(status)) {
1464 log_data_err("Could not load UCARules for locale %s\n", locName);
1465 status = U_ZERO_ERROR;
1466 } else if(u_strlen(rules) != len){
1467 log_err("UCARules string not nul terminated! \n");
1468 }
1469 ures_close(resB);
1470
374ca955
A
1471 log_verbose("The ures_getVersion(%s) tests begin : \n", U_ICUDATA_COLL);
1472 locs = ures_openAvailableLocales(U_ICUDATA_COLL, &status);
1473 if (U_FAILURE(status)) {
729e4ab9 1474 log_err_status(status, "enumeration of %s failed.: %s\n", U_ICUDATA_COLL, myErrorName(status));
374ca955
A
1475 return;
1476 }
1477
b331163b 1478 for (;;) {
374ca955
A
1479 log_verbose("Testing version number for locale %s\n", locName);
1480 resB = ures_open(U_ICUDATA_COLL,locName, &status);
1481 if (U_FAILURE(status)) {
1482 log_err("Resource bundle creation for locale %s:%s failed.: %s\n", U_ICUDATA_COLL, locName, myErrorName(status));
1483 ures_close(resB);
b331163b 1484 break;
374ca955
A
1485 }
1486 ures_getVersion(resB, versionArray);
1487 for (i=0; i<4; ++i) {
1488 if (versionArray[i] < minVersionArray[i] ||
1489 versionArray[i] > maxVersionArray[i])
1490 {
1491 log_err("Testing ures_getVersion(%-5s) - unexpected result: %d.%d.%d.%d\n",
1492 locName, versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
1493 break;
1494 }
1495 }
1496 ures_close(resB);
b331163b
A
1497 locName = uenum_next(locs, &locLen, &status);
1498 if(U_FAILURE(status)) {
1499 log_err("uenum_next(locs) error %s\n", u_errorName(status));
1500 break;
1501 }
1502 if(locName == NULL) {
1503 break;
1504 }
374ca955
A
1505 }
1506 uenum_close(locs);
57a6839d 1507#endif /* !UCONFIG_NO_COLLATION */
374ca955
A
1508}
1509
b75a7d8f
A
1510static void TestResourceBundles()
1511{
46f4442e
A
1512 UErrorCode status = U_ZERO_ERROR;
1513 loadTestData(&status);
1514 if(U_FAILURE(status)) {
1515 log_data_err("Could not load testdata.dat, status = %s\n", u_errorName(status));
1516 return;
1517 }
b75a7d8f 1518
374ca955
A
1519 testTag("only_in_Root", TRUE, FALSE, FALSE);
1520 testTag("in_Root_te", TRUE, TRUE, FALSE);
1521 testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
1522 testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
1523 testTag("only_in_te", FALSE, TRUE, FALSE);
1524 testTag("only_in_te_IN", FALSE, FALSE, TRUE);
1525 testTag("in_te_te_IN", FALSE, TRUE, TRUE);
1526 testTag("nonexistent", FALSE, FALSE, FALSE);
b75a7d8f 1527
374ca955 1528 log_verbose("Passed:= %d Failed= %d \n", pass, fail);
b75a7d8f
A
1529
1530}
1531
1532
1533static void TestConstruction1()
1534{
1535 UResourceBundle *test1 = 0, *test2 = 0,*empty = 0;
1536 const UChar *result1, *result2;
1537 UErrorCode status= U_ZERO_ERROR;
1538 UErrorCode err = U_ZERO_ERROR;
1539 const char* locale="te_IN";
1540 const char* testdatapath;
1541
1542 int32_t len1=0;
1543 int32_t len2=0;
1544 UVersionInfo versionInfo;
1545 char versionString[256];
1546 char verboseOutput[256];
1547
1548 U_STRING_DECL(rootVal, "ROOT", 4);
1549 U_STRING_DECL(te_inVal, "TE_IN", 5);
1550
1551 U_STRING_INIT(rootVal, "ROOT", 4);
1552 U_STRING_INIT(te_inVal, "TE_IN", 5);
1553
1554 testdatapath=loadTestData(&status);
1555 if(U_FAILURE(status))
1556 {
46f4442e 1557 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
1558 return;
1559 }
1560
1561 log_verbose("Testing ures_open()......\n");
1562
1563 empty = ures_open(testdatapath, "testempty", &status);
1564 if(empty == NULL || U_FAILURE(status)) {
1565 log_err("opening empty failed!\n");
1566 }
1567 ures_close(empty);
1568
1569 test1=ures_open(testdatapath, NULL, &err);
1570
1571 if(U_FAILURE(err))
1572 {
1573 log_err("construction of NULL did not succeed : %s \n", myErrorName(status));
1574 return;
1575 }
1576 test2=ures_open(testdatapath, locale, &err);
1577 if(U_FAILURE(err))
1578 {
1579 log_err("construction of %s did not succeed : %s \n", locale, myErrorName(status));
1580 return;
1581 }
73c04bcf
A
1582 result1= tres_getString(test1, -1, "string_in_Root_te_te_IN", &len1, &err);
1583 result2= tres_getString(test2, -1, "string_in_Root_te_te_IN", &len2, &err);
b75a7d8f
A
1584 if (U_FAILURE(err) || len1==0 || len2==0) {
1585 log_err("Something threw an error in TestConstruction(): %s\n", myErrorName(status));
1586 return;
1587 }
1588 log_verbose("for string_in_Root_te_te_IN, default.txt had %s\n", u_austrcpy(verboseOutput, result1));
1589 log_verbose("for string_in_Root_te_te_IN, te_IN.txt had %s\n", u_austrcpy(verboseOutput, result2));
1590 if(u_strcmp(result1, rootVal) !=0 || u_strcmp(result2, te_inVal) !=0 ){
1591 log_err("construction test failed. Run Verbose for more information");
1592 }
1593
1594
1595 /* Test getVersionNumber*/
1596 log_verbose("Testing version number\n");
1597 log_verbose("for getVersionNumber : %s\n", ures_getVersionNumber(test1));
1598
1599 log_verbose("Testing version \n");
1600 ures_getVersion(test1, versionInfo);
1601 u_versionToString(versionInfo, versionString);
1602
1603 log_verbose("for getVersion : %s\n", versionString);
1604
1605 if(strcmp(versionString, ures_getVersionNumber(test1)) != 0) {
1606 log_err("Versions differ: %s vs %s\n", versionString, ures_getVersionNumber(test1));
1607 }
1608
1609 ures_close(test1);
1610 ures_close(test2);
1611
1612}
1613
1614/*****************************************************************************/
1615/*****************************************************************************/
1616
1617static UBool testTag(const char* frag,
1618 UBool in_Root,
1619 UBool in_te,
1620 UBool in_te_IN)
1621{
1622 int32_t failNum = fail;
1623
1624 /* Make array from input params */
1625
1626 UBool is_in[3];
1627 const char *NAME[] = { "ROOT", "TE", "TE_IN" };
1628
1629 /* Now try to load the desired items */
1630 UResourceBundle* theBundle = NULL;
1631 char tag[99];
1632 char action[256];
1633 UErrorCode expected_status,status = U_ZERO_ERROR,expected_resource_status = U_ZERO_ERROR;
1634 UChar* base = NULL;
1635 UChar* expected_string = NULL;
1636 const UChar* string = NULL;
1637 char buf[5];
1638 char item_tag[10];
1639 int32_t i,j,row,col, len;
1640 int32_t actual_bundle;
1641 int32_t count = 0;
1642 int32_t row_count=0;
1643 int32_t column_count=0;
51004dcb 1644 int32_t idx = 0;
b75a7d8f
A
1645 int32_t tag_count= 0;
1646 const char* testdatapath;
1647 char verboseOutput[256];
1648 UResourceBundle* array=NULL;
1649 UResourceBundle* array2d=NULL;
1650 UResourceBundle* tags=NULL;
1651 UResourceBundle* arrayItem1=NULL;
1652
1653 testdatapath = loadTestData(&status);
1654 if(U_FAILURE(status))
1655 {
46f4442e 1656 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
b75a7d8f
A
1657 return FALSE;
1658 }
1659
1660 is_in[0] = in_Root;
1661 is_in[1] = in_te;
1662 is_in[2] = in_te_IN;
1663
1664 strcpy(item_tag, "tag");
1665
1666 for (i=0; i<bundles_count; ++i)
1667 {
1668 strcpy(action,"construction for ");
1669 strcat(action, param[i].name);
1670
1671
1672 status = U_ZERO_ERROR;
1673
1674 theBundle = ures_open(testdatapath, param[i].name, &status);
1675 CONFIRM_ErrorCode(status,param[i].expected_constructor_status);
1676
1677 if(i == 5)
1678 actual_bundle = 0; /* ne -> default */
1679 else if(i == 3)
1680 actual_bundle = 1; /* te_NE -> te */
1681 else if(i == 4)
1682 actual_bundle = 2; /* te_IN_NE -> te_IN */
1683 else
1684 actual_bundle = i;
1685
1686 expected_resource_status = U_MISSING_RESOURCE_ERROR;
1687 for (j=e_te_IN; j>=e_Root; --j)
1688 {
1689 if (is_in[j] && param[i].inherits[j])
1690 {
1691
1692 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
1693 expected_resource_status = U_ZERO_ERROR;
1694 else if(j == 0)
1695 expected_resource_status = U_USING_DEFAULT_WARNING;
1696 else
1697 expected_resource_status = U_USING_FALLBACK_WARNING;
1698
1699 log_verbose("%s[%d]::%s: in<%d:%s> inherits<%d:%s>. actual_bundle=%s\n",
1700 param[i].name,
1701 i,
1702 frag,
1703 j,
1704 is_in[j]?"Yes":"No",
1705 j,
1706 param[i].inherits[j]?"Yes":"No",
1707 param[actual_bundle].name);
1708
1709 break;
1710 }
1711 }
1712
1713 for (j=param[i].where; j>=0; --j)
1714 {
1715 if (is_in[j])
1716 {
1717 if(base != NULL) {
1718 free(base);
1719 base = NULL;
1720 }
1721 base=(UChar*)malloc(sizeof(UChar)*(strlen(NAME[j]) + 1));
1722 u_uastrcpy(base,NAME[j]);
1723
1724 break;
1725 }
1726 else {
1727 if(base != NULL) {
1728 free(base);
1729 base = NULL;
1730 }
1731 base = (UChar*) malloc(sizeof(UChar) * 1);
1732 *base = 0x0000;
1733 }
1734 }
1735
1736 /*----string---------------------------------------------------------------- */
1737
1738 strcpy(tag,"string_");
1739 strcat(tag,frag);
1740
1741 strcpy(action,param[i].name);
1742 strcat(action, ".ures_getStringByKey(" );
1743 strcat(action,tag);
1744 strcat(action, ")");
1745
1746
1747 status = U_ZERO_ERROR;
1748 len=0;
1749
73c04bcf 1750 string=tres_getString(theBundle, -1, tag, &len, &status);
b75a7d8f
A
1751 if(U_SUCCESS(status)) {
1752 expected_string=(UChar*)malloc(sizeof(UChar)*(u_strlen(base) + 4));
1753 u_strcpy(expected_string,base);
1754 CONFIRM_INT_EQ(len, u_strlen(expected_string));
1755 }else{
1756 expected_string = (UChar*)malloc(sizeof(UChar)*(u_strlen(kERROR) + 1));
1757 u_strcpy(expected_string,kERROR);
1758 string=kERROR;
1759 }
1760 log_verbose("%s got %d, expected %d\n", action, status, expected_resource_status);
1761
1762 CONFIRM_ErrorCode(status, expected_resource_status);
1763 CONFIRM_EQ(string, expected_string);
1764
1765
1766
1767 /*--------------array------------------------------------------------- */
1768
1769 strcpy(tag,"array_");
1770 strcat(tag,frag);
1771
1772 strcpy(action,param[i].name);
1773 strcat(action, ".ures_getByKey(" );
1774 strcat(action,tag);
1775 strcat(action, ")");
1776
1777 len=0;
1778
1779 count = kERROR_COUNT;
1780 status = U_ZERO_ERROR;
1781 array=ures_getByKey(theBundle, tag, array, &status);
1782 CONFIRM_ErrorCode(status,expected_resource_status);
1783 if (U_SUCCESS(status)) {
1784 /*confirm the resource type is an array*/
1785 CONFIRM_INT_EQ(ures_getType(array), URES_ARRAY);
1786 /*confirm the size*/
1787 count=ures_getSize(array);
1788 CONFIRM_INT_GE(count,1);
1789 for (j=0; j<count; ++j) {
1790 UChar element[3];
1791 u_strcpy(expected_string, base);
1792 u_uastrcpy(element, itoa1(j,buf));
1793 u_strcat(expected_string, element);
1794 arrayItem1=ures_getNextResource(array, arrayItem1, &status);
1795 if(U_SUCCESS(status)){
73c04bcf 1796 CONFIRM_EQ(tres_getString(arrayItem1, -1, NULL, &len, &status),expected_string);
b75a7d8f
A
1797 }
1798 }
1799
1800 }
1801 else {
1802 CONFIRM_INT_EQ(count,kERROR_COUNT);
1803 CONFIRM_ErrorCode(status, U_MISSING_RESOURCE_ERROR);
1804 /*CONFIRM_INT_EQ((int32_t)(unsigned long)array,(int32_t)0);*/
1805 count = 0;
1806 }
1807
1808 /*--------------arrayItem------------------------------------------------- */
1809
1810 strcpy(tag,"array_");
1811 strcat(tag,frag);
1812
1813 strcpy(action,param[i].name);
1814 strcat(action, ".ures_getStringByIndex(");
1815 strcat(action, tag);
1816 strcat(action, ")");
1817
1818
1819 for (j=0; j<10; ++j){
51004dcb 1820 idx = count ? (randi(count * 3) - count) : (randi(200) - 100);
b75a7d8f
A
1821 status = U_ZERO_ERROR;
1822 string=kERROR;
1823 array=ures_getByKey(theBundle, tag, array, &status);
1824 if(!U_FAILURE(status)){
1825 UChar *t=NULL;
51004dcb 1826 t=(UChar*)ures_getStringByIndex(array, idx, &len, &status);
b75a7d8f
A
1827 if(!U_FAILURE(status)){
1828 UChar element[3];
1829 string=t;
1830 u_strcpy(expected_string, base);
51004dcb 1831 u_uastrcpy(element, itoa1(idx,buf));
b75a7d8f
A
1832 u_strcat(expected_string, element);
1833 } else {
1834 u_strcpy(expected_string, kERROR);
1835 }
1836
1837 }
51004dcb 1838 expected_status = (idx >= 0 && idx < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
b75a7d8f
A
1839 CONFIRM_ErrorCode(status,expected_status);
1840 CONFIRM_EQ(string,expected_string);
1841
1842 }
1843
1844
1845 /*--------------2dArray------------------------------------------------- */
1846
1847 strcpy(tag,"array_2d_");
1848 strcat(tag,frag);
1849
1850 strcpy(action,param[i].name);
1851 strcat(action, ".ures_getByKey(" );
1852 strcat(action,tag);
1853 strcat(action, ")");
1854
1855
1856
1857 row_count = kERROR_COUNT, column_count = kERROR_COUNT;
1858 status = U_ZERO_ERROR;
1859 array2d=ures_getByKey(theBundle, tag, array2d, &status);
1860
1861 CONFIRM_ErrorCode(status,expected_resource_status);
1862 if (U_SUCCESS(status))
1863 {
1864 /*confirm the resource type is an 2darray*/
1865 CONFIRM_INT_EQ(ures_getType(array2d), URES_ARRAY);
1866 row_count=ures_getSize(array2d);
1867 CONFIRM_INT_GE(row_count,1);
1868
1869 for(row=0; row<row_count; ++row){
1870 UResourceBundle *tableRow=NULL;
1871 tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1872 CONFIRM_ErrorCode(status, expected_resource_status);
1873 if(U_SUCCESS(status)){
1874 /*confirm the resourcetype of each table row is an array*/
1875 CONFIRM_INT_EQ(ures_getType(tableRow), URES_ARRAY);
1876 column_count=ures_getSize(tableRow);
1877 CONFIRM_INT_GE(column_count,1);
1878
1879 for (col=0; j<column_count; ++j) {
1880 UChar element[3];
1881 u_strcpy(expected_string, base);
1882 u_uastrcpy(element, itoa1(row, buf));
1883 u_strcat(expected_string, element);
1884 u_uastrcpy(element, itoa1(col, buf));
1885 u_strcat(expected_string, element);
1886 arrayItem1=ures_getNextResource(tableRow, arrayItem1, &status);
1887 if(U_SUCCESS(status)){
73c04bcf 1888 const UChar *stringValue=tres_getString(arrayItem1, -1, NULL, &len, &status);
b75a7d8f
A
1889 CONFIRM_EQ(stringValue, expected_string);
1890 }
1891 }
1892 }
1893 ures_close(tableRow);
1894 }
1895 }else{
1896 CONFIRM_INT_EQ(row_count,kERROR_COUNT);
1897 CONFIRM_INT_EQ(column_count,kERROR_COUNT);
1898 row_count=column_count=0;
1899 }
1900
1901
1902 /*------2dArrayItem-------------------------------------------------------------- */
1903 /* 2dArrayItem*/
1904 for (j=0; j<10; ++j)
1905 {
1906 row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100);
1907 col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100);
1908 status = U_ZERO_ERROR;
1909 string = kERROR;
1910 len=0;
1911 array2d=ures_getByKey(theBundle, tag, array2d, &status);
1912 if(U_SUCCESS(status)){
1913 UResourceBundle *tableRow=NULL;
1914 tableRow=ures_getByIndex(array2d, row, tableRow, &status);
1915 if(U_SUCCESS(status)) {
1916 UChar *t=NULL;
1917 t=(UChar*)ures_getStringByIndex(tableRow, col, &len, &status);
1918 if(U_SUCCESS(status)){
1919 string=t;
1920 }
1921 }
1922 ures_close(tableRow);
1923 }
1924 expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
1925 expected_resource_status: U_MISSING_RESOURCE_ERROR;
1926 CONFIRM_ErrorCode(status,expected_status);
1927
1928 if (U_SUCCESS(status)){
1929 UChar element[3];
1930 u_strcpy(expected_string, base);
1931 u_uastrcpy(element, itoa1(row, buf));
1932 u_strcat(expected_string, element);
1933 u_uastrcpy(element, itoa1(col, buf));
1934 u_strcat(expected_string, element);
1935 } else {
1936 u_strcpy(expected_string,kERROR);
1937 }
1938 CONFIRM_EQ(string,expected_string);
1939
1940 }
1941
1942
1943 /*--------------taggedArray----------------------------------------------- */
1944 strcpy(tag,"tagged_array_");
1945 strcat(tag,frag);
1946
1947 strcpy(action,param[i].name);
1948 strcat(action,".ures_getByKey(");
1949 strcat(action, tag);
1950 strcat(action,")");
1951
1952
1953 status = U_ZERO_ERROR;
1954 tag_count=0;
1955 tags=ures_getByKey(theBundle, tag, tags, &status);
1956 CONFIRM_ErrorCode(status, expected_resource_status);
1957 if (U_SUCCESS(status)) {
1958 UResType bundleType=ures_getType(tags);
1959 CONFIRM_INT_EQ(bundleType, URES_TABLE);
1960
1961 tag_count=ures_getSize(tags);
1962 CONFIRM_INT_GE((int32_t)tag_count, (int32_t)0);
1963
51004dcb 1964 for(idx=0; idx <tag_count; idx++){
b75a7d8f
A
1965 UResourceBundle *tagelement=NULL;
1966 const char *key=NULL;
1967 UChar* value=NULL;
51004dcb 1968 tagelement=ures_getByIndex(tags, idx, tagelement, &status);
b75a7d8f
A
1969 key=ures_getKey(tagelement);
1970 value=(UChar*)ures_getNextString(tagelement, &len, &key, &status);
1971 log_verbose("tag = %s, value = %s\n", key, u_austrcpy(verboseOutput, value));
1972 if(strncmp(key, "tag", 3) == 0 && u_strncmp(value, base, u_strlen(base)) == 0){
1973 record_pass();
1974 }else{
1975 record_fail();
1976 }
1977 ures_close(tagelement);
1978 }
1979 }else{
1980 tag_count=0;
1981 }
1982
1983 /*---------taggedArrayItem----------------------------------------------*/
1984 count = 0;
51004dcb 1985 for (idx=-20; idx<20; ++idx)
b75a7d8f
A
1986 {
1987
1988 status = U_ZERO_ERROR;
1989 string = kERROR;
1990 strcpy(item_tag, "tag");
51004dcb 1991 strcat(item_tag, itoa1(idx,buf));
b75a7d8f
A
1992 tags=ures_getByKey(theBundle, tag, tags, &status);
1993 if(U_SUCCESS(status)){
1994 UResourceBundle *tagelement=NULL;
1995 UChar *t=NULL;
1996 tagelement=ures_getByKey(tags, item_tag, tagelement, &status);
1997 if(!U_FAILURE(status)){
1998 UResType elementType=ures_getType(tagelement);
1999 CONFIRM_INT_EQ(elementType, URES_STRING);
2000 if(strcmp(ures_getKey(tagelement), item_tag) == 0){
2001 record_pass();
2002 }else{
2003 record_fail();
2004 }
73c04bcf 2005 t=(UChar*)tres_getString(tagelement, -1, NULL, &len, &status);
b75a7d8f
A
2006 if(!U_FAILURE(status)){
2007 string=t;
2008 }
2009 }
51004dcb 2010 if (idx < 0) {
b75a7d8f
A
2011 CONFIRM_ErrorCode(status,U_MISSING_RESOURCE_ERROR);
2012 }
2013 else{
2014 if (status != U_MISSING_RESOURCE_ERROR) {
2015 UChar element[3];
2016 u_strcpy(expected_string, base);
51004dcb 2017 u_uastrcpy(element, itoa1(idx,buf));
b75a7d8f
A
2018 u_strcat(expected_string, element);
2019 CONFIRM_EQ(string,expected_string);
2020 count++;
2021 }
2022 }
2023 ures_close(tagelement);
2024 }
2025 }
2026 CONFIRM_INT_EQ(count, tag_count);
2027
2028 free(expected_string);
2029 ures_close(theBundle);
2030 }
2031 ures_close(array);
2032 ures_close(array2d);
2033 ures_close(tags);
2034 ures_close(arrayItem1);
2035 free(base);
2036 return (UBool)(failNum == fail);
2037}
2038
2039static void record_pass()
2040{
2041 ++pass;
2042}
2043
2044static void record_fail()
2045{
2046 ++fail;
2047}
2048
57a6839d
A
2049static void TestPreventFallback() {
2050 UResourceBundle* theBundle = NULL;
2051 const char* testdatapath;
2052 UErrorCode status = U_ZERO_ERROR;
2053 int32_t unused_len = 0;
2054
2055 testdatapath=loadTestData(&status);
2056 if(U_FAILURE(status))
2057 {
2058 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
2059 return;
2060 }
2061
2062 // In te_IN locale, fallback of string_in_te_no_te_IN_fallback is blocked
2063 // with the three empty-set (U+2205) chars.
2064 theBundle = ures_open(testdatapath, "te_IN_NE", &status);
2065 if(U_FAILURE(status))
2066 {
2067 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status));
2068 return;
2069 }
2070
2071 // Fallback is blocked
2072 ures_getStringByKeyWithFallback(theBundle, "string_in_te_no_te_IN_fallback", &unused_len, &status);
2073 if (status != U_MISSING_RESOURCE_ERROR)
2074 {
2075 log_err("Expected missing resource error for string_in_te_no_te_IN_fallback.");
2076 }
2077 status = U_ZERO_ERROR;
2078
2079 // This fallback should succeed
2080 ures_getStringByKeyWithFallback(theBundle, "string_only_in_te", &unused_len, &status);
2081 if(U_FAILURE(status))
2082 {
2083 log_err("Expected to find string_only_in_te %s \n",myErrorName(status));
2084 }
2085 status = U_ZERO_ERROR;
2086 ures_close(theBundle);
2087
2088 // From te locale, we should be able to fetch string_in_te_no_te_IN_fallback.
2089 theBundle = ures_open(testdatapath, "te", &status);
2090 if(U_FAILURE(status))
2091 {
2092 log_data_err("Could not open resource bundle te_IN_NE %s \n",myErrorName(status));
2093 return;
2094 }
2095 ures_getStringByKeyWithFallback(theBundle, "string_in_te_no_te_IN_fallback", &unused_len, &status);
2096 if(U_FAILURE(status))
2097 {
2098 log_err("Expected to find string_in_te_no_te_IN_fallback %s \n",myErrorName(status));
2099 }
2100 status = U_ZERO_ERROR;
2101 ures_close(theBundle);
2102}
2103
b75a7d8f
A
2104/**
2105 * Test to make sure that the U_USING_FALLBACK_ERROR and U_USING_DEFAULT_ERROR
2106 * are set correctly
2107 */
2108
2109static void TestFallback()
2110{
2111 UErrorCode status = U_ZERO_ERROR;
2112 UResourceBundle *fr_FR = NULL;
374ca955 2113 UResourceBundle *subResource = NULL;
b75a7d8f
A
2114 const UChar *junk; /* ignored */
2115 int32_t resultLen;
2116
2117 log_verbose("Opening fr_FR..");
2118 fr_FR = ures_open(NULL, "fr_FR", &status);
2119 if(U_FAILURE(status))
2120 {
729e4ab9 2121 log_err_status(status, "Couldn't open fr_FR - %s\n", u_errorName(status));
b75a7d8f
A
2122 return;
2123 }
2124
2125 status = U_ZERO_ERROR;
2126
2127
2128 /* clear it out.. just do some calls to get the gears turning */
73c04bcf 2129 junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
b75a7d8f 2130 status = U_ZERO_ERROR;
73c04bcf 2131 junk = tres_getString(fr_FR, -1, "LocaleString", &resultLen, &status);
b75a7d8f 2132 status = U_ZERO_ERROR;
73c04bcf 2133 junk = tres_getString(fr_FR, -1, "LocaleID", &resultLen, &status);
b75a7d8f 2134 status = U_ZERO_ERROR;
57a6839d 2135 (void)junk; /* Suppress set but not used warning. */
b75a7d8f
A
2136
2137 /* OK first one. This should be a Default value. */
57a6839d 2138 subResource = ures_getByKey(fr_FR, "layout", NULL, &status);
b75a7d8f
A
2139 if(status != U_USING_DEFAULT_WARNING)
2140 {
57a6839d 2141 log_data_err("Expected U_USING_DEFAULT_ERROR when trying to get layout from fr_FR, got %s\n",
b75a7d8f
A
2142 u_errorName(status));
2143 }
2144
2145 status = U_ZERO_ERROR;
374ca955 2146 ures_close(subResource);
b75a7d8f
A
2147
2148 /* and this is a Fallback, to fr */
729e4ab9 2149 junk = tres_getString(fr_FR, -1, "ExemplarCharacters", &resultLen, &status);
b75a7d8f
A
2150 if(status != U_USING_FALLBACK_WARNING)
2151 {
729e4ab9 2152 log_data_err("Expected U_USING_FALLBACK_ERROR when trying to get ExemplarCharacters from fr_FR, got %d\n",
b75a7d8f
A
2153 status);
2154 }
2155
2156 status = U_ZERO_ERROR;
2157
2158 ures_close(fr_FR);
2159 /* Temporary hack err actually should be U_USING_FALLBACK_ERROR */
2160 /* Test Jitterbug 552 fallback mechanism of aliased data */
2161 {
2162 UErrorCode err =U_ZERO_ERROR;
2163 UResourceBundle* myResB = ures_open(NULL,"no_NO_NY",&err);
374ca955 2164 UResourceBundle* resLocID = ures_getByKey(myResB, "Version", NULL, &err);
b75a7d8f 2165 UResourceBundle* tResB;
729e4ab9 2166 UResourceBundle* zoneResource;
73c04bcf 2167 const UChar* version = NULL;
340931cb 2168 static const UChar versionStr[] = u"36.1"; // 36.1 in nn_NO
374ca955 2169
b75a7d8f 2170 if(err != U_ZERO_ERROR){
374ca955 2171 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));
b75a7d8f
A
2172 return;
2173 }
73c04bcf
A
2174 version = tres_getString(resLocID, -1, NULL, &resultLen, &err);
2175 if(u_strcmp(version, versionStr) != 0){
46f4442e
A
2176 char x[100];
2177 char g[100];
2178 u_austrcpy(x, versionStr);
2179 u_austrcpy(g, version);
2180 log_data_err("ures_getString(resLocID, &resultLen, &err) returned an unexpected version value. Expected '%s', but got '%s'\n",
2181 x, g);
b75a7d8f 2182 }
729e4ab9
A
2183 zoneResource = ures_open(U_ICUDATA_ZONE, "no_NO_NY", &err);
2184 tResB = ures_getByKey(zoneResource, "zoneStrings", NULL, &err);
b75a7d8f 2185 if(err != U_USING_FALLBACK_WARNING){
73c04bcf 2186 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));
b75a7d8f 2187 }
729e4ab9
A
2188 ures_close(tResB);
2189 ures_close(zoneResource);
b75a7d8f
A
2190 ures_close(resLocID);
2191 ures_close(myResB);
b75a7d8f
A
2192 }
2193
2194}
2195
374ca955
A
2196/* static void printUChars(UChar* uchars){
2197/ int16_t i=0;
2198/ for(i=0; i<u_strlen(uchars); i++){
2199/ log_err("%04X ", *(uchars+i));
2200/ }
2201/ } */
b75a7d8f
A
2202
2203static void TestResourceLevelAliasing(void) {
374ca955
A
2204 UErrorCode status = U_ZERO_ERROR;
2205 UResourceBundle *aliasB = NULL, *tb = NULL;
2206 UResourceBundle *en = NULL, *uk = NULL, *testtypes = NULL;
2207 const char* testdatapath = NULL;
2208 const UChar *string = NULL, *sequence = NULL;
73c04bcf
A
2209 /*const uint8_t *binary = NULL, *binSequence = NULL;*/
2210 int32_t strLen = 0, seqLen = 0;/*, binLen = 0, binSeqLen = 0;*/
374ca955
A
2211 char buffer[100];
2212 char *s;
b75a7d8f 2213
374ca955
A
2214 testdatapath=loadTestData(&status);
2215 if(U_FAILURE(status))
2216 {
46f4442e 2217 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
374ca955 2218 return;
b75a7d8f
A
2219 }
2220
374ca955 2221 aliasB = ures_open(testdatapath, "testaliases", &status);
73c04bcf
A
2222
2223 if(U_FAILURE(status))
2224 {
46f4442e 2225 log_data_err("Could not load testaliases.res %s \n",myErrorName(status));
73c04bcf
A
2226 return;
2227 }
374ca955
A
2228 /* this should fail - circular alias */
2229 tb = ures_getByKey(aliasB, "aaa", tb, &status);
2230 if(status != U_TOO_MANY_ALIASES_ERROR) {
2231 log_err("Failed to detect circular alias\n");
73c04bcf
A
2232 }
2233 else {
374ca955
A
2234 status = U_ZERO_ERROR;
2235 }
2236 tb = ures_getByKey(aliasB, "aab", tb, &status);
2237 if(status != U_TOO_MANY_ALIASES_ERROR) {
2238 log_err("Failed to detect circular alias\n");
2239 } else {
2240 status = U_ZERO_ERROR;
2241 }
2242 if(U_FAILURE(status) ) {
2243 log_data_err("err loading tb resource\n");
2244 } else {
2245 /* testing aliasing to a non existing resource */
2246 tb = ures_getByKey(aliasB, "nonexisting", tb, &status);
2247 if(status != U_MISSING_RESOURCE_ERROR) {
2248 log_err("Managed to find an alias to non-existing resource\n");
2249 } else {
2250 status = U_ZERO_ERROR;
2251 }
374ca955 2252 /* testing referencing/composed alias */
b331163b 2253 uk = ures_findResource("ja/calendar/gregorian/DateTimePatterns/2", uk, &status);
374ca955 2254 if((uk == NULL) || U_FAILURE(status)) {
b331163b 2255 log_err_status(status, "Couldn't findResource('ja/calendar/gregorian/DateTimePatterns/2') err %s\n", u_errorName(status));
374ca955
A
2256 goto cleanup;
2257 }
2258
73c04bcf 2259 sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
374ca955
A
2260
2261 tb = ures_getByKey(aliasB, "referencingalias", tb, &status);
73c04bcf 2262 string = tres_getString(tb, -1, NULL, &strLen, &status);
374ca955
A
2263
2264 if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
729e4ab9 2265 log_err("Referencing alias didn't get the right string (1)\n");
374ca955
A
2266 }
2267
73c04bcf 2268 string = tres_getString(aliasB, -1, "referencingalias", &strLen, &status);
374ca955 2269 if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
729e4ab9 2270 log_err("Referencing alias didn't get the right string (2)\n");
374ca955
A
2271 }
2272
2273 checkStatus(__LINE__, U_ZERO_ERROR, status);
b331163b 2274 tb = ures_getByKey(aliasB, "DateTimePatterns", tb, &status);
374ca955
A
2275 checkStatus(__LINE__, U_ZERO_ERROR, status);
2276 tb = ures_getByIndex(tb, 2, tb, &status);
2277 checkStatus(__LINE__, U_ZERO_ERROR, status);
73c04bcf 2278 string = tres_getString(tb, -1, NULL, &strLen, &status);
374ca955
A
2279 checkStatus(__LINE__, U_ZERO_ERROR, status);
2280
2281 if(U_FAILURE(status)) {
2282 log_err("%s trying to get string via separate getters\n", u_errorName(status));
2283 } else if(seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
729e4ab9 2284 log_err("Referencing alias didn't get the right string (3)\n");
374ca955 2285 }
73c04bcf 2286
374ca955
A
2287 /* simple alias */
2288 testtypes = ures_open(testdatapath, "testtypes", &status);
2289 strcpy(buffer, "menu/file/open");
2290 s = buffer;
2291 uk = ures_findSubResource(testtypes, s, uk, &status);
73c04bcf 2292 sequence = tres_getString(uk, -1, NULL, &seqLen, &status);
374ca955
A
2293
2294 tb = ures_getByKey(aliasB, "simplealias", tb, &status);
73c04bcf 2295 string = tres_getString(tb, -1, NULL, &strLen, &status);
374ca955
A
2296
2297 if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
729e4ab9 2298 log_err("Referencing alias didn't get the right string (4)\n");
374ca955
A
2299 }
2300
2301 /* test indexed aliasing */
2302
2303 tb = ures_getByKey(aliasB, "zoneTests", tb, &status);
2304 tb = ures_getByKey(tb, "zoneAlias2", tb, &status);
73c04bcf 2305 string = tres_getString(tb, -1, NULL, &strLen, &status);
374ca955 2306
729e4ab9 2307 en = ures_findResource("/ICUDATA-zone/en/zoneStrings/3/0", en, &status);
73c04bcf 2308 sequence = tres_getString(en, -1, NULL, &seqLen, &status);
374ca955
A
2309
2310 if(U_FAILURE(status) || seqLen != strLen || u_strncmp(sequence, string, seqLen) != 0) {
729e4ab9 2311 log_err("Referencing alias didn't get the right string (5)\n");
374ca955
A
2312 }
2313 }
2314 /* test getting aliased string by index */
2315 {
2316 const char* keys[] = {
2317 "KeyAlias0PST",
2318 "KeyAlias1PacificStandardTime",
2319 "KeyAlias2PDT",
2320 "KeyAlias3LosAngeles"
2321 };
2322
2323 const char* strings[] = {
2324 "America/Los_Angeles",
2325 "Pacific Standard Time",
2326 "PDT",
2327 "Los Angeles",
2328 };
2329 UChar uBuffer[256];
2330 const UChar* result;
2331 int32_t uBufferLen = 0, resultLen = 0;
2332 int32_t i = 0;
2333 const char *key = NULL;
2334 tb = ures_getByKey(aliasB, "testGetStringByKeyAliasing", tb, &status);
2335 if(U_FAILURE(status)) {
729e4ab9
A
2336 log_err("FAIL: Couldn't get testGetStringByKeyAliasing resource: %s\n", u_errorName(status));
2337 } else {
2ca993e8 2338 for(i = 0; i < UPRV_LENGTHOF(strings); i++) {
729e4ab9
A
2339 result = tres_getString(tb, -1, keys[i], &resultLen, &status);
2340 if(U_FAILURE(status)){
2341 log_err("(1) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2342 continue;
2343 }
2344 uBufferLen = u_unescape(strings[i], uBuffer, 256);
2345 if(resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2346 log_err("(1) Didn't get correct string while accessing alias table by key (%s)\n", keys[i]);
2347 }
374ca955 2348 }
2ca993e8 2349 for(i = 0; i < UPRV_LENGTHOF(strings); i++) {
729e4ab9
A
2350 result = tres_getString(tb, i, NULL, &resultLen, &status);
2351 if(U_FAILURE(status)){
2352 log_err("(2) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2353 continue;
2354 }
2355 uBufferLen = u_unescape(strings[i], uBuffer, 256);
2356 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2357 log_err("(2) Didn't get correct string while accesing alias table by index (%s)\n", strings[i]);
2358 }
73c04bcf 2359 }
2ca993e8 2360 for(i = 0; i < UPRV_LENGTHOF(strings); i++) {
729e4ab9
A
2361 result = ures_getNextString(tb, &resultLen, &key, &status);
2362 if(U_FAILURE(status)){
2363 log_err("(3) Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2364 continue;
2365 }
2366 uBufferLen = u_unescape(strings[i], uBuffer, 256);
2367 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2368 log_err("(3) Didn't get correct string while iterating over alias table (%s)\n", strings[i]);
2369 }
374ca955
A
2370 }
2371 }
2372 tb = ures_getByKey(aliasB, "testGetStringByIndexAliasing", tb, &status);
2373 if(U_FAILURE(status)) {
729e4ab9
A
2374 log_err("FAIL: Couldn't get testGetStringByIndexAliasing resource: %s\n", u_errorName(status));
2375 } else {
2ca993e8 2376 for(i = 0; i < UPRV_LENGTHOF(strings); i++) {
729e4ab9
A
2377 result = tres_getString(tb, i, NULL, &resultLen, &status);
2378 if(U_FAILURE(status)){
2379 log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2380 continue;
2381 }
2382 uBufferLen = u_unescape(strings[i], uBuffer, 256);
2383 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2384 log_err("Didn't get correct string while accesing alias by index in an array (%s)\n", strings[i]);
2385 }
73c04bcf 2386 }
2ca993e8 2387 for(i = 0; i < UPRV_LENGTHOF(strings); i++) {
729e4ab9
A
2388 result = ures_getNextString(tb, &resultLen, &key, &status);
2389 if(U_FAILURE(status)){
2390 log_err("Fetching the resource with key %s failed. Error: %s\n", keys[i], u_errorName(status));
2391 continue;
2392 }
2393 uBufferLen = u_unescape(strings[i], uBuffer, 256);
2394 if(result==NULL || resultLen != uBufferLen || u_strncmp(result, uBuffer, resultLen) != 0) {
2395 log_err("Didn't get correct string while iterating over aliases in an array (%s)\n", strings[i]);
2396 }
374ca955
A
2397 }
2398 }
2399 }
73c04bcf
A
2400 tb = ures_getByKey(aliasB, "testAliasToTree", tb, &status);
2401 if(U_FAILURE(status)){
2402 log_err("Fetching the resource with key \"testAliasToTree\" failed. Error: %s\n", u_errorName(status));
46f4442e 2403 goto cleanup;
73c04bcf
A
2404 }
2405 if (strcmp(ures_getKey(tb), "collations") != 0) {
2406 log_err("ures_getKey(aliasB) unexpectedly returned %s instead of \"collations\"\n", ures_getKey(tb));
2407 }
374ca955
A
2408cleanup:
2409 ures_close(aliasB);
2410 ures_close(tb);
2411 ures_close(en);
2412 ures_close(uk);
2413 ures_close(testtypes);
2414}
b75a7d8f 2415
374ca955
A
2416static void TestDirectAccess(void) {
2417 UErrorCode status = U_ZERO_ERROR;
2418 UResourceBundle *t = NULL, *t2 = NULL;
2419 const char* key = NULL;
b75a7d8f 2420
374ca955
A
2421 char buffer[100];
2422 char *s;
73c04bcf
A
2423 /*const char* testdatapath=loadTestData(&status);
2424 if(U_FAILURE(status)){
2425 log_err("Could not load testdata.dat %s \n",myErrorName(status));
2426 return;
2427 }*/
b75a7d8f 2428
73c04bcf 2429 t = ures_findResource("/testdata/te/zoneStrings/3/2", t, &status);
374ca955 2430 if(U_FAILURE(status)) {
46f4442e 2431 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
374ca955
A
2432 status = U_ZERO_ERROR;
2433 } else {
2434 key = ures_getKey(t);
2435 if(key != NULL) {
2436 log_err("Got a strange key, expected NULL, got %s\n", key);
2437 }
b75a7d8f 2438 }
73c04bcf 2439 t = ures_findResource("en/calendar/gregorian/DateTimePatterns/3", t, &status);
374ca955 2440 if(U_FAILURE(status)) {
46f4442e 2441 log_data_err("Couldn't access indexed resource, error %s\n", u_errorName(status));
374ca955
A
2442 status = U_ZERO_ERROR;
2443 } else {
2444 key = ures_getKey(t);
2445 if(key != NULL) {
2446 log_err("Got a strange key, expected NULL, got %s\n", key);
2447 }
b75a7d8f 2448 }
b75a7d8f 2449
b331163b 2450 t = ures_findResource("ja/ExemplarCharacters", t, &status);
374ca955 2451 if(U_FAILURE(status)) {
46f4442e 2452 log_data_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
374ca955
A
2453 status = U_ZERO_ERROR;
2454 } else {
2455 key = ures_getKey(t);
b331163b
A
2456 if(strcmp(key, "ExemplarCharacters")!=0) {
2457 log_err("Got a strange key, expected 'ExemplarCharacters', got %s\n", key);
374ca955 2458 }
b75a7d8f 2459 }
374ca955 2460
729e4ab9 2461 t2 = ures_open(U_ICUDATA_LANG, "sr", &status);
374ca955 2462 if(U_FAILURE(status)) {
729e4ab9 2463 log_err_status(status, "Couldn't open 'sr' resource bundle, error %s\n", u_errorName(status));
374ca955
A
2464 log_data_err("No 'sr', no test - you have bigger problems than testing direct access. "
2465 "You probably have no data! Aborting this test\n");
b75a7d8f 2466 }
374ca955
A
2467
2468 if(U_SUCCESS(status)) {
2469 strcpy(buffer, "Languages/hr");
2470 s = buffer;
2471 t = ures_findSubResource(t2, s, t, &status);
2472 if(U_FAILURE(status)) {
2473 log_err("Couldn't access keyed resource, error %s\n", u_errorName(status));
2474 status = U_ZERO_ERROR;
2475 } else {
2476 key = ures_getKey(t);
2477 if(strcmp(key, "hr")!=0) {
2478 log_err("Got a strange key, expected 'hr', got %s\n", key);
2479 }
2480 }
b75a7d8f
A
2481 }
2482
73c04bcf 2483 t = ures_findResource("root/calendar/islamic-civil/DateTime", t, &status);
374ca955 2484 if(U_SUCCESS(status)) {
46f4442e 2485 log_data_err("This resource does not exist. How did it get here?\n");
374ca955
A
2486 }
2487 status = U_ZERO_ERROR;
b75a7d8f 2488
374ca955
A
2489 /* this one will freeze */
2490 t = ures_findResource("root/calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera", t, &status);
2491 if(U_SUCCESS(status)) {
46f4442e 2492 log_data_err("Second resource does not exist. How did it get here?\n");
b75a7d8f 2493 }
374ca955 2494 status = U_ZERO_ERROR;
b75a7d8f 2495
374ca955
A
2496 ures_close(t2);
2497 t2 = ures_open(NULL, "he", &status);
2498 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2499 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
73c04bcf 2500 t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
374ca955
A
2501 if(U_SUCCESS(status)) {
2502 log_err("This resource does not exist. How did it get here?\n");
2503 }
2504 status = U_ZERO_ERROR;
b75a7d8f 2505
374ca955
A
2506 ures_close(t2);
2507 t2 = ures_open(NULL, "he", &status);
2508 /* George's fix */
2509 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2510 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
2511 t2 = ures_getByKeyWithFallback(t2, "eras", t2, &status);
b75a7d8f 2512 if(U_FAILURE(status)) {
729e4ab9 2513 log_err_status(status, "Didn't get Eras. I know they are there!\n");
b75a7d8f 2514 }
374ca955
A
2515 status = U_ZERO_ERROR;
2516
2517 ures_close(t2);
2518 t2 = ures_open(NULL, "root", &status);
2519 t2 = ures_getByKeyWithFallback(t2, "calendar", t2, &status);
2520 t2 = ures_getByKeyWithFallback(t2, "islamic-civil", t2, &status);
73c04bcf 2521 t2 = ures_getByKeyWithFallback(t2, "DateTime", t2, &status);
374ca955
A
2522 if(U_SUCCESS(status)) {
2523 log_err("This resource does not exist. How did it get here?\n");
b75a7d8f 2524 }
374ca955
A
2525 status = U_ZERO_ERROR;
2526
2527 ures_close(t2);
2528 ures_close(t);
2529}
2530
51004dcb
A
2531static void TestTicket9804(void) {
2532 UErrorCode status = U_ZERO_ERROR;
2533 UResourceBundle *t = NULL;
2534 t = ures_open(NULL, "he", &status);
2535 t = ures_getByKeyWithFallback(t, "calendar/islamic-civil/DateTime", t, &status);
2536 if(U_SUCCESS(status)) {
2537 log_err("This resource does not exist. How did it get here?\n");
2538 }
2539 status = U_ZERO_ERROR;
2540 ures_close(t);
2541 t = ures_open(NULL, "he", &status);
2542 t = ures_getByKeyWithFallback(t, "calendar/islamic-civil/eras", t, &status);
2543 if(U_FAILURE(status)) {
2544 log_err_status(status, "Didn't get Eras. I know they are there!\n");
2545 } else {
2546 const char *locale = ures_getLocaleByType(t, ULOC_ACTUAL_LOCALE, &status);
2547 if (uprv_strcmp("he", locale) != 0) {
2548 log_err("Eras should be in the 'he' locale, but was in: %s", locale);
2549 }
2550 }
2551 status = U_ZERO_ERROR;
2552 ures_close(t);
2553}
2554
374ca955
A
2555static void TestJB3763(void) {
2556 /* Nasty bug prevented using parent as fill-in, since it would
2557 * stomp the path information.
2558 */
2559 UResourceBundle *t = NULL;
2560 UErrorCode status = U_ZERO_ERROR;
2561 t = ures_open(NULL, "sr_Latn", &status);
2562 t = ures_getByKeyWithFallback(t, "calendar", t, &status);
2563 t = ures_getByKeyWithFallback(t, "gregorian", t, &status);
2564 t = ures_getByKeyWithFallback(t, "AmPmMarkers", t, &status);
b75a7d8f 2565 if(U_FAILURE(status)) {
729e4ab9 2566 log_err_status(status, "This resource should be available?\n");
b75a7d8f 2567 }
374ca955
A
2568 status = U_ZERO_ERROR;
2569
2570 ures_close(t);
b75a7d8f 2571
b75a7d8f
A
2572}
2573
374ca955 2574static void TestGetKeywordValues(void) {
46f4442e
A
2575 UEnumeration *kwVals;
2576 UBool foundStandard = FALSE;
2577 UErrorCode status = U_ZERO_ERROR;
2578 const char *kw;
374ca955 2579#if !UCONFIG_NO_COLLATION
46f4442e 2580 kwVals = ures_getKeywordValues( U_ICUDATA_COLL, "collations", &status);
b75a7d8f 2581
46f4442e
A
2582 log_verbose("Testing getting collation keyword values:\n");
2583
2584 while((kw=uenum_next(kwVals, NULL, &status))) {
2585 log_verbose(" %s\n", kw);
2586 if(!strcmp(kw,"standard")) {
2587 if(foundStandard == FALSE) {
2588 foundStandard = TRUE;
2589 } else {
2590 log_err("'standard' was found twice in the keyword list.\n");
2591 }
2592 }
b75a7d8f 2593 }
46f4442e 2594 if(foundStandard == FALSE) {
729e4ab9 2595 log_err_status(status, "'standard' was not found in the keyword list.\n");
46f4442e
A
2596 }
2597 uenum_close(kwVals);
2598 if(U_FAILURE(status)) {
729e4ab9 2599 log_err_status(status, "err %s getting collation values\n", u_errorName(status));
46f4442e
A
2600 }
2601 status = U_ZERO_ERROR;
374ca955 2602#endif
46f4442e
A
2603 foundStandard = FALSE;
2604 kwVals = ures_getKeywordValues( "ICUDATA", "calendar", &status);
b75a7d8f 2605
46f4442e
A
2606 log_verbose("Testing getting calendar keyword values:\n");
2607
2608 while((kw=uenum_next(kwVals, NULL, &status))) {
2609 log_verbose(" %s\n", kw);
2610 if(!strcmp(kw,"japanese")) {
2611 if(foundStandard == FALSE) {
2612 foundStandard = TRUE;
2613 } else {
2614 log_err("'japanese' was found twice in the calendar keyword list.\n");
2615 }
2616 }
2617 }
2618 if(foundStandard == FALSE) {
729e4ab9 2619 log_err_status(status, "'japanese' was not found in the calendar keyword list.\n");
46f4442e
A
2620 }
2621 uenum_close(kwVals);
2622 if(U_FAILURE(status)) {
729e4ab9 2623 log_err_status(status, "err %s getting calendar values\n", u_errorName(status));
b75a7d8f 2624 }
374ca955 2625}
b75a7d8f 2626
46f4442e
A
2627static void TestGetFunctionalEquivalentOf(const char *path, const char *resName, const char *keyword, UBool truncate, const char * const testCases[]) {
2628 int32_t i;
2629 for(i=0;testCases[i];i+=3) {
2630 UBool expectAvail = (testCases[i][0]=='t')?TRUE:FALSE;
2631 UBool gotAvail = FALSE;
2632 const char *inLocale = testCases[i+1];
2633 const char *expectLocale = testCases[i+2];
2634 char equivLocale[256];
2635 int32_t len;
2636 UErrorCode status = U_ZERO_ERROR;
2637 log_verbose("%d: %c %s\texpect %s\n",i/3, expectAvail?'t':'f', inLocale, expectLocale);
2638 len = ures_getFunctionalEquivalent(equivLocale, 255, path,
2639 resName, keyword, inLocale,
2640 &gotAvail, truncate, &status);
2641 if(U_FAILURE(status) || (len <= 0)) {
729e4ab9 2642 log_err_status(status, "FAIL: got len %d, err %s on #%d: %c\t%s\t%s\n",
46f4442e
A
2643 len, u_errorName(status),
2644 i/3,expectAvail?'t':'f', inLocale, expectLocale);
2645 } else {
2646 log_verbose("got: %c %s\n", expectAvail?'t':'f',equivLocale);
374ca955 2647
46f4442e 2648 if((gotAvail != expectAvail) || strcmp(equivLocale, expectLocale)) {
f3c0d7a5
A
2649 log_err("FAIL: #%d: %s -> expect avail=%c, loc=%s but get %c, loc=%s\n",
2650 i/3, inLocale,
2651 expectAvail?'t':'f', expectLocale,
2652 gotAvail?'t':'f', equivLocale);
46f4442e
A
2653
2654 }
2655 }
b75a7d8f 2656 }
374ca955 2657}
b75a7d8f 2658
374ca955 2659static void TestGetFunctionalEquivalent(void) {
57a6839d 2660#if !UCONFIG_NO_COLLATION
46f4442e 2661 static const char * const collCases[] = {
f3c0d7a5 2662 /* avail locale equiv */
3d1f044b 2663 /* note: in ICU 64, empty locales are shown as available for collation */
57a6839d 2664 "f", "sv_US_CALIFORNIA", "sv",
46f4442e 2665 "f", "zh_TW@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_TW */
3d1f044b 2666 "t", "zh_Hant_TW@collation=stroke", "zh@collation=stroke",
57a6839d 2667 "f", "sv_CN@collation=pinyin", "sv",
46f4442e
A
2668 "t", "zh@collation=pinyin", "zh",
2669 "f", "zh_CN@collation=pinyin", "zh", /* alias of zh_Hans_CN */
3d1f044b 2670 "t", "zh_Hans_CN@collation=pinyin", "zh",
46f4442e 2671 "f", "zh_HK@collation=pinyin", "zh", /* alias of zh_Hant_HK */
3d1f044b 2672 "t", "zh_Hant_HK@collation=pinyin", "zh",
46f4442e 2673 "f", "zh_HK@collation=stroke", "zh@collation=stroke", /* alias of zh_Hant_HK */
3d1f044b 2674 "t", "zh_Hant_HK@collation=stroke", "zh@collation=stroke",
46f4442e 2675 "f", "zh_HK", "zh@collation=stroke", /* alias of zh_Hant_HK */
3d1f044b 2676 "t", "zh_Hant_HK", "zh@collation=stroke",
46f4442e 2677 "f", "zh_MO", "zh@collation=stroke", /* alias of zh_Hant_MO */
3d1f044b 2678 "t", "zh_Hant_MO", "zh@collation=stroke",
46f4442e 2679 "f", "zh_TW_STROKE", "zh@collation=stroke",
b331163b 2680 "f", "zh_TW_STROKE@collation=pinyin", "zh",
57a6839d
A
2681 "f", "sv_CN@calendar=japanese", "sv",
2682 "t", "sv@calendar=japanese", "sv",
b331163b 2683 "f", "zh_TW@collation=pinyin", "zh", /* alias of zh_Hant_TW */
3d1f044b 2684 "t", "zh_Hant_TW@collation=pinyin", "zh",
b331163b 2685 "f", "zh_CN@collation=stroke", "zh@collation=stroke", /* alias of zh_Hans_CN */
3d1f044b 2686 "t", "zh_Hans_CN@collation=stroke", "zh@collation=stroke",
b331163b 2687 "t", "de@collation=phonebook", "de@collation=phonebook",
46f4442e 2688 "t", "hi@collation=standard", "hi",
46f4442e 2689 "f", "hi_AU@collation=standard;currency=CHF;calendar=buddhist", "hi",
57a6839d
A
2690 "f", "sv_SE@collation=pinyin", "sv", /* bug 4582 tests */
2691 "f", "sv_SE_BONN@collation=pinyin", "sv",
46f4442e 2692 "t", "nl", "root",
57a6839d 2693 "f", "nl_NL", "root",
46f4442e
A
2694 "f", "nl_NL_EEXT", "root",
2695 "t", "nl@collation=stroke", "root",
57a6839d 2696 "f", "nl_NL@collation=stroke", "root",
46f4442e 2697 "f", "nl_NL_EEXT@collation=stroke", "root",
f3c0d7a5
A
2698 /* Additions to test aliased locales */
2699 "f", "yue_HK", "zh@collation=stroke",
2700 "f", "yue_Hant", "zh@collation=stroke",
2701 "f", "yue_Hant_HK", "zh@collation=stroke",
2702 "f", "yue@collation=stroke", "zh@collation=stroke",
2703 "f", "yue@collation=pinyin", "zh",
2704 "f", "yue_CN", "zh",
2705 "f", "yue_Hans", "zh",
2706 "f", "yue_Hans_CN", "zh",
2707 "f", "yue_Hans@collation=pinyin", "zh",
2708 "f", "yue_Hans@collation=stroke", "zh@collation=stroke",
3d1f044b 2709 "f", "mo", "mo", /* ? */
f3c0d7a5
A
2710 "f", "no", "no", /* ? */
2711 "f", "ars", "ars", /* ? */
340931cb 2712 "f", "wuu", "wuu", /* ? */
f3c0d7a5
A
2713 /* Additions to test locales without resources */
2714 "f", "en_CN", "root",
2715 "f", "zh_Hant_CN", "zh@collation=stroke",
2716 "f", "zh_Hant_US", "zh@collation=stroke",
2717 "f", "zh_Hans_US", "zh",
2718 "f", "yue_TW", "zh@collation=stroke",
2719 "f", "yue_US", "zh@collation=stroke",
2720 "f", "ja_CN", "ja",
2721 "f", "ja_US", "ja",
46f4442e
A
2722 NULL
2723 };
57a6839d 2724#endif /* !UCONFIG_NO_COLLATION */
46f4442e
A
2725
2726 static const char *calCases[] = {
f3c0d7a5 2727 /* avail locale equiv */
4388f060
A
2728 "t", "en_US_POSIX", "en@calendar=gregorian",
2729 "f", "ja_JP_TOKYO", "ja@calendar=gregorian",
46f4442e
A
2730 "f", "ja_JP_TOKYO@calendar=japanese", "ja@calendar=japanese",
2731 "t", "sr@calendar=gregorian", "sr@calendar=gregorian",
2732 "t", "en", "en@calendar=gregorian",
2733 NULL
2734 };
2735
374ca955 2736#if !UCONFIG_NO_COLLATION
46f4442e 2737 TestGetFunctionalEquivalentOf(U_ICUDATA_COLL, "collations", "collation", TRUE, collCases);
374ca955 2738#endif
46f4442e 2739 TestGetFunctionalEquivalentOf("ICUDATA", "calendar", "calendar", FALSE, calCases);
374ca955
A
2740
2741#if !UCONFIG_NO_COLLATION
46f4442e
A
2742 log_verbose("Testing error conditions:\n");
2743 {
2744 char equivLocale[256] = "???";
2745 int32_t len;
2746 UErrorCode status = U_ZERO_ERROR;
2747 UBool gotAvail = FALSE;
374ca955 2748
46f4442e
A
2749 len = ures_getFunctionalEquivalent(equivLocale, 255, U_ICUDATA_COLL,
2750 "calendar", "calendar", "ar_EG@calendar=islamic",
2751 &gotAvail, FALSE, &status);
57a6839d 2752 (void)len; /* Suppress set but not used warning. */
374ca955 2753
46f4442e
A
2754 if(status == U_MISSING_RESOURCE_ERROR) {
2755 log_verbose("PASS: Got expected U_MISSING_RESOURCE_ERROR\n");
2756 } else {
2757 log_err("ures_getFunctionalEquivalent returned locale %s, avail %c, err %s, but expected U_MISSING_RESOURCE_ERROR \n",
2758 equivLocale, gotAvail?'t':'f', u_errorName(status));
2759 }
374ca955 2760 }
374ca955 2761#endif
b75a7d8f 2762}
374ca955 2763
73c04bcf
A
2764static void TestXPath(void) {
2765 UErrorCode status = U_ZERO_ERROR;
2766 UResourceBundle *rb = NULL, *alias = NULL;
2767 int32_t len = 0;
2768 const UChar* result = NULL;
2769 const UChar expResult[] = { 0x0063, 0x006F, 0x0072, 0x0072, 0x0065, 0x0063, 0x0074, 0x0000 }; /* "correct" */
2770 /*const UChar expResult[] = { 0x0074, 0x0065, 0x0069, 0x006E, 0x0064, 0x0065, 0x0073, 0x0074, 0x0000 }; *//*teindest*/
2771
2772 const char *testdatapath=loadTestData(&status);
2773 if(U_FAILURE(status))
2774 {
46f4442e 2775 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
73c04bcf
A
2776 return;
2777 }
2778
2779 log_verbose("Testing ures_open()......\n");
2780
2781 rb = ures_open(testdatapath, "te_IN", &status);
2782 if(U_FAILURE(status)) {
2783 log_err("Could not open te_IN (%s)\n", myErrorName(status));
2784 return;
2785 }
2786 alias = ures_getByKey(rb, "rootAliasClient", alias, &status);
2787 if(U_FAILURE(status)) {
2788 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2789 ures_close(rb);
2790 return;
2791 }
2792
2793 result = tres_getString(alias, -1, NULL, &len, &status);
2794 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2795 log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2796 }
2797
2798 alias = ures_getByKey(rb, "aliasClient", alias, &status);
2799 if(U_FAILURE(status)) {
2800 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2801 ures_close(rb);
2802 return;
2803 }
2804
2805 result = tres_getString(alias, -1, NULL, &len, &status);
2806 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2807 log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2808 }
2809
2810 alias = ures_getByKey(rb, "nestedRootAliasClient", alias, &status);
2811 if(U_FAILURE(status)) {
2812 log_err("Couldn't find the aliased resource (%s)\n", myErrorName(status));
2813 ures_close(rb);
2814 return;
2815 }
2816
2817 result = tres_getString(alias, -1, NULL, &len, &status);
2818 if(U_FAILURE(status) || result == NULL || u_strcmp(result, expResult)) {
2819 log_err("Couldn't get correct string value (%s)\n", myErrorName(status));
2820 }
2821
2822 ures_close(alias);
2823 ures_close(rb);
2824}
2825static void TestCLDRStyleAliases(void) {
2826 UErrorCode status = U_ZERO_ERROR;
2827 UResourceBundle *rb = NULL, *alias = NULL, *a=NULL;
2828 int32_t i, len;
2829 char resource[256];
2830 const UChar *result = NULL;
2831 UChar expected[256];
2832 const char *expects[7] = { "", "a41", "a12", "a03", "ar4" };
2833 const char *testdatapath=loadTestData(&status);
2834 if(U_FAILURE(status)) {
46f4442e 2835 log_data_err("Could not load testdata.dat %s \n",myErrorName(status));
73c04bcf
A
2836 return;
2837 }
2838 log_verbose("Testing CLDR style aliases......\n");
2839
2840 rb = ures_open(testdatapath, "te_IN_REVISED", &status);
2841 if(U_FAILURE(status)) {
2842 log_err("Could not open te_IN (%s)\n", myErrorName(status));
2843 return;
2844 }
2845 alias = ures_getByKey(rb, "a", alias, &status);
2846 if(U_FAILURE(status)) {
2847 log_err("Couldn't find the aliased with name \"a\" resource (%s)\n", myErrorName(status));
2848 ures_close(rb);
2849 return;
2850 }
2851 for(i = 1; i < 5 ; i++) {
2852 resource[0]='a';
2853 resource[1]='0'+i;
2854 resource[2]=0;
2855 /* instead of sprintf(resource, "a%i", i); */
2856 a = ures_getByKeyWithFallback(alias, resource, a, &status);
2857 result = tres_getString(a, -1, NULL, &len, &status);
3d1f044b 2858 u_charsToUChars(expects[i], expected, (int32_t)strlen(expects[i])+1);
73c04bcf
A
2859 if(U_FAILURE(status) || !result || u_strcmp(result, expected)) {
2860 log_err("CLDR style aliases failed resource with name \"%s\" resource, exp %s, got %S (%s)\n", resource, expects[i], result, myErrorName(status));
2861 status = U_ZERO_ERROR;
2862 }
2863 }
2864
2865 ures_close(a);
2866 ures_close(alias);
2867 ures_close(rb);
2868}
2869
2870static void TestFallbackCodes(void) {
2871 UErrorCode status = U_ZERO_ERROR;
2872 const char *testdatapath=loadTestData(&status);
2873
2874 UResourceBundle *res = ures_open(testdatapath, "te_IN", &status);
2875
2876 UResourceBundle *r = NULL, *fall = NULL;
2877
2878 r = ures_getByKey(res, "tagged_array_in_Root_te_te_IN", r, &status);
2879
2880 status = U_ZERO_ERROR;
2881 fall = ures_getByKeyWithFallback(r, "tag2", fall, &status);
2882
2883 if(status != U_ZERO_ERROR) {
46f4442e 2884 log_data_err("Expected error code to be U_ZERO_ERROR, got %s\n", u_errorName(status));
73c04bcf
A
2885 status = U_ZERO_ERROR;
2886 }
2887
2888 fall = ures_getByKeyWithFallback(r, "tag7", fall, &status);
2889
2890 if(status != U_USING_FALLBACK_WARNING) {
46f4442e 2891 log_data_err("Expected error code to be U_USING_FALLBACK_WARNING, got %s\n", u_errorName(status));
73c04bcf
A
2892 }
2893 status = U_ZERO_ERROR;
2894
2895 fall = ures_getByKeyWithFallback(r, "tag1", fall, &status);
2896
2897 if(status != U_USING_DEFAULT_WARNING) {
46f4442e 2898 log_data_err("Expected error code to be U_USING_DEFAULT_WARNING, got %s\n", u_errorName(status));
73c04bcf
A
2899 }
2900 status = U_ZERO_ERROR;
2901
2902 ures_close(fall);
2903 ures_close(r);
2904 ures_close(res);
2905}
2906
2907/* This test will crash if this doesn't work. Results don't need testing. */
2908static void TestStackReuse(void) {
2909 UResourceBundle table;
2910 UErrorCode errorCode = U_ZERO_ERROR;
2911 UResourceBundle *rb = ures_open(NULL, "en_US", &errorCode);
2912
2913 if(U_FAILURE(errorCode)) {
46f4442e 2914 log_data_err("Could not load en_US locale. status=%s\n",myErrorName(errorCode));
73c04bcf
A
2915 return;
2916 }
2917 ures_initStackObject(&table);
2918 ures_getByKeyWithFallback(rb, "Types", &table, &errorCode);
2919 ures_getByKeyWithFallback(&table, "collation", &table, &errorCode);
2920 ures_close(rb);
2921 ures_close(&table);
2922}
2923
2924/* Test ures_getUTF8StringXYZ() --------------------------------------------- */
2925
2926/*
2927 * Replace most ures_getStringXYZ() with this function which wraps the
2928 * desired call and also calls the UTF-8 variant and checks that it works.
2929 */
2930extern const UChar *
2931tres_getString(const UResourceBundle *resB,
51004dcb 2932 int32_t idx, const char *key,
73c04bcf
A
2933 int32_t *length,
2934 UErrorCode *status) {
2935 char buffer8[16];
2936 char *p8;
2937 const UChar *s16;
2938 const char *s8;
2939 UChar32 c16, c8;
2940 int32_t length16, length8, i16, i8;
2941 UBool forceCopy;
2942
2943 if(length == NULL) {
2944 length = &length16;
2945 }
51004dcb
A
2946 if(idx >= 0) {
2947 s16 = ures_getStringByIndex(resB, idx, length, status);
73c04bcf
A
2948 } else if(key != NULL) {
2949 s16 = ures_getStringByKey(resB, key, length, status);
2950 } else {
2951 s16 = ures_getString(resB, length, status);
2952 }
2953 if(U_FAILURE(*status)) {
2954 return s16;
2955 }
2956 length16 = *length;
2957
2958 /* try the UTF-8 variant of ures_getStringXYZ() */
2959 for(forceCopy = FALSE; forceCopy <= TRUE; ++forceCopy) {
2960 p8 = buffer8;
2961 length8 = (int32_t)sizeof(buffer8);
51004dcb
A
2962 if(idx >= 0) {
2963 s8 = ures_getUTF8StringByIndex(resB, idx, p8, &length8, forceCopy, status);
73c04bcf
A
2964 } else if(key != NULL) {
2965 s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2966 } else {
2967 s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2968 }
2969 if(*status == U_INVALID_CHAR_FOUND) {
2970 /* the UTF-16 string contains an unpaired surrogate, can't test UTF-8 variant */
2971 return s16;
2972 }
2973 if(*status == U_BUFFER_OVERFLOW_ERROR) {
2974 *status = U_ZERO_ERROR;
2975 p8 = (char *)malloc(++length8);
2976 if(p8 == NULL) {
2977 return s16;
2978 }
51004dcb
A
2979 if(idx >= 0) {
2980 s8 = ures_getUTF8StringByIndex(resB, idx, p8, &length8, forceCopy, status);
73c04bcf
A
2981 } else if(key != NULL) {
2982 s8 = ures_getUTF8StringByKey(resB, key, p8, &length8, forceCopy, status);
2983 } else {
2984 s8 = ures_getUTF8String(resB, p8, &length8, forceCopy, status);
2985 }
2986 }
2987 if(U_FAILURE(*status)) {
2988 /* something unexpected happened */
2989 if(p8 != buffer8) {
2990 free(p8);
2991 }
2992 return s16;
2993 }
2994
2995 if(forceCopy && s8 != p8) {
2996 log_err("ures_getUTF8String(%p, %ld, '%s') did not write the string to dest\n",
51004dcb 2997 resB, (long)idx, key);
73c04bcf
A
2998 }
2999
3000 /* verify NUL-termination */
340931cb 3001 if((p8 != buffer8 || length8 < (int32_t)sizeof(buffer8)) && s8[length8] != 0) {
73c04bcf 3002 log_err("ures_getUTF8String(%p, %ld, '%s') did not NUL-terminate\n",
51004dcb 3003 resB, (long)idx, key);
73c04bcf
A
3004 }
3005 /* verify correct string */
3006 i16 = i8 = 0;
3007 while(i16 < length16 && i8 < length8) {
3008 U16_NEXT(s16, i16, length16, c16);
3009 U8_NEXT(s8, i8, length8, c8);
3010 if(c16 != c8) {
3011 log_err("ures_getUTF8String(%p, %ld, '%s') got a bad string, c16=U+%04lx!=U+%04lx=c8 before i16=%ld\n",
51004dcb 3012 resB, (long)idx, key, (long)c16, (long)c8, (long)i16);
73c04bcf
A
3013 }
3014 }
3015 /* verify correct length */
3016 if(i16 < length16) {
3017 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too short, length8=%ld, length16=%ld\n",
51004dcb 3018 resB, (long)idx, key, (long)length8, (long)length16);
73c04bcf
A
3019 }
3020 if(i8 < length8) {
3021 log_err("ures_getUTF8String(%p, %ld, '%s') UTF-8 string too long, length8=%ld, length16=%ld\n",
51004dcb 3022 resB, (long)idx, key, (long)length8, (long)length16);
73c04bcf
A
3023 }
3024
3025 /* clean up */
3026 if(p8 != buffer8) {
3027 free(p8);
3028 }
3029 }
3030 return s16;
3031}
3032
3033/*
3034 * API tests for ures_getUTF8String().
3035 * Most cases are handled by tres_getString(), which leaves argument checking
3036 * to be tested here.
3037 * Since the variants share most of their implementation, we only need to test
3038 * one of them.
3039 * We also need not test for checking arguments which will be checked by the
3040 * UTF-16 ures_getStringXYZ() that are called internally.
3041 */
3042static void
3043TestGetUTF8String() {
3044 UResourceBundle *res;
3045 const char *testdatapath;
3046 char buffer8[16];
3047 const char *s8;
3048 int32_t length8;
3049 UErrorCode status;
3050
3051 status = U_ZERO_ERROR;
3052 testdatapath = loadTestData(&status);
3053 if(U_FAILURE(status)) {
46f4442e 3054 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
73c04bcf
A
3055 return;
3056 }
3057
3058 res = ures_open(testdatapath, "", &status);
3059 if(U_FAILURE(status)) {
3060 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status));
3061 return;
3062 }
3063
3064 /* one good call */
3065 status = U_ZERO_ERROR;
3066 length8 = (int32_t)sizeof(buffer8);
3067 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
57a6839d 3068 (void)s8; /* Suppress set but not used warning. */
73c04bcf
A
3069 if(status != U_ZERO_ERROR) {
3070 log_err("ures_getUTF8StringByKey(testdata/root string) malfunctioned - %s\n", u_errorName(status));
3071 }
3072
3073 /* negative capacity */
3074 status = U_ZERO_ERROR;
3075 length8 = -1;
3076 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", buffer8, &length8, FALSE, &status);
3077 if(status != U_ILLEGAL_ARGUMENT_ERROR) {
3078 log_err("ures_getUTF8StringByKey(capacity<0) malfunctioned - %s\n", u_errorName(status));
3079 }
3080
3081 /* capacity>0 but dest=NULL */
3082 status = U_ZERO_ERROR;
3083 length8 = (int32_t)sizeof(buffer8);
3084 s8 = ures_getUTF8StringByKey(res, "string_only_in_Root", NULL, &length8, FALSE, &status);
3085 if(status != U_ILLEGAL_ARGUMENT_ERROR) {
3086 log_err("ures_getUTF8StringByKey(dest=NULL capacity>0) malfunctioned - %s\n", u_errorName(status));
3087 }
3088
3089 ures_close(res);
3090}
729e4ab9
A
3091
3092static void TestCLDRVersion(void) {
3093 UVersionInfo zeroVersion;
3094 UVersionInfo testExpect;
3095 UVersionInfo testCurrent;
3096 UVersionInfo cldrVersion;
3097 char tmp[200];
3098 UErrorCode status = U_ZERO_ERROR;
3099
3100 /* setup the constant value */
3101 u_versionFromString(zeroVersion, "0.0.0.0");
3102
3103 /* test CLDR value from API */
3104 ulocdata_getCLDRVersion(cldrVersion, &status);
3105 if(U_FAILURE(status)) {
3106 /* the show is pretty much over at this point */
3107 log_err_status(status, "FAIL: ulocdata_getCLDRVersion() returned %s\n", u_errorName(status));
3108 return;
3109 } else {
3110 u_versionToString(cldrVersion, tmp);
3111 log_info("ulocdata_getCLDRVersion() returned: '%s'\n", tmp);
3112 }
3113
3114
3115 /* setup from resource bundle */
3116 {
3117 UResourceBundle *res;
3118 const char *testdatapath;
3119
3120 status = U_ZERO_ERROR;
3121 testdatapath = loadTestData(&status);
3122 if(U_FAILURE(status)) {
3123 log_data_err("Could not load testdata.dat - %s\n", u_errorName(status));
3124 return;
3125 }
3126
3127 res = ures_openDirect(testdatapath, "root", &status);
3128 if(U_FAILURE(status)) {
3129 log_err("Unable to ures_open(testdata, \"\") - %s\n", u_errorName(status
3130));
3131 return;
3132 }
3133 ures_getVersionByKey(res, "ExpectCLDRVersionAtLeast", testExpect, &status);
3134 ures_getVersionByKey(res, "CurrentCLDRVersion", testCurrent, &status);
3135 ures_close(res);
3136 if(U_FAILURE(status)) {
3137 log_err("Unable to get test data for CLDR version - %s\n", u_errorName(status));
3138 }
3139 }
3140 if(U_FAILURE(status)) return;
3141
3142
3143 u_versionToString(testExpect,tmp);
3144 log_verbose("(data) ExpectCLDRVersionAtLeast { %s }\n", tmp);
3145 if(memcmp(cldrVersion, testExpect, sizeof(UVersionInfo)) < 0) {
3146 log_data_err("CLDR version is too old, expect at least %s.", tmp);
3147 }
3148 u_versionToString(testCurrent,tmp);
3149 log_verbose("(data) CurrentCLDRVersion { %s }\n", tmp);
3150 switch(memcmp(cldrVersion, testCurrent, sizeof(UVersionInfo))) {
3151 case 0: break; /* OK- current. */
3152 case -1: log_info("CLDR version is behind 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3153 case 1: log_info("CLDR version is ahead of 'current' (for testdata/root.txt) %s. Some things may fail.\n", tmp); break;
3154 }
3155
3156}