1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1997-2015, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
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"
18 #include "putilimp.h"
19 #include "cstring.h"
20
21 #include <float.h>
22 #include <stdio.h> // for sprintf
23 #include <stdlib.h>
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;
32 UBool NumberFormatRoundTripTest::DEBUG_VAR = false;
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
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)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
failure(UErrorCode status,const char * msg,UBool possibleDataError)49 NumberFormatRoundTripTest::failure(UErrorCode status, const char* msg, UBool possibleDataError)
50 {
51 if(U_FAILURE(status)) {
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 }
57 return true;
58 }
59
60 return false;
61 }
62
63 uint32_t
randLong()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
randomDouble(double range)82 NumberFormatRoundTripTest::randomDouble(double range)
83 {
84 double a = randFraction();
85 return (2.0 * range * a) - range;
86 }
87
88 void
start()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);
100 if (!failure(status, "NumberFormat::createInstance", true)){
101 test(fmt);
102 }
103 delete fmt;
104
105 fmt = NumberFormat::createCurrencyInstance(status);
106 if (!failure(status, "NumberFormat::createCurrencyInstance", true)){
107 test(fmt);
108 }
109 delete fmt;
110
111 fmt = NumberFormat::createPercentInstance(status);
112 if (!failure(status, "NumberFormat::createPercentInstance", true)){
113 test(fmt);
114 }
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
test(NumberFormat * fmt)150 NumberFormatRoundTripTest::test(NumberFormat *fmt)
151 {
152 #if IEEE_754 && U_PLATFORM != U_PF_OS400
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));
173 #if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
174 test(fmt, randomDouble(1e100));
175 #elif IEEE_754
176 test(fmt, randomDouble(1e75));
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)
188 DecimalFormat *df = dynamic_cast<DecimalFormat *>(fmt);
189 if(df != NULL)
190 {
191 #if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
192 /* DBL_MAX/2 is here because randomDouble does a *2 in the math */
193 test(fmt, randomDouble(DBL_MAX/2.0) / df->getMultiplier());
194 #elif IEEE_754
195 test(fmt, randomDouble(1e75) / df->getMultiplier());
196 #else
197 test(fmt, randomDouble(1e65) / df->getMultiplier());
198 #endif
199 }
200
201 #if (defined(_MSC_VER) && _MSC_VER < 1400) || defined(__alpha__) || defined(U_OSF)
202 // These machines and compilers don't fully support denormalized doubles,
203 test(fmt, randomDouble(1e-292));
204 test(fmt, randomDouble(1e-100));
205 #elif U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400
206 // i5/OS (OS/400) throws exceptions on denormalized numbers
207 # if IEEE_754
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.
211 # endif
212 #else
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 */
215 test(fmt, randomDouble(1e-100));
216 #endif
217 }
218 }
219
220 void
test(NumberFormat * fmt,double value)221 NumberFormatRoundTripTest::test(NumberFormat *fmt, double value)
222 {
223 test(fmt, Formattable(value));
224 }
225
226 void
test(NumberFormat * fmt,int32_t value)227 NumberFormatRoundTripTest::test(NumberFormat *fmt, int32_t value)
228 {
229 test(fmt, Formattable(value));
230 }
231
232 void
test(NumberFormat * fmt,const Formattable & value)233 NumberFormatRoundTripTest::test(NumberFormat *fmt, const Formattable& value)
234 {
235 fmt->setMaximumFractionDigits(999);
236 DecimalFormat *df = dynamic_cast<DecimalFormat *>(fmt);
237 if(df != NULL) {
238 df->setRoundingIncrement(0.0);
239 }
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;
249 if(DEBUG_VAR)
250 logln(/*value.getString(temp) +*/ " F> " + escape(s));
251
252 fmt->parse(s, n, status);
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 }
259 if(DEBUG_VAR)
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
267 if(DEBUG_VAR)
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> " +
300 escape(s) + " P> " + (n.getType() == Formattable::kDouble ? n.getDouble() : (double)n.getLong())
301 /*n.getString(temp) */ + typeOf(n, temp) + " F> " +
302 escape(s2));
303 }
304 }
305
306 double
proportionalError(const Formattable & a,const Formattable & b)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&
typeOf(const Formattable & n,UnicodeString & result)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&
escape(UnicodeString & s)347 NumberFormatRoundTripTest::escape(UnicodeString& s)
348 {
349 UnicodeString copy(s);
350 s.remove();
351 for(int i = 0; i < copy.length(); ++i) {
352 UChar32 c = copy.char32At(i);
353 if (c >= 0x10000) {
354 ++i;
355 }
356 if(c < 0x00FF) {
357 s += c;
358 } else {
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 */
369