< prev index next >

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

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2003, 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) 2003, 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.

*** 23,31 ***
  
  /*
   * @test
   * @bug 4018937 8008577
   * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.
!  * @library /java/text/testlib
-  * @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalParse
   */
  
  import java.math.BigDecimal;
  import java.text.*;
  import java.util.*;
  
! public class BigDecimalParse extends IntlTest {
  
!     public static void main(String[] args) throws Exception {
!         Locale loc = Locale.getDefault();
!         try {
!             Locale.setDefault(Locale.US);
!             new BigDecimalParse().run(args);
!         } finally {
!             // restore the reserved locale
!             Locale.setDefault(loc);
-         }
      }
  
      static final String nonsep_int =
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +
--- 23,31 ---
  
  /*
   * @test
   * @bug 4018937 8008577
   * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.
!  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalParse
   */
  
  import java.math.BigDecimal;
  import java.text.*;
  import java.util.*;
  
! import org.junit.jupiter.api.Test;
+ import org.junit.jupiter.api.BeforeAll;
  
! import static org.junit.jupiter.api.Assertions.fail;
! 
! public class BigDecimalParse {
! 
!     // Change JVM default Locale
!     @BeforeAll
!     static void initAll() {
!         Locale.setDefault(Locale.US);
      }
  
+ 
      static final String nonsep_int =
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +
          "123456789012345678901234567890123456789012345678901234567890" +

*** 106,11 ***
      DecimalFormat df;
  
      /**
       * Test for normal big numbers which have the fraction part
       */
!     void test_Parse_in_DecimalFormat_BigDecimal() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 1234...7890.012...789
          // To:   BigDecimal 1234...7890.012...789
--- 106,12 ---
      DecimalFormat df;
  
      /**
       * Test for normal big numbers which have the fraction part
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_BigDecimal() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 1234...7890.012...789
          // To:   BigDecimal 1234...7890.012...789

*** 148,11 ***
      }
  
      /**
       * Test for normal big numbers which have the fraction part with multiplier
       */
!     void test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 250,0...0,000.000...000
          // To:   1000...0000.000...000
--- 149,12 ---
      }
  
      /**
       * Test for normal big numbers which have the fraction part with multiplier
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 250,0...0,000.000...000
          // To:   1000...0000.000...000

*** 190,11 ***
      }
  
      /**
       * Test for division by zero (BigDecimal)
       */
!     void test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
          df.setMultiplier(0);
  
          // From: 1000.000
--- 192,12 ---
      }
  
      /**
       * Test for division by zero (BigDecimal)
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
          df.setMultiplier(0);
  
          // From: 1000.000

*** 211,11 ***
      }
  
      /**
       * Test for division by zero (Double)
       */
!     void test_Parse_in_DecimalFormat_Double_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(false);
          df.setMultiplier(0);
  
          // From: 1000.000
--- 214,12 ---
      }
  
      /**
       * Test for division by zero (Double)
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_Double_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(false);
          df.setMultiplier(0);
  
          // From: 1000.000

*** 248,11 ***
      }
  
      /**
       * Test for division by zero (Long)
       */
!     void test_Parse_in_DecimalFormat_Long_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(false);
          df.setMultiplier(0);
  
          // From: 1000
--- 252,12 ---
      }
  
      /**
       * Test for division by zero (Long)
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_Long_DivisionByZero() {
          df = new DecimalFormat();
          df.setParseBigDecimal(false);
          df.setMultiplier(0);
  
          // From: 1000

*** 269,11 ***
      }
  
      /**
       * Test for normal big numbers which don't have the fraction part
       */
!     void test_Parse_in_DecimalFormat_BigInteger() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 123...890
          // To:   BigDecimal 123...890
--- 274,12 ---
      }
  
      /**
       * Test for normal big numbers which don't have the fraction part
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_BigInteger() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 123...890
          // To:   BigDecimal 123...890

*** 294,11 ***
  
      /**
       * Test for normal big numbers which don't have the fraction part with
       * multiplier
       */
!     void test_Parse_in_DecimalFormat_BigInteger_usingMultiplier() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 250,0...0,000
          // To:   1000...0000
--- 300,12 ---
  
      /**
       * Test for normal big numbers which don't have the fraction part with
       * multiplier
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_BigInteger_usingMultiplier() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          // From: 250,0...0,000
          // To:   1000...0000

*** 335,11 ***
       * Test for special numbers
       *    Double.NaN
       *    Double.POSITIVE_INFINITY
       *    Double.NEGATIVE_INFINITY
       */
