1 /*
2 *******************************************************************************
3 * Copyright (C) 2014, International Business Machines Corporation and         *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 *
7 * File SCINUMBERFORMATTERTEST.CPP
8 *
9 *******************************************************************************
10 */
11 #include "unicode/utypes.h"
12 
13 #include "intltest.h"
14 
15 #if !UCONFIG_NO_FORMATTING
16 
17 #include "unicode/scientificnumberformatter.h"
18 #include "unicode/numfmt.h"
19 #include "unicode/decimfmt.h"
20 #include "unicode/localpointer.h"
21 
22 class ScientificNumberFormatterTest : public IntlTest {
23 public:
24     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
25 private:
26     void TestBasic();
27     void TestFarsi();
28     void TestPlusSignInExponentMarkup();
29     void TestPlusSignInExponentSuperscript();
30     void TestFixedDecimalMarkup();
31     void TestFixedDecimalSuperscript();
32 };
33 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)34 void ScientificNumberFormatterTest::runIndexedTest(
35         int32_t index, UBool exec, const char *&name, char *) {
36     if (exec) {
37         logln("TestSuite ScientificNumberFormatterTest: ");
38     }
39     TESTCASE_AUTO_BEGIN;
40     TESTCASE_AUTO(TestBasic);
41     TESTCASE_AUTO(TestFarsi);
42     TESTCASE_AUTO(TestPlusSignInExponentMarkup);
43     TESTCASE_AUTO(TestPlusSignInExponentSuperscript);
44     TESTCASE_AUTO(TestFixedDecimalMarkup);
45     TESTCASE_AUTO(TestFixedDecimalSuperscript);
46     TESTCASE_AUTO_END;
47 }
48 
TestBasic()49 void ScientificNumberFormatterTest::TestBasic() {
50     UErrorCode status = U_ZERO_ERROR;
51     UnicodeString prefix("String: ");
52     UnicodeString appendTo(prefix);
53     LocalPointer<ScientificNumberFormatter> fmt(
54             ScientificNumberFormatter::createMarkupInstance(
55                     "en" , "<sup>", "</sup>", status));
56     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
57         return;
58     }
59     fmt->format(1.23456e-78, appendTo, status);
60     const char *expected = "String: 1.23456\\u00d710<sup>-78</sup>";
61     assertEquals(
62             "markup style",
63             UnicodeString(expected).unescape(),
64             appendTo);
65 
66     // Test superscript style
67     fmt.adoptInstead(
68             ScientificNumberFormatter::createSuperscriptInstance(
69                     "en", status));
70     if (!assertSuccess("Can't create ScientificNumberFormatter2", status)) {
71         return;
72     }
73     appendTo = prefix;
74     fmt->format(1.23456e-78, appendTo, status);
75     expected = "String: 1.23456\\u00d710\\u207b\\u2077\\u2078";
76     assertEquals(
77             "superscript style",
78             UnicodeString(expected).unescape(),
79             appendTo);
80 
81     // Test clone
82     LocalPointer<ScientificNumberFormatter> fmt3(fmt->clone());
83     if (fmt3.isNull()) {
84        errln("Allocating clone failed.");
85        return;
86     }
87     appendTo = prefix;
88     fmt3->format(1.23456e-78, appendTo, status);
89     expected = "String: 1.23456\\u00d710\\u207b\\u2077\\u2078";
90     assertEquals(
91             "superscript style",
92             UnicodeString(expected).unescape(),
93             appendTo);
94     assertSuccess("", status);
95 }
96 
TestFarsi()97 void ScientificNumberFormatterTest::TestFarsi() {
98     UErrorCode status = U_ZERO_ERROR;
99     UnicodeString prefix("String: ");
100     UnicodeString appendTo(prefix);
101     LocalPointer<ScientificNumberFormatter> fmt(
102             ScientificNumberFormatter::createMarkupInstance(
103                     "fa", "<sup>", "</sup>", status));
104     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
105         return;
106     }
107     fmt->format(1.23456e-78, appendTo, status);
108     const char *expected = "String: \\u06F1\\u066B\\u06F2\\u06F3\\u06F4\\u06F5\\u06F6\\u00d7\\u06F1\\u06F0<sup>\\u200E\\u2212\\u06F7\\u06F8</sup>";
109     assertEquals(
110             "",
111             UnicodeString(expected).unescape(),
112             appendTo);
113     assertSuccess("", status);
114 }
115 
TestPlusSignInExponentMarkup()116 void ScientificNumberFormatterTest::TestPlusSignInExponentMarkup() {
117     UErrorCode status = U_ZERO_ERROR;
118     LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createScientificInstance("en", status));
119     if (U_FAILURE(status)) {
120         dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", status) - %s", u_errorName(status));
121         return;
122     }
123     decfmt->applyPattern("0.00E+0", status);
124     if (!assertSuccess("", status)) {
125         return;
126     }
127     UnicodeString appendTo;
128     LocalPointer<ScientificNumberFormatter> fmt(
129             ScientificNumberFormatter::createMarkupInstance(
130                     new DecimalFormat(*decfmt), "<sup>", "</sup>", status));
131     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
132         return;
133     }
134     fmt->format(6.02e23, appendTo, status);
135     const char *expected = "6.02\\u00d710<sup>+23</sup>";
136     assertEquals(
137             "",
138             UnicodeString(expected).unescape(),
139             appendTo);
140     assertSuccess("", status);
141 }
142 
TestPlusSignInExponentSuperscript()143 void ScientificNumberFormatterTest::TestPlusSignInExponentSuperscript() {
144     UErrorCode status = U_ZERO_ERROR;
145     LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createScientificInstance("en", status));
146     if (U_FAILURE(status)) {
147         dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", status) - %s", u_errorName(status));
148         return;
149     }
150     decfmt->applyPattern("0.00E+0", status);
151     if (!assertSuccess("", status)) {
152         return;
153     }
154     UnicodeString appendTo;
155     LocalPointer<ScientificNumberFormatter> fmt(
156             ScientificNumberFormatter::createSuperscriptInstance(
157                     new DecimalFormat(*decfmt), status));
158     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
159         return;
160     }
161     fmt->format(6.02e23, appendTo, status);
162     const char *expected = "6.02\\u00d710\\u207a\\u00b2\\u00b3";
163     assertEquals(
164             "",
165             UnicodeString(expected).unescape(),
166             appendTo);
167     assertSuccess("", status);
168 }
169 
TestFixedDecimalMarkup()170 void ScientificNumberFormatterTest::TestFixedDecimalMarkup() {
171     UErrorCode status = U_ZERO_ERROR;
172     LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createInstance("en", status));
173     if (assertSuccess("NumberFormat::createInstance", status, TRUE) == FALSE) {
174         return;
175     }
176     LocalPointer<ScientificNumberFormatter> fmt(
177             ScientificNumberFormatter::createMarkupInstance(
178                     new DecimalFormat(*decfmt), "<sup>", "</sup>", status));
179     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
180         return;
181     }
182     UnicodeString appendTo;
183     fmt->format(123456.0, appendTo, status);
184     const char *expected = "123,456";
185     assertEquals(
186             "",
187             UnicodeString(expected).unescape(),
188             appendTo);
189     assertSuccess("", status);
190 }
191 
TestFixedDecimalSuperscript()192 void ScientificNumberFormatterTest::TestFixedDecimalSuperscript() {
193     UErrorCode status = U_ZERO_ERROR;
194     LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createInstance("en", status));
195     if (assertSuccess("NumberFormat::createInstance", status, TRUE) == FALSE) {
196         return;
197     }
198     LocalPointer<ScientificNumberFormatter> fmt(
199             ScientificNumberFormatter::createSuperscriptInstance(
200                     new DecimalFormat(*decfmt), status));
201     if (!assertSuccess("Can't create ScientificNumberFormatter", status)) {
202         return;
203     }
204     UnicodeString appendTo;
205     fmt->format(123456.0, appendTo, status);
206     const char *expected = "123,456";
207     assertEquals(
208             "",
209             UnicodeString(expected).unescape(),
210             appendTo);
211     assertSuccess("", status);
212 }
213 
createScientificNumberFormatterTest()214 extern IntlTest *createScientificNumberFormatterTest() {
215     return new ScientificNumberFormatterTest();
216 }
217 
218 #endif /* !UCONFIG_NO_FORMATTING */
219