1 /* 2 * Copyright (c) 2005, 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 24 /** 25 * @test 26 * @bug 6806261 27 * @summary Tests of BigDecimal.longValueExact 28 */ 29 import java.math.*; 30 31 public class LongValueExactTests { 32 longValueExactTests()33 private static int longValueExactTests() { 34 int failures = 0; 35 36 String[] testStrings = { 37 "9223372036854775807", 38 "9223372036854775807.0", 39 "9223372036854775807.00", 40 "-9223372036854775808", 41 "-9223372036854775808.0", 42 "-9223372036854775808.00", 43 }; 44 45 for (String longValue : testStrings) { 46 try { 47 BigDecimal bd = new BigDecimal(longValue); 48 long longValueExact = bd.longValueExact(); 49 } catch (Exception e) { 50 failures++; 51 } 52 } 53 54 // The following Strings are supposed to make longValueExact throw 55 // ArithmeticException. 56 String[] testStrings2 = { 57 "9223372036854775808", 58 "9223372036854775808.0", 59 "9223372036854775808.00", 60 "-9223372036854775809", 61 "-9223372036854775808.1", 62 "-9223372036854775808.01", 63 }; 64 65 for (String bigValue : testStrings2) { 66 try { 67 BigDecimal bd = new BigDecimal(bigValue); 68 long longValueExact = bd.longValueExact(); 69 failures++; 70 } catch (ArithmeticException e) { 71 // Success; 72 } 73 } 74 return failures; 75 } 76 main(String argv[])77 public static void main(String argv[]) { 78 int failures = 0; 79 80 failures += longValueExactTests(); 81 82 if (failures > 0) { 83 throw new RuntimeException("Incurred " + failures + 84 " failures while testing longValueExact."); 85 } 86 } 87 } 88