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