~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/test/intltest/restsnew.cpp

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 * COPYRIGHT: 
 
3
 * Copyright (c) 1997-2001, International Business Machines Corporation and
 
4
 * others. All Rights Reserved.
 
5
 ********************************************************************/
 
6
 
 
7
#include <stdlib.h>
 
8
#include <time.h>
 
9
#include <string.h>
 
10
 
 
11
#define RESTEST_HEAP_CHECK 0
 
12
 
 
13
#include "unicode/utypes.h"
 
14
 
 
15
#if defined(_WIN32) && !defined(__WINDOWS__)
 
16
#define _CRTDBG_MAP_ALLOC
 
17
#include <crtdbg.h>
 
18
#endif
 
19
 
 
20
#include "cstring.h"
 
21
#include "unicode/unistr.h"
 
22
#include "unicode/resbund.h"
 
23
#include "restsnew.h"
 
24
#include <limits.h>
 
25
 
 
26
//***************************************************************************************
 
27
 
 
28
static const UChar kErrorUChars[] = { 0x45, 0x52, 0x52, 0x4f, 0x52, 0 };
 
29
static const int32_t kErrorLength = 5;
 
30
static const int32_t kERROR_COUNT = -1234567;
 
31
 
 
32
//***************************************************************************************
 
33
 
 
34
enum E_Where
 
35
{
 
36
    e_Root,
 
37
    e_te,
 
38
    e_te_IN,
 
39
    e_Where_count
 
40
};
 
41
 
 
42
//***************************************************************************************
 
43
 
 
44
#define CONFIRM_EQ(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); OUT << action << " returned " << (actual) << " instead of " << (expected) << endl; }
 
45
#define CONFIRM_GE(actual,expected) if ((actual)>=(expected)) { record_pass(); } else { record_fail(); OUT << action << " returned " << (actual) << " instead of x >= " << (expected) << endl; }
 
46
#define CONFIRM_NE(actual,expected) if ((expected)!=(actual)) { record_pass(); } else { record_fail(); OUT << action << " returned " << (actual) << " instead of x != " << (expected) << endl; }
 
47
 
 
48
#define CONFIRM_UErrorCode(actual,expected) if ((expected)==(actual)) { record_pass(); } else { record_fail(); OUT << action << " returned " << u_errorName(actual) << " instead of " << u_errorName(expected) << endl; }
 
49
 
 
50
//***************************************************************************************
 
51
 
 
52
/**
 
53
 * Convert an integer, positive or negative, to a character string radix 10.
 
54
 */
 
55
static char*
 
56
itoa(int32_t i, char* buf)
 
57
{
 
58
    char* result = buf;
 
59
 
 
60
    // Handle negative
 
61
    if (i < 0)
 
62
    {
 
63
        *buf++ = '-';
 
64
        i = -i;
 
65
    }
 
66
 
 
67
    // Output digits in reverse order
 
68
    char* p = buf;
 
69
    do
 
70
    {
 
71
        *p++ = (char)('0' + (i % 10));
 
72
        i /= 10;
 
73
    }
 
74
    while (i);
 
75
    *p-- = 0;
 
76
 
 
77
    // Reverse the string
 
78
    while (buf < p)
 
79
    {
 
80
        char c = *buf;
 
81
        *buf++ = *p;
 
82
        *p-- = c;
 
83
    }
 
84
 
 
85
    return result;
 
86
}
 
87
 
 
88
 
 
89
 
 
90
//***************************************************************************************
 
91
 
 
92
// Array of our test objects
 
93
 
 
94
static struct
 
95
{
 
96
    const char* name;
 
97
    Locale *locale;
 
98
    UErrorCode expected_constructor_status;
 
99
    E_Where where;
 
100
    UBool like[e_Where_count];
 
101
    UBool inherits[e_Where_count];
 
102
}
 
103
param[] =
 
104
{
 
105
    // "te" means test
 
106
    // "IN" means inherits
 
107
    // "NE" or "ne" means "does not exist"
 
108
 
 
109
    { "root",       0,   U_ZERO_ERROR,             e_Root,      { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } },
 
110
    { "te",         0,   U_ZERO_ERROR,             e_te,        { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE  } },
 
111
    { "te_IN",      0,   U_ZERO_ERROR,             e_te_IN,     { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE   } },
 
112
    { "te_NE",      0,   U_USING_FALLBACK_ERROR,   e_te,        { FALSE, TRUE, FALSE }, { TRUE, TRUE, FALSE  } },
 
113
    { "te_IN_NE",   0,   U_USING_FALLBACK_ERROR,   e_te_IN,     { FALSE, FALSE, TRUE }, { TRUE, TRUE, TRUE   } },
 
114
    { "ne",         0,   U_USING_DEFAULT_ERROR,    e_Root,      { TRUE, FALSE, FALSE }, { TRUE, FALSE, FALSE } }
 
115
};
 
116
 
 
117
static int32_t bundles_count = sizeof(param) / sizeof(param[0]);
 
118
 
 
119
//***************************************************************************************
 
120
 
 
121
/**
 
122
 * Return a random unsigned long l where 0N <= l <= ULONG_MAX.
 
123
 */
 
124
 
 
125
static uint32_t
 
