]>
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"
22 #include <stdio.h> // for sprintf
25 // *****************************************************************************
26 // class NumberFormatRoundTripTest
27 // *****************************************************************************
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;
37 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
39 void NumberFormatRoundTripTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
41 // if (exec) logln((UnicodeString)"TestSuite NumberFormatRoundTripTest");
44 default: name
= ""; break;
49 NumberFormatRoundTripTest::failure(UErrorCode status
, const char* msg
, UBool possibleDataError
)
51 if(U_FAILURE(status
)) {
52 if (possibleDataError
) {
53 dataerrln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
55 errln(UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
64 NumberFormatRoundTripTest::randLong()
66 // Assume 8-bit (or larger) rand values. Also assume
67 // that the system rand() function is very poor, which it always is.
70 char* poke
= (char*)&d
;
71 for (i
=0; i
< sizeof(uint32_t); ++i
)
73 poke
[i
] = (char)(rand() & 0xFF);
79 * Return a random value from -range..+range.
82 NumberFormatRoundTripTest::randomDouble(double range
)
84 double a
= randFraction();
85 return (2.0 * range
* a
) - range
;
89 NumberFormatRoundTripTest::start()
91 // test(NumberFormat.getInstance(new Locale("sr", "", "")));
93 UErrorCode status
= U_ZERO_ERROR
;
95 NumberFormat
*fmt
= NULL
;
97 logln("Default Locale");
99 fmt
= NumberFormat::createInstance(status
);
100 if (!failure(status
, "NumberFormat::createInstance", TRUE
)){
105 fmt
= NumberFormat::createCurrencyInstance(status
);
106 if (!failure(status
, "NumberFormat::createCurrencyInstance", TRUE
)){
111 fmt
= NumberFormat::createPercentInstance(status
);
112 if (!failure(status
, "NumberFormat::createPercentInstance", TRUE
)){
118 int32_t locCount
= 0;
119 const Locale
*loc
= NumberFormat::getAvailableLocales(locCount
);
123 logln("Quick mode: only testing first 5 Locales");
125 for(int i
= 0; i
< locCount
; ++i
) {
127 logln(loc
[i
].getDisplayName(name
));
129 fmt
= NumberFormat::createInstance(loc
[i
], status
);
130 failure(status
, "NumberFormat::createInstance");
134 fmt
= NumberFormat::createCurrencyInstance(loc
[i
], status
);
135 failure(status
, "NumberFormat::createCurrencyInstance");
139 fmt
= NumberFormat::createPercentInstance(loc
[i
], status
);
140 failure(status
, "NumberFormat::createPercentInstance");
145 logln(UnicodeString("Numeric error ") + min_numeric_error
+ " to " + max_numeric_error
);
150 NumberFormatRoundTripTest::test(NumberFormat
*fmt
)
152 #if IEEE_754 && U_PLATFORM != U_PF_OS400
153 test(fmt
, uprv_getNaN());
154 test(fmt
, uprv_getInfinity());
155 test(fmt
, -uprv_getInfinity());
158 test(fmt
, (int32_t)500);
159 test(fmt
, (int32_t)0);
160 test(fmt
, (int32_t)-0);
162 double negZero
= 0.0; negZero
/= -1.0;
164 test(fmt
, 9223372036854775808.0);
165 test(fmt
, -9223372036854775809.0);
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
));
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
);
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());
195 test(fmt
, randomDouble(1e75
) / df
->getMultiplier());
197 test(fmt
, randomDouble(1e65
) / df
->getMultiplier());
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
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.
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));
221 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, double value
)
223 test(fmt
, Formattable(value
));
227 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, int32_t value
)
229 test(fmt
, Formattable(value
));
233 NumberFormatRoundTripTest::test(NumberFormat
*fmt
, const Formattable
& value
)
235 fmt
->setMaximumFractionDigits(999);
236 DecimalFormat
*df
= dynamic_cast<DecimalFormat
*>(fmt
);
238 df
->setRoundingIncrement(0.0);
240 UErrorCode status
= U_ZERO_ERROR
;
241 UnicodeString s
, s2
, temp
;
243 s
= fmt
->format(value
.getDouble(), s
);
245 s
= fmt
->format(value
.getLong(), s
);
248 UBool show
= verbose
;
250 logln(/*value.getString(temp) +*/ " F> " + escape(s
));
252 fmt
->parse(s
, n
, status
);
253 failure(status
, "fmt->parse");
255 logln(escape(s
) + " P> " /*+ n.getString(temp)*/);
258 s2
= fmt
->format(n
.getDouble(), s2
);
260 s2
= fmt
->format(n
.getLong(), s2
);
263 logln(/*n.getString(temp) +*/ " F> " + escape(s2
));
267 errln("*** STRING ERROR \"" + escape(s
) + "\" != \"" + escape(s2
) + "\"");
272 if(EXACT_NUMERIC_COMPARE
) {
274 errln("*** NUMERIC ERROR");
279 // Compute proportional error
280 double error
= proportionalError(value
, n
);
282 if(error
> MAX_ERROR
) {
283 errln(UnicodeString("*** NUMERIC ERROR ") + error
);
287 if (error
> max_numeric_error
)
288 max_numeric_error
= error
;
289 if (error
< min_numeric_error
)
290 min_numeric_error
= error
;
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> " +
302 NumberFormatRoundTripTest::proportionalError(const Formattable
& a
, const Formattable
& b
)
316 double error
= aa
- bb
;
317 if(aa
!= 0 && bb
!= 0)
320 return uprv_fabs(error
);
324 NumberFormatRoundTripTest::typeOf(const Formattable
& n
, UnicodeString
& result
)
326 if(n
.getType() == Formattable::kLong
) {
327 result
= UnicodeString(" Long");
329 else if(n
.getType() == Formattable::kDouble
) {
330 result
= UnicodeString(" Double");
332 else if(n
.getType() == Formattable::kString
) {
333 result
= UnicodeString(" UnicodeString");
342 NumberFormatRoundTripTest::escape(UnicodeString
& s
)
344 UnicodeString
copy(s
);
346 for(int i
= 0; i
< copy
.length(); ++i
) {
347 UChar32 c
= copy
.char32At(i
);
356 sprintf(temp
, "%4X", c
); // might not work
363 #endif /* #if !UCONFIG_NO_FORMATTING */