Path: blob/master/test/jdk/java/math/BigDecimal/RangeTests.java
41152 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 703658226* @summary Some new tests for the add method and constructor with MathContext.27* @run main RangeTests28* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 RangeTests29* @author Sergey V. Kuksenko30*/3132import java.math.BigDecimal;33import java.math.BigInteger;34import java.math.MathContext;3536public class RangeTests {373839private static int addTest(BigDecimal arg1, BigDecimal arg2, BigDecimal expectedResult) {40int failures = 0;41BigDecimal result = arg1.add(arg2);42if (!result.equals(expectedResult)) {43System.out.println("Sum:" +44arg1 + " + " +45arg2 + " == " +46result + "; expected " +47expectedResult48);49failures++;50}51result = arg2.add(arg1);52if (!result.equals(expectedResult)) {53System.out.println("Sum:" +54arg2 + " + " +55arg1 + " == " +56result + "; expected " +57expectedResult58);59failures++;60}61return failures;62}6364/*65* Test BigDecimal.add(BigDecimal) when values are withing different ranges:66* 1. within 32 bits67* 2. within 64 bits68* 3. outside 64 bits.69*/70private static int addBoundaryTest() {71int failures = 0;72failures += addTest(73new BigDecimal("85070591730234615847396907784232501249"),74BigDecimal.valueOf(0),75new BigDecimal("85070591730234615847396907784232501249") );76failures += addTest(77new BigDecimal("-85070591730234615847396907784232501249"),78BigDecimal.valueOf(0),79new BigDecimal("-85070591730234615847396907784232501249") );80failures += addTest(81new BigDecimal("85070591730234615847396907784232501249"),82BigDecimal.valueOf(1),83new BigDecimal("85070591730234615847396907784232501250") );84failures += addTest(85new BigDecimal("85070591730234615847396907784232501249"),86BigDecimal.valueOf(-1),87new BigDecimal("85070591730234615847396907784232501248") );88failures += addTest(89new BigDecimal("-85070591730234615847396907784232501250"),90BigDecimal.valueOf(-1),91new BigDecimal("-85070591730234615847396907784232501251") );92failures += addTest(93new BigDecimal("-85070591730234615847396907784232501249"),94BigDecimal.valueOf(1),95new BigDecimal("-85070591730234615847396907784232501248") );96failures += addTest(97new BigDecimal("147573952589676412927"),98BigDecimal.valueOf(Integer.MAX_VALUE),99new BigDecimal("147573952591823896574") );100failures += addTest(101new BigDecimal("-147573952589676412927"),102BigDecimal.valueOf(Integer.MAX_VALUE),103new BigDecimal("-147573952587528929280") );104failures += addTest(105new BigDecimal("79228162514264337593543950335"),106BigDecimal.valueOf(999),107new BigDecimal("79228162514264337593543951334") );108failures += addTest(109new BigDecimal("79228162514264337593543950335"),110BigDecimal.valueOf(Integer.MAX_VALUE/2),111new BigDecimal("79228162514264337594617692158") );112failures += addTest(113new BigDecimal("79228162514264337593543950335"),114BigDecimal.valueOf(Integer.MIN_VALUE/2),115new BigDecimal("79228162514264337592470208511") );116failures += addTest(117new BigDecimal("-79228162514264337593543950335"),118BigDecimal.valueOf(Integer.MAX_VALUE/2),119new BigDecimal("-79228162514264337592470208512") );120failures += addTest(121new BigDecimal("79228162514264337593543950335"),122BigDecimal.valueOf(-(Integer.MIN_VALUE/2)),123new BigDecimal("79228162514264337594617692159") );124failures += addTest(125new BigDecimal("79228162514264337593543950335"),126BigDecimal.valueOf(Long.MAX_VALUE/2),127new BigDecimal("79228162518876023611971338238") );128failures += addTest(129new BigDecimal("79228162514264337593543950335"),130BigDecimal.valueOf(Long.MIN_VALUE/2),131new BigDecimal("79228162509652651575116562431") );132failures += addTest(133new BigDecimal("-79228162514264337593543950335"),134BigDecimal.valueOf(Long.MAX_VALUE/2),135new BigDecimal("-79228162509652651575116562432") );136failures += addTest(137new BigDecimal("79228162514264337593543950335"),138BigDecimal.valueOf(-(Long.MIN_VALUE/2)),139new BigDecimal("79228162518876023611971338239") );140failures += addTest(141new BigDecimal("-9223372036854775808"),142BigDecimal.valueOf(1),143new BigDecimal("-9223372036854775807") );144failures += addTest(145new BigDecimal("-9223372036854775808"),146BigDecimal.valueOf(Long.MAX_VALUE/2),147new BigDecimal("-4611686018427387905") );148failures += addTest(149new BigDecimal("9223372036854775808"),150BigDecimal.valueOf(-1),151new BigDecimal("9223372036854775807") );152failures += addTest(153new BigDecimal("9223372036854775808"),154BigDecimal.valueOf(-Long.MAX_VALUE/2),155new BigDecimal("4611686018427387905") );156157return failures;158}159160private static int testRoundingFromBigInteger(BigInteger bi, int scale, MathContext mc) {161int failures = 0;162BigDecimal bd1 = new BigDecimal(bi,scale, mc);163BigDecimal bd2 = (new BigDecimal(bi,scale)).round(mc);164if (!bd1.equals(bd2)) {165System.out.println("new BigDecimal(BigInteger,int,MathContext):" +166"BigInteger == " +167bi + "; scale == " + scale + "; result == " +168bd1 + "; expected == " +169bd2170);171failures++;172}173return failures;174}175176private static int roundingConstructorTest() {177int failures = 0;178failures += testRoundingFromBigInteger(179new BigInteger("85070591730234615847396907784232501249"),1807, MathContext.DECIMAL64);181failures += testRoundingFromBigInteger(182new BigInteger("85070591730234615847396907784232501249"),1830, MathContext.DECIMAL64);184failures += testRoundingFromBigInteger(185new BigInteger("85070591730234615847396907784232501249"),186-7, MathContext.DECIMAL64);187failures += testRoundingFromBigInteger(188new BigInteger("85070591730234615847396907784232501249"),1897, MathContext.DECIMAL128);190failures += testRoundingFromBigInteger(191new BigInteger("85070591730234615847396907784232501249"),192177, MathContext.DECIMAL128);193failures += testRoundingFromBigInteger(194new BigInteger("85070591730234615847396907784232501249"),195177, MathContext.DECIMAL32);196failures += testRoundingFromBigInteger(197new BigInteger("85070591730234615847396907784232501249"),198177, MathContext.UNLIMITED);199failures += testRoundingFromBigInteger(200new BigInteger("85070591730234615847396907784232501249"),2010, MathContext.UNLIMITED);202return failures;203}204205private static int minLongConstructorTest(MathContext mc) {206int failures = 0;207BigDecimal bd1 = new BigDecimal(Long.MIN_VALUE,mc);208BigDecimal bd2 = new BigDecimal(Long.MIN_VALUE).round(mc);209if (!bd1.equals(bd2)) {210System.out.println("new BigDecimal(long,MathContext):" +211"long == " +212Long.MIN_VALUE + "; result == " +213bd1 + "; expected == " +214bd2215);216failures++;217}218return failures;219}220221private static int minLongConstructorTest() {222int failures = 0;223failures+=minLongConstructorTest(MathContext.UNLIMITED);224failures+=minLongConstructorTest(MathContext.DECIMAL32);225failures+=minLongConstructorTest(MathContext.DECIMAL64);226failures+=minLongConstructorTest(MathContext.DECIMAL128);227return failures;228}229230public static void main(String argv[]) {231int failures = 0;232233failures += addBoundaryTest();234failures += roundingConstructorTest();235failures += minLongConstructorTest();236if (failures > 0) {237throw new RuntimeException("Incurred " + failures +238" failures while testing.");239}240}241242243244}245246247