Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ******************************************************************************* |
| 5 | * Copyright (C) 2014, International Business Machines Corporation and * |
| 6 | * others. All Rights Reserved. * |
| 7 | ******************************************************************************* |
| 8 | * |
| 9 | * File SCINUMBERFORMATTERTEST.CPP |
| 10 | * |
| 11 | ******************************************************************************* |
| 12 | */ |
| 13 | #include "unicode/utypes.h" |
| 14 | |
| 15 | #include "intltest.h" |
| 16 | |
| 17 | #if !UCONFIG_NO_FORMATTING |
| 18 | |
| 19 | #include "unicode/scientificnumberformatter.h" |
| 20 | #include "unicode/numfmt.h" |
| 21 | #include "unicode/decimfmt.h" |
| 22 | #include "unicode/localpointer.h" |
| 23 | |
| 24 | class ScientificNumberFormatterTest : public IntlTest { |
| 25 | public: |
| 26 | void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0) override; |
| 27 | private: |
| 28 | void TestBasic(); |
| 29 | void TestFarsi(); |
| 30 | void TestPlusSignInExponentMarkup(); |
| 31 | void TestPlusSignInExponentSuperscript(); |
| 32 | void TestFixedDecimalMarkup(); |
| 33 | void TestFixedDecimalSuperscript(); |
| 34 | }; |
| 35 | |
| 36 | void ScientificNumberFormatterTest::runIndexedTest( |
| 37 | int32_t index, UBool exec, const char *&name, char *) { |
| 38 | if (exec) { |
| 39 | logln("TestSuite ScientificNumberFormatterTest: "); |
| 40 | } |
| 41 | TESTCASE_AUTO_BEGIN; |
| 42 | TESTCASE_AUTO(TestBasic); |
| 43 | TESTCASE_AUTO(TestFarsi); |
| 44 | TESTCASE_AUTO(TestPlusSignInExponentMarkup); |
| 45 | TESTCASE_AUTO(TestPlusSignInExponentSuperscript); |
| 46 | TESTCASE_AUTO(TestFixedDecimalMarkup); |
| 47 | TESTCASE_AUTO(TestFixedDecimalSuperscript); |
| 48 | TESTCASE_AUTO_END; |
| 49 | } |
| 50 | |
| 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)) { |
| 59 | return; |
| 60 | } |
| 61 | fmt->format(1.23456e-78, appendTo, status); |
| 62 | const char *expected = "String: 1.23456\\u00d710<sup>-78</sup>"; |
| 63 | assertEquals( |
| 64 | "markup style", |
| 65 | UnicodeString(expected).unescape(), |
| 66 | appendTo); |
| 67 | |
| 68 | // Test superscript style |
| 69 | fmt.adoptInstead( |
| 70 | ScientificNumberFormatter::createSuperscriptInstance( |
| 71 | "en", status)); |
| 72 | if (!assertSuccess("Can't create ScientificNumberFormatter2", status)) { |
| 73 | return; |
| 74 | } |
| 75 | appendTo = prefix; |
| 76 | fmt->format(1.23456e-78, appendTo, status); |
| 77 | expected = "String: 1.23456\\u00d710\\u207b\\u2077\\u2078"; |
| 78 | assertEquals( |
| 79 | "superscript style", |
| 80 | UnicodeString(expected).unescape(), |
| 81 | appendTo); |
| 82 | |
| 83 | // Test clone |
| 84 | LocalPointer<ScientificNumberFormatter> fmt3(fmt->clone()); |
| 85 | if (fmt3.isNull()) { |
| 86 | errln("Allocating clone failed."); |
| 87 | return; |
| 88 | } |
| 89 | appendTo = prefix; |
| 90 | fmt3->format(1.23456e-78, appendTo, status); |
| 91 | expected = "String: 1.23456\\u00d710\\u207b\\u2077\\u2078"; |
| 92 | assertEquals( |
| 93 | "superscript style", |
| 94 | UnicodeString(expected).unescape(), |
| 95 | appendTo); |
| 96 | assertSuccess("", status); |
| 97 | } |
| 98 | |
| 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)) { |
| 107 | return; |
| 108 | } |
| 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>"; |
| 111 | assertEquals( |
| 112 | "", |
| 113 | UnicodeString(expected).unescape(), |
| 114 | appendTo); |
| 115 | assertSuccess("", status); |
| 116 | } |
| 117 | |
| 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)); |
| 123 | return; |
| 124 | } |
| 125 | decfmt->applyPattern("0.00E+0", status); |
| 126 | if (!assertSuccess("", status)) { |
| 127 | return; |
| 128 | } |
| 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)) { |
| 134 | return; |
| 135 | } |
| 136 | fmt->format(6.02e23, appendTo, status); |
| 137 | const char *expected = "6.02\\u00d710<sup>+23</sup>"; |
| 138 | assertEquals( |
| 139 | "", |
| 140 | UnicodeString(expected).unescape(), |
| 141 | appendTo); |
| 142 | assertSuccess("", status); |
| 143 | } |
| 144 | |
| 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)); |
| 150 | return; |
| 151 | } |
| 152 | decfmt->applyPattern("0.00E+0", status); |
| 153 | if (!assertSuccess("", status)) { |
| 154 | return; |
| 155 | } |
| 156 | UnicodeString appendTo; |
| 157 | LocalPointer<ScientificNumberFormatter> fmt( |
| 158 | ScientificNumberFormatter::createSuperscriptInstance( |
| 159 | new DecimalFormat(*decfmt), status)); |
| 160 | if (!assertSuccess("Can't create ScientificNumberFormatter", status)) { |
| 161 | return; |
| 162 | } |
| 163 | fmt->format(6.02e23, appendTo, status); |
| 164 | const char *expected = "6.02\\u00d710\\u207a\\u00b2\\u00b3"; |
| 165 | assertEquals( |
| 166 | "", |
| 167 | UnicodeString(expected).unescape(), |
| 168 | appendTo); |
| 169 | assertSuccess("", status); |
| 170 | } |
| 171 | |
| 172 | void ScientificNumberFormatterTest::TestFixedDecimalMarkup() { |
| 173 | UErrorCode status = U_ZERO_ERROR; |
| 174 | LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createInstance("en", status)); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 175 | if (assertSuccess("NumberFormat::createInstance", status, true) == false) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | LocalPointer<ScientificNumberFormatter> fmt( |
| 179 | ScientificNumberFormatter::createMarkupInstance( |
| 180 | new DecimalFormat(*decfmt), "<sup>", "</sup>", status)); |
| 181 | if (!assertSuccess("Can't create ScientificNumberFormatter", status)) { |
| 182 | return; |
| 183 | } |
| 184 | UnicodeString appendTo; |
| 185 | fmt->format(123456.0, appendTo, status); |
| 186 | const char *expected = "123,456"; |
| 187 | assertEquals( |
| 188 | "", |
| 189 | UnicodeString(expected).unescape(), |
| 190 | appendTo); |
| 191 | assertSuccess("", status); |
| 192 | } |
| 193 | |
| 194 | void ScientificNumberFormatterTest::TestFixedDecimalSuperscript() { |
| 195 | UErrorCode status = U_ZERO_ERROR; |
| 196 | LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createInstance("en", status)); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 197 | if (assertSuccess("NumberFormat::createInstance", status, true) == false) { |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | LocalPointer<ScientificNumberFormatter> fmt( |
| 201 | ScientificNumberFormatter::createSuperscriptInstance( |
| 202 | new DecimalFormat(*decfmt), status)); |
| 203 | if (!assertSuccess("Can't create ScientificNumberFormatter", status)) { |
| 204 | return; |
| 205 | } |
| 206 | UnicodeString appendTo; |
| 207 | fmt->format(123456.0, appendTo, status); |
| 208 | const char *expected = "123,456"; |
| 209 | assertEquals( |
| 210 | "", |
| 211 | UnicodeString(expected).unescape(), |
| 212 | appendTo); |
| 213 | assertSuccess("", status); |
| 214 | } |
| 215 | |
| 216 | extern IntlTest *createScientificNumberFormatterTest() { |
| 217 | return new ScientificNumberFormatterTest(); |
| 218 | } |
| 219 | |
| 220 | #endif /* !UCONFIG_NO_FORMATTING */ |