PowTests.java revision 809
d0be1e954bd4674fc27f2616c72adb37cf3525a2David Lawrence/*
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * This code is free software; you can redistribute it and/or modify it
d0be1e954bd4674fc27f2616c72adb37cf3525a2David Lawrence * under the terms of the GNU General Public License version 2 only, as
d0be1e954bd4674fc27f2616c72adb37cf3525a2David Lawrence * published by the Free Software Foundation.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * This code is distributed in the hope that it will be useful, but WITHOUT
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * version 2 for more details (a copy is included in the LICENSE file that
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * accompanied this code).
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * You should have received a copy of the GNU General Public License version
d0be1e954bd4674fc27f2616c72adb37cf3525a2David Lawrence * 2 along with this work; if not, write to the Free Software Foundation,
d0be1e954bd4674fc27f2616c72adb37cf3525a2David Lawrence * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater *
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews * CA 95054 USA or visit www.sun.com if you need additional information or
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * have any questions.
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer */
ee6fe1d1975604af266af6c370fc6193dae80fddMichael Sawyer
f333ea9bdd3f85b74ae790e6c8ce2684295b3483Andreas Gustafsson/*
4f37905cc38162128a507e619e38ae535720686bAndreas Gustafsson * @test
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer * @bug 4916097
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer * @summary Some exponent over/undeflow tests for the pow method
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer * @author Joseph D. Darcy
5a77e9620a0b2f7417469c98be374de49d0eccc6Andreas Gustafsson * @compile -source 1.5 PowTests.java
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer * @run main PowTests
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer */
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyerimport java.math.*;
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyerpublic class PowTests {
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer static int zeroAndOneTests() {
dec3e76636390eb25db44659bdd7b3ff8b0468cbMichael Sawyer int failures = 0;
BigDecimal[][] testCases = {
{BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
{BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
{BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
{BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
{BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
{BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
{BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
{BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
{BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
{BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MAX_VALUE)},
{BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(2), null}, // overflow
{BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(999999999), null}, // overflow
{BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
{BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MIN_VALUE)},
{BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(2), null}, // underflow
{BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(999999999), null}, // underflow
};
for(BigDecimal[] testCase: testCases) {
int exponent = testCase[1].intValueExact();
BigDecimal result;
try{
result = testCase[0].pow(exponent);
if (!result.equals(testCase[2]) ) {
failures++;
System.err.println("Unexpected result while raising " +
testCase[0] +
" to the " + exponent + " power; expected " +
testCase[2] + ", got " + result + ".");
}
} catch (ArithmeticException e) {
if (testCase[2] != null) {
failures++;
System.err.println("Unexpected exception while raising " + testCase[0] +
" to the " + exponent + " power.");
}
}
}
return failures;
}
public static void main(String argv[]) {
int failures = 0;
failures += zeroAndOneTests();
if (failures > 0) {
throw new RuntimeException("Incurred " + failures +
" failures while testing pow methods.");
}
}
}