OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ******************************************************************************* |
| 3 * Copyright (C) 2014, International Business Machines Corporation and * |
| 4 * others. All Rights Reserved. * |
| 5 ******************************************************************************* |
| 6 * |
| 7 * File SCIFORMATHELPERTEST.CPP |
| 8 * |
| 9 ******************************************************************************* |
| 10 */ |
| 11 #include "unicode/utypes.h" |
| 12 |
| 13 #include "intltest.h" |
| 14 |
| 15 #if !UCONFIG_NO_FORMATTING |
| 16 |
| 17 #include "unicode/scientificformathelper.h" |
| 18 #include "unicode/numfmt.h" |
| 19 #include "unicode/decimfmt.h" |
| 20 #include "unicode/localpointer.h" |
| 21 |
| 22 class ScientificFormatHelperTest : 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 |
| 34 void ScientificFormatHelperTest::runIndexedTest( |
| 35 int32_t index, UBool exec, const char *&name, char *) { |
| 36 if (exec) { |
| 37 logln("TestSuite ScientificFormatHelperTest: "); |
| 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 |
| 49 void ScientificFormatHelperTest::TestBasic() { |
| 50 UErrorCode status = U_ZERO_ERROR; |
| 51 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createSci
entificInstance("en", status)); |
| 52 if (U_FAILURE(status)) { |
| 53 dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", st
atus) - %s", u_errorName(status)); |
| 54 return; |
| 55 } |
| 56 UnicodeString appendTo("String: "); |
| 57 FieldPositionIterator fpositer; |
| 58 decfmt->format(1.23456e-78, appendTo, &fpositer, status); |
| 59 FieldPositionIterator fpositer2(fpositer); |
| 60 FieldPositionIterator fpositer3(fpositer); |
| 61 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 62 UnicodeString result; |
| 63 const char *expected = "String: 1.23456\\u00d710<sup>-78</sup>"; |
| 64 assertEquals( |
| 65 "insertMarkup", |
| 66 UnicodeString(expected).unescape(), |
| 67 helper.insertMarkup(appendTo, fpositer, "<sup>", "</sup>", result, s
tatus)); |
| 68 result.remove(); |
| 69 expected = "String: 1.23456\\u00d710\\u207b\\u2077\\u2078"; |
| 70 assertEquals( |
| 71 "toSuperscriptExponentDigits", |
| 72 UnicodeString(expected).unescape(), |
| 73 helper.toSuperscriptExponentDigits(appendTo, fpositer2, result, stat
us)); |
| 74 assertSuccess("", status); |
| 75 result.remove(); |
| 76 |
| 77 // The 'a' is an invalid exponent character. |
| 78 helper.toSuperscriptExponentDigits("String: 1.23456e-7a", fpositer3, result,
status); |
| 79 if (status != U_INVALID_CHAR_FOUND) { |
| 80 errln("Expected U_INVALID_CHAR_FOUND"); |
| 81 } |
| 82 } |
| 83 |
| 84 void ScientificFormatHelperTest::TestFarsi() { |
| 85 UErrorCode status = U_ZERO_ERROR; |
| 86 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createSci
entificInstance("fa", status)); |
| 87 if (U_FAILURE(status)) { |
| 88 dataerrln("Failed call NumberFormat::createScientificInstance(\"fa\", st
atus) - %s", u_errorName(status)); |
| 89 return; |
| 90 } |
| 91 UnicodeString appendTo("String: "); |
| 92 FieldPositionIterator fpositer; |
| 93 decfmt->format(1.23456e-78, appendTo, &fpositer, status); |
| 94 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 95 UnicodeString result; |
| 96 const char *expected = "String: \\u06F1\\u066B\\u06F2\\u06F3\\u06F4\\u06F5\\
u06F6\\u00d7\\u06F1\\u06F0<sup>\\u200E\\u2212\\u06F7\\u06F8</sup>"; |
| 97 assertEquals( |
| 98 "insertMarkup", |
| 99 UnicodeString(expected).unescape(), |
| 100 helper.insertMarkup(appendTo, fpositer, "<sup>", "</sup>", result, s
tatus)); |
| 101 assertSuccess("", status); |
| 102 } |
| 103 |
| 104 void ScientificFormatHelperTest::TestPlusSignInExponentMarkup() { |
| 105 UErrorCode status = U_ZERO_ERROR; |
| 106 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createSci
entificInstance("en", status)); |
| 107 if (U_FAILURE(status)) { |
| 108 dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", st
atus) - %s", u_errorName(status)); |
| 109 return; |
| 110 } |
| 111 decfmt->applyPattern("0.00E+0", status); |
| 112 assertSuccess("", status); |
| 113 UnicodeString appendTo; |
| 114 FieldPositionIterator fpositer; |
| 115 decfmt->format(6.02e23, appendTo, &fpositer, status); |
| 116 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 117 UnicodeString result; |
| 118 const char *expected = "6.02\\u00d710<sup>+23</sup>"; |
| 119 assertEquals( |
| 120 "", |
| 121 UnicodeString(expected).unescape(), |
| 122 helper.insertMarkup(appendTo, fpositer, "<sup>", "</sup>", result, s
tatus)); |
| 123 assertSuccess("", status); |
| 124 } |
| 125 |
| 126 void ScientificFormatHelperTest::TestPlusSignInExponentSuperscript() { |
| 127 UErrorCode status = U_ZERO_ERROR; |
| 128 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createSci
entificInstance("en", status)); |
| 129 if (U_FAILURE(status)) { |
| 130 dataerrln("Failed call NumberFormat::createScientificInstance(\"en\", st
atus) - %s", u_errorName(status)); |
| 131 return; |
| 132 } |
| 133 decfmt->applyPattern("0.00E+0", status); |
| 134 assertSuccess("", status); |
| 135 UnicodeString appendTo; |
| 136 FieldPositionIterator fpositer; |
| 137 decfmt->format(6.02e23, appendTo, &fpositer, status); |
| 138 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 139 UnicodeString result; |
| 140 const char *expected = "6.02\\u00d710\\u207a\\u00b2\\u00b3"; |
| 141 assertEquals( |
| 142 "", |
| 143 UnicodeString(expected).unescape(), |
| 144 helper.toSuperscriptExponentDigits(appendTo, fpositer, result, statu
s)); |
| 145 assertSuccess("", status); |
| 146 } |
| 147 |
| 148 void ScientificFormatHelperTest::TestFixedDecimalMarkup() { |
| 149 UErrorCode status = U_ZERO_ERROR; |
| 150 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createIns
tance("en", status)); |
| 151 if (assertSuccess("NumberFormat::createInstance", status, TRUE) == FALSE) { |
| 152 return; |
| 153 } |
| 154 UnicodeString appendTo; |
| 155 FieldPositionIterator fpositer; |
| 156 decfmt->format(123456.0, appendTo, &fpositer, status); |
| 157 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 158 assertSuccess("", status); |
| 159 UnicodeString result; |
| 160 helper.insertMarkup(appendTo, fpositer, "<sup>", "</sup>", result, status); |
| 161 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 162 errln("Expected U_ILLEGAL_ARGUMENT_ERROR with fixed decimal number."); |
| 163 } |
| 164 } |
| 165 |
| 166 void ScientificFormatHelperTest::TestFixedDecimalSuperscript() { |
| 167 UErrorCode status = U_ZERO_ERROR; |
| 168 LocalPointer<DecimalFormat> decfmt((DecimalFormat *) NumberFormat::createIns
tance("en", status)); |
| 169 if (assertSuccess("NumberFormat::createInstance", status, TRUE) == FALSE) { |
| 170 return; |
| 171 } |
| 172 UnicodeString appendTo; |
| 173 FieldPositionIterator fpositer; |
| 174 decfmt->format(123456.0, appendTo, &fpositer, status); |
| 175 ScientificFormatHelper helper(*decfmt->getDecimalFormatSymbols(), status); |
| 176 assertSuccess("", status); |
| 177 UnicodeString result; |
| 178 helper.toSuperscriptExponentDigits(appendTo, fpositer, result, status); |
| 179 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 180 errln("Expected U_ILLEGAL_ARGUMENT_ERROR with fixed decimal number."); |
| 181 } |
| 182 } |
| 183 |
| 184 extern IntlTest *createScientificFormatHelperTest() { |
| 185 return new ScientificFormatHelperTest(); |
| 186 } |
| 187 |
| 188 #endif /* !UCONFIG_NO_FORMATTING */ |
OLD | NEW |