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 */ 30 31 import java.math.*; 32 33 public class PowTests { 34 static int zeroAndOneTests() { 35 int failures = 0; 36 37 BigDecimal[][] testCases = { 38 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 39 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 40 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 41 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MAX_VALUE)}, 42 43 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 44 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 45 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 46 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MIN_VALUE)}, 47 48 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 49 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MAX_VALUE)}, 50 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(2), null}, // overflow 51 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(999999999), null}, // overflow 52 53 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)}, 54 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MIN_VALUE)}, 55 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(2), null}, // underflow 56 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(999999999), null}, // underflow 57 }; 58 59 for(BigDecimal[] testCase: testCases) { 60 int exponent = testCase[1].intValueExact(); 61 BigDecimal result; 62 63 try{ 64 result = testCase[0].pow(exponent); 65 if (!result.equals(testCase[2]) ) { 66 failures++; 67 System.err.println("Unexpected result while raising " + 68 testCase[0] + 69 " to the " + exponent + " power; expected " + 70 testCase[2] + ", got " + result + "."); 71 } 72 } catch (ArithmeticException e) { 73 if (testCase[2] != null) { 74 failures++; 75 System.err.println("Unexpected exception while raising " + testCase[0] + 76 " to the " + exponent + " power."); 77 78 } 79 } 80 } 81 82 return failures; 83 } 84 85 public static void main(String argv[]) { 86 int failures = 0; 87 88 failures += zeroAndOneTests(); 89 90 if (failures > 0) { 91 throw new RuntimeException("Incurred " + failures + 92 " failures while testing pow methods."); 93 } 94 } 95 96 }