]>
git.saurik.com Git - apple/icu.git/blob - 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 /***********************************************************************
5 * Copyright (c) 1997-2015, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/dcfmtsym.h"
16 #include "unicode/decimfmt.h"
17 #include "unicode/locid.h"
21 #include <stdio.h> // for sprintf
24 // *****************************************************************************
25 // class NumberFormatRoundTripTest
26 // *****************************************************************************
28 UBool
NumberFormatRoundTripTest::verbose
= FALSE
;
29 UBool
NumberFormatRoundTripTest::STRING_COMPARE
= TRUE
;
30 UBool
NumberFormatRoundTripTest::EXACT_NUMERIC_COMPARE
= FALSE
;
31 UBool
NumberFormatRoundTripTest::DEBUG_VAR
= FALSE
;
32 double NumberFormatRoundTripTest::MAX_ERROR
= 1e-14;
33 double NumberFormatRoundTripTest::max_numeric_error
= 0.0;
34 double NumberFormatRoundTripTest::min_numeric_error
= 1.0;
36 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
38 void NumberFormatRoundTripTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
40 // if (exec) logln((UnicodeString)"TestSuite NumberFormatRoundTripTest");
43 default: name
= ""; break;
48 NumberFormatRoundTripTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
50 if(U_FAILURE(status
)) {
51 if (possibleDataError
) {
52 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
54 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
63 NumberFormatRoundTripTest::randLong()
65 // Assume 8-bit (or larger) rand values. Also assume
66 // that the system rand() function is very poor, which it always is.
69 char* poke
= (char*)&d
;
70 for (i
=0; i
< sizeof(uint32_t); ++i
)
72 poke
[i
] = (char)(rand() & 0xFF);
78 * Return a random value from -range..+range.
81 NumberFormatRoundTripTest::randomDouble(double range
)
83 double a
= randFraction();
84 return (2.0 * range
* a
) - range
;
88 NumberFormatRoundTripTest::start()
90 // test(NumberFormat.getInstance(new Locale("sr", "", "")));
92 UErrorCode status
= U_ZERO_ERROR
;
94 NumberFormat
*fmt
= NULL
;
96 logln("Default Locale");
98 fmt
= NumberFormat::createInstance(status
);
99 if (!failure(status
, "NumberFormat::createInstance", TRUE
)){
104 fmt
= NumberFormat::createCurrencyInstance(status
);
105 if (!failure(status
, "NumberFormat::createCurrencyInstance", TRUE
)){
110 fmt
= NumberFormat::createPercentInstance(status
);
111 if (!failure(status
, "NumberFormat::createPercentInstance", TRUE
)){
117 int32_t locCount
= 0;
118 const Locale
*loc
= NumberFormat::getAvailableLocales(locCount
);
122 logln("Quick mode: only testing first 5 Locales");
124 for(int i
= 0; i
< locCount
; ++i
) {
126 logln(loc
[i
].getDisplayName(name
));
128 fmt
= NumberFormat::createInstance(loc
[i
], status
);
129 failure(status
, "NumberFormat::createInstance");
133 fmt
= NumberFormat::createCurrencyInstance(loc
[i
], status
);
134 failure(status
, "NumberFormat::createCurrencyInstance");
138 fmt
= NumberFormat::createPercentInstance(loc
[i
], status
);
139 failure(status
, "NumberFormat::createPercentInstance");
144 logln(UnicodeString("Numeric error ") + min_numeric_error
+ " to " + max_numeric_error
);
149 NumberFormatRoundTripTest::test(NumberFormat
*fmt
)
151 #if IEEE_754 && U_PLATFORM != U_PF_OS400
152 test(fmt
, uprv_getNaN());
153 test(fmt
, uprv_getInfinity());
154 test(fmt
, -uprv_getInfinity());
157 test(fmt
, (int32_t)500);
158 test(fmt
, (int32_t)0);
159 test(fmt
, (int32_t)-0);
161 double negZero
= 0.0; negZero
/= -1.0;
163 test(fmt
, 9223372036854775808.0);
164 test(fmt
, -9223372036854775809.0);
166 for(int i
= 0; i
< 10; ++i
) {
167 test(fmt
, randomDouble(1));
168 test(fmt
, randomDouble(10000));
169 test(fmt
, uprv_floor((randomDouble(10000))));
170 test(fmt
, randomDouble(1e50
));
171 test(fmt
, randomDouble(1e-50));
172 #if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
173 test(fmt
, randomDouble(1e100
));
175 test(fmt
, randomDouble(1e75
));
176 #endif /* OS390 and OS400 */
177 // {sfb} When formatting with a percent instance, numbers very close to
178 // DBL_MAX will fail the round trip. This is because:
179 // 1) Format the double into a string --> INF% (since 100 * double > DBL_MAX)
180 // 2) Parse the string into a double --> INF
181 // 3) Re-format the double --> INF%
182 // 4) The strings are equal, so that works.
183 // 5) Calculate the proportional error --> INF, so the test will fail
184 // I'll get around this by dividing by the multiplier to make sure
185 // the double will stay in range.
186 //if(fmt->getMultipler() == 1)
187 DecimalFormat
*df
= dynamic_cast<DecimalFormat
*>(fmt
);
190 #if !(U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400)
191 /* DBL_MAX/2 is here because randomDouble does a *2 in the math */
192 test(fmt
, randomDouble(DBL_MAX
/2.0) / df
->getMultiplier());
194 test(fmt
, randomDouble(1e75
) / df
->getMultiplier());
196 test(fmt
, randomDouble(1e65
) / df
->getMultiplier());
200 #if (defined(_MSC_VER) && _MSC_VER < 1400) || defined(__alpha__) || defined(U_OSF)
201 // These machines and compilers don't fully support denormalized doubles,
202 test(fmt
, randomDouble(1e-292));
203 test(fmt
, randomDouble(1e-100));
204 #elif U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400
205 // i5/OS (OS/400) throws exceptions on denormalized numbers
207 test(fmt
, randomDouble(1e-78));
208 test(fmt
, randomDouble(1e-78));
209 // #else we're using something like the old z/OS floating point.
212 // This is a normal machine that can support IEEE754 denormalized doubles without throwing an error.
213 test(fmt
, randomDouble(DBL_MIN
)); /* Usually 2.2250738585072014e-308 */
214 test(fmt
, randomDouble(1e-100));
220 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, double value
)
222 test(fmt
, Formattable(value
));
226 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, int32_t value
)
228 test(fmt
, Formattable(value
));
232 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, const Formattable
& value
)
234 fmt
->setMaximumFractionDigits(999);
235 DecimalFormat
*df
= dynamic_cast<DecimalFormat
*>(fmt
);
237 df
->setRoundingIncrement(0.0);
239 UErrorCode status
= U_ZERO_ERROR
;
240 UnicodeString s
, s2
, temp
;
242 s
= fmt
->format(value
.getDouble(), s
);
244 s
= fmt
->format(value
.getLong(), s
);
247 UBool show
= verbose
;
249 logln(/*value.getString(temp) +*/ " F> " + escape(s
));
251 fmt
->parse(s
, n
, status
);
252 failure(status
, "fmt->parse");
254 logln(escape(s
) + " P> " /*+ n.getString(temp)*/);
257 s2
= fmt
->format(n
.getDouble(), s2
);
259 s2
= fmt
->format(n
.getLong(), s2
);
262 logln(/*n.getString(temp) +*/ " F> " + escape(s2
));
266 errln("*** STRING ERROR \"" + escape(s
) + "\" != \"" + escape(s2
) + "\"");
271 if(EXACT_NUMERIC_COMPARE
) {
273 errln("*** NUMERIC ERROR");
278 // Compute proportional error
279 double error
= proportionalError(value
, n
);
281 if(error
> MAX_ERROR
) {
282 errln(UnicodeString("*** NUMERIC ERROR ") + error
);
286 if (error
> max_numeric_error
)
287 max_numeric_error
= error
;
288 if (error
< min_numeric_error
)
289 min_numeric_error
= error
;
293 errln(/*value.getString(temp) +*/ typeOf(value
, temp
) + " F> " +
294 escape(s
) + " P> " + (n
.getType() == Formattable::kDouble
? n
.getDouble() : (double)n
.getLong())
295 /*n.getString(temp) */ + typeOf(n
, temp
) + " F> " +
301 NumberFormatRoundTripTest::proportionalError(const Formattable
& a
, const Formattable
& b
)
315 double error
= aa
- bb
;
316 if(aa
!= 0 && bb
!= 0)
319 return uprv_fabs(error
);
323 NumberFormatRoundTripTest::typeOf(const Formattable
& n
, UnicodeString
& result
)
325 if(n
.getType() == Formattable::kLong
) {
326 result
= UnicodeString(" Long");
328 else if(n
.getType() == Formattable::kDouble
) {
329 result
= UnicodeString(" Double");
331 else if(n
.getType() == Formattable::kString
) {
332 result
= UnicodeString(" UnicodeString");
341 NumberFormatRoundTripTest::escape(UnicodeString
& s
)
343 UnicodeString
copy(s
);
345 for(int i
= 0; i
< copy
.length(); ++i
) {
352 sprintf(temp
, "%4X", c
); // might not work
359 #endif /* #if !UCONFIG_NO_FORMATTING */