]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/restest.cpp
ICU-6.2.13.tar.gz
[apple/icu.git] / icuSources / test / intltest / restest.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #include "cstring.h"
10 #include "unicode/unistr.h"
11 #include "unicode/uniset.h"
12 #include "unicode/resbund.h"
13 #include "restest.h"
14
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <limits.h>
19
20 //***************************************************************************************
21
22 static const UChar kErrorUChars[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
23 static const int32_t kErrorLength = 5;
24 static const int32_t kERROR_COUNT = -1234567;
25
26 //***************************************************************************************
27
28 enum E_Where
29 {
30 e_Root,
31 e_te,
32 e_te_IN,
33 e_Where_count
34 };
35
36 //***************************************************************************************
37
38 #define CONFIRM_EQ(actual, expected, myAction) if ((expected)==(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of " + (expected) + "\n");}
39 #define CONFIRM_GE(actual, expected, myAction) if ((actual)>=(expected)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x >= " + (expected) + "\n");}
40 #define CONFIRM_NE(actual, expected, myAction) if ((expected)!=(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + (actual) + (UnicodeString)" instead of x != " + (expected) + "\n");}
41
42 #define CONFIRM_UErrorCode(actual, expected, myAction) if ((expected)==(actual)) { record_pass(myAction); } else { record_fail(myAction + (UnicodeString)" returned " + u_errorName(actual) + " instead of " + u_errorName(expected) + "\n"); }
43
44 //***************************************************************************************
45
46 /**
47 * Convert an integer, positive or negative, to a character string radix 10.
48 */
49 char*
50 itoa(int32_t i, char* buf)
51 {
52 char* result = buf;
53
54 // Handle negative
55 if (i < 0)
56 {
57 *buf++ = '-';
58 i = -i;
59 }
60
61 // Output digits in reverse order
62 char* p = buf;
63 do
64 {
65 *p++ = (char)('0' + (i % 10));
66 i /= 10;
67 }
68 while (i);
69 *p-- = 0;
70
71 // Reverse the string
72 while (buf < p)
73 {
74 char c = *buf;
75 *buf++ = *p;
76 *p-- = c;
77 }
78
79 return result;
80 }
81
82
83
84 //***************************************************************************************
85
86 // Array of our test objects
87
88 static struct
89 {
90 const char* name;
91 Locale *locale;
92 UErrorCode expected_constructor_status;
93 E_Where where;
94 UBool like[e_Where_count];
95 UBool inherits[e_Where_count];
96 }
97 param[] =
98 {
99 // "te" means test
100 // "IN" means inherits
101 // "NE" or "ne" means "does not exist"
102
103 { "root", NULL, U_ZERO_ERROR, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
104 { "te", NULL, U_ZERO_ERROR, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
105 { "te_IN", NULL, U_ZERO_ERROR, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
106 { "te_NE", NULL, U_USING_FALLBACK_WARNING, e_te, { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE } },
107 { "te_IN_NE", NULL, U_USING_FALLBACK_WARNING, e_te_IN, { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE } },
108 { "ne", NULL, U_USING_DEFAULT_WARNING, e_Root, { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
109 };
110
111 static const int32_t bundles_count = sizeof(param) / sizeof(param[0]);
112
113 //***************************************************************************************
114
115 /**
116 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
117 */
118
119 uint32_t
120 randul()
121 {
122 static UBool initialized = FALSE;
123 if (!initialized)
124 {
125 srand((unsigned)time(NULL));
126 initialized = TRUE;
127 }
128 // Assume rand has at least 12 bits of precision
129 uint32_t l = 0;
130 for (uint32_t i=0; i<sizeof(l); ++i)
131 ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
132 return l;
133 }
134
135 /**
136 * Return a random double x where 0.0 <= x < 1.0.
137 */
138 double
139 randd()
140 {
141 return (double)(randul() / ULONG_MAX);
142 }
143
144 /**
145 * Return a random integer i where 0 <= i < n.
146 */
147 int32_t randi(int32_t n)
148 {
149 return (int32_t)(randd() * n);
150 }
151
152 //***************************************************************************************
153
154 /*
155 Don't use more than one of these at a time because of the Locale names
156 */
157 ResourceBundleTest::ResourceBundleTest()
158 : pass(0),
159 fail(0)
160 {
161 if (param[5].locale == NULL) {
162 param[0].locale = new Locale("root");
163 param[1].locale = new Locale("te");
164 param[2].locale = new Locale("te", "IN");
165 param[3].locale = new Locale("te", "NE");
166 param[4].locale = new Locale("te", "IN", "NE");
167 param[5].locale = new Locale("ne");
168 }
169 }
170
171 ResourceBundleTest::~ResourceBundleTest()
172 {
173 if (param[5].locale) {
174 int idx;
175 for (idx = 0; idx < (int)(sizeof(param)/sizeof(param[0])); idx++) {
176 delete param[idx].locale;
177 param[idx].locale = NULL;
178 }
179 }
180 }
181
182 void ResourceBundleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
183 {
184 if (exec) logln("TestSuite ResourceBundleTest: ");
185 switch (index) {
186 case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break;
187 case 1: name = "TestConstruction"; if (exec) TestConstruction(); break;
188 case 2: name = "TestExemplar"; if (exec) TestExemplar(); break;
189 case 3: name = "TestGetSize"; if (exec) TestGetSize(); break;
190 case 4: name = "TestGetLocaleByType"; if (exec) TestGetLocaleByType(); break;
191 default: name = ""; break; //needed to end loop
192 }
193 }
194
195 //***************************************************************************************
196
197 void
198 ResourceBundleTest::TestResourceBundles()
199 {
200 testTag("only_in_Root", TRUE, FALSE, FALSE);
201 testTag("only_in_te", FALSE, TRUE, FALSE);
202 testTag("only_in_te_IN", FALSE, FALSE, TRUE);
203 testTag("in_Root_te", TRUE, TRUE, FALSE);
204 testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
205 testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
206 testTag("in_te_te_IN", FALSE, TRUE, TRUE);
207 testTag("nonexistent", FALSE, FALSE, FALSE);
208 logln("Passed: %d\nFailed: %d", pass, fail);
209 }
210
211 void
212 ResourceBundleTest::TestConstruction()
213 {
214 {
215 UErrorCode err = U_ZERO_ERROR;
216 const char* testdatapath;
217 Locale locale("te", "IN");
218
219 testdatapath=loadTestData(err);
220 if(U_FAILURE(err))
221 {
222 errln("Could not load testdata.dat " + UnicodeString(testdatapath) + ", " + UnicodeString(u_errorName(err)));
223 return;
224 }
225 ResourceBundle test1((UnicodeString)testdatapath, err);
226 ResourceBundle test2(testdatapath, locale, err);
227 //ResourceBundle test1("c:\\icu\\icu\\source\\test\\testdata\\testdata", err);
228 //ResourceBundle test2("c:\\icu\\icu\\source\\test\\testdata\\testdata", locale, err);
229
230 UnicodeString result1(test1.getStringEx("string_in_Root_te_te_IN", err));
231 UnicodeString result2(test2.getStringEx("string_in_Root_te_te_IN", err));
232
233 if (U_FAILURE(err)) {
234 errln("Something threw an error in TestConstruction()");
235 return;
236 }
237
238 logln("for string_in_Root_te_te_IN, default.txt had " + result1);
239 logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
240
241 if (result1 != "ROOT" || result2 != "TE_IN")
242 errln("Construction test failed; run verbose for more information");
243
244 const char* version1;
245 const char* version2;
246
247 version1 = test1.getVersionNumber();
248 version2 = test2.getVersionNumber();
249
250 char *versionID1 = new char[1+strlen(version1)]; // + 1 for zero byte
251 char *versionID2 = new char[1+ strlen(version2)]; // + 1 for zero byte
252
253 strcpy(versionID1, "44.0"); // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
254
255 strcpy(versionID2, "55.0"); // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
256
257 logln(UnicodeString("getVersionNumber on default.txt returned ") + version1);
258 logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2);
259
260 if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0)
261 errln("getVersionNumber() failed");
262
263 delete[] versionID1;
264 delete[] versionID2;
265 }
266 }
267
268 //***************************************************************************************
269
270 UBool
271 ResourceBundleTest::testTag(const char* frag,
272 UBool in_Root,
273 UBool in_te,
274 UBool in_te_IN)
275 {
276 int32_t failOrig = fail;
277
278 // Make array from input params
279
280 UBool is_in[] = { in_Root, in_te, in_te_IN };
281
282 const char* NAME[] = { "ROOT", "TE", "TE_IN" };
283
284 // Now try to load the desired items
285
286 char tag[100];
287 UnicodeString action;
288
289 int32_t i,j,actual_bundle;
290 // int32_t row,col;
291 int32_t index;
292 UErrorCode status = U_ZERO_ERROR;
293 const char* testdatapath;
294 testdatapath=loadTestData(status);
295 if(U_FAILURE(status))
296 {
297 errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
298 return FALSE;
299 }
300
301 for (i=0; i<bundles_count; ++i)
302 {
303 action = "Constructor for ";
304 action += param[i].name;
305
306 status = U_ZERO_ERROR;
307 ResourceBundle theBundle( testdatapath, *param[i].locale, status);
308 //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
309 CONFIRM_UErrorCode(status, param[i].expected_constructor_status, action);
310
311 if(i == 5)
312 actual_bundle = 0; /* ne -> default */
313 else if(i == 3)
314 actual_bundle = 1; /* te_NE -> te */
315 else if(i == 4)
316 actual_bundle = 2; /* te_IN_NE -> te_IN */
317 else
318 actual_bundle = i;
319
320
321 UErrorCode expected_resource_status = U_MISSING_RESOURCE_ERROR;
322 for (j=e_te_IN; j>=e_Root; --j)
323 {
324 if (is_in[j] && param[i].inherits[j])
325 {
326 if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
327 expected_resource_status = U_ZERO_ERROR;
328 else if(j == 0)
329 expected_resource_status = U_USING_DEFAULT_WARNING;
330 else
331 expected_resource_status = U_USING_FALLBACK_WARNING;
332
333 break;
334 }
335 }
336
337 UErrorCode expected_status;
338
339 UnicodeString base;
340 for (j=param[i].where; j>=0; --j)
341 {
342 if (is_in[j])
343 {
344 base = NAME[j];
345 break;
346 }
347 }
348
349 //--------------------------------------------------------------------------
350 // string
351
352 uprv_strcpy(tag, "string_");
353 uprv_strcat(tag, frag);
354
355 action = param[i].name;
356 action += ".getString(";
357 action += tag;
358 action += ")";
359
360
361 status = U_ZERO_ERROR;
362
363 UnicodeString string(theBundle.getStringEx(tag, status));
364
365 if(U_FAILURE(status)) {
366 string.setTo(TRUE, kErrorUChars, kErrorLength);
367 }
368
369 CONFIRM_UErrorCode(status, expected_resource_status, action);
370
371 UnicodeString expected_string(kErrorUChars);
372 if (U_SUCCESS(status)) {
373 expected_string = base;
374 }
375
376 CONFIRM_EQ(string, expected_string, action);
377
378 //--------------------------------------------------------------------------
379 // array
380
381 uprv_strcpy(tag, "array_");
382 uprv_strcat(tag, frag);
383
384 action = param[i].name;
385 action += ".get(";
386 action += tag;
387 action += ")";
388
389 status = U_ZERO_ERROR;
390 ResourceBundle arrayBundle(theBundle.get(tag, status));
391 CONFIRM_UErrorCode(status, expected_resource_status, action);
392 int32_t count = arrayBundle.getSize();
393
394 if (U_SUCCESS(status))
395 {
396 CONFIRM_GE(count, 1, action);
397
398 for (j=0; j < count; ++j)
399 {
400 char buf[32];
401 UnicodeString value(arrayBundle.getStringEx(j, status));
402 expected_string = base;
403 expected_string += itoa(j,buf);
404 CONFIRM_EQ(value, expected_string, action);
405 }
406
407 action = param[i].name;
408 action += ".getStringEx(";
409 action += tag;
410 action += ")";
411
412 for (j=0; j<100; ++j)
413 {
414 index = count ? (randi(count * 3) - count) : (randi(200) - 100);
415 status = U_ZERO_ERROR;
416 string = kErrorUChars;
417 UnicodeString t(arrayBundle.getStringEx(index, status));
418 expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
419 CONFIRM_UErrorCode(status, expected_status, action);
420
421 if (U_SUCCESS(status))
422 {
423 char buf[32];
424 expected_string = base;
425 expected_string += itoa(index,buf);
426 }
427 else
428 {
429 expected_string = kErrorUChars;
430 }
431 CONFIRM_EQ(string, expected_string, action);
432 }
433 }
434 else if (status != expected_resource_status)
435 {
436 record_fail("Error getting " + (UnicodeString)tag);
437 return (UBool)(failOrig != fail);
438 }
439
440 }
441
442 return (UBool)(failOrig != fail);
443 }
444
445 void
446 ResourceBundleTest::record_pass(UnicodeString passMessage)
447 {
448 logln(passMessage);
449 ++pass;
450 }
451 void
452 ResourceBundleTest::record_fail(UnicodeString errMessage)
453 {
454 err(errMessage);
455 ++fail;
456 }
457
458 void
459 ResourceBundleTest::TestExemplar(){
460
461 int32_t locCount = uloc_countAvailable();
462 int32_t locIndex=0;
463 int num=0;
464 UErrorCode status = U_ZERO_ERROR;
465 for(;locIndex<locCount;locIndex++){
466 const char* locale = uloc_getAvailable(locIndex);
467 UResourceBundle *resb =ures_open(NULL,locale,&status);
468 if(U_SUCCESS(status) && status!=U_USING_FALLBACK_WARNING && status!=U_USING_DEFAULT_WARNING){
469 int32_t len=0;
470 const UChar* strSet = ures_getStringByKey(resb,"ExemplarCharacters",&len,&status);
471 UnicodeSet set(strSet,status);
472 if(U_FAILURE(status)){
473 errln("Could not construct UnicodeSet from pattern for ExemplarCharacters in locale : %s. Error: %s",locale,u_errorName(status));
474 status=U_ZERO_ERROR;
475 }
476 num++;
477 }
478 ures_close(resb);
479 }
480 logln("Number of installed locales with exemplar characters that could be tested: %d",num);
481
482 }
483
484 void
485 ResourceBundleTest::TestGetSize(void)
486 {
487 const struct {
488 const char* key;
489 int32_t size;
490 } test[] = {
491 { "zerotest", 1},
492 { "one", 1},
493 { "importtest", 1},
494 { "integerarray", 1},
495 { "emptyarray", 0},
496 { "emptytable", 0},
497 { "emptystring", 1}, /* empty string is still a string */
498 { "emptyint", 1},
499 { "emptybin", 1},
500 { "testinclude", 1},
501 { "collations", 1}, /* not 2 - there is hidden %%CollationBin */
502 };
503
504 UErrorCode status = U_ZERO_ERROR;
505
506 const char* testdatapath = loadTestData(status);
507 int32_t i = 0, j = 0;
508 int32_t size = 0;
509
510 if(U_FAILURE(status))
511 {
512 err("Could not load testdata.dat %s\n", u_errorName(status));
513 return;
514 }
515
516 ResourceBundle rb(testdatapath, "testtypes", status);
517 if(U_FAILURE(status))
518 {
519 err("Could not testtypes resource bundle %s\n", u_errorName(status));
520 return;
521 }
522
523 for(i = 0; i < (int32_t)(sizeof(test)/sizeof(test[0])); i++) {
524 ResourceBundle res = rb.get(test[i].key, status);
525 if(U_FAILURE(status))
526 {
527 err("Couldn't find the key %s. Error: %s\n", u_errorName(status));
528 return;
529 }
530 size = res.getSize();
531 if(size != test[i].size) {
532 err("Expected size %i, got size %i for key %s\n", test[i].size, size, test[i].key);
533 for(j = 0; j < size; j++) {
534 ResourceBundle helper = res.get(j, status);
535 err("%s\n", helper.getKey());
536 }
537 }
538 }
539 }
540
541 void
542 ResourceBundleTest::TestGetLocaleByType(void)
543 {
544 const struct {
545 const char *requestedLocale;
546 const char *resourceKey;
547 const char *validLocale;
548 const char *actualLocale;
549 } test[] = {
550 { "te_IN_BLAH", "string_only_in_te_IN", "te_IN", "te_IN" },
551 { "te_IN_BLAH", "string_only_in_te", "te_IN", "te" },
552 { "te_IN_BLAH", "string_only_in_Root", "te_IN", "root" },
553 { "te_IN_BLAH_01234567890_01234567890_01234567890_01234567890_01234567890_01234567890", "array_2d_only_in_Root", "te_IN", "root" },
554 { "te_IN_BLAH@currency=euro", "array_2d_only_in_te_IN", "te_IN", "te_IN" },
555 { "te_IN_BLAH@calendar=thai;collation=phonebook", "array_2d_only_in_te", "te_IN", "te" }
556 };
557
558 UErrorCode status = U_ZERO_ERROR;
559
560 const char* testdatapath = loadTestData(status);
561 int32_t i = 0;
562 Locale locale;
563
564 if(U_FAILURE(status))
565 {
566 err("Could not load testdata.dat %s\n", u_errorName(status));
567 return;
568 }
569
570 for(i = 0; i < (int32_t)(sizeof(test)/sizeof(test[0])); i++) {
571 ResourceBundle rb(testdatapath, test[i].requestedLocale, status);
572 if(U_FAILURE(status))
573 {
574 err("Could not open resource bundle %s (error %s)\n", test[i].requestedLocale, u_errorName(status));
575 status = U_ZERO_ERROR;
576 continue;
577 }
578
579 ResourceBundle res = rb.get(test[i].resourceKey, status);
580 if(U_FAILURE(status))
581 {
582 err("Couldn't find the key %s. Error: %s\n", test[i].resourceKey, u_errorName(status));
583 status = U_ZERO_ERROR;
584 continue;
585 }
586
587 locale = res.getLocale(ULOC_REQUESTED_LOCALE, status);
588 if(locale != Locale::getDefault()) {
589 err("Expected requested locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
590 }
591 locale = res.getLocale(ULOC_VALID_LOCALE, status);
592 if(strcmp(locale.getName(), test[i].validLocale) != 0) {
593 err("Expected valid locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
594 }
595 locale = res.getLocale(ULOC_ACTUAL_LOCALE, status);
596 if(strcmp(locale.getName(), test[i].actualLocale) != 0) {
597 err("Expected actual locale to be %s. Got %s\n", test[i].requestedLocale, locale.getName());
598 }
599 }
600 }
601
602 //eof
603