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