| 1 | /*********************************************************************** |
| 2 | * COPYRIGHT: |
| 3 | * Copyright (c) 1997-2006, International Business Machines Corporation |
| 4 | * and others. All Rights Reserved. |
| 5 | ***********************************************************************/ |
| 6 | |
| 7 | #include "unicode/utypes.h" |
| 8 | |
| 9 | #if !UCONFIG_NO_FORMATTING |
| 10 | |
| 11 | #include "nmfmtrt.h" |
| 12 | |
| 13 | #include "unicode/dcfmtsym.h" |
| 14 | #include "unicode/decimfmt.h" |
| 15 | #include "unicode/locid.h" |
| 16 | #include "putilimp.h" |
| 17 | |
| 18 | #include <float.h> |
| 19 | #include <stdio.h> // for sprintf |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | // ***************************************************************************** |
| 23 | // class NumberFormatRoundTripTest |
| 24 | // ***************************************************************************** |
| 25 | |
| 26 | UBool NumberFormatRoundTripTest::verbose = FALSE; |
| 27 | UBool NumberFormatRoundTripTest::STRING_COMPARE = TRUE; |
| 28 | UBool NumberFormatRoundTripTest::EXACT_NUMERIC_COMPARE = FALSE; |
| 29 | UBool NumberFormatRoundTripTest::DEBUG = FALSE; |
| 30 | double NumberFormatRoundTripTest::MAX_ERROR = 1e-14; |
| 31 | double NumberFormatRoundTripTest::max_numeric_error = 0.0; |
| 32 | double NumberFormatRoundTripTest::min_numeric_error = 1.0; |
| 33 | |
| 34 | #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; |
| 35 | |
| 36 | void NumberFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) |
| 37 | { |
| 38 | // if (exec) logln((UnicodeString)"TestSuite NumberFormatRoundTripTest"); |
| 39 | switch (index) { |
| 40 | CASE(0, start) |
| 41 | default: name = ""; break; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | UBool |
| 46 | NumberFormatRoundTripTest::failure(UErrorCode status, const char* msg) |
| 47 | { |
| 48 | if(U_FAILURE(status)) { |
| 49 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); |
| 50 | return TRUE; |
| 51 | } |
| 52 | |
| 53 | return FALSE; |
| 54 | } |
| 55 | |
| 56 | uint32_t |
| 57 | NumberFormatRoundTripTest::randLong() |
| 58 | { |
| 59 | // Assume 8-bit (or larger) rand values. Also assume |
| 60 | // that the system rand() function is very poor, which it always is. |
| 61 | uint32_t d; |
| 62 | uint32_t i; |
| 63 | char* poke = (char*)&d; |
| 64 | for (i=0; i < sizeof(uint32_t); ++i) |
| 65 | { |
| 66 | poke[i] = (char)(rand() & 0xFF); |
| 67 | } |
| 68 | return d; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Return a random value from -range..+range. |
| 73 | */ |
| 74 | double |
| 75 | NumberFormatRoundTripTest::randomDouble(double range) |
| 76 | { |
| 77 | double a = randFraction(); |
| 78 | return (2.0 * range * a) - range; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | NumberFormatRoundTripTest::start() |
| 83 | { |
| 84 | // test(NumberFormat.getInstance(new Locale("sr", "", ""))); |
| 85 | |
| 86 | UErrorCode status = U_ZERO_ERROR; |
| 87 | |
| 88 | NumberFormat *fmt = NULL; |
| 89 | |
| 90 | logln("Default Locale"); |
| 91 | |
| 92 | fmt = NumberFormat::createInstance(status); |
| 93 | if (!failure(status, "NumberFormat::createInstance")){ |
| 94 | test(fmt); |
| 95 | } |
| 96 | delete fmt; |
| 97 | |
| 98 | fmt = NumberFormat::createCurrencyInstance(status); |
| 99 | if (!failure(status, "NumberFormat::createCurrencyInstance")){ |
| 100 | test(fmt); |
| 101 | } |
| 102 | delete fmt; |
| 103 | |
| 104 | fmt = NumberFormat::createPercentInstance(status); |
| 105 | if (!failure(status, "NumberFormat::createPercentInstance")){ |
| 106 | test(fmt); |
| 107 | } |
| 108 | delete fmt; |
| 109 | |
| 110 | |
| 111 | int32_t locCount = 0; |
| 112 | const Locale *loc = NumberFormat::getAvailableLocales(locCount); |
| 113 | if(quick) { |
| 114 | if(locCount > 5) |
| 115 | locCount = 5; |
| 116 | logln("Quick mode: only testing first 5 Locales"); |
| 117 | } |
| 118 | for(int i = 0; i < locCount; ++i) { |
| 119 | UnicodeString name; |
| 120 | logln(loc[i].getDisplayName(name)); |
| 121 | |
| 122 | fmt = NumberFormat::createInstance(loc[i], status); |
| 123 | failure(status, "NumberFormat::createInstance"); |
| 124 | test(fmt); |
| 125 | delete fmt; |
| 126 | |
| 127 | fmt = NumberFormat::createCurrencyInstance(loc[i], status); |
| 128 | failure(status, "NumberFormat::createCurrencyInstance"); |
| 129 | test(fmt); |
| 130 | delete fmt; |
| 131 | |
| 132 | fmt = NumberFormat::createPercentInstance(loc[i], status); |
| 133 | failure(status, "NumberFormat::createPercentInstance"); |
| 134 | test(fmt); |
| 135 | delete fmt; |
| 136 | } |
| 137 | |
| 138 | logln(UnicodeString("Numeric error ") + min_numeric_error + " to " + max_numeric_error); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void |
| 143 | NumberFormatRoundTripTest::test(NumberFormat *fmt) |
| 144 | { |
| 145 | #if IEEE_754 && !defined(OS400) |
| 146 | test(fmt, uprv_getNaN()); |
| 147 | test(fmt, uprv_getInfinity()); |
| 148 | test(fmt, -uprv_getInfinity()); |
| 149 | #endif |
| 150 | |
| 151 | test(fmt, (int32_t)500); |
| 152 | test(fmt, (int32_t)0); |
| 153 | test(fmt, (int32_t)-0); |
| 154 | test(fmt, 0.0); |
| 155 | double negZero = 0.0; negZero /= -1.0; |
| 156 | test(fmt, negZero); |
| 157 | test(fmt, 9223372036854775808.0); |
| 158 | test(fmt, -9223372036854775809.0); |
| 159 | |
| 160 | for(int i = 0; i < 10; ++i) { |
| 161 | test(fmt, randomDouble(1)); |
| 162 | test(fmt, randomDouble(10000)); |
| 163 | test(fmt, uprv_floor((randomDouble(10000)))); |
| 164 | test(fmt, randomDouble(1e50)); |
| 165 | test(fmt, randomDouble(1e-50)); |
| 166 | #if !defined(OS390) && !defined(OS400) |
| 167 | test(fmt, randomDouble(1e100)); |
| 168 | #elif IEEE_754 |
| 169 | test(fmt, randomDouble(1e75)); /*OS390 and OS400*/ |
| 170 | #endif /* OS390 and OS400 */ |
| 171 | // {sfb} When formatting with a percent instance, numbers very close to |
| 172 | // DBL_MAX will fail the round trip. This is because: |
| 173 | // 1) Format the double into a string --> INF% (since 100 * double > DBL_MAX) |
| 174 | // 2) Parse the string into a double --> INF |
| 175 | // 3) Re-format the double --> INF% |
| 176 | // 4) The strings are equal, so that works. |
| 177 | // 5) Calculate the proportional error --> INF, so the test will fail |
| 178 | // I'll get around this by dividing by the multiplier to make sure |
| 179 | // the double will stay in range. |
| 180 | //if(fmt->getMultipler() == 1) |
| 181 | if(fmt->getDynamicClassID() == DecimalFormat::getStaticClassID()) |
| 182 | { |
| 183 | #if !defined(OS390) && !defined(OS400) |
| 184 | /* DBL_MAX/2 is here because randomDouble does a *2 in the math */ |
| 185 | test(fmt, randomDouble(DBL_MAX/2.0) / ((DecimalFormat*)fmt)->getMultiplier()); |
| 186 | #elif IEEE_754 |
| 187 | test(fmt, randomDouble(1e75) / ((DecimalFormat*)fmt)->getMultiplier()); |
| 188 | #else |
| 189 | test(fmt, randomDouble(1e65) / ((DecimalFormat*)fmt)->getMultiplier()); /*OS390*/ |
| 190 | #endif |
| 191 | } |
| 192 | |
| 193 | #if defined XP_MAC || defined __alpha__ || defined U_OSF |
| 194 | // These machines don't support denormalized doubles, |
| 195 | // so the low-end range doesn't match Windows |
| 196 | test(fmt, randomDouble(1e-292)); |
| 197 | #elif defined(OS390) || defined(OS400) |
| 198 | # if IEEE_754 |
| 199 | test(fmt, randomDouble(1e-78)); /*OS390 and OS400*/ |
| 200 | # endif |
| 201 | #else |
| 202 | test(fmt, randomDouble(1e-323)); |
| 203 | #endif /* OS390 and OS400*/ |
| 204 | #if !defined(OS390) && !defined(OS400) |
| 205 | test(fmt, randomDouble(1e-100)); |
| 206 | #elif IEEE_754 |
| 207 | test(fmt, randomDouble(1e-78)); /*OS390 and OS400*/ |
| 208 | #endif /* OS390 and OS400*/ |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | NumberFormatRoundTripTest::test(NumberFormat *fmt, double value) |
| 214 | { |
| 215 | test(fmt, Formattable(value)); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | NumberFormatRoundTripTest::test(NumberFormat *fmt, int32_t value) |
| 220 | { |
| 221 | test(fmt, Formattable(value)); |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | NumberFormatRoundTripTest::test(NumberFormat *fmt, const Formattable& value) |
| 226 | { |
| 227 | fmt->setMaximumFractionDigits(999); |
| 228 | if(fmt->getDynamicClassID() == DecimalFormat::getStaticClassID()) { |
| 229 | ((DecimalFormat *)fmt)->setRoundingIncrement(0.0); |
| 230 | } |
| 231 | UErrorCode status = U_ZERO_ERROR; |
| 232 | UnicodeString s, s2, temp; |
| 233 | if(isDouble(value)) |
| 234 | s = fmt->format(value.getDouble(), s); |
| 235 | else |
| 236 | s = fmt->format(value.getLong(), s); |
| 237 | |
| 238 | Formattable n; |
| 239 | UBool show = verbose; |
| 240 | if(DEBUG) |
| 241 | logln(/*value.getString(temp) +*/ " F> " + escape(s)); |
| 242 | |
| 243 | fmt->parse(s, n, status); |
| 244 | failure(status, "fmt->parse"); |
| 245 | if(DEBUG) |
| 246 | logln(escape(s) + " P> " /*+ n.getString(temp)*/); |
| 247 | |
| 248 | if(isDouble(n)) |
| 249 | s2 = fmt->format(n.getDouble(), s2); |
| 250 | else |
| 251 | s2 = fmt->format(n.getLong(), s2); |
| 252 | |
| 253 | if(DEBUG) |
| 254 | logln(/*n.getString(temp) +*/ " F> " + escape(s2)); |
| 255 | |
| 256 | if(STRING_COMPARE) { |
| 257 | if (s != s2) { |
| 258 | errln("*** STRING ERROR \"" + escape(s) + "\" != \"" + escape(s2) + "\""); |
| 259 | show = TRUE; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if(EXACT_NUMERIC_COMPARE) { |
| 264 | if(value != n) { |
| 265 | errln("*** NUMERIC ERROR"); |
| 266 | show = TRUE; |
| 267 | } |
| 268 | } |
| 269 | else { |
| 270 | // Compute proportional error |
| 271 | double error = proportionalError(value, n); |
| 272 | |
| 273 | if(error > MAX_ERROR) { |
| 274 | errln(UnicodeString("*** NUMERIC ERROR ") + error); |
| 275 | show = TRUE; |
| 276 | } |
| 277 | |
| 278 | if (error > max_numeric_error) |
| 279 | max_numeric_error = error; |
| 280 | if (error < min_numeric_error) |
| 281 | min_numeric_error = error; |
| 282 | } |
| 283 | |
| 284 | if (show) { |
| 285 | errln(/*value.getString(temp) +*/ typeOf(value, temp) + " F> " + |
| 286 | escape(s) + " P> " + |
| 287 | /*n.getString(temp) +*/ typeOf(n, temp) + " F> " + |
| 288 | escape(s2)); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | double |
| 293 | NumberFormatRoundTripTest::proportionalError(const Formattable& a, const Formattable& b) |
| 294 | { |
| 295 | double aa,bb; |
| 296 | |
| 297 | if(isDouble(a)) |
| 298 | aa = a.getDouble(); |
| 299 | else |
| 300 | aa = a.getLong(); |
| 301 | |
| 302 | if(isDouble(b)) |
| 303 | bb = b.getDouble(); |
| 304 | else |
| 305 | bb = b.getLong(); |
| 306 | |
| 307 | double error = aa - bb; |
| 308 | if(aa != 0 && bb != 0) |
| 309 | error /= aa; |
| 310 | |
| 311 | return uprv_fabs(error); |
| 312 | } |
| 313 | |
| 314 | UnicodeString& |
| 315 | NumberFormatRoundTripTest::typeOf(const Formattable& n, UnicodeString& result) |
| 316 | { |
| 317 | if(n.getType() == Formattable::kLong) { |
| 318 | result = UnicodeString(" Long"); |
| 319 | } |
| 320 | else if(n.getType() == Formattable::kDouble) { |
| 321 | result = UnicodeString(" Double"); |
| 322 | } |
| 323 | else if(n.getType() == Formattable::kString) { |
| 324 | result = UnicodeString(" UnicodeString"); |
| 325 | UnicodeString temp; |
| 326 | } |
| 327 | |
| 328 | return result; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | UnicodeString& |
| 333 | NumberFormatRoundTripTest::escape(UnicodeString& s) |
| 334 | { |
| 335 | UnicodeString copy(s); |
| 336 | s.remove(); |
| 337 | for(int i = 0; i < copy.length(); ++i) { |
| 338 | UChar c = copy[i]; |
| 339 | if(c < 0x00FF) |
| 340 | s += c; |
| 341 | else { |
| 342 | s += "+U"; |
| 343 | char temp[16]; |
| 344 | sprintf(temp, "%4X", c); // might not work |
| 345 | s += temp; |
| 346 | } |
| 347 | } |
| 348 | return s; |
| 349 | } |
| 350 | |
| 351 | #endif /* #if !UCONFIG_NO_FORMATTING */ |