1 /* 2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4916097 27 * @summary Some exponent over/undeflow tests for the pow method 28 * @author Joseph D. Darcy 29 * @compile -source 1.5 PowTests.java 30 * @run main PowTests 31 */ 32 33 import java.math.*; 34 35 public class PowTests { 36 static int zeroAndOneTests() { 37 int failures = 0; 38 39 BigDecimal[][] testCases = { 40 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 41 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 42 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 43 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 44 45 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 46 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 47 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 48 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 49 50 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 51 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MAX_VALUE)}, 52 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(2), null}, // overflow 53 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(999999999), null}, // overflow 54 55 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 56 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MIN_VALUE)}, 57 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(2), null}, // underflow 58 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(999999999), null}, // underflow 59 }; 60 61 for(BigDecimal[] testCase: testCases) { 62 int exponent = testCase[1].intValueExact(); 63 BigDecimal result; 64 65 try{ 66 result = testCase[0].pow(exponent); 67 if (!result.equals(testCase[2]) ) { 68 failures++; 69 System.err.println("Unexpected result while raising " + 70 testCase[0] + 71 " to the " + exponent + " power; expected " + 72 testCase[2] + ", got " + result + "."); 73 } 74 } catch (ArithmeticException e) { 75 if (testCase[2] != null) { 76 failures++; 77 System.err.println("Unexpected exception while raising " + testCase[0] + 78 " to the " + exponent + " power."); 79 80 } 81 } 82 } 83 84 return failures; 85 } 86 87 public static void main(String argv[]) { 88 int failures = 0; 89 90 failures += zeroAndOneTests(); 91 92 if (failures > 0) { 93 throw new RuntimeException("Incurred " + failures + 94 " failures while testing pow methods."); 95 } 96 } 97 98 }