You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/16 12:18:22 UTC
[commons-numbers] 07/26: Fraction: common test cases for arithmetic
with an integer
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit bcdbb3f3531d80a4d366e91b629b802a94667c9e
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Apr 13 21:42:24 2020 +0100
Fraction: common test cases for arithmetic with an integer
add/subtract/multiply/divide using an int argument.
---
.../commons/numbers/fraction/BigFractionTest.java | 84 +++++------
.../commons/numbers/fraction/CommonTestCases.java | 157 ++++++++++++++++++++-
.../commons/numbers/fraction/FractionTest.java | 45 +++---
3 files changed, 214 insertions(+), 72 deletions(-)
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 a1e53e0..777d0a9 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
@@ -35,9 +35,17 @@ public class BigFractionTest {
/** The zero representation with negative denominator. */
private static final BigFraction ZERO_N = BigFraction.of(0, -1);
+ private static void assertFraction(int expectedNumerator, int expectedDenominator, BigFraction actual) {
+ Assertions.assertEquals(expectedNumerator, actual.getNumeratorAsInt());
+ Assertions.assertEquals(expectedDenominator, actual.getDenominatorAsInt());
+ Assertions.assertEquals(
+ Integer.signum(expectedNumerator) * Integer.signum(expectedDenominator),
+ actual.signum());
+ }
+
private static void assertFraction(long expectedNumerator, long expectedDenominator, BigFraction actual) {
- Assertions.assertEquals(BigInteger.valueOf(expectedNumerator), actual.getNumerator());
- Assertions.assertEquals(BigInteger.valueOf(expectedDenominator), actual.getDenominator());
+ Assertions.assertEquals(expectedNumerator, actual.getNumeratorAsLong());
+ Assertions.assertEquals(expectedDenominator, actual.getDenominatorAsLong());
Assertions.assertEquals(
Long.signum(expectedNumerator) * Long.signum(expectedDenominator),
actual.signum());
@@ -472,15 +480,17 @@ public class BigFractionTest {
BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.addIntTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add((long) i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(BigInteger.valueOf(i2)));
+ }
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.add((BigFraction) null));
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.add((BigInteger) null));
- BigFraction f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1);
- assertFraction(Integer.MAX_VALUE, 1, f1.add(1));
- assertFraction(Integer.MAX_VALUE, 1, f1.add(1L));
- assertFraction(Integer.MAX_VALUE, 1, f1.add(BigInteger.ONE));
-
// Special cases
BigFraction f2 = BigFraction.of(1, 2);
assertFraction(1, 2, f2.add(BigInteger.ZERO));
@@ -494,6 +504,13 @@ public class BigFractionTest {
BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.divideByIntTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide((long) i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(BigInteger.valueOf(i2)));
+ }
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.divide((BigFraction) null));
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.divide((BigInteger) null));
@@ -503,19 +520,8 @@ public class BigFractionTest {
Assertions.assertThrows(FractionException.class, () -> BigFraction.of(1, 2).divide(0L));
Assertions.assertThrows(FractionException.class, () -> BigFraction.of(1, 2).divide(BigInteger.ZERO));
- BigFraction f1 = BigFraction.of(0, 5);
- Assertions.assertEquals(BigFraction.ZERO, f1.divide(BigFraction.of(2, 7)));
- Assertions.assertEquals(BigFraction.ZERO, f1.divide(11));
- Assertions.assertEquals(BigFraction.ZERO, f1.divide(11L));
- Assertions.assertEquals(BigFraction.ZERO, f1.divide(BigInteger.valueOf(11)));
-
- f1 = BigFraction.of(6, 35);
- assertFraction(2, 175, f1.divide(15));
- assertFraction(2, 175, f1.divide(15L));
- assertFraction(2, 175, f1.divide(BigInteger.valueOf(15)));
-
// Special cases
- f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ BigFraction f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
assertFraction(-1, -Integer.MAX_VALUE, f1.divide(Integer.MIN_VALUE));
assertFraction(-1, -Integer.MAX_VALUE, f1.divide((long) Integer.MIN_VALUE));
assertFraction(-1, -Integer.MAX_VALUE, f1.divide(BigInteger.valueOf(Integer.MIN_VALUE)));
@@ -528,26 +534,16 @@ public class BigFractionTest {
BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.multiplyByIntTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply((long) i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(BigInteger.valueOf(i2)));
+ }
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.multiply((BigFraction) null));
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.multiply((BigInteger) null));
-
- BigFraction f1 = BigFraction.of(6, 35);
- assertFraction(18, 7, f1.multiply(15));
- assertFraction(18, 7, f1.multiply((long) 15));
- assertFraction(18, 7, f1.multiply(BigInteger.valueOf(15)));
-
- f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
- assertFraction(Integer.MIN_VALUE, 1, f1.multiply(Integer.MAX_VALUE));
- assertFraction(Integer.MIN_VALUE, 1, f1.multiply((long) Integer.MAX_VALUE));
- assertFraction(Integer.MIN_VALUE, 1, f1.multiply(BigInteger.valueOf(Integer.MAX_VALUE)));
-
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(42));
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(42L));
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(BigInteger.valueOf(42)));
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(0));
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(0L));
- Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(BigInteger.ZERO));
}
@Test
@@ -573,18 +569,16 @@ public class BigFractionTest {
BigFraction f2 = BigFraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.subtractIntTestCases()) {
+ BigFraction f1 = BigFraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract((long) i2));
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(BigInteger.valueOf(i2)));
+ }
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.subtract((BigFraction) null));
Assertions.assertThrows(NullPointerException.class, () -> BigFraction.ONE.subtract((BigInteger) null));
-
- assertFraction(-3, 1, BigFraction.ZERO.subtract(3));
- assertFraction(-3, 1, BigFraction.ZERO.subtract(3L));
- assertFraction(-3, 1, BigFraction.ZERO.subtract(BigInteger.valueOf(3)));
-
- BigFraction f1 = BigFraction.of(2, 3);
- assertFraction(-7, 3, f1.subtract(3));
- assertFraction(-7, 3, f1.subtract(3L));
- assertFraction(-7, 3, f1.subtract(BigInteger.valueOf(3)));
}
@Test
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 39ad560..fd4a37c 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
@@ -52,29 +52,47 @@ final class CommonTestCases {
*/
private static final List<UnaryOperatorTestCase> negateTestCasesList;
- // TODO addIntTestCasesList
-
/**
* See {@link #addFractionTestCases()}
*/
private static final List<BinaryOperatorTestCase> addFractionTestCasesList;
/**
+ * See {@link #addIntTestCases()}
+ */
+ private static final List<BinaryIntOperatorTestCase> addIntTestCasesList;
+
+ /**
* See {@link #divideByFractionTestCases()}
*/
private static final List<BinaryOperatorTestCase> divideByFractionTestCasesList;
/**
+ * See {@link #divideByIntTestCases()}
+ */
+ private static final List<BinaryIntOperatorTestCase> divideByIntTestCasesList;
+
+ /**
* See {@link #multiplyByFractionTestCases()}
*/
private static final List<BinaryOperatorTestCase> multiplyByFractionTestCasesList;
/**
+ * See {@link #multiplyByIntTestCases()}
+ */
+ private static final List<BinaryIntOperatorTestCase> multiplyByIntTestCasesList;
+
+ /**
* See {@link #subtractFractionTestCases()}
*/
private static final List<BinaryOperatorTestCase> subtractFractionTestCasesList;
/**
+ * See {@link #subtractIntTestCases()}
+ */
+ private static final List<BinaryIntOperatorTestCase> subtractIntTestCasesList;
+
+ /**
* See {@link #powTestCases()}
*/
private static final List<BinaryIntOperatorTestCase> powTestCasesList;
@@ -86,9 +104,13 @@ final class CommonTestCases {
reciprocalTestCasesList = collectReciprocalTestCases();
negateTestCasesList = collectNegateTestCases();
addFractionTestCasesList = collectAddFractionTestCases();
+ addIntTestCasesList = collectAddIntTestCases();
divideByFractionTestCasesList = collectDivideByFractionTestCases();
+ divideByIntTestCasesList = collectDivideByIntTestCases();
multiplyByFractionTestCasesList = collectMultiplyByFractionTestCases();
+ multiplyByIntTestCasesList = collectMultiplyByIntTestCases();
subtractFractionTestCasesList = collectSubtractFractionTestCases();
+ subtractIntTestCasesList = collectSubtractIntTestCases();
powTestCasesList = collectPowTestCases();
}
@@ -290,6 +312,23 @@ final class CommonTestCases {
}
/**
+ * Defines test cases as described in {@link #addIntTestCases()} and
+ * collects them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryIntOperatorTestCase> collectAddIntTestCases() {
+ List<BinaryIntOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, 0, 1, 3));
+ testCases.add(new BinaryIntOperatorTestCase(-1, 3, 0, -1, 3));
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, 1, 4, 3));
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, -1, -2, 3));
+ testCases.add(new BinaryIntOperatorTestCase(Integer.MAX_VALUE - 1, 1, 1, Integer.MAX_VALUE, 1));
+
+ return testCases;
+ }
+
+ /**
* Defines test cases as described in
* {@link #divideByFractionTestCases()} and collects them into a {@code List}.
* @return a list of test cases as described above
@@ -323,6 +362,24 @@ final class CommonTestCases {
/**
* Defines test cases as described in
+ * {@link #divideByIntTestCases()} and collects them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryIntOperatorTestCase> collectDivideByIntTestCases() {
+ List<BinaryIntOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, 1, 1, 3));
+ testCases.add(new BinaryIntOperatorTestCase(0, 5, 11, 0, 1));
+ testCases.add(new BinaryIntOperatorTestCase(6, 35, 15, 2, 175));
+
+ // This captures an implementation detail
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, -1, 1, -3));
+
+ return testCases;
+ }
+
+ /**
+ * Defines test cases as described in
* {@link #multiplyByFractionTestCases()} and collects them into a {@code List}.
* @return a list of test cases as described above
*/
@@ -348,6 +405,27 @@ final class CommonTestCases {
/**
* Defines test cases as described in
+ * {@link #multiplyByIntTestCases()} and collects them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryIntOperatorTestCase> collectMultiplyByIntTestCases() {
+ List<BinaryIntOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, 1, 1, 3));
+ testCases.add(new BinaryIntOperatorTestCase(6, 35, 15, 18, 7));
+ testCases.add(new BinaryIntOperatorTestCase(Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, 1));
+ // Test zero with multiply by integer
+ testCases.add(new BinaryIntOperatorTestCase(0, 1, 42, 0, 1));
+ testCases.add(new BinaryIntOperatorTestCase(1, 1, 0, 0, 1));
+
+ // This captures an implementation detail
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, -1, -1, 3));
+
+ return testCases;
+ }
+
+ /**
+ * Defines test cases as described in
* {@link #subtractFractionTestCases()} and collects them into a {@code List}.
* @return a list of test cases as described above
*/
@@ -387,6 +465,22 @@ final class CommonTestCases {
/**
* Defines test cases as described in
+ * {@link #subtractIntTestCases()} and collects them into a {@code List}.
+ * @return a list of test cases as described above
+ */
+ private static List<BinaryIntOperatorTestCase> collectSubtractIntTestCases() {
+ List<BinaryIntOperatorTestCase> testCases = new ArrayList<>();
+
+ testCases.add(new BinaryIntOperatorTestCase(1, 3, 1, -2, 3));
+ testCases.add(new BinaryIntOperatorTestCase(0, 1, 3, -3, 1));
+ testCases.add(new BinaryIntOperatorTestCase(2, 3, 3, -7, 3));
+ testCases.add(new BinaryIntOperatorTestCase(2, 3, 0, 2, 3));
+
+ return testCases;
+ }
+
+ /**
+ * Defines test cases as described in
* {@link #powTestCases()} and collects them into a {@code List}.
* @return a list of test cases as described above
*/
@@ -479,6 +573,7 @@ final class CommonTestCases {
* specified numerator and denominator in the {@code int} range, should
* be added, 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<BinaryOperatorTestCase> addFractionTestCases() {
@@ -488,6 +583,19 @@ final class CommonTestCases {
/**
* Provides a list of test cases where a fraction, created from a
* specified numerator and denominator in the {@code int} range, should
+ * be added to a specified value 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> addIntTestCases() {
+ return Collections.unmodifiableList(addIntTestCasesList);
+ }
+
+ /**
+ * 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
@@ -505,6 +613,22 @@ final class CommonTestCases {
/**
* 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 a specified value in the {@code int} range,
+ * and the expected numerator and denominator of the resulting fraction
+ * are in the {@code int} range as well.
+ *
+ * <p>The first operand in each test case is the dividend and the second
+ * operand is the divisor.</p>
+ *
+ * @return a list of test cases as described above
+ */
+ static List<BinaryIntOperatorTestCase> divideByIntTestCases() {
+ return Collections.unmodifiableList(divideByIntTestCasesList);
+ }
+
+ /**
+ * Provides a list of test cases where a fraction, created from a
+ * specified numerator and denominator in the {@code int} range, should
* be multiplied 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
@@ -519,6 +643,19 @@ final class CommonTestCases {
/**
* Provides a list of test cases where a fraction, created from a
* specified numerator and denominator in the {@code int} range, should
+ * be multiplied by a specified value 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> multiplyByIntTestCases() {
+ return Collections.unmodifiableList(multiplyByIntTestCasesList);
+ }
+
+ /**
+ * 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
@@ -536,6 +673,22 @@ final class CommonTestCases {
/**
* Provides a list of test cases where a fraction, created from a
* specified numerator and denominator in the {@code int} range, should
+ * have a specified value in the {@code int} range subtracted,
+ * and the expected numerator and denominator of the resulting fraction
+ * are in the {@code int} range as well.
+ *
+ * <p>The first operand in each test case is the minuend and the second
+ * operand is the subtrahend.</p>
+ *
+ * @return a list of test cases as described above
+ */
+ static List<BinaryIntOperatorTestCase> subtractIntTestCases() {
+ return Collections.unmodifiableList(subtractIntTestCasesList);
+ }
+
+ /**
+ * 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.
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 6188d55..82fe803 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
@@ -303,12 +303,14 @@ public class FractionTest {
Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.addIntTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.add(i2));
+ }
Assertions.assertThrows(NullPointerException.class, () -> Fraction.ONE.add((Fraction) null));
- Fraction f1 = Fraction.of(Integer.MAX_VALUE - 1, 1);
- assertFraction(Integer.MAX_VALUE, 1, f1.add(1));
-
// Special cases
final Fraction f3 = Fraction.of(Integer.MAX_VALUE, 1);
Assertions.assertThrows(ArithmeticException.class,
@@ -350,19 +352,17 @@ public class FractionTest {
Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.divideByIntTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(i2));
+ }
Assertions.assertThrows(NullPointerException.class, () -> Fraction.ONE.divide((Fraction) null));
Assertions.assertThrows(FractionException.class, () -> Fraction.of(1, 2).divide(Fraction.ZERO));
Assertions.assertThrows(FractionException.class, () -> Fraction.of(1, 2).divide(0));
- Fraction f1 = Fraction.of(0, 5);
- Assertions.assertEquals(Fraction.ZERO, f1.divide(Fraction.of(2, 7)));
- Assertions.assertEquals(Fraction.ZERO, f1.divide(11));
-
- f1 = Fraction.of(6, 35);
- assertFraction(2, 175, f1.divide(15));
-
// Special cases
final Fraction f3 = Fraction.of(1, Integer.MAX_VALUE);
Assertions.assertThrows(ArithmeticException.class,
@@ -382,18 +382,13 @@ public class FractionTest {
Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.multiplyByIntTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.multiply(i2));
+ }
Assertions.assertThrows(NullPointerException.class, () -> Fraction.ONE.multiply((Fraction) null));
-
- Fraction f1 = Fraction.of(6, 35);
- assertFraction(18, 7, f1.multiply(15));
-
- f1 = Fraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
- assertFraction(Integer.MIN_VALUE, 1, f1.multiply(Integer.MAX_VALUE));
-
- // Test zero with multiply by integer
- Assertions.assertEquals(Fraction.ZERO, Fraction.ZERO.multiply(42));
- Assertions.assertEquals(Fraction.ZERO, Fraction.ONE.multiply(0));
}
@Test
@@ -417,14 +412,14 @@ public class FractionTest {
Fraction f2 = Fraction.of(testCase.secondOperandNumerator, testCase.secondOperandDenominator);
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(f2));
}
+ for (CommonTestCases.BinaryIntOperatorTestCase testCase : CommonTestCases.subtractIntTestCases()) {
+ Fraction f1 = Fraction.of(testCase.firstOperandNumerator, testCase.firstOperandDenominator);
+ int i2 = testCase.secondOperand;
+ assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.subtract(i2));
+ }
Assertions.assertThrows(NullPointerException.class, () -> Fraction.ONE.add((Fraction) null));
- assertFraction(-3, 1, Fraction.ZERO.subtract(3));
-
- Fraction f1 = Fraction.of(2, 3);
- assertFraction(-7, 3, f1.subtract(3));
-
final Fraction f3 = Fraction.of(1, Integer.MAX_VALUE);
final Fraction f4 = Fraction.of(1, Integer.MAX_VALUE - 1);
Assertions.assertThrows(ArithmeticException.class,