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