test/java/math/BigDecimal/IntegralDivisionTests.java

Print this page
rev 7924 : 6378503: In java.math.BigDecimal, division by one returns zero
6446965: Using BigDecimal.divideToIntegralValue with extreme scales can lead to an incorrect result
Summary: Fix overflow of ints and ensure appropriate values passed to checkScaleNonZero()
Reviewed-by: darcy, martin
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


   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  * @test
  25  * @bug 4904082 4917089 6337226
  26  * @summary Tests that integral division and related methods return the proper result and scale.
  27  * @author Joseph D. Darcy
  28  */
  29 import java.math.*;
  30 public class IntegralDivisionTests {
  31 
  32     static int dividetoIntegralValueTests() {
  33         int failures = 0;
  34 
  35         // Exact integer quotient should have the same results from
  36         // the exact divide and dividetoIntegralValue
  37 
  38 
  39         // Rounded results
  40         BigDecimal [][] moreTestCases = {
  41             {new BigDecimal("11003"),   new BigDecimal("10"),   new BigDecimal("1100")},
  42             {new BigDecimal("11003"),   new BigDecimal("1e1"),  new BigDecimal("1100.0")},
  43             {new BigDecimal("1e9"),     new BigDecimal("1"),    new BigDecimal("1e9")},
  44             {new BigDecimal("1e9"),     new BigDecimal("1.00"), new BigDecimal("1e9")},
  45             {new BigDecimal("1e9"),     new BigDecimal("0.1"),  new BigDecimal("1e10")},
  46             {new BigDecimal("10e8"),    new BigDecimal("0.1"),  new BigDecimal("10e9")},
  47             {new BigDecimal("400e1"),   new BigDecimal("5"),    new BigDecimal("80e1")},
  48             {new BigDecimal("400e1"),   new BigDecimal("4.999999999"),  new BigDecimal("8e2")},
  49             {new BigDecimal("40e2"),    new BigDecimal("5"),    new BigDecimal("8e2")},



  50         };
  51 
  52         for(BigDecimal [] testCase: moreTestCases) {
  53             BigDecimal quotient;
  54             if (! (quotient=testCase[0].divideToIntegralValue(testCase[1])).equals(testCase[2]) ){
  55                 failures++;
  56                 // BigDecimal exact = testCase[0].divide(testCase[1]);
  57                 System.err.println();
  58                 System.err.println("dividend  = " + testCase[0] + " scale = " + testCase[0].scale());
  59                 System.err.println("divisor   = " + testCase[1] + " scale = " + testCase[1].scale());
  60                 System.err.println("quotient  = " + quotient    + " scale = " + quotient.scale());
  61                 System.err.println("expected  = " + testCase[2] + " scale = " + testCase[2].scale());
  62                 // System.err.println("exact     = " + exact       + " scale = " + exact.scale());
  63             }
  64         }
  65 
  66         return failures;
  67     }
  68 
  69     static int dividetoIntegralValueRoundedTests() {




   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  * @test
  25  * @bug 4904082 4917089 6337226 6378503
  26  * @summary Tests that integral division and related methods return the proper result and scale.
  27  * @author Joseph D. Darcy
  28  */
  29 import java.math.*;
  30 public class IntegralDivisionTests {
  31 
  32     static int dividetoIntegralValueTests() {
  33         int failures = 0;
  34 
  35         // Exact integer quotient should have the same results from
  36         // the exact divide and dividetoIntegralValue
  37 
  38 
  39         // Rounded results
  40         BigDecimal [][] moreTestCases = {
  41             {new BigDecimal("11003"),   new BigDecimal("10"),   new BigDecimal("1100")},
  42             {new BigDecimal("11003"),   new BigDecimal("1e1"),  new BigDecimal("1100.0")},
  43             {new BigDecimal("1e9"),     new BigDecimal("1"),    new BigDecimal("1e9")},
  44             {new BigDecimal("1e9"),     new BigDecimal("1.00"), new BigDecimal("1e9")},
  45             {new BigDecimal("1e9"),     new BigDecimal("0.1"),  new BigDecimal("1e10")},
  46             {new BigDecimal("10e8"),    new BigDecimal("0.1"),  new BigDecimal("10e9")},
  47             {new BigDecimal("400e1"),   new BigDecimal("5"),    new BigDecimal("80e1")},
  48             {new BigDecimal("400e1"),   new BigDecimal("4.999999999"),  new BigDecimal("8e2")},
  49             {new BigDecimal("40e2"),    new BigDecimal("5"),    new BigDecimal("8e2")},
  50             {BigDecimal.valueOf(1, Integer.MIN_VALUE),
  51                 BigDecimal.valueOf(1, -(Integer.MAX_VALUE & 0x7fffff00)),
  52                 BigDecimal.valueOf(1, -256)},
  53         };
  54 
  55         for(BigDecimal [] testCase: moreTestCases) {
  56             BigDecimal quotient;
  57             if (! (quotient=testCase[0].divideToIntegralValue(testCase[1])).equals(testCase[2]) ){
  58                 failures++;
  59                 // BigDecimal exact = testCase[0].divide(testCase[1]);
  60                 System.err.println();
  61                 System.err.println("dividend  = " + testCase[0] + " scale = " + testCase[0].scale());
  62                 System.err.println("divisor   = " + testCase[1] + " scale = " + testCase[1].scale());
  63                 System.err.println("quotient  = " + quotient    + " scale = " + quotient.scale());
  64                 System.err.println("expected  = " + testCase[2] + " scale = " + testCase[2].scale());
  65                 // System.err.println("exact     = " + exact       + " scale = " + exact.scale());
  66             }
  67         }
  68 
  69         return failures;
  70     }
  71 
  72     static int dividetoIntegralValueRoundedTests() {