]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/nmfmtrt.cpp
ICU-62123.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / nmfmtrt.cpp
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
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
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
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
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);
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
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
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);
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 failure(status, "fmt->parse");
254 if(DEBUG_VAR)
255 logln(escape(s) + " P> " /*+ n.getString(temp)*/);
256
257 if(isDouble(n))
258 s2 = fmt->format(n.getDouble(), s2);
259 else
260 s2 = fmt->format(n.getLong(), s2);
261
262 if(DEBUG_VAR)
263 logln(/*n.getString(temp) +*/ " F> " + escape(s2));
264
265 if(STRING_COMPARE) {
266 if (s != s2) {
267 errln("*** STRING ERROR \"" + escape(s) + "\" != \"" + escape(s2) + "\"");
268 show = TRUE;
269 }
270 }
271
272 if(EXACT_NUMERIC_COMPARE) {
273 if(value != n) {
274 errln("*** NUMERIC ERROR");
275 show = TRUE;
276 }
277 }
278 else {
279 // Compute proportional error
280 double error = proportionalError(value, n);
281
282 if(error > MAX_ERROR) {
283 errln(UnicodeString("*** NUMERIC ERROR ") + error);
284 show = TRUE;
285 }
286
287 if (error > max_numeric_error)
288 max_numeric_error = error;
289 if (error < min_numeric_error)
290 min_numeric_error = error;
291 }
292
293 if (show) {
294 errln(/*value.getString(temp) +*/ typeOf(value, temp) + " F> " +
295 escape(s) + " P> " + (n.getType() == Formattable::kDouble ? n.getDouble() : (double)n.getLong())
296 /*n.getString(temp) */ + typeOf(n, temp) + " F> " +
297 escape(s2));
298 }
299 }
300
301 double
302 NumberFormatRoundTripTest::proportionalError(const Formattable& a, const Formattable& b)
303 {
304 double aa,bb;
305
306 if(isDouble(a))
307 aa = a.getDouble();
308 else
309 aa = a.getLong();
310
311 if(isDouble(b))
312 bb = b.getDouble();
313 else
314 bb = b.getLong();
315
316 double error = aa - bb;
317 if(aa != 0 && bb != 0)
318 error /= aa;
319
320 return uprv_fabs(error);
321 }
322
323 UnicodeString&
324 NumberFormatRoundTripTest::typeOf(const Formattable& n, UnicodeString& result)
325 {
326 if(n.getType() == Formattable::kLong) {
327 result = UnicodeString(" Long");
328 }
329 else if(n.getType() == Formattable::kDouble) {
330 result = UnicodeString(" Double");
331 }
332 else if(n.getType() == Formattable::kString) {
333 result = UnicodeString(" UnicodeString");
334 UnicodeString temp;
335 }
336
337 return result;
338 }
339
340
341 UnicodeString&
342 NumberFormatRoundTripTest::escape(UnicodeString& s)
343 {
344 UnicodeString copy(s);
345 s.remove();
346 for(int i = 0; i < copy.length(); ++i) {
347 UChar32 c = copy.char32At(i);
348 if (c >= 0x10000) {
349 ++i;
350 }
351 if(c < 0x00FF) {
352 s += c;
353 } else {
354 s += "+U";
355 char temp[16];
356 sprintf(temp, "%4X", c); // might not work
357 s += temp;
358 }
359 }
360 return s;
361 }
362
363 #endif /* #if !UCONFIG_NO_FORMATTING */