< prev index next >

test/jdk/java/text/Format/NumberFormat/NumberRoundTrip.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
--- 1,7 ---
  /*
!  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 22,48 ***
   */
  
  /*
   * @test
   * @summary round trip test NumberFormat
-  * @library /java/text/testlib
   * @key randomness
   */
  
! import java.text.*;
! import java.util.*;
  
  /**
   * This class tests the round-trip behavior of NumberFormat, DecimalFormat, and DigitList.
   * Round-trip behavior is tested by taking a numeric value and formatting it, then
   * parsing the resulting string, and comparing this result with the original value.
   * Two tests are applied:  String preservation, and numeric preservation.  String
   * preservation is exact; numeric preservation is not.  However, numeric preservation
   * should extend to the few least-significant bits.
   * //bug472
   */
! public class NumberRoundTrip extends IntlTest {
      static final boolean STRING_COMPARE = true;
      static final boolean EXACT_NUMERIC_COMPARE = false;
      static final double MAX_ERROR = 1e-14;
-     static boolean DEBUG = false;
      static double max_numeric_error = 0;
      static double min_numeric_error = 1;
  
      String localeName, formatName;
  
!     public static void main(String[] args) throws Exception {
-         if (args.length > 0 && args[0].equals("-debug")) {
-             DEBUG = true;
-             String[] newargs = new String[args.length - 1];
-             System.arraycopy(args, 1, newargs, 0, newargs.length);
-             args = newargs;
-         }
-         new NumberRoundTrip().run(args);
-     }
- 
      public void TestNumberFormatRoundTrip() {
!         logln("Default Locale");
          localeName = "Default Locale";
          formatName = "getInstance";
          doTest(NumberFormat.getInstance());
          formatName = "getNumberInstance";
          doTest(NumberFormat.getNumberInstance());
--- 22,44 ---
   */
  
  /*
   * @test
   * @summary round trip test NumberFormat
   * @key randomness
+  * @run junit NumberRoundTrip
   */
  
! import java.text.DecimalFormat;
! import java.text.NumberFormat;
+ import java.text.ParseException;
+ import java.util.Locale;
+ 
+ import org.junit.jupiter.api.Test;
+ 
+ import static org.junit.jupiter.api.Assertions.fail;
  
  /**
   * This class tests the round-trip behavior of NumberFormat, DecimalFormat, and DigitList.
   * Round-trip behavior is tested by taking a numeric value and formatting it, then
   * parsing the resulting string, and comparing this result with the original value.
   * Two tests are applied:  String preservation, and numeric preservation.  String
   * preservation is exact; numeric preservation is not.  However, numeric preservation
   * should extend to the few least-significant bits.
   * //bug472
   */
! public class NumberRoundTrip {
      static final boolean STRING_COMPARE = true;
      static final boolean EXACT_NUMERIC_COMPARE = false;
      static final double MAX_ERROR = 1e-14;
      static double max_numeric_error = 0;
      static double min_numeric_error = 1;
  
      String localeName, formatName;
  
!     @Test
      public void TestNumberFormatRoundTrip() {
!         System.out.println("Default Locale");
          localeName = "Default Locale";
          formatName = "getInstance";
          doTest(NumberFormat.getInstance());
          formatName = "getNumberInstance";
          doTest(NumberFormat.getNumberInstance());

*** 72,11 ***
          formatName = "getPercentInstance";
          doTest(NumberFormat.getPercentInstance());
  
          Locale[] loc = NumberFormat.getAvailableLocales();
          for (int i=0; i<loc.length; ++i) {
!             logln(loc[i].getDisplayName());
              localeName = loc[i].toString();
              formatName = "getInstance";
              doTest(NumberFormat.getInstance(loc[i]));
              formatName = "getNumberInstance";
              doTest(NumberFormat.getNumberInstance(loc[i]));
--- 68,11 ---
          formatName = "getPercentInstance";
          doTest(NumberFormat.getPercentInstance());
  
          Locale[] loc = NumberFormat.getAvailableLocales();
          for (int i=0; i<loc.length; ++i) {
!             System.out.println(loc[i].getDisplayName());
              localeName = loc[i].toString();
              formatName = "getInstance";
              doTest(NumberFormat.getInstance(loc[i]));
              formatName = "getNumberInstance";
              doTest(NumberFormat.getNumberInstance(loc[i]));

*** 84,11 ***
              doTest(NumberFormat.getCurrencyInstance(loc[i]));
              formatName = "getPercentInstance";
              doTest(NumberFormat.getPercentInstance(loc[i]));
          }
  
!         logln("Numeric error " +
                min_numeric_error + " to " +
                max_numeric_error);
      }
  
      public void doTest(NumberFormat fmt) {
--- 80,11 ---
              doTest(NumberFormat.getCurrencyInstance(loc[i]));
              formatName = "getPercentInstance";
              doTest(NumberFormat.getPercentInstance(loc[i]));
          }
  
!         System.out.println("Numeric error " +
                min_numeric_error + " to " +
                max_numeric_error);
      }
  
      public void doTest(NumberFormat fmt) {

*** 148,25 ***
          fmt.setMaximumFractionDigits(Integer.MAX_VALUE);
          String s = fmt.format(value), s2 = null;
          Number n = null;
          String err = "";
          try {
!             if (DEBUG) logln("  " + value + " F> " + escape(s));
              n = fmt.parse(s);
!             if (DEBUG) logln("  " + escape(s) + " P> " + n);
              s2 = fmt.format(n);
!             if (DEBUG) logln("  " + n + " F> " + escape(s2));
  
              if (STRING_COMPARE) {
                  if (!s.equals(s2)) {
                      if (fmt instanceof DecimalFormat) {
!                         logln("Text mismatch: expected: " + s + ", got: " + s2 + " --- Try BigDecimal parsing.");
                          ((DecimalFormat)fmt).setParseBigDecimal(true);
                          n = fmt.parse(s);
!                         if (DEBUG) logln("  " + escape(s) + " P> " + n);
                          s2 = fmt.format(n);
!                         if (DEBUG) logln("  " + n + " F> " + escape(s2));
                          ((DecimalFormat)fmt).setParseBigDecimal(false);
  
                          if (!s.equals(s2)) {
                              err = "STRING ERROR(DecimalFormat): ";
                          }
--- 144,25 ---
          fmt.setMaximumFractionDigits(Integer.MAX_VALUE);
          String s = fmt.format(value), s2 = null;
          Number n = null;
          String err = "";
          try {
!             System.out.println("  " + value + " F> " + escape(s));
              n = fmt.parse(s);
!             System.out.println("  " + escape(s) + " P> " + n);
              s2 = fmt.format(n);
!             System.out.println("  " + n + " F> " + escape(s2));
  
              if (STRING_COMPARE) {
                  if (!s.equals(s2)) {
                      if (fmt instanceof DecimalFormat) {
!                         System.out.println("Text mismatch: expected: " + s + ", got: " + s2 + " --- Try BigDecimal parsing.");
                          ((DecimalFormat)fmt).setParseBigDecimal(true);
                          n = fmt.parse(s);
!                         System.out.println("  " + escape(s) + " P> " + n);
                          s2 = fmt.format(n);
!                         System.out.println("  " + n + " F> " + escape(s2));
                          ((DecimalFormat)fmt).setParseBigDecimal(false);
  
                          if (!s.equals(s2)) {
                              err = "STRING ERROR(DecimalFormat): ";
                          }

*** 195,18 ***
              String message = value + typeOf(value) + " F> " +
                  escape(s) + " P> " +
                  n + typeOf(n) + " F> " +
                  escape(s2);
              if (err.length() > 0) {
!                 errln("*** " + err + " with " +
                        formatName + " in " + localeName +
                        " " + message);
              } else {
!                 logln(message);
              }
          } catch (ParseException e) {
!             errln("*** " + e.toString() + " with " +
                    formatName + " in " + localeName);
          }
      }
  
      static String typeOf(Number n) {
--- 191,18 ---
              String message = value + typeOf(value) + " F> " +
                  escape(s) + " P> " +
                  n + typeOf(n) + " F> " +
                  escape(s2);
              if (err.length() > 0) {
!                 fail("*** " + err + " with " +
                        formatName + " in " + localeName +
                        " " + message);
              } else {
!                 System.out.println(message);
              }
          } catch (ParseException e) {
!             fail("*** " + e.toString() + " with " +
                    formatName + " in " + localeName);
          }
      }
  
      static String typeOf(Number n) {
< prev index next >