1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7
8 #include "unicode/dcfmtsym.h"
9
10 #include "cstr.h"
11 #include "numbertest.h"
12 #include "number_utils.h"
13 #include "number_skeletons.h"
14 #include "putilimp.h"
15
16 using namespace icu::number::impl;
17
18
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)19 void NumberSkeletonTest::runIndexedTest(int32_t index, UBool exec, const char*& name, char*) {
20 if (exec) {
21 logln("TestSuite AffixUtilsTest: ");
22 }
23 TESTCASE_AUTO_BEGIN;
24 TESTCASE_AUTO(validTokens);
25 TESTCASE_AUTO(invalidTokens);
26 TESTCASE_AUTO(unknownTokens);
27 TESTCASE_AUTO(unexpectedTokens);
28 TESTCASE_AUTO(duplicateValues);
29 TESTCASE_AUTO(stemsRequiringOption);
30 TESTCASE_AUTO(defaultTokens);
31 TESTCASE_AUTO(flexibleSeparators);
32 TESTCASE_AUTO(wildcardCharacters);
33 TESTCASE_AUTO(perUnitInArabic);
34 TESTCASE_AUTO(perUnitToSkeleton);
35 TESTCASE_AUTO_END;
36 }
37
validTokens()38 void NumberSkeletonTest::validTokens() {
39 IcuTestErrorCode status(*this, "validTokens");
40
41 // This tests only if the tokens are valid, not their behavior.
42 // Most of these are from the design doc.
43 static const char16_t* cases[] = {
44 u"precision-integer",
45 u"precision-unlimited",
46 u"@@@##",
47 u"@@*",
48 u"@@+",
49 u"@@+/w",
50 u".000##",
51 u".00*",
52 u".00+",
53 u".",
54 u"./w",
55 u".*",
56 u".+",
57 u".+/w",
58 u".######",
59 u".00/@@*",
60 u".00/@@+",
61 u".00/@##",
62 u".00/@##/w",
63 u".00/@",
64 u".00/@r",
65 u".00/@@s",
66 u".00/@@#r",
67 u"precision-increment/3.14",
68 u"precision-increment/3.14/w",
69 u"precision-currency-standard",
70 u"precision-currency-standard/w",
71 u"precision-integer rounding-mode-half-up",
72 u".00# rounding-mode-ceiling",
73 u".00/@@* rounding-mode-floor",
74 u".00/@@+ rounding-mode-floor",
75 u"scientific",
76 u"scientific/*ee",
77 u"scientific/+ee",
78 u"scientific/sign-always",
79 u"scientific/*ee/sign-always",
80 u"scientific/+ee/sign-always",
81 u"scientific/sign-always/*ee",
82 u"scientific/sign-always/+ee",
83 u"scientific/sign-except-zero",
84 u"engineering",
85 u"engineering/*eee",
86 u"engineering/+eee",
87 u"compact-short",
88 u"compact-long",
89 u"notation-simple",
90 u"percent",
91 u"permille",
92 u"measure-unit/length-meter",
93 u"measure-unit/area-square-meter",
94 u"measure-unit/energy-joule per-measure-unit/length-meter",
95 u"unit/square-meter-per-square-meter",
96 u"currency/XXX",
97 u"currency/ZZZ",
98 u"currency/usd",
99 u"group-off",
100 u"group-min2",
101 u"group-auto",
102 u"group-on-aligned",
103 u"group-thousands",
104 u"integer-width/00",
105 u"integer-width/#0",
106 u"integer-width/*00",
107 u"integer-width/+00",
108 u"sign-always",
109 u"sign-auto",
110 u"sign-never",
111 u"sign-accounting",
112 u"sign-accounting-always",
113 u"sign-except-zero",
114 u"sign-accounting-except-zero",
115 u"unit-width-narrow",
116 u"unit-width-short",
117 u"unit-width-iso-code",
118 u"unit-width-full-name",
119 u"unit-width-hidden",
120 u"decimal-auto",
121 u"decimal-always",
122 u"scale/5.2",
123 u"scale/-5.2",
124 u"scale/100",
125 u"scale/1E2",
126 u"scale/1",
127 u"latin",
128 u"numbering-system/arab",
129 u"numbering-system/latn",
130 u"precision-integer/@##",
131 u"precision-integer rounding-mode-ceiling",
132 u"precision-currency-cash rounding-mode-ceiling",
133 u"0",
134 u"00",
135 u"000",
136 u"E0",
137 u"E00",
138 u"E000",
139 u"EE0",
140 u"EE00",
141 u"EE+?0",
142 u"EE+?00",
143 u"EE+!0",
144 u"EE+!00",
145 };
146
147 for (auto& cas : cases) {
148 UnicodeString skeletonString(cas);
149 status.setScope(skeletonString);
150 UParseError perror;
151 NumberFormatter::forSkeleton(skeletonString, perror, status);
152 assertSuccess(CStr(skeletonString)(), status, true);
153 assertEquals(skeletonString, -1, perror.offset);
154 status.errIfFailureAndReset();
155 }
156 }
157
invalidTokens()158 void NumberSkeletonTest::invalidTokens() {
159 static const char16_t* cases[] = {
160 u".00x",
161 u".00i",
162 u".00/x",
163 u".00/ww",
164 u".00##0",
165 u".##*",
166 u".00##*",
167 u".0#*",
168 u"@#*",
169 u".##+",
170 u".00##+",
171 u".0#+",
172 u"@#+",
173 u"@@x",
174 u"@@##0",
175 u".00/@@",
176 u".00/@@x",
177 u".00/@@#",
178 u".00/@@#*",
179 u".00/floor/@@*", // wrong order
180 u".00/@@#+",
181 u".00/@@@+r",
182 u".00/floor/@@+", // wrong order
183 u"precision-increment/français", // non-invariant characters for C++
184 u"scientific/ee",
185 u"precision-increment/xxx",
186 u"precision-increment/NaN",
187 u"precision-increment/0.1.2",
188 u"scale/xxx",
189 u"scale/NaN",
190 u"scale/0.1.2",
191 u"scale/français", // non-invariant characters for C++
192 u"currency/dummy",
193 u"currency/ççç", // three characters but not ASCII
194 u"measure-unit/foo",
195 u"integer-width/xxx",
196 u"integer-width/0*",
197 u"integer-width/*0#",
198 u"integer-width/*#",
199 u"integer-width/*#0",
200 u"integer-width/0+",
201 u"integer-width/+0#",
202 u"integer-width/+#",
203 u"integer-width/+#0",
204 u"scientific/foo",
205 u"E",
206 u"E1",
207 u"E+",
208 u"E+?",
209 u"E+!",
210 u"E+0",
211 u"EE",
212 u"EE+",
213 u"EEE",
214 u"EEE0",
215 u"001",
216 u"00*",
217 u"00+",
218 };
219
220 expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
221 }
222
unknownTokens()223 void NumberSkeletonTest::unknownTokens() {
224 static const char16_t* cases[] = {
225 u"maesure-unit",
226 u"measure-unit/foo-bar",
227 u"numbering-system/dummy",
228 u"français",
229 u"measure-unit/français-français", // non-invariant characters for C++
230 u"numbering-system/français", // non-invariant characters for C++
231 u"currency-USD"};
232
233 expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
234 }
235
unexpectedTokens()236 void NumberSkeletonTest::unexpectedTokens() {
237 static const char16_t* cases[] = {
238 u".00/w/w",
239 u"group-thousands/foo",
240 u"precision-integer//@## group-off",
241 u"precision-integer//@## group-off",
242 u"precision-integer/ group-off",
243 u"precision-integer// group-off"};
244
245 expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
246 }
247
duplicateValues()248 void NumberSkeletonTest::duplicateValues() {
249 static const char16_t* cases[] = {
250 u"precision-integer precision-integer",
251 u"precision-integer .00+",
252 u"precision-integer precision-unlimited",
253 u"precision-integer @@@",
254 u"scientific engineering",
255 u"engineering compact-long",
256 u"sign-auto sign-always"};
257
258 expectedErrorSkeleton(cases, UPRV_LENGTHOF(cases));
259 }
260
stemsRequiringOption()261 void NumberSkeletonTest::stemsRequiringOption() {
262 static const char16_t* stems[] = {
263 u"precision-increment",
264 u"measure-unit",
265 u"per-measure-unit",
266 u"currency",
267 u"integer-width",
268 u"numbering-system",
269 u"scale"};
270 static const char16_t* suffixes[] = {u"", u"/@##", u" scientific", u"/@## scientific"};
271
272 for (auto& stem : stems) {
273 for (auto& suffix : suffixes) {
274 UnicodeString skeletonString = UnicodeString(stem) + suffix;
275 UErrorCode status = U_ZERO_ERROR;
276 UParseError perror;
277 NumberFormatter::forSkeleton(skeletonString, perror, status);
278 assertEquals(skeletonString, U_NUMBER_SKELETON_SYNTAX_ERROR, status);
279
280 // Check the UParseError for integrity.
281 // If an option is present, the option is wrong; error offset is at the start of the option
282 // If an option is not present, the error offset is at the token separator (end of stem)
283 int32_t expectedOffset = u_strlen(stem) + ((suffix[0] == u'/') ? 1 : 0);
284 assertEquals(skeletonString, expectedOffset, perror.offset);
285 UnicodeString expectedPreContext = skeletonString.tempSubString(0, expectedOffset);
286 if (expectedPreContext.length() >= U_PARSE_CONTEXT_LEN - 1) {
287 expectedPreContext = expectedPreContext.tempSubString(expectedOffset - U_PARSE_CONTEXT_LEN + 1);
288 }
289 assertEquals(skeletonString, expectedPreContext, perror.preContext);
290 UnicodeString expectedPostContext = skeletonString.tempSubString(expectedOffset);
291 // None of the postContext strings in this test exceed U_PARSE_CONTEXT_LEN
292 assertEquals(skeletonString, expectedPostContext, perror.postContext);
293 }
294 }
295 }
296
defaultTokens()297 void NumberSkeletonTest::defaultTokens() {
298 IcuTestErrorCode status(*this, "defaultTokens");
299
300 static const char16_t* cases[] = {
301 u"notation-simple",
302 u"base-unit",
303 u"group-auto",
304 u"integer-width/+0",
305 u"sign-auto",
306 u"unit-width-short",
307 u"decimal-auto"};
308
309 for (auto& cas : cases) {
310 UnicodeString skeletonString(cas);
311 status.setScope(skeletonString);
312 UnicodeString normalized = NumberFormatter::forSkeleton(
313 skeletonString, status).toSkeleton(status);
314 // Skeleton should become empty when normalized
315 assertEquals(skeletonString, u"", normalized);
316 status.errIfFailureAndReset();
317 }
318 }
319
flexibleSeparators()320 void NumberSkeletonTest::flexibleSeparators() {
321 IcuTestErrorCode status(*this, "flexibleSeparators");
322
323 static struct TestCase {
324 const char16_t* skeleton;
325 const char16_t* expected;
326 } cases[] = {{u"precision-integer group-off", u"5142"},
327 {u"precision-integer group-off", u"5142"},
328 {u"precision-integer/@## group-off", u"5140"},
329 {u"precision-integer/@## group-off", u"5140"}};
330
331 for (auto& cas : cases) {
332 UnicodeString skeletonString(cas.skeleton);
333 UnicodeString expected(cas.expected);
334 status.setScope(skeletonString);
335 UnicodeString actual = NumberFormatter::forSkeleton(skeletonString, status).locale("en")
336 .formatDouble(5142.3, status)
337 .toString(status);
338 if (!status.errDataIfFailureAndReset()) {
339 assertEquals(skeletonString, expected, actual);
340 }
341 status.errIfFailureAndReset();
342 }
343 }
344
wildcardCharacters()345 void NumberSkeletonTest::wildcardCharacters() {
346 IcuTestErrorCode status(*this, "wildcardCharacters");
347
348 struct TestCase {
349 const char16_t* star;
350 const char16_t* plus;
351 } cases[] = {
352 { u".00*", u".00+" },
353 { u"@@*", u"@@+" },
354 { u"scientific/*ee", u"scientific/+ee" },
355 { u"integer-width/*00", u"integer-width/+00" },
356 };
357
358 for (const auto& cas : cases) {
359 UnicodeString star(cas.star);
360 UnicodeString plus(cas.plus);
361 status.setScope(star);
362
363 UnicodeString normalized = NumberFormatter::forSkeleton(plus, status)
364 .toSkeleton(status);
365 assertEquals("Plus should normalize to star", star, normalized);
366 status.errIfFailureAndReset();
367 }
368 }
369
370 // In C++, there is no distinguishing between "invalid", "unknown", and "unexpected" tokens.
expectedErrorSkeleton(const char16_t ** cases,int32_t casesLen)371 void NumberSkeletonTest::expectedErrorSkeleton(const char16_t** cases, int32_t casesLen) {
372 for (int32_t i = 0; i < casesLen; i++) {
373 UnicodeString skeletonString(cases[i]);
374 UErrorCode status = U_ZERO_ERROR;
375 NumberFormatter::forSkeleton(skeletonString, status);
376 assertEquals(skeletonString, U_NUMBER_SKELETON_SYNTAX_ERROR, status);
377 }
378 }
379
perUnitInArabic()380 void NumberSkeletonTest::perUnitInArabic() {
381 IcuTestErrorCode status(*this, "perUnitInArabic");
382
383 struct TestCase {
384 const char16_t* type;
385 const char16_t* subtype;
386 } cases[] = {
387 {u"area", u"acre"},
388 {u"digital", u"bit"},
389 {u"digital", u"byte"},
390 {u"temperature", u"celsius"},
391 {u"length", u"centimeter"},
392 {u"duration", u"day"},
393 {u"angle", u"degree"},
394 {u"temperature", u"fahrenheit"},
395 {u"volume", u"fluid-ounce"},
396 {u"length", u"foot"},
397 {u"volume", u"gallon"},
398 {u"digital", u"gigabit"},
399 {u"digital", u"gigabyte"},
400 {u"mass", u"gram"},
401 {u"area", u"hectare"},
402 {u"duration", u"hour"},
403 {u"length", u"inch"},
404 {u"digital", u"kilobit"},
405 {u"digital", u"kilobyte"},
406 {u"mass", u"kilogram"},
407 {u"length", u"kilometer"},
408 {u"volume", u"liter"},
409 {u"digital", u"megabit"},
410 {u"digital", u"megabyte"},
411 {u"length", u"meter"},
412 {u"length", u"mile"},
413 {u"length", u"mile-scandinavian"},
414 {u"volume", u"milliliter"},
415 {u"length", u"millimeter"},
416 {u"duration", u"millisecond"},
417 {u"duration", u"minute"},
418 {u"duration", u"month"},
419 {u"mass", u"ounce"},
420 {u"concentr", u"percent"},
421 {u"digital", u"petabyte"},
422 {u"mass", u"pound"},
423 {u"duration", u"second"},
424 {u"mass", u"stone"},
425 {u"digital", u"terabit"},
426 {u"digital", u"terabyte"},
427 {u"duration", u"week"},
428 {u"length", u"yard"},
429 {u"duration", u"year"},
430 };
431
432 for (const auto& cas1 : cases) {
433 for (const auto& cas2 : cases) {
434 UnicodeString skeleton(u"measure-unit/");
435 skeleton += cas1.type;
436 skeleton += u"-";
437 skeleton += cas1.subtype;
438 skeleton += u" ";
439 skeleton += u"per-measure-unit/";
440 skeleton += cas2.type;
441 skeleton += u"-";
442 skeleton += cas2.subtype;
443
444 status.setScope(skeleton);
445 UnicodeString actual = NumberFormatter::forSkeleton(skeleton, status).locale("ar")
446 .formatDouble(5142.3, status)
447 .toString(status);
448 status.errIfFailureAndReset();
449 }
450 }
451 }
452
perUnitToSkeleton()453 void NumberSkeletonTest::perUnitToSkeleton() {
454 IcuTestErrorCode status(*this, "perUnitToSkeleton");
455 struct TestCase {
456 const char16_t* type;
457 const char16_t* subtype;
458 } cases[] = {
459 {u"area", u"acre"},
460 {u"concentr", u"percent"},
461 {u"concentr", u"permille"},
462 {u"concentr", u"permillion"},
463 {u"concentr", u"permyriad"},
464 {u"digital", u"bit"},
465 {u"length", u"yard"},
466 };
467
468 for (const auto& cas1 : cases) {
469 for (const auto& cas2 : cases) {
470 UnicodeString skeleton(u"measure-unit/");
471 skeleton += cas1.type;
472 skeleton += u"-";
473 skeleton += cas1.subtype;
474 skeleton += u" ";
475 skeleton += u"per-measure-unit/";
476 skeleton += cas2.type;
477 skeleton += u"-";
478 skeleton += cas2.subtype;
479
480 status.setScope(skeleton);
481 if (cas1.type != cas2.type && cas1.subtype != cas2.subtype) {
482 UnicodeString toSkeleton = NumberFormatter::forSkeleton(
483 skeleton, status).toSkeleton(status);
484 if (status.errIfFailureAndReset()) {
485 continue;
486 }
487 // Ensure both subtype are in the toSkeleton.
488 UnicodeString msg;
489 msg.append(toSkeleton)
490 .append(" should contain '")
491 .append(UnicodeString(cas1.subtype))
492 .append("' when constructed from ")
493 .append(skeleton);
494 assertTrue(msg, toSkeleton.indexOf(cas1.subtype) >= 0);
495
496 msg.remove();
497 msg.append(toSkeleton)
498 .append(" should contain '")
499 .append(UnicodeString(cas2.subtype))
500 .append("' when constructed from ")
501 .append(skeleton);
502 assertTrue(msg, toSkeleton.indexOf(cas2.subtype) >= 0);
503 }
504 }
505 }
506 }
507
508 #endif /* #if !UCONFIG_NO_FORMATTING */
509