1 /*
  2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @summary round trip test NumberFormat
 27  * @key randomness
 28  * @run junit NumberRoundTrip
 29  */
 30 
 31 import java.text.DecimalFormat;
 32 import java.text.NumberFormat;
 33 import java.text.ParseException;
 34 import java.util.Locale;
 35 
 36 import org.junit.jupiter.api.Test;
 37 
 38 import static org.junit.jupiter.api.Assertions.fail;
 39 
 40 /**
 41  * This class tests the round-trip behavior of NumberFormat, DecimalFormat, and DigitList.
 42  * Round-trip behavior is tested by taking a numeric value and formatting it, then
 43  * parsing the resulting string, and comparing this result with the original value.
 44  * Two tests are applied:  String preservation, and numeric preservation.  String
 45  * preservation is exact; numeric preservation is not.  However, numeric preservation
 46  * should extend to the few least-significant bits.
 47  * //bug472
 48  */
 49 public class NumberRoundTrip {
 50     static final boolean STRING_COMPARE = true;
 51     static final boolean EXACT_NUMERIC_COMPARE = false;
 52     static final double MAX_ERROR = 1e-14;
 53     static double max_numeric_error = 0;
 54     static double min_numeric_error = 1;
 55 
 56     String localeName, formatName;
 57 
 58     @Test
 59     public void TestNumberFormatRoundTrip() {
 60         System.out.println("Default Locale");
 61         localeName = "Default Locale";
 62         formatName = "getInstance";
 63         doTest(NumberFormat.getInstance());
 64         formatName = "getNumberInstance";
 65         doTest(NumberFormat.getNumberInstance());
 66         formatName = "getCurrencyInstance";
 67         doTest(NumberFormat.getCurrencyInstance());
 68         formatName = "getPercentInstance";
 69         doTest(NumberFormat.getPercentInstance());
 70 
 71         Locale[] loc = NumberFormat.getAvailableLocales();
 72         for (int i=0; i<loc.length; ++i) {
 73             System.out.println(loc[i].getDisplayName());
 74             localeName = loc[i].toString();
 75             formatName = "getInstance";
 76             doTest(NumberFormat.getInstance(loc[i]));
 77             formatName = "getNumberInstance";
 78             doTest(NumberFormat.getNumberInstance(loc[i]));
 79             formatName = "getCurrencyInstance";
 80             doTest(NumberFormat.getCurrencyInstance(loc[i]));
 81             formatName = "getPercentInstance";
 82             doTest(NumberFormat.getPercentInstance(loc[i]));
 83         }
 84 
 85         System.out.println("Numeric error " +
 86               min_numeric_error + " to " +
 87               max_numeric_error);
 88     }
 89 
 90     public void doTest(NumberFormat fmt) {
 91         doTest(fmt, Double.NaN);
 92         doTest(fmt, Double.POSITIVE_INFINITY);
 93         doTest(fmt, Double.NEGATIVE_INFINITY);
 94 
 95         doTest(fmt, 500);
 96         doTest(fmt, 0);
 97         doTest(fmt, 5555555555555555L);
 98         doTest(fmt, 55555555555555555L);
 99         doTest(fmt, 9223372036854775807L);
100         doTest(fmt, 9223372036854775808.0);
101         doTest(fmt, -9223372036854775808L);
102         doTest(fmt, -9223372036854775809.0);
103 
104         for (int i=0; i<2; ++i) {
105             doTest(fmt, randomDouble(1));
106             doTest(fmt, randomDouble(10000));
107             doTest(fmt, Math.floor(randomDouble(10000)));
108             doTest(fmt, randomDouble(1e50));
109             doTest(fmt, randomDouble(1e-50));
110             doTest(fmt, randomDouble(1e100));
111             // The use of double d such that isInfinite(100d) causes the
112             // numeric test to fail with percent formats (bug 4266589).
113             // Largest double s.t. 100d < Inf: d=1.7976931348623156E306
114             doTest(fmt, randomDouble(1e306));
115             doTest(fmt, randomDouble(1e-323));
116             doTest(fmt, randomDouble(1e-100));
117         }
118     }
119 
120     /**
121      * Return a random value from -range..+range.
122      */
123     public double randomDouble(double range) {
124         double a = Math.random();
125         return (2.0 * range * a) - range;
126     }
127 
128     public void doTest(NumberFormat fmt, double value) {
129         doTest(fmt, Double.valueOf(value));
130     }
131 
132     public void doTest(NumberFormat fmt, long value) {
133         doTest(fmt, Long.valueOf(value));
134     }
135 
136     static double proportionalError(Number a, Number b) {
137         double aa = a.doubleValue(), bb = b.doubleValue();
138         double error = aa - bb;
139         if (aa != 0 && bb != 0) error /= aa;
140         return Math.abs(error);
141     }
142 
143     public void doTest(NumberFormat fmt, Number value) {
144         fmt.setMaximumFractionDigits(Integer.MAX_VALUE);
145         String s = fmt.format(value), s2 = null;
146         Number n = null;
147         String err = "";
148         try {
149             System.out.println("  " + value + " F> " + escape(s));
150             n = fmt.parse(s);
151             System.out.println("  " + escape(s) + " P> " + n);
152             s2 = fmt.format(n);
153             System.out.println("  " + n + " F> " + escape(s2));
154 
155             if (STRING_COMPARE) {
156                 if (!s.equals(s2)) {
157                     if (fmt instanceof DecimalFormat) {
158                         System.out.println("Text mismatch: expected: " + s + ", got: " + s2 + " --- Try BigDecimal parsing.");
159                         ((DecimalFormat)fmt).setParseBigDecimal(true);
160                         n = fmt.parse(s);
161                         System.out.println("  " + escape(s) + " P> " + n);
162                         s2 = fmt.format(n);
163                         System.out.println("  " + n + " F> " + escape(s2));
164                         ((DecimalFormat)fmt).setParseBigDecimal(false);
165 
166                         if (!s.equals(s2)) {
167                             err = "STRING ERROR(DecimalFormat): ";
168                         }
169                     } else {
170                         err = "STRING ERROR(NumberFormat): ";
171                     }
172                 }
173             }
174 
175             if (EXACT_NUMERIC_COMPARE) {
176                 if (value.doubleValue() != n.doubleValue()) {
177                     err += "NUMERIC ERROR: ";
178                 }
179             } else {
180                 // Compute proportional error
181                 double error = proportionalError(value, n);
182 
183                 if (error > MAX_ERROR) {
184                     err += "NUMERIC ERROR " + error + ": ";
185                 }
186 
187                 if (error > max_numeric_error) max_numeric_error = error;
188                 if (error < min_numeric_error) min_numeric_error = error;
189             }
190 
191             String message = value + typeOf(value) + " F> " +
192                 escape(s) + " P> " +
193                 n + typeOf(n) + " F> " +
194                 escape(s2);
195             if (err.length() > 0) {
196                 fail("*** " + err + " with " +
197                       formatName + " in " + localeName +
198                       " " + message);
199             } else {
200                 System.out.println(message);
201             }
202         } catch (ParseException e) {
203             fail("*** " + e.toString() + " with " +
204                   formatName + " in " + localeName);
205         }
206     }
207 
208     static String typeOf(Number n) {
209         if (n instanceof Long) return " Long";
210         if (n instanceof Double) return " Double";
211         return " Number";
212     }
213 
214     static String escape(String s) {
215         StringBuffer buf = new StringBuffer();
216         for (int i=0; i<s.length(); ++i) {
217             char c = s.charAt(i);
218             if (c < (char)0xFF) {
219                 buf.append(c);
220             } else {
221                 buf.append("\\U");
222                 buf.append(Integer.toHexString((c & 0xF000) >> 12));
223                 buf.append(Integer.toHexString((c & 0x0F00) >> 8));
224                 buf.append(Integer.toHexString((c & 0x00F0) >> 4));
225                 buf.append(Integer.toHexString(c & 0x000F));
226             }
227         }
228         return buf.toString();
229     }
230 }