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