126
randul()
 
127
{
 
128
    static UBool initialized = FALSE;
 
129
    if (!initialized)
 
130
    {
 
131
        srand((unsigned)time(NULL));
 
132
        initialized = TRUE;
 
133
    }
 
134
    // Assume rand has at least 12 bits of precision
 
135
    uint32_t l = 0;
 
136
    for (uint32_t i=0; i<sizeof(l); ++i)
 
137
        ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
 
138
    return l;
 
139
}
 
140
 
 
141
/**
 
142
 * Return a random double x where 0.0 <= x < 1.0.
 
143
 */
 
144
static double
 
145
randd()
 
146
{
 
147
    return (double)(randul() / ULONG_MAX);
 
148
}
 
149
 
 
150
/**
 
151
 * Return a random integer i where 0 <= i < n.
 
152
 */
 
153
static int32_t randi(int32_t n)
 
154
{
 
155
    return (int32_t)(randd() * n);
 
156
}
 
157
 
 
158
//***************************************************************************************
 
159
 
 
160
/*
 
161
 Don't use more than one of these at a time because of the Locale names
 
162
*/
 
163
NewResourceBundleTest::NewResourceBundleTest()
 
164
: pass(0),
 
165
  fail(0),
 
166
  OUT(it_out)
 
167
{
 
168
    if (param[5].locale == NULL) {
 
169
        param[0].locale = new Locale("root");
 
170
        param[1].locale = new Locale("te");
 
171
        param[2].locale = new Locale("te", "IN");
 
172
        param[3].locale = new Locale("te", "NE");
 
173
        param[4].locale = new Locale("te", "IN", "NE");
 
174
        param[5].locale = new Locale("ne");
 
175
    }
 
176
}
 
177
 
 
178
NewResourceBundleTest::~NewResourceBundleTest()
 
179
{
 
180
    if (param[5].locale) {
 
181
        int idx;
 
182
        for (idx = 0; idx < (int)(sizeof(param)/sizeof(param[0])); idx++) {
 
183
            delete param[idx].locale;
 
184
            param[idx].locale = NULL;
 
185
        }
 
186
    }
 
187
}
 
188
 
 
189
void NewResourceBundleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
 
190
{
 
191
    if (exec) logln("TestSuite ResourceBundleTest: ");
 
192
    switch (index) {
 
193
    case 0: name = "TestResourceBundles"; if (exec) TestResourceBundles(); break;
 
194
    case 1: name = "TestConstruction"; if (exec) TestConstruction(); break;
 
195
    case 2: name = "TestIteration"; if (exec) TestIteration(); break;
 
196
    case 3: name = "TestOtherAPI";  if(exec) TestOtherAPI(); break;
 
197
 
 
198
        default: name = ""; break; //needed to end loop
 
199
    }
 
200
}
 
201
 
 
202
//***************************************************************************************
 
203
 
 
204
void
 
205
NewResourceBundleTest::TestResourceBundles()
 
206
{
 
207
#if defined(_WIN32) && !defined(__WINDOWS__)
 
208
#if defined(_DEBUG) && RESTEST_HEAP_CHECK
 
209
    /*
 
210
     * Set the debug-heap flag to keep freed blocks in the
 
211
     * heap's linked list - This will allow us to catch any
 
212
     * inadvertent use of freed memory
 
213
     */
 
214
    int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
 
215
    tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
 
216
    tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
 
217
    tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF;
 
218
    _CrtSetDbgFlag(tmpDbgFlag);
 
219
 
 
220
    _CrtMemState memstate;
 
221
    _CrtMemCheckpoint(&memstate);
 
222
    {
 
223
#endif
 
224
#endif
 
225
 
 
226
    testTag("only_in_Root", TRUE, FALSE, FALSE);
 
227
    testTag("only_in_te", FALSE, TRUE, FALSE);
 
228
    testTag("only_in_te_IN", FALSE, FALSE, TRUE);
 
229
    testTag("in_Root_te", TRUE, TRUE, FALSE);
 
230
    testTag("in_Root_te_te_IN", TRUE, TRUE, TRUE);
 
231
    testTag("in_Root_te_IN", TRUE, FALSE, TRUE);
 
232
    testTag("in_te_te_IN", FALSE, TRUE, TRUE);
 
233
    testTag("nonexistent", FALSE, FALSE, FALSE);
 
234
    OUT << "Passed: " << pass << "\nFailed: " << fail << endl;
 
235
 
 
236
#if defined(_WIN32) && !defined(__WINDOWS__)
 
237
#if defined(_DEBUG) && RESTEST_HEAP_CHECK
 
238
    }
 
239
    _CrtMemDumpAllObjectsSince(&memstate);
 
240
 
 
241
    /*
 
242
     * Set the debug-heap flag to keep freed blocks in the
 
243
     * heap's linked list - This will allow us to catch any
 
244
     * inadvertent use of freed memory
 
245
     */
 
246
    tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
 
247
    tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
 
248
    tmpDbgFlag &= ~_CRTDBG_LEAK_CHECK_DF;
 
249
    tmpDbgFlag &= ~_CRTDBG_CHECK_ALWAYS_DF;
 
250
    _CrtSetDbgFlag(tmpDbgFlag);
 
251
#endif
 
252
#endif
 
253
}
 