!     void test_Parse_in_DecimalFormat_SpecialNumber() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          String[] numbers = {
              "0", "0.0", "25", "25.0", "25.5", "\u221e", "\ufffd",
--- 342,12 ---
       * Test for special numbers
       *    Double.NaN
       *    Double.POSITIVE_INFINITY
       *    Double.NEGATIVE_INFINITY
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_SpecialNumber() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          String[] numbers = {
              "0", "0.0", "25", "25.0", "25.5", "\u221e", "\ufffd",

*** 376,11 ***
      }
  
      /**
       * Test for special numbers
       */
!     void test_Parse_in_DecimalFormat_Other() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          String[] numbers = {
              "-9223372036854775808",     // Long.MIN_VALUE
--- 384,12 ---
      }
  
      /**
       * Test for special numbers
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_Other() {
          df = new DecimalFormat();
          df.setParseBigDecimal(true);
  
          String[] numbers = {
              "-9223372036854775808",     // Long.MIN_VALUE

*** 470,11 ***
      };
  
      /**
       * Test for MessageFormat: setParseIntegerOnly(false)
       */
!     void test_Parse_in_MessageFormat_NotParseIntegerOnly() {
          for (int i=0; i < patterns.length; i++) {
              pp = new ParsePosition(0);
              Object[] parsed = null;
  
              try {
--- 479,12 ---
      };
  
      /**
       * Test for MessageFormat: setParseIntegerOnly(false)
       */
!     @Test
+     public void test_Parse_in_MessageFormat_NotParseIntegerOnly() {
          for (int i=0; i < patterns.length; i++) {
              pp = new ParsePosition(0);
              Object[] parsed = null;
  
              try {

*** 485,23 ***
                  }
  
                  parsed = mf.parse(from[i], pp);
  
                  if (pp.getErrorIndex() != -1) {
!                     errln("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:-1, got:"+
                            pp.getErrorIndex() + " for " + from[i]);
                  }
                  if (pp.getIndex() != parsePosition1[i]) {
!                     errln("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition1[i] + ", got:"+ pp.getIndex() +
                            " for " + from[i]);
                  }
              }
              catch(Exception e) {
!                 errln("Unexpected exception: " + e.getMessage());
              }
  
              checkType(from[i], getType(new BigDecimal(expected1[i])),
                        getType((Number)parsed[0]));
              checkParse(from[i], new BigDecimal(expected1[i]),
--- 495,23 ---
                  }
  
                  parsed = mf.parse(from[i], pp);
  
                  if (pp.getErrorIndex() != -1) {
!                     fail("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:-1, got:"+
                            pp.getErrorIndex() + " for " + from[i]);
                  }
                  if (pp.getIndex() != parsePosition1[i]) {
!                     fail("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition1[i] + ", got:"+ pp.getIndex() +
                            " for " + from[i]);
                  }
              }
              catch(Exception e) {
!                 fail("Unexpected exception: " + e.getMessage());
              }
  
              checkType(from[i], getType(new BigDecimal(expected1[i])),
                        getType((Number)parsed[0]));
              checkParse(from[i], new BigDecimal(expected1[i]),

*** 556,11 ***
      };
  
      /**
       * Test for MessageFormat: setParseIntegerOnly(true)
       */
!     void test_Parse_in_MessageFormat_ParseIntegerOnly() {
          for (int i=0; i < patterns.length; i++) {
              pp = new ParsePosition(0);
              Object[] parsed = null;
  
              try {
--- 566,12 ---
      };
  
      /**
       * Test for MessageFormat: setParseIntegerOnly(true)
       */
!     @Test
+     public void test_Parse_in_MessageFormat_ParseIntegerOnly() {
          for (int i=0; i < patterns.length; i++) {
              pp = new ParsePosition(0);
              Object[] parsed = null;
  
              try {

*** 572,24 ***
                  }
  
                  parsed = mf.parse(from[i], pp);
  
                  if (pp.getErrorIndex() != parsePosition2[i][0]) {
!                     errln("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:" +
                            parsePosition2[i][0] + ", got:"+ pp.getErrorIndex() +
                            " for " + from[i]);
                  }
                  if (pp.getIndex() != parsePosition2[i][1]) {
!                     errln("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition2[i][1] + ", got:"+ pp.getIndex() +
                            " for " + from[i]);
                  }
              }
              catch(Exception e) {
!                 errln("Unexpected exception: " + e.getMessage());
              }
  
              if (parsePosition2[i][0] == -1) {
                  checkType(from[i], getType(new BigDecimal(expected2[i])),
                            getType((Number)parsed[0]));
--- 583,24 ---
                  }
  
                  parsed = mf.parse(from[i], pp);
  
                  if (pp.getErrorIndex() != parsePosition2[i][0]) {
!                     fail("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:" +
                            parsePosition2[i][0] + ", got:"+ pp.getErrorIndex() +
                            " for " + from[i]);
                  }
                  if (pp.getIndex() != parsePosition2[i][1]) {
!                     fail("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition2[i][1] + ", got:"+ pp.getIndex() +
                            " for " + from[i]);
                  }
              }
              catch(Exception e) {
!                 fail("Unexpected exception: " + e.getMessage());
              }
  
              if (parsePosition2[i][0] == -1) {
                  checkType(from[i], getType(new BigDecimal(expected2[i])),
                            getType((Number)parsed[0]));

*** 622,11 ***
      };
  
      /**
       * Test for DecimalFormat: setParseIntegerOnly(true)
       */
!     void test_Parse_in_DecimalFormat_ParseIntegerOnly() {
          DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance();
          df.setParseBigDecimal(true);
  
          for (int i=0; i < from3.length; i++) {
              pp = new ParsePosition(0);
--- 633,12 ---
      };
  
      /**
       * Test for DecimalFormat: setParseIntegerOnly(true)
       */
!     @Test
+     public void test_Parse_in_DecimalFormat_ParseIntegerOnly() {
          DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance();
          df.setParseBigDecimal(true);
  
          for (int i=0; i < from3.length; i++) {
              pp = new ParsePosition(0);

*** 634,24 ***
  
              try {
                  parsed = df.parse(from3[i], pp);
  
                  if (pp.getErrorIndex() != parsePosition3[i][0]) {
!                     errln("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:" +
                            parsePosition3[i][0] + ", got:"+ pp.getErrorIndex() +
                            " for " + from3[i]);
                  }
                  if (pp.getIndex() != parsePosition3[i][1]) {
!                     errln("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition3[i][1] + ", got:"+ pp.getIndex() +
                            " for " + from3[i]);
                  }
              }
              catch(Exception e) {
!                 errln("Unexpected exception: " + e.getMessage());
              }
  
              if (parsePosition3[i][0] == -1) {
                  checkType(from3[i], getType(new BigDecimal(expected3[i])),
                            getType(parsed));
--- 646,24 ---
  
              try {
                  parsed = df.parse(from3[i], pp);
  
                  if (pp.getErrorIndex() != parsePosition3[i][0]) {
!                     fail("Case" + (i+1) +
                            ": getErrorIndex() returns wrong value. expected:" +
                            parsePosition3[i][0] + ", got:"+ pp.getErrorIndex() +
                            " for " + from3[i]);
                  }
                  if (pp.getIndex() != parsePosition3[i][1]) {
!                     fail("Case" + (i+1) +
                            ": getIndex() returns wrong value. expected:" +
                            parsePosition3[i][1] + ", got:"+ pp.getIndex() +
                            " for " + from3[i]);
                  }
              }
              catch(Exception e) {
!                 fail("Unexpected exception: " + e.getMessage());
              }
  
              if (parsePosition3[i][0] == -1) {
                  checkType(from3[i], getType(new BigDecimal(expected3[i])),
                            getType(parsed));

*** 665,40 ***
          try {
              parsed = df.parse(from, pp);
          }
          catch(Exception e) {
              exceptionOccurred = true;
!             errln(e.getMessage());
          }
          if (!exceptionOccurred) {
              checkParse(from, to, parsed);
              checkType(from, getType(to), getType(parsed));
              checkParsePosition(from, from.length(), pp.getIndex());
          }
      }
  
      private void checkParse(String orig, Number expected, Number got) {
          if (!expected.equals(got)) {
!             errln("Parsing... failed." +
                    "\n   original: " + orig +
                    "\n   parsed:   " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
  
      private void checkType(String orig, String expected, String got) {
          if (!expected.equals(got)) {
!             errln("Parsing... unexpected Class returned." +
                    "\n   original: " + orig +
                    "\n   got:      " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
  
      private void checkParsePosition(String orig, int expected, int got) {
          if (expected != got) {
!             errln("Parsing... wrong ParsePosition returned." +
                    "\n   original: " + orig +
                    "\n   got:      " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
--- 677,40 ---
          try {
              parsed = df.parse(from, pp);
          }
          catch(Exception e) {
              exceptionOccurred = true;
!             fail(e.getMessage());
          }
          if (!exceptionOccurred) {
              checkParse(from, to, parsed);
              checkType(from, getType(to), getType(parsed));
              checkParsePosition(from, from.length(), pp.getIndex());
          }
      }
  
      private void checkParse(String orig, Number expected, Number got) {
          if (!expected.equals(got)) {
!             fail("Parsing... failed." +
                    "\n   original: " + orig +
                    "\n   parsed:   " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
  
      private void checkType(String orig, String expected, String got) {
          if (!expected.equals(got)) {
!             fail("Parsing... unexpected Class returned." +
                    "\n   original: " + orig +
                    "\n   got:      " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
  
      private void checkParsePosition(String orig, int expected, int got) {
          if (expected != got) {
!             fail("Parsing... wrong ParsePosition returned." +
                    "\n   original: " + orig +
                    "\n   got:      " + got +
                    "\n   expected: " + expected + "\n");
          }
      }
< prev index next >