1 /* 2 * Copyright (c) 2003, 2018, 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 package test.java.math.BigDecimal; 24 25 /* 26 * @test 27 * @bug 7036582 28 * @summary Some new tests for the add method and constructor with MathContext. 29 * @run main RangeTests 30 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 RangeTests 31 * @author Sergey V. Kuksenko 32 */ 33 34 import java.math.BigDecimal; 35 import java.math.BigInteger; 36 import java.math.MathContext; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Test; 40 41 // Android-changed: Replace error counting with asserts. 42 public class RangeTests { 43 addTest(BigDecimal arg1, BigDecimal arg2, BigDecimal expectedResult)44 private static void addTest(BigDecimal arg1, BigDecimal arg2, BigDecimal expectedResult) { 45 BigDecimal result = arg1.add(arg2); 46 Assert.assertEquals(result, expectedResult, "Sum:" + 47 arg1 + " + " + 48 arg2 + " == " + 49 result + "; expected " + 50 expectedResult 51 ); 52 result = arg2.add(arg1); 53 Assert.assertEquals(result, expectedResult, "Sum:" + 54 arg2 + " + " + 55 arg1 + " == " + 56 result + "; expected " + 57 expectedResult 58 ); 59 } 60 61 /* 62 * Test BigDecimal.add(BigDecimal) when values are withing different ranges: 63 * 1. within 32 bits 64 * 2. within 64 bits 65 * 3. outside 64 bits. 66 */ 67 @Test addBoundaryTest()68 public void addBoundaryTest() { 69 addTest( 70 new BigDecimal("85070591730234615847396907784232501249"), 71 BigDecimal.valueOf(0), 72 new BigDecimal("85070591730234615847396907784232501249") ); 73 addTest( 74 new BigDecimal("-85070591730234615847396907784232501249"), 75 BigDecimal.valueOf(0), 76 new BigDecimal("-85070591730234615847396907784232501249") ); 77 addTest( 78 new BigDecimal("85070591730234615847396907784232501249"), 79 BigDecimal.valueOf(1), 80 new BigDecimal("85070591730234615847396907784232501250") ); 81 addTest( 82 new BigDecimal("85070591730234615847396907784232501249"), 83 BigDecimal.valueOf(-1), 84 new BigDecimal("85070591730234615847396907784232501248") ); 85 addTest( 86 new BigDecimal("-85070591730234615847396907784232501250"), 87 BigDecimal.valueOf(-1), 88 new BigDecimal("-85070591730234615847396907784232501251") ); 89 addTest( 90 new BigDecimal("-85070591730234615847396907784232501249"), 91 BigDecimal.valueOf(1), 92 new BigDecimal("-85070591730234615847396907784232501248") ); 93 addTest( 94 new BigDecimal("147573952589676412927"), 95 BigDecimal.valueOf(Integer.MAX_VALUE), 96 new BigDecimal("147573952591823896574") ); 97 addTest( 98 new BigDecimal("-147573952589676412927"), 99 BigDecimal.valueOf(Integer.MAX_VALUE), 100 new BigDecimal("-147573952587528929280") ); 101 addTest( 102 new BigDecimal("79228162514264337593543950335"), 103 BigDecimal.valueOf(999), 104 new BigDecimal("79228162514264337593543951334") ); 105 addTest( 106 new BigDecimal("79228162514264337593543950335"), 107 BigDecimal.valueOf(Integer.MAX_VALUE/2), 108 new BigDecimal("79228162514264337594617692158") ); 109 addTest( 110 new BigDecimal("79228162514264337593543950335"), 111 BigDecimal.valueOf(Integer.MIN_VALUE/2), 112 new BigDecimal("79228162514264337592470208511") ); 113 addTest( 114 new BigDecimal("-79228162514264337593543950335"), 115 BigDecimal.valueOf(Integer.MAX_VALUE/2), 116 new BigDecimal("-79228162514264337592470208512") ); 117 addTest( 118 new BigDecimal("79228162514264337593543950335"), 119 BigDecimal.valueOf(-(Integer.MIN_VALUE/2)), 120 new BigDecimal("79228162514264337594617692159") ); 121 addTest( 122 new BigDecimal("79228162514264337593543950335"), 123 BigDecimal.valueOf(Long.MAX_VALUE/2), 124 new BigDecimal("79228162518876023611971338238") ); 125 addTest( 126 new BigDecimal("79228162514264337593543950335"), 127 BigDecimal.valueOf(Long.MIN_VALUE/2), 128 new BigDecimal("79228162509652651575116562431") ); 129 addTest( 130 new BigDecimal("-79228162514264337593543950335"), 131 BigDecimal.valueOf(Long.MAX_VALUE/2), 132 new BigDecimal("-79228162509652651575116562432") ); 133 addTest( 134 new BigDecimal("79228162514264337593543950335"), 135 BigDecimal.valueOf(-(Long.MIN_VALUE/2)), 136 new BigDecimal("79228162518876023611971338239") ); 137 addTest( 138 new BigDecimal("-9223372036854775808"), 139 BigDecimal.valueOf(1), 140 new BigDecimal("-9223372036854775807") ); 141 addTest( 142 new BigDecimal("-9223372036854775808"), 143 BigDecimal.valueOf(Long.MAX_VALUE/2), 144 new BigDecimal("-4611686018427387905") ); 145 addTest( 146 new BigDecimal("9223372036854775808"), 147 BigDecimal.valueOf(-1), 148 new BigDecimal("9223372036854775807") ); 149 addTest( 150 new BigDecimal("9223372036854775808"), 151 BigDecimal.valueOf(-Long.MAX_VALUE/2), 152 new BigDecimal("4611686018427387905") ); 153 } 154 testRoundingFromBigInteger(BigInteger bi, int scale, MathContext mc)155 private static void testRoundingFromBigInteger(BigInteger bi, int scale, MathContext mc) { 156 BigDecimal bd1 = new BigDecimal(bi,scale, mc); 157 BigDecimal bd2 = (new BigDecimal(bi,scale)).round(mc); 158 Assert.assertEquals(bd1, bd2, "new BigDecimal(BigInteger,int,MathContext):" + 159 "BigInteger == " + 160 bi + "; scale == " + scale + "; result == " + 161 bd1 + "; expected == " + 162 bd2 163 ); 164 } 165 166 @Test roundingConstructorTest()167 public void roundingConstructorTest() { 168 testRoundingFromBigInteger( 169 new BigInteger("85070591730234615847396907784232501249"), 170 7, MathContext.DECIMAL64); 171 testRoundingFromBigInteger( 172 new BigInteger("85070591730234615847396907784232501249"), 173 0, MathContext.DECIMAL64); 174 testRoundingFromBigInteger( 175 new BigInteger("85070591730234615847396907784232501249"), 176 -7, MathContext.DECIMAL64); 177 testRoundingFromBigInteger( 178 new BigInteger("85070591730234615847396907784232501249"), 179 7, MathContext.DECIMAL128); 180 testRoundingFromBigInteger( 181 new BigInteger("85070591730234615847396907784232501249"), 182 177, MathContext.DECIMAL128); 183 testRoundingFromBigInteger( 184 new BigInteger("85070591730234615847396907784232501249"), 185 177, MathContext.DECIMAL32); 186 testRoundingFromBigInteger( 187 new BigInteger("85070591730234615847396907784232501249"), 188 177, MathContext.UNLIMITED); 189 testRoundingFromBigInteger( 190 new BigInteger("85070591730234615847396907784232501249"), 191 0, MathContext.UNLIMITED); 192 } 193 minLongConstructorTest(MathContext mc)194 private static void minLongConstructorTest(MathContext mc) { 195 BigDecimal bd1 = new BigDecimal(Long.MIN_VALUE,mc); 196 BigDecimal bd2 = new BigDecimal(Long.MIN_VALUE).round(mc); 197 Assert.assertEquals(bd1, bd2, "new BigDecimal(long,MathContext):" + 198 "long == " + 199 Long.MIN_VALUE + "; result == " + 200 bd1 + "; expected == " + 201 bd2 202 ); 203 } 204 205 @Test minLongConstructorTest()206 public void minLongConstructorTest() { 207 minLongConstructorTest(MathContext.UNLIMITED); 208 minLongConstructorTest(MathContext.DECIMAL32); 209 minLongConstructorTest(MathContext.DECIMAL64); 210 minLongConstructorTest(MathContext.DECIMAL128); 211 } 212 } 213