254
 
 
255
void
 
256
NewResourceBundleTest::TestConstruction()
 
257
{
 
258
    {
 
259
        UErrorCode   err = U_ZERO_ERROR;
 
260
        const char* testdatapath;
 
261
        Locale       locale("te", "IN");
 
262
        testdatapath=loadTestData(err);
 
263
        if(U_FAILURE(err))
 
264
        {
 
265
            errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
 
266
            return;
 
267
        }
 
268
 
 
269
        ResourceBundle  test1((UnicodeString)testdatapath, err);
 
270
        ResourceBundle  test2(testdatapath, locale, err);
 
271
     
 
272
        UnicodeString   result1;
 
273
        UnicodeString   result2;
 
274
 
 
275
        result1 = test1.getStringEx("string_in_Root_te_te_IN", err);
 
276
        result2 = test2.getStringEx("string_in_Root_te_te_IN", err);
 
277
        if (U_FAILURE(err)) {
 
278
            errln("Something threw an error in TestConstruction()");
 
279
            return;
 
280
        }
 
281
 
 
282
        logln("for string_in_Root_te_te_IN, root.txt had " + result1);
 
283
        logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
 
284
 
 
285
        if (result1 != "ROOT" || result2 != "TE_IN")
 
286
            errln("Construction test failed; run verbose for more information");
 
287
 
 
288
        const char* version1;
 
289
        const char* version2;
 
290
 
 
291
        version1 = test1.getVersionNumber();
 
292
        version2 = test2.getVersionNumber();
 
293
 
 
294
        char *versionID1 = new char[1 + strlen(U_ICU_VERSION) + strlen(version1)]; // + 1 for zero byte
 
295
        char *versionID2 = new char[1 + strlen(U_ICU_VERSION) + strlen(version2)]; // + 1 for zero byte
 
296
 
 
297
        strcpy(versionID1, "44.0");  // hardcoded, please change if the default.txt file or ResourceBundle::kVersionSeparater is changed.
 
298
 
 
299
        strcpy(versionID2, "55.0");  // hardcoded, please change if the te_IN.txt file or ResourceBundle::kVersionSeparater is changed.
 
300
 
 
301
        logln(UnicodeString("getVersionNumber on default.txt returned ") + version1 + UnicodeString(" Expect: " ) + versionID1);
 
302
        logln(UnicodeString("getVersionNumber on te_IN.txt returned ") + version2 + UnicodeString(" Expect: " ) + versionID2);
 
303
 
 
304
        if (strcmp(version1, versionID1) != 0 || strcmp(version2, versionID2) != 0)
 
305
            errln("getVersionNumber() failed");
 
306
        delete[] versionID1;
 
307
        delete[] versionID2;
 
308
    }
 
309
    {
 
310
        UErrorCode   err = U_ZERO_ERROR;
 
311
        const char* testdatapath;
 
312
        Locale       locale("te", "IN");
 
313
 
 
314
        testdatapath=loadTestData(err);
 
315
        if(U_FAILURE(err))
 
316
        {
 
317
            errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
 
318
            return;
 
319
        }
 
320
 
 
321
 
 
322
        wchar_t* wideDirectory = new wchar_t[256];
 
323
        mbstowcs(wideDirectory, testdatapath, 256);
 
324
    
 
325
        ResourceBundle  test2(wideDirectory, locale, err);
 
326
 
 
327
        UnicodeString   result2;
 
328
 
 
329
        result2 = test2.getStringEx("string_in_Root_te_te_IN", err);
 
330
        if (U_FAILURE(err)) {
 
331
            errln("Something threw an error in TestConstruction()");
 
332
            return;
 
333
        }
 
334
 
 
335
        logln("for string_in_Root_te_te_IN, te_IN.txt had " + result2);
 
336
 
 
337
        if (result2 != "TE_IN")
 
338
            errln("Construction test failed; run verbose for more information");
 
339
 
 
340
        delete[] wideDirectory;
 
341
    }
 
342
}
 
343
void
 
344
NewResourceBundleTest::TestIteration()
 
