2
*******************************************************************************
3
* Copyright (C) 1996-2000, International Business Machines Corporation and *
4
* others. All Rights Reserved. *
5
*******************************************************************************
9
#include "unicode/umachine.h"
11
#include "unicode/tblcoll.h"
12
#include "unicode/coleitr.h"
13
#include "unicode/ures.h"
14
#include "unicode/ustring.h"
19
// import com.ibm.text.RuleBasedNumberFormat;
20
// import com.ibm.test.TestFmwk;
22
// import java.util.Locale;
23
// import java.text.NumberFormat;
25
// current macro not in icu1.8.1
26
#define TESTCASE(id,test) \
31
logln((UnicodeString)""); \
36
void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
38
if (exec) logln("TestSuite RuleBasedNumberFormat");
41
TESTCASE(0, TestEnglishSpellout);
42
TESTCASE(1, TestOrdinalAbbreviations);
43
TESTCASE(2, TestDurations);
44
TESTCASE(3, TestSpanishSpellout);
45
TESTCASE(4, TestFrenchSpellout);
46
TESTCASE(5, TestSwissFrenchSpellout);
47
TESTCASE(6, TestItalianSpellout);
48
TESTCASE(7, TestGermanSpellout);
49
TESTCASE(8, TestThaiSpellout);
51
TESTCASE(10, TestFractionalRuleSet);
52
// TESTCASE(11, TestLLong);
54
TESTCASE(0, TestRBNFDisabled);
65
IntlTestRBNF::TestAPI() {
66
// This test goes through the APIs that were not tested before.
67
// These tests are too small to have separate test classes/functions
69
UErrorCode status = U_ZERO_ERROR;
70
RuleBasedNumberFormat* formatter
71
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
73
logln("RBNF API test starting");
76
logln("Testing Clone");
77
RuleBasedNumberFormat* rbnfClone = (RuleBasedNumberFormat *)formatter->clone();
78
if(rbnfClone != NULL) {
79
if(!(*rbnfClone == *formatter)) {
80
errln("Clone should be semantically equivalent to the original!");
84
errln("Cloning failed!");
90
logln("Testing assignment operator");
91
RuleBasedNumberFormat assignResult(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
92
assignResult = *formatter;
93
if(!(assignResult == *formatter)) {
94
errln("Assignment result should be semantically equivalent to the original!");
98
// test rule constructor
100
logln("Testing rule constructor");
101
UResourceBundle *en = ures_open(NULL, "en", &status);
102
if(U_FAILURE(status)) {
103
errln("Unable to access resource bundle with data!");
106
const UChar *spelloutRules = ures_getStringByKey(en, "SpelloutRules", &ruleLen, &status);
107
if(U_FAILURE(status) || ruleLen == 0 || spelloutRules == NULL) {
108
errln("Unable to access the rules string!");
111
RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::getUS(), perror, status);
112
if(!(ruleCtorResult == *formatter)) {
113
errln("Formatter constructed from the original rules should be semantically equivalent to the original!");
122
logln("Testing getRules function");
123
UnicodeString rules = formatter->getRules();
125
RuleBasedNumberFormat fromRulesResult(rules, Locale::getUS(), perror, status);
127
if(!(fromRulesResult == *formatter)) {
128
errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!");
134
logln("Testing copy constructor");
135
RuleBasedNumberFormat copyCtorResult(*formatter);
136
if(!(copyCtorResult == *formatter)) {
137
errln("Copy constructor result result should be semantically equivalent to the original!");
141
// test ruleset names
143
logln("Testing getNumberOfRuleSetNames, getRuleSetName and format using rule set names");
144
int32_t noOfRuleSetNames = formatter->getNumberOfRuleSetNames();
145
if(noOfRuleSetNames == 0) {
146
errln("Number of rule set names should be more than zero");
148
UnicodeString ruleSetName;
150
int32_t intFormatNum = 34567;
151
double doubleFormatNum = 893411.234;
152
logln("number of rule set names is %i", noOfRuleSetNames);
153
for(i = 0; i < noOfRuleSetNames; i++) {
154
FieldPosition pos1, pos2;
155
UnicodeString intFormatResult, doubleFormatResult;
156
Formattable intParseResult, doubleParseResult;
158
ruleSetName = formatter->getRuleSetName(i);
159
log("Rule set name %i is ", i);
161
logln(". Format results are: ");
162
intFormatResult = formatter->format(intFormatNum, ruleSetName, intFormatResult, pos1, status);
163
doubleFormatResult = formatter->format(doubleFormatNum, ruleSetName, doubleFormatResult, pos2, status);
164
if(U_FAILURE(status)) {
165
errln("Format using a rule set failed");
168
logln(intFormatResult);
169
logln(doubleFormatResult);
170
formatter->setLenient(TRUE);
171
formatter->parse(intFormatResult, intParseResult, status);
172
formatter->parse(doubleFormatResult, doubleParseResult, status);
174
logln("Parse results for lenient = TRUE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
176
formatter->setLenient(FALSE);
177
formatter->parse(intFormatResult, intParseResult, status);
178
formatter->parse(doubleFormatResult, doubleParseResult, status);
180
logln("Parse results for lenient = FALSE, %i, %f", intParseResult.getLong(), doubleParseResult.getDouble());
182
if(U_FAILURE(status)) {
183
errln("Error during parsing");
186
intFormatResult = formatter->format(intFormatNum, "BLABLA", intFormatResult, pos1, status);
187
if(U_SUCCESS(status)) {
188
errln("Using invalid rule set name should have failed");
191
status = U_ZERO_ERROR;
192
doubleFormatResult = formatter->format(doubleFormatNum, "TRUC", doubleFormatResult, pos2, status);
193
if(U_SUCCESS(status)) {
194
errln("Using invalid rule set name should have failed");
197
status = U_ZERO_ERROR;
199
status = U_ZERO_ERROR;
203
logln("Cleaning up");
207
void IntlTestRBNF::TestFractionalRuleSet()
209
UnicodeString fracRules(
211
// this rule formats the number if it's 1 or more. It formats
212
// the integral part using a DecimalFormat ("#,##0" puts
213
// thousands separators in the right places) and the fractional
214
// part using %%frac. If there is no fractional part, it
215
// just shows the integral part.
216
" x.0: <#,##0<[ >%%frac>];\n"
217
// this rule formats the number if it's between 0 and 1. It
218
// shows only the fractional part (0.5 shows up as "1/2," not
221
// the fraction rule set. This works the same way as the one in the
222
// preceding example: We multiply the fractional part of the number
223
// being formatted by each rule's base value and use the rule that
224
// produces the result closest to 0 (or the first rule that produces 0).
225
// Since we only provide rules for the numbers from 2 to 10, we know
226
// we'll get a fraction with a denominator between 2 and 10.
227
// "<0<" causes the numerator of the fraction to be formatted
240
UErrorCode status = U_ZERO_ERROR;
242
RuleBasedNumberFormat formatter(fracRules, Locale::getEnglish(), perror, status);
243
if (U_FAILURE(status)) {
244
errln("FAIL: could not construct formatter");
246
static const char* testData[][2] = {
259
{ "3.125", "3 1/8" },
260
{ "4.1428", "4 1/7" },
261
{ "5.1667", "5 1/6" },
264
{ "8.333", "8 1/3" },
269
{ "1.2856", "1 2/7" },
272
doTest(&formatter, testData, FALSE); // exact values aren't parsable from fractions
277
#define LLAssert(a) \
278
if (!(a)) errln("FAIL: " #a)
280
void IntlTestRBNF::TestLLongConstructors()
282
logln("Testing constructors");
284
// constant (shouldn't really be public)
285
LLAssert(llong(llong::kD32).asDouble() == llong::kD32);
287
// internal constructor (shouldn't really be public)
288
LLAssert(llong(0, 1).asDouble() == 1);
289
LLAssert(llong(1, 0).asDouble() == llong::kD32);
290
LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1);
292
// public empty constructor
293
LLAssert(llong().asDouble() == 0);
295
// public int32_t constructor
296
LLAssert(llong((int32_t)0).asInt() == (int32_t)0);
297
LLAssert(llong((int32_t)1).asInt() == (int32_t)1);
298
LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1);
299
LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff);
300
LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1);
301
LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000);
303
// public int16_t constructor
304
LLAssert(llong((int16_t)0).asInt() == (int16_t)0);
305
LLAssert(llong((int16_t)1).asInt() == (int16_t)1);
306
LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1);
307
LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff);
308
LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff);
309
LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000);
311
// public int8_t constructor
312
LLAssert(llong((int8_t)0).asInt() == (int8_t)0);
313
LLAssert(llong((int8_t)1).asInt() == (int8_t)1);
314
LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1);
315
LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f);
316
LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff);
317
LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80);
319
// public uint16_t constructor
320
LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0);
321
LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1);
322
LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1);
323
LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff);
324
LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff);
325
LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000);
327
// public uint32_t constructor
328
LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0);
329
LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1);
330
LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1);
331
LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff);
332
LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1);
333
LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
335
// public double constructor
336
LLAssert(llong((double)0).asDouble() == (double)0);
337
LLAssert(llong((double)1).asDouble() == (double)1);
338
LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff);
339
LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000);
340
LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001);
342
// can't access uprv_maxmantissa, so fake it
343
double maxmantissa = (llong((int32_t)1) << 40).asDouble();
344
LLAssert(llong(maxmantissa).asDouble() == maxmantissa);
345
LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa);
348
LLAssert(llong(llong(0, 1)).asDouble() == 1);
349
LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32);
350
LLAssert(llong(llong(-1, (uint32_t)-1)).asDouble() == -1);
352
// asInt - test unsigned to signed narrowing conversion
353
LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff);
354
LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000);
356
// asUInt - test signed to unsigned narrowing conversion
357
LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1);
358
LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000);
360
// asDouble already tested
364
void IntlTestRBNF::TestLLongSimpleOperators()
366
logln("Testing simple operators");
369
LLAssert(llong() == llong(0, 0));
370
LLAssert(llong(1,0) == llong(1, 0));
371
LLAssert(llong(0,1) == llong(0, 1));
374
LLAssert(llong(1,0) != llong(1,1));
375
LLAssert(llong(0,1) != llong(1,1));
376
LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff));
379
LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff)));
382
LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1)));
385
LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff)));
386
LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1)));
389
LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1)));
390
LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1)));
393
LLAssert(llong(1, 1) > llong(1, 0));
394
LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff));
395
LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0));
396
LLAssert(llong(1, 0) > llong(0, 0x7fffffff));
397
LLAssert(llong(1, 0) > llong(0, 0xffffffff));
398
LLAssert(llong(0, 0) > llong(0x80000000, 1));
401
LLAssert(llong(1, 0) < llong(1, 1));
402
LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000));
403
LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1));
404
LLAssert(llong(0, 0x7fffffff) < llong(1, 0));
405
LLAssert(llong(0, 0xffffffff) < llong(1, 0));
406
LLAssert(llong(0x80000000, 1) < llong(0, 0));
409
LLAssert(llong(1, 1) >= llong(1, 0));
410
LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff));
411
LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0));
412
LLAssert(llong(1, 0) >= llong(0, 0x7fffffff));
413
LLAssert(llong(1, 0) >= llong(0, 0xffffffff));
414
LLAssert(llong(0, 0) >= llong(0x80000000, 1));
415
LLAssert(llong() >= llong(0, 0));
416
LLAssert(llong(1,0) >= llong(1, 0));
417
LLAssert(llong(0,1) >= llong(0, 1));
420
LLAssert(llong(1, 0) <= llong(1, 1));
421
LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000));
422
LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1));
423
LLAssert(llong(0, 0x7fffffff) <= llong(1, 0));
424
LLAssert(llong(0, 0xffffffff) <= llong(1, 0));
425
LLAssert(llong(0x80000000, 1) <= llong(0, 0));
426
LLAssert(llong() <= llong(0, 0));
427
LLAssert(llong(1,0) <= llong(1, 0));
428
LLAssert(llong(0,1) <= llong(0, 1));
431
LLAssert(llong() == (int32_t)0);
432
LLAssert(llong(0,1) == (int32_t)1);
435
LLAssert(llong(1,0) != (int32_t)0);
436
LLAssert(llong(0,1) != (int32_t)2);
437
LLAssert(llong(0,0xffffffff) != (int32_t)-1);
439
llong negOne(0xffffffff, 0xffffffff);
442
LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff);
443
LLAssert(negOne > (int32_t)-2);
444
LLAssert(llong(1, 0) > (int32_t)0x7fffffff);
445
LLAssert(llong(0, 0) > (int32_t)-1);
448
LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff);
449
LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1);
452
LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff);
453
LLAssert(negOne >= (int32_t)-2);
454
LLAssert(llong(1, 0) >= (int32_t)0x7fffffff);
455
LLAssert(llong(0, 0) >= (int32_t)-1);
456
LLAssert(llong() >= (int32_t)0);
457
LLAssert(llong(0,1) >= (int32_t)1);
460
LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff);
461
LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1);
462
LLAssert(llong() <= (int32_t)0);
463
LLAssert(llong(0,1) <= (int32_t)1);
466
LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1);
469
LLAssert((llong(1, 1) <<= 0) == llong(1, 1));
470
LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000));
471
LLAssert((llong(1, 1) <<= 32) == llong(1, 0));
472
LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0));
473
LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used
474
LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used
477
LLAssert((llong((int32_t)1) << 5).asUInt() == 32);
479
// operator >>= (sign extended)
480
LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc));
481
LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde));
482
LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff));
483
LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000));
484
LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
485
LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used
487
// operator >> sign extended)
488
LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde));
490
// ushr (right shift without sign extension)
491
LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc));
492
LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde));
493
LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1));
494
LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000));
495
LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used
496
LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used
499
LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
502
LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
505
LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
508
LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
511
LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
514
LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
517
LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa));
520
LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000));
523
LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff));
526
LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff));
528
// operator&=(uint32)
529
LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000));
531
// operator|=(uint32)
532
LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff));
534
// operator^=(uint32)
535
LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff));
538
LLAssert(llong(1, 0) == ++llong(0,0xffffffff));
541
LLAssert(llong(0,0xffffffff) == --llong(1, 0));
545
llong n(0, 0xffffffff);
546
LLAssert(llong(0, 0xffffffff) == n++);
547
LLAssert(llong(1, 0) == n);
553
LLAssert(llong(1, 0) == n--);
554
LLAssert(llong(0, 0xffffffff) == n);
558
LLAssert(llong(0, 0) == -llong(0, 0));
559
LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1));
560
LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff));
561
LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1));
562
LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow
567
LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff));
568
LLAssert(n == llong(0xffffffff, 0xffffffff));
571
LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff));
572
LLAssert(n == llong(0, 0xffffffff));
578
LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff));
579
LLAssert(n == llong(0, 0));
582
LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff));
583
LLAssert(n == llong(1, 0));
588
llong n(0xffffffff, 0xffffffff);
589
LLAssert((n += llong(0, 1)) == llong(0, 0));
590
LLAssert(n == llong(0, 0));
592
n = llong(0, 0xffffffff);
593
LLAssert((n += llong(0, 1)) == llong(1, 0));
594
LLAssert(n == llong(1, 0));
599
llong n(0xffffffff, 0xffffffff);
600
LLAssert((n + llong(0, 1)) == llong(0, 0));
601
LLAssert(n == llong(0xffffffff, 0xffffffff));
603
n = llong(0, 0xffffffff);
604
LLAssert((n + llong(0, 1)) == llong(1, 0));
605
LLAssert(n == llong(0, 0xffffffff));
610
void IntlTestRBNF::TestLLong()
612
logln("Starting TestLLong");
614
TestLLongConstructors();
616
TestLLongSimpleOperators();
618
logln("Testing operator*=, operator*");
620
// operator*=, operator*
621
// small and large values, positive, &NEGative, zero
622
// also test commutivity
625
const llong ONE(0, 1);
626
const llong NEG_ONE((int32_t)-1);
627
const llong THREE(0, 3);
628
const llong NEG_THREE((int32_t)-3);
629
const llong TWO_TO_16(0, 0x10000);
630
const llong NEG_TWO_TO_16 = -TWO_TO_16;
631
const llong TWO_TO_32(1, 0);
632
const llong NEG_TWO_TO_32 = -TWO_TO_32;
634
const llong NINE(0, 9);
635
const llong NEG_NINE = -NINE;
637
const llong TWO_TO_16X3(0, 0x00030000);
638
const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3;
640
const llong TWO_TO_32X3(3, 0);
641
const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3;
643
const llong TWO_TO_48(0x10000, 0);
644
const llong NEG_TWO_TO_48 = -TWO_TO_48;
646
const int32_t VALUE_WIDTH = 9;
647
const llong* values[VALUE_WIDTH] = {
648
&ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32
651
const llong* answers[VALUE_WIDTH*VALUE_WIDTH] = {
652
&ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO,
653
&ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32,
654
&ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32,
655
&ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3,
656
&ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3,
657
&ZERO, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_48, &NEG_TWO_TO_48,
658
&ZERO, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_48, &TWO_TO_48,
659
&ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO,
660
&ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO
663
for (int i = 0; i < VALUE_WIDTH; ++i) {
664
for (int j = 0; j < VALUE_WIDTH; ++j) {
665
llong lhs = *values[i];
666
llong rhs = *values[j];
667
llong ans = *answers[i*VALUE_WIDTH + j];
671
LLAssert((n *= rhs) == ans);
675
LLAssert((n * rhs) == ans);
681
logln("Testing operator/=, operator/");
682
// operator/=, operator/
683
// test num = 0, div = 0, pos/neg, > 2^32, div > num
686
const llong ONE(0, 1);
687
const llong NEG_ONE = -ONE;
688
const llong MAX(0x7fffffff, 0xffffffff);
689
const llong MIN(0x80000000, 0);
690
const llong TWO(0, 2);
691
const llong NEG_TWO = -TWO;
692
const llong FIVE(0, 5);
693
const llong NEG_FIVE = -FIVE;
694
const llong TWO_TO_32(1, 0);
695
const llong NEG_TWO_TO_32 = -TWO_TO_32;
696
const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0);
697
const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5;
698
const llong TWO_TO_32X5 = TWO_TO_32 * FIVE;
699
const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5;
701
const llong* tuples[] = { // lhs, rhs, ans
704
&NEG_ONE, &ZERO, &MIN,
706
&ONE, &NEG_ONE, &NEG_ONE,
707
&NEG_ONE, &ONE, &NEG_ONE,
708
&NEG_ONE, &NEG_ONE, &ONE,
710
&FIVE, &NEG_TWO, &NEG_TWO,
711
&NEG_FIVE, &TWO, &NEG_TWO,
712
&NEG_FIVE, &NEG_TWO, &TWO,
714
&TWO, &NEG_FIVE, &ZERO,
715
&NEG_TWO, &FIVE, &ZERO,
716
&NEG_TWO, &NEG_FIVE, &ZERO,
717
&TWO_TO_32, &TWO_TO_32, &ONE,
718
&TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE,
719
&NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE,
720
&NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE,
721
&TWO_TO_32, &FIVE, &TWO_TO_32d5,
722
&TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5,
723
&NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5,
724
&NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5,
725
&TWO_TO_32X5, &FIVE, &TWO_TO_32,
726
&TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32,
727
&NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32,
728
&NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32,
729
&TWO_TO_32X5, &TWO_TO_32, &FIVE,
730
&TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE,
731
&NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE,
732
&NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE
734
const int TUPLE_WIDTH = 3;
735
const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH;
736
for (int i = 0; i < TUPLE_COUNT; ++i) {
737
const llong lhs = *tuples[i*TUPLE_WIDTH+0];
738
const llong rhs = *tuples[i*TUPLE_WIDTH+1];
739
const llong ans = *tuples[i*TUPLE_WIDTH+2];
742
if (!((n /= rhs) == ans)) {
743
errln("fail: (n /= rhs) == ans");
748
LLAssert((n / rhs) == ans);
753
logln("Testing operator%%=, operator%%");
754
//operator%=, operator%
757
const llong ONE(0, 1);
758
const llong TWO(0, 2);
759
const llong THREE(0,3);
760
const llong FOUR(0, 4);
761
const llong FIVE(0, 5);
762
const llong SIX(0, 6);
764
const llong NEG_ONE = -ONE;
765
const llong NEG_TWO = -TWO;
766
const llong NEG_THREE = -THREE;
767
const llong NEG_FOUR = -FOUR;
768
const llong NEG_FIVE = -FIVE;
769
const llong NEG_SIX = -SIX;
771
const llong NINETY_NINE(0, 99);
772
const llong HUNDRED(0, 100);
773
const llong HUNDRED_ONE(0, 101);
775
const llong BIG(0x12345678, 0x9abcdef0);
776
const llong BIG_FIVE(BIG * FIVE);
777
const llong BIG_FIVEm1 = BIG_FIVE - ONE;
778
const llong BIG_FIVEp1 = BIG_FIVE + ONE;
780
const llong* tuples[] = {
784
&THREE, &FIVE, &THREE,
788
&ZERO, &NEG_FIVE, &ZERO,
789
&ONE, &NEG_FIVE, &ONE,
790
&TWO, &NEG_FIVE, &TWO,
791
&THREE, &NEG_FIVE, &THREE,
792
&FOUR, &NEG_FIVE, &FOUR,
793
&FIVE, &NEG_FIVE, &ZERO,
794
&SIX, &NEG_FIVE, &ONE,
795
&NEG_ONE, &FIVE, &NEG_ONE,
796
&NEG_TWO, &FIVE, &NEG_TWO,
797
&NEG_THREE, &FIVE, &NEG_THREE,
798
&NEG_FOUR, &FIVE, &NEG_FOUR,
799
&NEG_FIVE, &FIVE, &ZERO,
800
&NEG_SIX, &FIVE, &NEG_ONE,
801
&NEG_ONE, &NEG_FIVE, &NEG_ONE,
802
&NEG_TWO, &NEG_FIVE, &NEG_TWO,
803
&NEG_THREE, &NEG_FIVE, &NEG_THREE,
804
&NEG_FOUR, &NEG_FIVE, &NEG_FOUR,
805
&NEG_FIVE, &NEG_FIVE, &ZERO,
806
&NEG_SIX, &NEG_FIVE, &NEG_ONE,
807
&NINETY_NINE, &FIVE, &FOUR,
808
&HUNDRED, &FIVE, &ZERO,
809
&HUNDRED_ONE, &FIVE, &ONE,
810
&BIG_FIVEm1, &FIVE, &FOUR,
811
&BIG_FIVE, &FIVE, &ZERO,
812
&BIG_FIVEp1, &FIVE, &ONE
814
const int TUPLE_WIDTH = 3;
815
const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH;
816
for (int i = 0; i < TUPLE_COUNT; ++i) {
817
const llong lhs = *tuples[i*TUPLE_WIDTH+0];
818
const llong rhs = *tuples[i*TUPLE_WIDTH+1];
819
const llong ans = *tuples[i*TUPLE_WIDTH+2];
822
if (!((n %= rhs) == ans)) {
823
errln("fail: (n %= rhs) == ans");
828
LLAssert((n % rhs) == ans);
833
logln("Testing pow");
835
LLAssert(llong(0, 0).pow(0) == llong(0, 0));
836
LLAssert(llong(0, 0).pow(2) == llong(0, 0));
837
LLAssert(llong(0, 2).pow(0) == llong(0, 1));
838
LLAssert(llong(0, 2).pow(2) == llong(0, 4));
839
LLAssert(llong(0, 2).pow(32) == llong(1, 0));
840
LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5));
844
const llong n(0xffffffff,0xffffffff);
845
LLAssert(n.abs() == llong(0, 1));
849
logln("Testing atoll");
851
const char empty[] = "";
852
const char zero[] = "0";
853
const char neg_one[] = "-1";
854
const char neg_12345[] = "-12345";
855
const char big1[] = "123456789abcdef0";
856
const char big2[] = "fFfFfFfFfFfFfFfF";
857
LLAssert(llong::atoll(empty) == llong(0, 0));
858
LLAssert(llong::atoll(zero) == llong(0, 0));
859
LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff));
860
LLAssert(llong::atoll(neg_12345) == -llong(0, 12345));
861
LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0));
862
LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff));
866
const UChar uempty[] = { 0 };
867
const UChar uzero[] = { 0x30, 0 };
868
const UChar uneg_one[] = { 0x2d, 0x31, 0 };
869
const UChar uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 };
870
const UChar ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 };
871
const UChar ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 };
872
LLAssert(llong::utoll(uempty) == llong(0, 0));
873
LLAssert(llong::utoll(uzero) == llong(0, 0));
874
LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff));
875
LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345));
876
LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0));
877
LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff));
880
logln("Testing lltoa");
883
char buf[64]; // ascii
884
LLAssert((llong(0, 0).lltoa(buf, (uint32_t)sizeof(buf)) == 1) && (strcmp(buf, zero) == 0));
885
LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, (uint32_t)sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0));
886
LLAssert(((-llong(0, 12345)).lltoa(buf, (uint32_t)sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0));
887
LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, (uint32_t)sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0));
891
logln("Testing u_lltoa");
895
LLAssert((llong(0, 0).lltou(buf, (uint32_t)sizeof(buf)) == 1) && (u_strcmp(buf, uzero) == 0));
896
LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, (uint32_t)sizeof(buf)) == 2) && (u_strcmp(buf, uneg_one) == 0));
897
LLAssert(((-llong(0, 12345)).lltou(buf, (uint32_t)sizeof(buf)) == 6) && (u_strcmp(buf, uneg_12345) == 0));
898
LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, (uint32_t)sizeof(buf), 16) == 16) && (u_strcmp(buf, ubig1) == 0));
906
IntlTestRBNF::TestEnglishSpellout()
908
UErrorCode status = U_ZERO_ERROR;
909
RuleBasedNumberFormat* formatter
910
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
912
if (U_FAILURE(status)) {
913
errln("FAIL: could not construct formatter");
915
static const char* testData[][2] = {
920
{ "23", "twenty-three" },
921
{ "73", "seventy-three" },
922
{ "88", "eighty-eight" },
923
{ "100", "one hundred" },
924
{ "106", "one hundred and six" },
925
{ "127", "one hundred and twenty-seven" },
926
{ "200", "two hundred" },
927
{ "579", "five hundred and seventy-nine" },
928
{ "1,000", "one thousand" },
929
{ "2,000", "two thousand" },
930
{ "3,004", "three thousand and four" },
931
{ "4,567", "four thousand five hundred and sixty-seven" },
932
{ "15,943", "fifteen thousand nine hundred and forty-three" },
933
{ "2,345,678", "two million, three hundred and forty-five thousand, six hundred and seventy-eight" },
934
{ "-36", "minus thirty-six" },
935
{ "234.567", "two hundred and thirty-four point five six seven" },
939
doTest(formatter, testData, TRUE);
941
formatter->setLenient(TRUE);
942
static const char* lpTestData[][2] = {
944
{ " fifty-7", "57" },
945
{ " fifty-7", "57" },
946
{ "2 thousand six HUNDRED fifty-7", "2,657" },
947
{ "fifteen hundred and zero", "1,500" },
948
{ "FOurhundred thiRTY six", "436" },
951
doLenientParseTest(formatter, lpTestData);
957
IntlTestRBNF::TestOrdinalAbbreviations()
959
UErrorCode status = U_ZERO_ERROR;
960
RuleBasedNumberFormat* formatter
961
= new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status);
963
if (U_FAILURE(status)) {
964
errln("FAIL: could not construct formatter");
966
static const char* testData[][2] = {
983
{ "12,345", "12,345th" },
987
doTest(formatter, testData, FALSE);
993
IntlTestRBNF::TestDurations()
995
UErrorCode status = U_ZERO_ERROR;
996
RuleBasedNumberFormat* formatter
997
= new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status);
999
if (U_FAILURE(status)) {
1000
errln("FAIL: could not construct formatter");
1002
static const char* testData[][2] = {
1003
{ "3,600", "1:00:00" }, //move me and I fail
1006
{ "24", "24 sec." },
1011
// { "3,600", "1:00:00" },
1012
{ "3,740", "1:02:20" },
1013
{ "10,293", "2:51:33" },
1017
doTest(formatter, testData, TRUE);
1019
formatter->setLenient(TRUE);
1020
static const char* lpTestData[][2] = {
1021
{ "2-51-33", "10,293" },
1024
doLenientParseTest(formatter, lpTestData);
1030
IntlTestRBNF::TestSpanishSpellout()
1032
UErrorCode status = U_ZERO_ERROR;
1033
RuleBasedNumberFormat* formatter
1034
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "ES", ""), status);
1036
if (U_FAILURE(status)) {
1037
errln("FAIL: could not construct formatter");
1039
static const char* testData[][2] = {
1042
{ "16", "diecis\\u00e9is" },
1044
{ "24", "veinticuatro" },
1045
{ "26", "veintis\\u00e9is" },
1046
{ "73", "setenta y tres" },
1047
{ "88", "ochenta y ocho" },
1049
{ "106", "ciento seis" },
1050
{ "127", "ciento veintisiete" },
1051
{ "200", "doscientos" },
1052
{ "579", "quinientos setenta y nueve" },
1054
{ "2,000", "dos mil" },
1055
{ "3,004", "tres mil cuatro" },
1056
{ "4,567", "cuatro mil quinientos sesenta y siete" },
1057
{ "15,943", "quince mil novecientos cuarenta y tres" },
1058
{ "2,345,678", "dos mill\\u00f3n trescientos cuarenta y cinco mil seiscientos setenta y ocho"},
1059
{ "-36", "menos treinta y seis" },
1060
{ "234.567", "doscientos treinta y cuatro punto cinco seis siete" },
1064
doTest(formatter, testData, TRUE);
1070
IntlTestRBNF::TestFrenchSpellout()
1072
UErrorCode status = U_ZERO_ERROR;
1073
RuleBasedNumberFormat* formatter
1074
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status);
1076
if (U_FAILURE(status)) {
1077
errln("FAIL: could not construct formatter");
1079
static const char* testData[][2] = {
1083
{ "21", "vingt-et-un" },
1084
{ "23", "vingt-trois" },
1085
{ "62", "soixante-deux" },
1086
{ "70", "soixante-dix" },
1087
{ "71", "soixante et onze" },
1088
{ "73", "soixante-treize" },
1089
{ "80", "quatre-vingts" },
1090
{ "88", "quatre-vingt-huit" },
1092
{ "106", "cent six" },
1093
{ "127", "cent vingt-sept" },
1094
{ "200", "deux cents" },
1095
{ "579", "cinq cents soixante-dix-neuf" },
1096
{ "1,000", "mille" },
1097
{ "1,123", "onze cents vingt-trois" },
1098
{ "1,594", "mille cinq cents quatre-vingt-quatorze" },
1099
{ "2,000", "deux mille" },
1100
{ "3,004", "trois mille quatre" },
1101
{ "4,567", "quatre mille cinq cents soixante-sept" },
1102
{ "15,943", "quinze mille neuf cents quarante-trois" },
1103
{ "2,345,678", "deux million trois cents quarante-cinq mille six cents soixante-dix-huit" },
1104
{ "-36", "moins trente-six" },
1105
{ "234.567", "deux cents trente-quatre virgule cinq six sept" },
1109
doTest(formatter, testData, TRUE);
1111
formatter->setLenient(TRUE);
1112
static const char* lpTestData[][2] = {
1113
{ "trente-un", "31" },
1114
{ "un cents quatre vingt dix huit", "198" },
1117
doLenientParseTest(formatter, lpTestData);
1123
IntlTestRBNF::TestSwissFrenchSpellout()
1125
UErrorCode status = U_ZERO_ERROR;
1126
RuleBasedNumberFormat* formatter
1127
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH", ""), status);
1129
if (U_FAILURE(status)) {
1130
errln("FAIL: could not construct formatter");
1132
static const char* testData[][2] = {
1136
{ "21", "vingt-et-un" },
1137
{ "23", "vingt-trois" },
1138
{ "62", "soixante-deux" },
1139
{ "70", "septante" },
1140
{ "71", "septante-et-un" },
1141
{ "73", "septante-trois" },
1142
{ "80", "octante" },
1143
{ "88", "octante-huit" },
1145
{ "106", "cent six" },
1146
{ "127", "cent vingt-sept" },
1147
{ "200", "deux cents" },
1148
{ "579", "cinq cents septante-neuf" },
1149
{ "1,000", "mille" },
1150
{ "1,123", "onze cents vingt-trois" },
1151
{ "1,594", "mille cinq cents nonante-quatre" },
1152
{ "2,000", "deux mille" },
1153
{ "3,004", "trois mille quatre" },
1154
{ "4,567", "quatre mille cinq cents soixante-sept" },
1155
{ "15,943", "quinze mille neuf cents quarante-trois" },
1156
{ "2,345,678", "deux million trois cents quarante-cinq mille six cents septante-huit" },
1157
{ "-36", "moins trente-six" },
1158
{ "234.567", "deux cents trente-quatre virgule cinq six sept" },
1162
doTest(formatter, testData, TRUE);
1168
IntlTestRBNF::TestItalianSpellout()
1170
UErrorCode status = U_ZERO_ERROR;
1171
RuleBasedNumberFormat* formatter
1172
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status);
1174
if (U_FAILURE(status)) {
1175
errln("FAIL: could not construct formatter");
1177
static const char* testData[][2] = {
1179
{ "15", "quindici" },
1181
{ "23", "ventitre" },
1182
{ "73", "settantatre" },
1183
{ "88", "ottantotto" },
1185
{ "106", "centosei" },
1186
{ "108", "centotto" },
1187
{ "127", "centoventisette" },
1188
{ "181", "centottantuno" },
1189
{ "200", "duecento" },
1190
{ "579", "cinquecentosettantanove" },
1191
{ "1,000", "mille" },
1192
{ "2,000", "duemila" },
1193
{ "3,004", "tremilaquattro" },
1194
{ "4,567", "quattromilacinquecentosessantasette" },
1195
{ "15,943", "quindicimilanovecentoquarantatre" },
1196
{ "-36", "meno trentisei" },
1197
{ "234.567", "duecentotrentiquattro virgola cinque sei sette" },
1201
doTest(formatter, testData, TRUE);
1207
IntlTestRBNF::TestGermanSpellout()
1209
UErrorCode status = U_ZERO_ERROR;
1210
RuleBasedNumberFormat* formatter
1211
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status);
1213
if (U_FAILURE(status)) {
1214
errln("FAIL: could not construct formatter");
1216
static const char* testData[][2] = {
1218
{ "15", "f\\u00fcnfzehn" },
1219
{ "20", "zwanzig" },
1220
{ "23", "dreiundzwanzig" },
1221
{ "73", "dreiundsiebzig" },
1222
{ "88", "achtundachtzig" },
1223
{ "100", "hundert" },
1224
{ "106", "hundertsechs" },
1225
{ "127", "hundertsiebenundzwanzig" },
1226
{ "200", "zweihundert" },
1227
{ "579", "f\\u00fcnfhundertneunundsiebzig" },
1228
{ "1,000", "tausend" },
1229
{ "2,000", "zweitausend" },
1230
{ "3,004", "dreitausendvier" },
1231
{ "4,567", "viertausendf\\u00fcnfhundertsiebenundsechzig" },
1232
{ "15,943", "f\\u00fcnfzehntausendneunhundertdreiundvierzig" },
1233
{ "2,345,678", "zwei Millionen dreihundertf\\u00fcnfundvierzigtausendsechshundertachtundsiebzig" },
1237
doTest(formatter, testData, TRUE);
1239
formatter->setLenient(TRUE);
1240
static const char* lpTestData[][2] = {
1241
{ "ein Tausend sechs Hundert fuenfunddreissig", "1,635" },
1244
doLenientParseTest(formatter, lpTestData);
1250
IntlTestRBNF::TestThaiSpellout()
1252
UErrorCode status = U_ZERO_ERROR;
1253
RuleBasedNumberFormat* formatter
1254
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("th"), status);
1256
if (U_FAILURE(status)) {
1257
errln("FAIL: could not construct formatter");
1259
static const char* testData[][2] = {
1260
{ "0", "\\u0e28\\u0e39\\u0e19\\u0e22\\u0e4c" },
1261
{ "1", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1262
{ "10", "\\u0e2a\\u0e34\\u0e1a" },
1263
{ "11", "\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" },
1264
{ "21", "\\u0e22\\u0e35\\u0e48\\u0e2a\\u0e34\\u0e1a\\u0e40\\u0e2d\\u0e47\\u0e14" },
1265
{ "101", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e23\\u0e49\\u0e2d\\u0e22\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07" },
1266
{ "1.234", "\\u0e2b\\u0e19\\u0e36\\u0e48\\u0e07\\u0e08\\u0e38\\u0e14\\u0e2a\\u0e2d\\u0e07\\u0e2a\\u0e32\\u0e21\\u0e2a\\u0e35\\u0e48" },
1270
doTest(formatter, testData, TRUE);
1276
IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* testData[][2], UBool testParsing)
1278
// man, error reporting would be easier with printf-style syntax for unicode string and formattable
1280
UErrorCode status = U_ZERO_ERROR;
1281
NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
1282
if (U_FAILURE(status)) {
1283
errln("FAIL: could not create NumberFormat");
1285
for (int i = 0; testData[i][0]; ++i) {
1286
const char* numString = testData[i][0];
1287
const char* expectedWords = testData[i][1];
1289
Formattable expectedNumber;
1290
decFmt->parse(numString, expectedNumber, status);
1291
if (U_FAILURE(status)) {
1292
errln("FAIL: decFmt could not parse %s", numString);
1295
UnicodeString actualString;
1297
formatter->format(expectedNumber, actualString/* , pos*/, status);
1298
if (U_FAILURE(status)) {
1299
UnicodeString msg = "Fail: formatter could not format ";
1300
decFmt->format(expectedNumber, msg, status);
1304
UnicodeString expectedString = UnicodeString(expectedWords).unescape();
1305
if (actualString != expectedString) {
1306
UnicodeString msg = "FAIL: check failed for ";
1307
decFmt->format(expectedNumber, msg, status);
1308
msg.append(", expected ");
1309
msg.append(expectedString);
1310
msg.append(" but got ");
1311
msg.append(actualString);
1314
} else if (testParsing) {
1315
Formattable parsedNumber;
1316
formatter->parse(actualString, parsedNumber, status);
1317
if (U_FAILURE(status)) {
1318
UnicodeString msg = "FAIL: formatter could not parse ";
1319
msg.append(actualString);
1320
msg.append(" status code: " );
1322
sprintf(buffer, "0x%x", status);
1327
if (parsedNumber != expectedNumber) {
1328
UnicodeString msg = "FAIL: parse failed for ";
1329
msg.append(actualString);
1330
msg.append(", expected ");
1331
decFmt->format(expectedNumber, msg, status);
1332
msg.append(", but got ");
1333
decFmt->format(parsedNumber, msg, status);
1347
IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2])
1349
UErrorCode status = U_ZERO_ERROR;
1350
NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status);
1351
if (U_FAILURE(status)) {
1352
errln("FAIL: could not create NumberFormat");
1354
for (int i = 0; testData[i][0]; ++i) {
1355
const char* spelledNumber = testData[i][0]; // spelled-out number
1356
const char* asciiUSNumber = testData[i][1]; // number as ascii digits formatted for US locale
1358
UnicodeString spelledNumberString = UnicodeString(spelledNumber).unescape();
1359
Formattable actualNumber;
1360
formatter->parse(spelledNumberString, actualNumber, status);
1361
if (U_FAILURE(status)) {
1362
UnicodeString msg = "FAIL: formatter could not parse ";
1363
msg.append(spelledNumberString);
1367
// I changed the logic of this test somewhat from Java-- instead of comparing the
1368
// strings, I compare the Formattables. Hmmm, but the Formattables don't compare,
1369
// so change it back.
1371
UnicodeString asciiUSNumberString = asciiUSNumber;
1372
Formattable expectedNumber;
1373
decFmt->parse(asciiUSNumberString, expectedNumber, status);
1374
if (U_FAILURE(status)) {
1375
UnicodeString msg = "FAIL: decFmt could not parse ";
1376
msg.append(asciiUSNumberString);
1380
UnicodeString actualNumberString;
1381
UnicodeString expectedNumberString;
1382
decFmt->format(actualNumber, actualNumberString, status);
1383
decFmt->format(expectedNumber, expectedNumberString, status);
1384
if (actualNumberString != expectedNumberString) {
1385
UnicodeString msg = "FAIL: parsing";
1386
msg.append(asciiUSNumberString);
1388
msg.append(" lenient parse failed for ");
1389
msg.append(spelledNumberString);
1390
msg.append(", expected ");
1391
msg.append(expectedNumberString);
1392
msg.append(", but got ");
1393
msg.append(actualNumberString);
1408
IntlTestRBNF::TestRBNFDisabled() {
1409
errln("*** RBNF currently disabled on this platform ***\n");