Added fraction power test cases
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index dfb104e..a33cf1b 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -635,6 +635,12 @@
@Test
public void testPow() {
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.powFractionTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int exponent = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.pow(exponent));
+ }
+
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13));
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13L));
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(BigInteger.valueOf(13L)));
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
index f6b6a7f..3f1f343 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
@@ -72,6 +72,11 @@
*/
private static final List<BinaryOperatorTestCase> subtractFractionTestCasesList;
+ /**
+ * See {@link #powFractionTestCases()}
+ */
+ private static final List<BinaryIntOperatorTestCase> powFractionTestCasesList;
+
static {
numDenConstructorTestCasesList = collectNumDenConstructorTestCases();
doubleConstructorTestCasesList = collectDoubleConstructorTestCases();
@@ -82,6 +87,7 @@
divideByFractionTestCasesList = collectDivideByFractionTestCases();
multiplyByFractionTestCasesList = collectMultiplyByFractionTestCases();
subtractFractionTestCasesList = collectSubtractFractionTestCases();
+ powFractionTestCasesList = collectPowFractionTestCases();
}
private CommonTestCases() {}
@@ -362,10 +368,40 @@
}
/**
+ * Defines test cases as described in
+ * {@link #powFractionTestCases()} and collects them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryIntOperatorTestCase> collectPowFractionTestCases() {
+ List<BinaryIntOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryIntOperatorTestCase(3, 7, 0, 1, 1));
+ testCases.add(new BinaryIntOperatorTestCase(3, 7, 1, 3, 7));
+ testCases.add(new BinaryIntOperatorTestCase(3, 7, -1, 7, 3));
+ testCases.add(new BinaryIntOperatorTestCase(3, 7, 2, 9, 49));
+ testCases.add(new BinaryIntOperatorTestCase(3, 7, -2, 49, 9));
+
+ testCases.add(new BinaryIntOperatorTestCase(3, -7, 0, 1, 1));
+ testCases.add(new BinaryIntOperatorTestCase(3, -7, 1, 3, -7));
+ testCases.add(new BinaryIntOperatorTestCase(3, -7, -1, -7, 3));
+ testCases.add(new BinaryIntOperatorTestCase(3, -7, 2, 9, 49));
+ testCases.add(new BinaryIntOperatorTestCase(3, -7, -2, 49, 9));
+
+ testCases.add(new BinaryIntOperatorTestCase(2, 3, 13, 8192, 1594323));
+ testCases.add(new BinaryIntOperatorTestCase(2, 3, -13, 1594323, 8192));
+
+ testCases.add(new BinaryIntOperatorTestCase(0, 1, Integer.MAX_VALUE, 0, 1));
+ testCases.add(new BinaryIntOperatorTestCase(0, -1, Integer.MAX_VALUE, 0, -1));
+
+ return testCases;
+ }
+
+ /**
* Provides a list of test cases where a fraction should be created from
* a specified numerator and denominator, both in the {@code int} range,
* and the expected numerator and denominator of the created fraction are
* also in the {@code int} range.
+ *
* @return a list of test cases as described above
*/
static List<UnaryOperatorTestCase> numDenConstructorTestCases() {
@@ -377,6 +413,7 @@
* converted to a fraction with a certain amount of absolute error
* allowed, and the expected numerator and denominator of the resulting
* fraction are in the {@code int} range.
+ *
* @return a list of test cases as described above
*/
static List<DoubleToFractionTestCase> doubleConstructorTestCases() {
@@ -400,6 +437,7 @@
* the {@code int} range, should be calculated, and the expected
* numerator and denominator of the resulting fraction are also in the
* {@code int} range.
+ *
* @return a list of test cases as described above
*/
static List<UnaryOperatorTestCase> reciprocalTestCases() {
@@ -411,6 +449,7 @@
* created from a specified numerator and denominator, both in the {@code
* int} range, should be calculated, and the expected numerator and
* denominator of the resulting fraction are also in the {@code int} range.
+ *
* @return a list of test cases as described above
*/
static List<UnaryOperatorTestCase> negateTestCases() {
@@ -429,12 +468,12 @@
}
/**
- * <p>Provides a list of test cases where a fraction, created from a
+ * Provides a list of test cases where a fraction, created from a
* specified numerator and denominator in the {@code int} range, should
* be divided by another fraction, also created from a specified
* numerator and denominator in the {@code int} range, and the expected
* numerator and denominator of the resulting fraction are in the {@code
- * int} range as well.</p>
+ * int} range as well.
*
* <p>The first operand in each test case is the dividend and the second
* operand is the divisor.</p>
@@ -452,6 +491,7 @@
* numerator and denominator in the {@code int} range, and the expected
* numerator and denominator of the resulting fraction are in the {@code
* int} range as well.
+ *
* @return a list of test cases as described above
*/
static List<BinaryOperatorTestCase> multiplyByFractionTestCases() {
@@ -459,12 +499,12 @@
}
/**
- * <p>Provides a list of test cases where a fraction, created from a
+ * Provides a list of test cases where a fraction, created from a
* specified numerator and denominator in the {@code int} range, should
* be subtracted from another fraction, also created from a specified
* numerator and denominator in the {@code int} range, and the expected
* numerator and denominator of the resulting fraction are in the {@code
- * int} range as well.</p>
+ * int} range as well.
*
* <p>The first operand in each test case is the minuend and the second
* operand is the subtrahend.</p>
@@ -475,6 +515,19 @@
return Collections.unmodifiableList(subtractFractionTestCasesList);
}
+ /**
+ * Provides a list of test cases where a fraction, created from a
+ * specified numerator and denominator in the {@code int} range, should
+ * be raised to a specified power specified in the {@code int} range,
+ * and the expected numerator and denominator of the resulting fraction
+ * are in the {@code int} range as well.
+ *
+ * @return a list of test cases as described above
+ */
+ static List<BinaryIntOperatorTestCase> powFractionTestCases() {
+ return Collections.unmodifiableList(powFractionTestCasesList);
+ }
+
// CHECKSTYLE: stop VisibilityModifier
/**
@@ -533,6 +586,34 @@
}
/**
+ * Represents a test case where a binary operation should be performed on
+ * a specified combination of numerator and denominator and a integer argument,
+ * with the numerator and denominator in the {@code int}
+ * range, and the numerator and denominator of the expected result are
+ * also in the {@code int} range.
+ */
+ static class BinaryIntOperatorTestCase {
+ final int firstOperandNumerator;
+ final int firstOperandDenominator;
+ final int secondOperand;
+ final int expectedNumerator;
+ final int expectedDenominator;
+
+ BinaryIntOperatorTestCase(
+ int firstOperandNumerator,
+ int firstOperandDenominator,
+ int secondOperand,
+ int expectedNumerator,
+ int expectedDenominator) {
+ this.firstOperandNumerator = firstOperandNumerator;
+ this.firstOperandDenominator = firstOperandDenominator;
+ this.secondOperand = secondOperand;
+ this.expectedNumerator = expectedNumerator;
+ this.expectedDenominator = expectedDenominator;
+ }
+ }
+
+ /**
* Represents a test case where an operation that yields a fraction
* should be performed on a {@code double} value and the numerator and
* denominator of the expected result
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
index 2a5d5f1..931b4c5 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -360,22 +360,11 @@
@Test
public void testPow() {
- Fraction a = Fraction.of(3, 7);
- assertFraction(1, 1, a.pow(0));
- assertFraction(3, 7, a.pow(1));
- assertFraction(7, 3, a.pow(-1));
- assertFraction(9, 49, a.pow(2));
- assertFraction(49, 9, a.pow(-2));
-
- Fraction b = Fraction.of(3, -7);
- assertFraction(1, 1, b.pow(0));
- assertFraction(3, -7, b.pow(1));
- assertFraction(-7, 3, b.pow(-1));
- assertFraction(9, 49, b.pow(2));
- assertFraction(49, 9, b.pow(-2));
-
- Fraction c = Fraction.of(0, -11);
- assertFraction(0, -1, c.pow(Integer.MAX_VALUE));
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.powFractionTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int exponent = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.pow(exponent));
+ }
Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(Integer.MAX_VALUE).pow(2));
Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(1, Integer.MAX_VALUE).pow(2));