345
{
 
346
    UErrorCode   err = U_ZERO_ERROR;
 
347
    const char* testdatapath;
 
348
    const char* data[]={
 
349
        "string_in_Root_te_te_IN",   "1",
 
350
        "array_in_Root_te_te_IN",    "5",
 
351
        "array_2d_in_Root_te_te_IN", "4",
 
352
    };
 
353
 
 
354
    Locale       *locale=new Locale("te_IN");
 
355
 
 
356
    testdatapath=loadTestData(err);
 
357
    if(U_FAILURE(err))
 
358
    {
 
359
        errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
 
360
        return;
 
361
    }
 
362
 
 
363
    ResourceBundle  test1(testdatapath, *locale, err);
 
364
    if(U_FAILURE(err)){
 
365
        errln("Construction failed");
 
366
    }
 
367
    uint32_t i;
 
368
    int32_t count, row=0, col=0;
 
369
    char buf[5];
 
370
    UnicodeString expected;
 
371
    UnicodeString element("TE_IN");
 
372
    UnicodeString action;
 
373
 
 
374
 
 
375
    for(i=0; i<sizeof(data)/sizeof(data[0]); i=i+2){
 
376
        action = "te_IN";
 
377
        action +=".get(";
 
378
        action += data[i];
 
379
        action +=", err)";
 
380
        err=U_ZERO_ERROR;
 
381
        ResourceBundle bundle = test1.get(data[i], err); 
 
382
        if(!U_FAILURE(err)){
 
383
            action = "te_IN";
 
384
            action +=".getKey()";
 
385
 
 
386
            CONFIRM_EQ((UnicodeString)bundle.getKey(), (UnicodeString)data[i]);
 
387
 
 
388
            count=0;
 
389
            row=0;
 
390
            while(bundle.hasNext()){
 
391
                action = data[i];
 
392
                action +=".getNextString(err)";
 
393
                row=count;   
 
394
                UnicodeString got=bundle.getNextString(err);
 
395
                if(U_SUCCESS(err)){
 
396
                    expected=element;
 
397
                    if(bundle.getSize() > 1){
 
398
                        CONFIRM_EQ(bundle.getType(), RES_ARRAY);
 
399
                        expected+=itoa(row, buf);
 
400
                        ResourceBundle rowbundle=bundle.get(row, err);
 
401
                        if(!U_FAILURE(err) && rowbundle.getSize()>1){
 
402
                            col=0;
 
403
                            while(rowbundle.hasNext()){
 
404
                                expected=element;
 
405
                                got=rowbundle.getNextString(err);
 
406
                                if(!U_FAILURE(err)){
 
407
                                    expected+=itoa(row, buf);
 
408
                                    expected+=itoa(col, buf);
 
409
                                    col++;
 
410
                                    CONFIRM_EQ(got, expected);
 
411
                                }
 
412
                            }
 
413
                            CONFIRM_EQ(col, rowbundle.getSize());
 
414
                        }
 
415
                    }
 
416
                    else{
 
417
                        CONFIRM_EQ(bundle.getType(), (int32_t)RES_STRING);
 
418
                    }
 
419
                }
 
420
                CONFIRM_EQ(got, expected);
 
421
                count++;
 
422
            }
 
423
            action = data[i];
 
424
            action +=".getSize()";
 
425
            CONFIRM_EQ(bundle.getSize(), count);
 
426
            CONFIRM_EQ(count, atoi(data[i+1]));
 
427
            //after reaching the end
 
428
            err=U_ZERO_ERROR;
 
429
            ResourceBundle errbundle=bundle.getNext(err);
 
430
            action = "After reaching the end of the Iterator:-  ";
 
431
            action +=data[i];
 
432
            action +=".getNext()";
 
433
            CONFIRM_NE(err, (int32_t)U_ZERO_ERROR);
 
434
            CONFIRM_EQ(u_errorName(err), u_errorName(U_INDEX_OUTOFBOUNDS_ERROR));
 
435
            //reset the iterator
 
436
            err = U_ZERO_ERROR;
 
437
            bundle.resetIterator();
 
438
         /*  The following code is causing a crash
 
439
         ****CRASH******
 
440
         */
 
441
 
 
442
            bundle.getNext(err);
 
443
            if(U_FAILURE(err)){
 
444
                errln("ERROR: getNext()  throw an error");
 
445
            }/**/
 
446
 
 
447
 
 
448
        }
 
449
 
 
450
 
 
451
 
 
452
    }
 
453
    delete locale;
 
454
}
 
455
 
 
456
void
 
457
NewResourceBundleTest::TestOtherAPI(){
 
458
    UErrorCode   err = U_ZERO_ERROR;
 
459
    const char* testdatapath;
 
460
    testdatapath=loadTestData(err);
 
461
    if(U_FAILURE(err))
 
462
    {
 
463
        errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(err)));
 
464
        return;
 
465
    }
 
466
    Locale       *locale=new Locale("te_IN");
 
467
 
 
468
    ResourceBundle  test1(testdatapath, *locale, err);
 
469
    if(U_FAILURE(err)){
 
470
        errln("Construction failed");
 
471
        return;
 
472
    }
 
473
 
 
474
    logln("Testing getLocale()\n");
 
475
    if(strcmp(test1.getLocale().getName(), locale->getName()) !=0 ){
 
476
        errln("FAIL: ResourceBundle::getLocale() failed\n");
 
477
    }
 
478
 
 
479
    delete locale;
 
480
 
 
481
    logln("Testing ResourceBundle(UErrorCode)\n");
 
482
    ResourceBundle defaultresource(err);
 
483
    if(U_FAILURE(err)){
 
484
        errln("Construction of default resourcebundle failed");
 
485
    }
 
486
    if(strcmp(defaultresource.getLocale().getName(), Locale::getDefault().getName()) != 0){
 
487
        errln("Construction of default resourcebundle didn't take the defaultlocale\n");
 
488
    }
 
489
    
 
490
 
 
491
    ResourceBundle copyRes(defaultresource);
 
492
    if(strcmp(copyRes.getName(), defaultresource.getName() ) !=0  ||
 
493
        strcmp(test1.getName(), defaultresource.getName() ) ==0 ||
 
494
        strcmp(copyRes.getLocale().getName(), defaultresource.getLocale().getName() ) !=0  ||
 
495
        strcmp(test1.getLocale().getName(), defaultresource.getLocale().getName() ) ==0 ){
 
496
        errln("copy construction failed\n");
 
497
    }
 
