/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4851776 4907265 6177836 6876282
* @summary Some tests for the divide methods.
* @author Joseph D. Darcy
*/
public class DivideTests {
// Preliminary exact divide method; could be used for comparison
// purposes.
/*
* Handle zero cases first.
*/
throw new ArithmeticException("Division by zero");
}
return BigDecimal.ZERO;
else {
/*
* Determine if there is a result with a terminating
* decimal expansion. Putting aside overflow and
* underflow considerations, the existance of an exact
* result only depends on the ratio of the intVal's of the
* dividend (i.e. this) and and divisor since the scales
* of the argument just affect where the decimal point
* lies.
*
* For the ratio of (a = this.intVal) and (b =
* divisor.intVal) to have a finite decimal expansion,
* once a/b is put in lowest terms, b must be equal to
* (2^i)*(5^j) for some integer i,j >= 0. Therefore, we
* to (2^i)*(5^j).
*/
boolean goodDivisor = false;
int i=0, j=0;
badDivisor: {
switch(b_primeModTen) {
case 0:
// b_prime divisible by 10=2*5, increment i and j
i++;
j++;
break;
case 5:
// b_prime divisible by 5, increment j
j++;
break;
case 2:
case 4:
case 6:
case 8:
// b_prime divisible by 2, increment i
i++;
break;
default: // hit something we shouldn't have
break badDivisor;
}
}
goodDivisor = true;
}
if( ! goodDivisor ) {
throw new ArithmeticException("Non terminating decimal expansion");
}
else {
// What is a rule for determining how many digits are
// needed? Once that is determined, cons up a new
// MathContext object and pass it on to the divide(bd,
// mc) method; precision == ?, roundingMode is unnecessary.
// Are we sure this is the right scale to use? Should
// also determine a precision-based method.
// Should do some more work here to rescale, etc.
}
}
}
public static int powersOf2and5() {
int failures = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 6; j++) {
int product;
try {
} catch (ArithmeticException e) {
failures++;
e.printStackTrace();
}
try {
} catch (ArithmeticException e) {
failures++;
e.printStackTrace();
}
try {
} catch (ArithmeticException e) {
failures++;
e.printStackTrace();
}
}
}
return failures;
}
public static int nonTerminating() {
int failures = 0;
// For each pair of prime products, verify the ratio of
// non-equal products has a non-terminating expansion.
try {
failures++;
" returned for non-terminating fraction " +
}
catch (ArithmeticException e) {
; // Correct result
}
}
}
}
}
}
return failures;
}
public static int properScaleTests(){
int failures = 0;
BigDecimal[][] testCases = {
};
failures++;
}
}
return failures;
}
public static int trailingZeroTests() {
int failures = 0;
BigDecimal[][] testCases = {
};
failures++;
}
}
return failures;
}
public static int scaledRoundedDivideTests() {
int failures = 0;
// Tests of the traditional scaled divide under different
// rounding modes.
// Encode rounding mode and scale for the divide in a
// BigDecimal with the significand equal to the rounding mode
// and the scale equal to the number's scale.
// {dividend, dividisor, rounding, quotient}
// Ad hoc tests
BigDecimal[][] testCases = {
};
failures++;
}
}
// 6876282
BigDecimal[][] testCases2 = {
// { dividend, divisor, expected quotient }
new BigDecimal(441) },
new BigDecimal("0.000115309916") },
new BigDecimal(4) },
new BigDecimal(4) },
new BigDecimal(5) },
new BigDecimal(4) },
new BigDecimal(4) },
};
failures++;
" rounding mode HALF_UP" +
}
}
return failures;
}
int failures = 0;
failures += powersOf2and5();
failures += nonTerminating();
failures += properScaleTests();
failures += trailingZeroTests();
if (failures > 0) {
" failures while testing exact divide.");
}
}
}