1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2014, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 * File SCINUMBERFORMATTERTEST.CPP
11 *******************************************************************************
13 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/scientificnumberformatter.h"
20 #include "unicode/numfmt.h"
21 #include "unicode/decimfmt.h"
22 #include "unicode/localpointer.h"
24 class ScientificNumberFormatterTest
: public IntlTest
{
26 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=0);
30 void TestPlusSignInExponentMarkup();
31 void TestPlusSignInExponentSuperscript();
32 void TestFixedDecimalMarkup();
33 void TestFixedDecimalSuperscript();
36 void ScientificNumberFormatterTest::runIndexedTest(
37 int32_t index
, UBool exec
, const char *&name
, char *) {
39 logln("TestSuite ScientificNumberFormatterTest: ");
42 TESTCASE_AUTO(TestBasic
);
43 TESTCASE_AUTO(TestFarsi
);
44 TESTCASE_AUTO(TestPlusSignInExponentMarkup
);
45 TESTCASE_AUTO(TestPlusSignInExponentSuperscript
);
46 TESTCASE_AUTO(TestFixedDecimalMarkup
);
47 TESTCASE_AUTO(TestFixedDecimalSuperscript
);
51 void ScientificNumberFormatterTest::TestBasic() {
52 UErrorCode status
= U_ZERO_ERROR
;
53 UnicodeString
prefix("String: ");
54 UnicodeString
appendTo(prefix
);
55 LocalPointer
<ScientificNumberFormatter
> fmt(
56 ScientificNumberFormatter::createMarkupInstance(
57 "en" , "<sup>", "</sup>", status
));
58 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
61 fmt
->format(1.23456e-78, appendTo
, status
);
62 const char *expected
= "String: 1.23456\\u00d710<sup>-78</sup>";
65 UnicodeString(expected
).unescape(),
68 // Test superscript style
70 ScientificNumberFormatter::createSuperscriptInstance(
72 if (!assertSuccess("Can't create ScientificNumberFormatter2", status
)) {
76 fmt
->format(1.23456e-78, appendTo
, status
);
77 expected
= "String: 1.23456\\u00d710\\u207b\\u2077\\u2078";
80 UnicodeString(expected
).unescape(),
84 LocalPointer
<ScientificNumberFormatter
> fmt3(fmt
->clone());
86 errln("Allocating clone failed.");
90 fmt3
->format(1.23456e-78, appendTo
, status
);
91 expected
= "String: 1.23456\\u00d710\\u207b\\u2077\\u2078";
94 UnicodeString(expected
).unescape(),
96 assertSuccess("", status
);
99 void ScientificNumberFormatterTest::TestFarsi() {
100 UErrorCode status
= U_ZERO_ERROR
;
101 UnicodeString
prefix("String: ");
102 UnicodeString
appendTo(prefix
);
103 LocalPointer
<ScientificNumberFormatter
> fmt(
104 ScientificNumberFormatter::createMarkupInstance(
105 "fa", "<sup>", "</sup>", status
));
106 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
109 fmt
->format(1.23456e-78, appendTo
, status
);
110 const char *expected
= "String: \\u06F1\\u066B\\u06F2\\u06F3\\u06F4\\u06F5\\u06F6\\u00d7\\u06F1\\u06F0<sup>\\u200E\\u2212\\u06F7\\u06F8</sup>";
113 UnicodeString(expected
).unescape(),
115 assertSuccess("", status
);
118 void ScientificNumberFormatterTest::TestPlusSignInExponentMarkup() {
119 UErrorCode status
= U_ZERO_ERROR
;
120 LocalPointer
<DecimalFormat
> decfmt((DecimalFormat
*) NumberFormat::createScientificInstance("en", status
));
121 if (U_FAILURE(status
)) {
122 dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", status) - %s", u_errorName(status
));
125 decfmt
->applyPattern("0.00E+0", status
);
126 if (!assertSuccess("", status
)) {
129 UnicodeString appendTo
;
130 LocalPointer
<ScientificNumberFormatter
> fmt(
131 ScientificNumberFormatter::createMarkupInstance(
132 new DecimalFormat(*decfmt
), "<sup>", "</sup>", status
));
133 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
136 fmt
->format(6.02e23
, appendTo
, status
);
137 const char *expected
= "6.02\\u00d710<sup>+23</sup>";
140 UnicodeString(expected
).unescape(),
142 assertSuccess("", status
);
145 void ScientificNumberFormatterTest::TestPlusSignInExponentSuperscript() {
146 UErrorCode status
= U_ZERO_ERROR
;
147 LocalPointer
<DecimalFormat
> decfmt((DecimalFormat
*) NumberFormat::createScientificInstance("en", status
));
148 if (U_FAILURE(status
)) {
149 dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", status) - %s", u_errorName(status
));
152 decfmt
->applyPattern("0.00E+0", status
);
153 if (!assertSuccess("", status
)) {
156 UnicodeString appendTo
;
157 LocalPointer
<ScientificNumberFormatter
> fmt(
158 ScientificNumberFormatter::createSuperscriptInstance(
159 new DecimalFormat(*decfmt
), status
));
160 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
163 fmt
->format(6.02e23
, appendTo
, status
);
164 const char *expected
= "6.02\\u00d710\\u207a\\u00b2\\u00b3";
167 UnicodeString(expected
).unescape(),
169 assertSuccess("", status
);
172 void ScientificNumberFormatterTest::TestFixedDecimalMarkup() {
173 UErrorCode status
= U_ZERO_ERROR
;
174 LocalPointer
<DecimalFormat
> decfmt((DecimalFormat
*) NumberFormat::createInstance("en", status
));
175 if (assertSuccess("NumberFormat::createInstance", status
, TRUE
) == FALSE
) {
178 LocalPointer
<ScientificNumberFormatter
> fmt(
179 ScientificNumberFormatter::createMarkupInstance(
180 new DecimalFormat(*decfmt
), "<sup>", "</sup>", status
));
181 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
184 UnicodeString appendTo
;
185 fmt
->format(123456.0, appendTo
, status
);
186 const char *expected
= "123,456";
189 UnicodeString(expected
).unescape(),
191 assertSuccess("", status
);
194 void ScientificNumberFormatterTest::TestFixedDecimalSuperscript() {
195 UErrorCode status
= U_ZERO_ERROR
;
196 LocalPointer
<DecimalFormat
> decfmt((DecimalFormat
*) NumberFormat::createInstance("en", status
));
197 if (assertSuccess("NumberFormat::createInstance", status
, TRUE
) == FALSE
) {
200 LocalPointer
<ScientificNumberFormatter
> fmt(
201 ScientificNumberFormatter::createSuperscriptInstance(
202 new DecimalFormat(*decfmt
), status
));
203 if (!assertSuccess("Can't create ScientificNumberFormatter", status
)) {
206 UnicodeString appendTo
;
207 fmt
->format(123456.0, appendTo
, status
);
208 const char *expected
= "123,456";
211 UnicodeString(expected
).unescape(),
213 assertSuccess("", status
);
216 extern IntlTest
*createScientificNumberFormatterTest() {
217 return new ScientificNumberFormatterTest();
220 #endif /* !UCONFIG_NO_FORMATTING */