498
 
 
499
    ResourceBundle defaultSub = defaultresource.get(1, err);
 
500
    ResourceBundle defSubCopy(defaultSub);
 
501
    if(strcmp(defSubCopy.getName(), defaultSub.getName() ) !=0  ||
 
502
        strcmp(defSubCopy.getLocale().getName(), defaultSub.getLocale().getName() ) !=0  ){
 
503
        errln("copy construction for subresource failed\n");
 
504
    }
 
505
 
 
506
 
 
507
 
 
508
    UVersionInfo ver;
 
509
    copyRes.getVersion(ver);
 
510
 
 
511
    logln("Version returned: [%d.%d.%d.%d]\n", ver[0], ver[1], ver[2], ver[3]);
 
512
 
 
513
    logln("Testing C like UnicodeString APIs\n");
 
514
 
 
515
    UResourceBundle *testCAPI = NULL, *bundle = NULL, *rowbundle = NULL, *temp = NULL;
 
516
    err = U_ZERO_ERROR;
 
517
    const char* data[]={
 
518
        "string_in_Root_te_te_IN",   "1",
 
519
        "array_in_Root_te_te_IN",    "5",
 
520
        "array_2d_in_Root_te_te_IN", "4",
 
521
    };
 
522
 
 
523
 
 
524
 
 
525
    testCAPI = ures_open(testdatapath, "te_IN", &err);
 
526
 
 
527
    if(U_SUCCESS(err)) {
 
528
      // Do the testing
 
529
      // first iteration
 
530
 
 
531
      uint32_t i;
 
532
      int32_t count, row=0, col=0;
 
533
      char buf[5];
 
534
      UnicodeString expected;
 
535
      UnicodeString element("TE_IN");
 
536
      UnicodeString action;
 
537
 
 
538
 
 
539
      for(i=0; i<sizeof(data)/sizeof(data[0]); i=i+2){
 
540
          action = "te_IN";
 
541
          action +=".get(";
 
542
          action += data[i];
 
543
          action +=", err)";
 
544
          err=U_ZERO_ERROR;
 
545
          bundle = ures_getByKey(testCAPI, data[i], bundle, &err); 
 
546
          if(!U_FAILURE(err)){
 
547
            const char* key = NULL;
 
548
              action = "te_IN";
 
549
              action +=".getKey()";
 
550
 
 
551
              CONFIRM_EQ((UnicodeString)ures_getKey(bundle), (UnicodeString)data[i]);
 
552
 
 
553
              count=0;
 
554
              row=0;
 
555
              while(ures_hasNext(bundle)){
 
556
                  action = data[i];
 
557
                  action +=".getNextString(err)";
 
558
                  row=count;   
 
559
                  UnicodeString got=ures_getNextUnicodeString(bundle, &key, &err);
 
560
                  if(U_SUCCESS(err)){
 
561
                      expected=element;
 
562
                      if(ures_getSize(bundle) > 1){
 
563
                          CONFIRM_EQ(ures_getType(bundle), RES_ARRAY);
 
564
                          expected+=itoa(row, buf);
 
565
                          rowbundle=ures_getByIndex(bundle, row, rowbundle, &err);
 
566
                          if(!U_FAILURE(err) && ures_getSize(rowbundle)>1){
 
567
                              col=0;
 
568
                              while(ures_hasNext(rowbundle)){
 
569
                                  expected=element;
 
570
                                  got=ures_getNextUnicodeString(rowbundle, &key, &err);
 
571
                                  temp = ures_getByIndex(rowbundle, col, temp, &err);
 
572
                                  UnicodeString bla = ures_getUnicodeString(temp, &err);
 
573
                                  UnicodeString bla2 = ures_getUnicodeStringByIndex(rowbundle, col, &err);
 
574
                                  if(!U_FAILURE(err)){
 
575
                                      expected+=itoa(row, buf);
 
576
                                      expected+=itoa(col, buf);
 
577
                                      col++;
 
578
                                      CONFIRM_EQ(got, expected);
 
579
                                      CONFIRM_EQ(bla, expected);
 
580
                                      CONFIRM_EQ(bla2, expected);
 
581
                                  }
 
582
                              }
 
583
                              CONFIRM_EQ(col, ures_getSize(rowbundle));
 
584
                          }
 
585
                      }
 
586
                      else{
 
587
                          CONFIRM_EQ(ures_getType(bundle), (int32_t)RES_STRING);
 
588
                      }
 
589
                  }
 
590
                  CONFIRM_EQ(got, expected);
 
591
                  count++;
 
592
              }
 
593
          }
 
594
      }
 
595
      ures_close(temp);
 
596
      ures_close(rowbundle);
 
597
      ures_close(bundle);
 
598
      ures_close(testCAPI);
 
599
    } else {
 
600
      errln("failed to open a resource bundle\n");
 
601
    }
 
602
 
 
603
 
 
604
 
 
605
 
 
606
 
 
607
 
 
608
    
 
609
 
 
610
}
 
611
 
 
612
 
 
613
 
 
614
 
 
615
 
 
616
 
 
617
//***************************************************************************************
 
618
 
 
619
UBool
 
620
NewResourceBundleTest::testTag(const char* frag,
 
621
                            UBool in_Root,
 
622
                            UBool in_te,
 
623
                            UBool in_te_IN)
 
624
{
 
625
    int32_t failOrig = fail;
 
626
 
 
627
    // Make array from input params
 
628
 
 
629
    UBool is_in[] = { in_Root, in_te, in_te_IN };
 
630
 
 
631
    const char* NAME[] = { "ROOT", "TE", "TE_IN" };
 
632
 
 
633
    // Now try to load the desired items
 
634
 
 
635
    char tag[100];
 
636
    UnicodeString action;
 
637
 
 
638
    int32_t i,j,row,col, actual_bundle;
 
639
    int32_t index;
 
640
    const char* testdatapath;
 
641
 
 
642
    UErrorCode status = U_ZERO_ERROR;
 
643
    testdatapath=loadTestData(status);
 
644
    if(U_FAILURE(status))
 
645
    {
 
646
        errln("Could not load testdata.dat %s " + UnicodeString(u_errorName(status)));
 
647
        return FALSE;
 
648
    }
 
649
 
 
650
    for (i=0; i<bundles_count; ++i)
 
651
    {
 
652
        action = "Constructor for ";
 
653
        action += param[i].name;
 
654
 
 
655
        status = U_ZERO_ERROR;
 
656
        ResourceBundle theBundle( testdatapath, *param[i].locale, status);
 
657
        //ResourceBundle theBundle( "c:\\icu\\icu\\source\\test\\testdata\\testdata", *param[i].locale, status);
 
658
        CONFIRM_UErrorCode(status,param[i].expected_constructor_status);
 
659
 
 
660
        if(i == 5)
 
661
          actual_bundle = 0; /* ne -> default */
 
662
        else if(i == 3)
 
663
          actual_bundle = 1; /* te_NE -> te */
 
664
        else if(i == 4)
 
665
          actual_bundle = 2; /* te_IN_NE -> te_IN */
 
666
        else
 
667
          actual_bundle = i;
 
668
 
 
669
 
 
670
        UErrorCode expected_resource_status = U_MISSING_RESOURCE_ERROR;
 
671
        for (j=e_te_IN; j>=e_Root; --j)
 
672
        {
 
673
            if (is_in[j] && param[i].inherits[j])
 
674
              {
 
675
                if(j == actual_bundle) /* it's in the same bundle OR it's a nonexistent=default bundle (5) */
 
676
                  expected_resource_status = U_ZERO_ERROR;
 
677
                else if(j == 0)
 
678
                  expected_resource_status = U_USING_DEFAULT_ERROR;
 
679
                else
 
680
                  expected_resource_status = U_USING_FALLBACK_ERROR;
 
681
                
 
682
                break;
 
683
            }
 
684
        }
 
685
 
 
686
        UErrorCode expected_status;
 
687
 
 
688
        UnicodeString base;
 
689
        for (j=param[i].where; j>=0; --j)
 
690
        {
 
691
            if (is_in[j])
 
692
            {
 
693
                base = NAME[j];
 
694
                break;
 
695
            }
 
696
        }
 
697
 
 
698
        //--------------------------------------------------------------------------
 
699
        // string
 
700
 
 
701
        uprv_strcpy(tag, "string_");
 
702
        uprv_strcat(tag, frag);
 
703
 
 
704
        action = param[i].name;
 
705
        action += ".getStringEx(";
 
706
        action += tag;
 
707
        action += ")";
 
708
 
 
709
 
 
710
        status = U_ZERO_ERROR;
 
711
        UnicodeString string = theBundle.getStringEx(tag, status);
 
712
        if(U_FAILURE(status)) {
 
713
            string.setTo(TRUE, kErrorUChars, kErrorLength);
 
714
        }
 
715
 
 
716
        CONFIRM_UErrorCode(status, expected_resource_status);
 
717
 
 
718
        UnicodeString expected_string(kErrorUChars);
 
719
        if (U_SUCCESS(status)) {
 
720
            expected_string = base;
 
721
        }
 
722
 
 
723
        CONFIRM_EQ(string, expected_string);
 
724
 
 
725
        //--------------------------------------------------------------------------
 
726
        // array   ResourceBundle using the key
 
727
 
 
728
        uprv_strcpy(tag, "array_");
 
729
        uprv_strcat(tag, frag);
 
730
 
 
731
        action = param[i].name;
 
732
        action += ".get(";
 
733
        action += tag;
 
734
        action += ")";
 
735
 
 
736
        int32_t count = kERROR_COUNT;
 
737
        status = U_ZERO_ERROR;
 
738
        ResourceBundle array = theBundle.get(tag, status);
 
739
        CONFIRM_UErrorCode(status,expected_resource_status);
 
740
 
 
741
 
 
742
        if (U_SUCCESS(status))
 
743
        {
 
744
            //confirm the resource type is an array
 
745
            UResType bundleType=array.getType();
 
746
            CONFIRM_EQ(bundleType, RES_ARRAY);
 
747
 
 
748
            count=array.getSize();
 
749
            CONFIRM_GE(count,1);
 
750
           
 
751
            for (j=0; j<count; ++j)
 
752
            {
 
753
                char buf[32];
 
754
                expected_string = base;
 
755
                expected_string += itoa(j,buf);
 
756
                CONFIRM_EQ(array.getNextString(status),expected_string);
 
757
            }
 
758
 
 
759
        }
 
760
        else
 
761
        {
 
762
            CONFIRM_EQ(count,kERROR_COUNT);
 
763
            //       CONFIRM_EQ((int32_t)(unsigned long)array,(int32_t)0);
 
764
            count = 0;
 
765
        }
 
766
 
 
767
        //--------------------------------------------------------------------------
 
768
        // arrayItem ResourceBundle using the index
 
769
 
 
770
 
 
771
        for (j=0; j<100; ++j)
 
772
        {
 
773
            index = count ? (randi(count * 3) - count) : (randi(200) - 100);
 
774
            status = U_ZERO_ERROR;
 
775
            string = kErrorUChars;
 
776
            ResourceBundle array = theBundle.get(tag, status);
 
777
            if(!U_FAILURE(status)){
 
778
                UnicodeString t = array.getStringEx(index, status);
 
779
                if(!U_FAILURE(status)) {
 
780
                   string=t;
 
781
                }
 
782
            }
 
783
 
 
784
            expected_status = (index >= 0 && index < count) ? expected_resource_status : U_MISSING_RESOURCE_ERROR;
 
785
            CONFIRM_UErrorCode(status,expected_status);
 
786
 
 
787
            if (U_SUCCESS(status)){
 
788
                char buf[32];
 
789
                expected_string = base;
 
790
                expected_string += itoa(index,buf);
 
791
            } else {
 
792
                expected_string = kErrorUChars;
 
793
            }
 
794
               CONFIRM_EQ(string,expected_string);
 
795
 
 
796
        }
 
797
 
 
798
        //--------------------------------------------------------------------------
 
799
        // 2dArray
 
800
 
 
801
        uprv_strcpy(tag, "array_2d_");
 
802
        uprv_strcat(tag, frag);
 
803
 
 
804
        action = param[i].name;
 
805
        action += ".get(";
 
806
        action += tag;
 
807
        action += ")";
 
808
 
 
809
 
 
810
        int32_t row_count = kERROR_COUNT, column_count = kERROR_COUNT;
 
811
        status = U_ZERO_ERROR;
 
812
        ResourceBundle array2d=theBundle.get(tag, status);
 
813
 
 
814
        //const UnicodeString** array2d = theBundle.get2dArray(tag, row_count, column_count, status);
 
815
        CONFIRM_UErrorCode(status,expected_resource_status);
 
816
 
 
817
        if (U_SUCCESS(status))
 
818
        {
 
819
            //confirm the resource type is an 2darray
 
820
            UResType bundleType=array2d.getType();
 
821
            CONFIRM_EQ(bundleType, RES_ARRAY);
 
822
 
 
823
            row_count=array2d.getSize();
 
824
            CONFIRM_GE(row_count,1);
 
825
 
 
826
            for(row=0; row<row_count; ++row){
 
827
                ResourceBundle tablerow=array2d.get(row, status);
 
828
                CONFIRM_UErrorCode(status, expected_resource_status);
 
829
                if(U_SUCCESS(status)){
 
830
                    //confirm the resourcetype of each table row is an array
 
831
                    UResType rowType=tablerow.getType();
 
832
                    CONFIRM_EQ(rowType, RES_ARRAY);
 
833
 
 
834
                    column_count=tablerow.getSize();
 
835
                    CONFIRM_GE(column_count,1);
 
836
 
 
837
                    for (col=0; j<column_count; ++j) {
 
838
                           char buf[32];
 
839
                           expected_string = base;
 
840
                           expected_string += itoa(row,buf);
 
841
                           expected_string += itoa(col,buf);
 
842
                           CONFIRM_EQ(tablerow.getNextString(status),expected_string);
 
843
                    }
 
844
                }
 
845
            }
 
846
        }else{
 
847
            CONFIRM_EQ(row_count,kERROR_COUNT);
 
848
            CONFIRM_EQ(column_count,kERROR_COUNT);
 
849
             row_count=column_count=0;
 
850
        }
 
851
 
 
852
 
 
853
 
 
854
 
 
855
       //--------------------------------------------------------------------------
 
856
       // 2dArrayItem
 
857
       for (j=0; j<200; ++j)
 
858
       {
 
859
           row = row_count ? (randi(row_count * 3) - row_count) : (randi(200) - 100);
 
860
           col = column_count ? (randi(column_count * 3) - column_count) : (randi(200) - 100);
 
861
           status = U_ZERO_ERROR;
 
862
           string = kErrorUChars;
 
863
           ResourceBundle array2d=theBundle.get(tag, status);
 
864
           if(U_SUCCESS(status)){
 
865
                ResourceBundle tablerow=array2d.get(row, status);
 
866
                if(U_SUCCESS(status)) {
 
867
                    UnicodeString t=tablerow.getStringEx(col, status);
 
868
                    if(U_SUCCESS(status)){
 
869
                       string=t;
 
870
                    }
 
871
                }
 
872
           }
 
873
           expected_status = (row >= 0 && row < row_count && col >= 0 && col < column_count) ?
 
874
                                  expected_resource_status: U_MISSING_RESOURCE_ERROR;
 
875
           CONFIRM_UErrorCode(status,expected_status);
 
876
 
 
877
           if (U_SUCCESS(status)){
 
878
               char buf[32];
 
879
               expected_string = base;
 
880
               expected_string += itoa(row,buf);
 
881
               expected_string += itoa(col,buf);
 
882
           } else {
 
883
               expected_string = kErrorUChars;
 
884
           }
 
885
               CONFIRM_EQ(string,expected_string);
 
886
 
 
887
        }
 
888
 
 
889
        //--------------------------------------------------------------------------
 
890
        // taggedArray
 
891
 
 
892
        uprv_strcpy(tag, "tagged_array_");
 
893
        uprv_strcat(tag, frag);
 
894
 
 
895
        action = param[i].name;
 
896
        action += ".get(";
 
897
        action += tag;
 
898
        action += ")";
 
899
 
 
900
        int32_t         tag_count;
 
901
        status = U_ZERO_ERROR;
 
902
 
 
903
        ResourceBundle tags=theBundle.get(tag, status);
 
904
        CONFIRM_UErrorCode(status, expected_resource_status);
 
905
 
 
906
        if (U_SUCCESS(status)) {
 
907
            UResType bundleType=tags.getType();
 
908
            CONFIRM_EQ(bundleType, RES_TABLE);
 
909
 
 
910
            tag_count=tags.getSize();
 
911
            CONFIRM_GE((int32_t)tag_count, (int32_t)0); 
 
912
 
 
913
            for(index=0; index <tag_count; index++){
 
914
                ResourceBundle tagelement=tags.get(index, status);
 
915
                UnicodeString key=tagelement.getKey();
 
916
                UnicodeString value=tagelement.getNextString(status);
 
917
                logln("tag = " + key + ", value = " + value );
 
918
                if(key.startsWith("tag") && value.startsWith(base)){
 
919
                    record_pass();
 
920
                }else{
 
921
                    record_fail();
 
922
                }
 
923
 
 
924
            }
 
925
 
 
926
            for(index=0; index <tag_count; index++){
 
927
                ResourceBundle tagelement=tags.get(index, status);
 
928
                const char *tkey=NULL;
 
929
                UnicodeString value=tagelement.getNextString(&tkey, status);
 
930
                UnicodeString key(tkey);
 
931
                logln("tag = " + key + ", value = " + value );
 
932
                if(value.startsWith(base)){
 
933
                    record_pass();
 
934
                }else{
 
935
                    record_fail();
 
936
                }
 
937
            }
 
938
 
 
939
        }else{
 
940
            tag_count=0;
 
941
        }
 
942
 
 
943
 
 
944
 
 
945
 
 
946
        //--------------------------------------------------------------------------
 
947
        // taggedArrayItem
 
948
 
 
949
        action = param[i].name;
 
950
        action += ".get(";
 
951
        action += tag;
 
952
        action += ")";
 
953
 
 
954
        count = 0;
 
955
        for (index=-20; index<20; ++index)
 
956
        {
 
957
            char buf[32];
 
958
            status = U_ZERO_ERROR;
 
959
            string = kErrorUChars;
 
960
            char item_tag[8];
 
961
            uprv_strcpy(item_tag, "tag");
 
962
            uprv_strcat(item_tag, itoa(index,buf));
 
963
            ResourceBundle tags=theBundle.get(tag, status);
 
964
            if(U_SUCCESS(status)){
 
965
                ResourceBundle tagelement=tags.get(item_tag, status);
 
966
                if(!U_FAILURE(status)){
 
967
                    UResType elementType=tagelement.getType();
 
968
                    CONFIRM_EQ(elementType, (int32_t)RES_STRING);
 
969
                    const char* key=tagelement.getKey();
 
970
                    CONFIRM_EQ((UnicodeString)key, (UnicodeString)item_tag);
 
971
                    UnicodeString t=tagelement.getString(status);
 
972
                    if(!U_FAILURE(status)){
 
973
                        string=t;
 
974
                    }
 
975
                }
 
976
                if (index < 0) {
 
977
                    CONFIRM_UErrorCode(status,U_MISSING_RESOURCE_ERROR);
 
978
                }
 
979
                else{
 
980
                   if (status != U_MISSING_RESOURCE_ERROR) {
 
981
                       count++;
 
982
                       expected_string = base;
 
983
                       expected_string += buf;
 
984
                       CONFIRM_EQ(string,expected_string);
 
985
                   }
 
986
                }
 
987
            }
 
988
 
 
989
        }
 
990
        CONFIRM_EQ(count, tag_count);
 
991
 
 
992
    }
 
993
    return (UBool)(failOrig == fail);
 
994
}
 
995
 
 
996
void
 
997
NewResourceBundleTest::record_pass()
 
998
{
 
999
  ++pass;
 
1000
}
 
1001
void
 
1002
NewResourceBundleTest::record_fail()
 
1003
{
 
1004
  err();
 
1005
  ++fail;
 
1006
}
 
1007
//eof
 
1008