You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/11/02 22:11:49 UTC
[commons-numbers] branch master updated: Unit tests.
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/master by this push:
new 1544a1d Unit tests.
1544a1d is described below
commit 1544a1dbb32a5c161d2fbc08d7135f71eff11683
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sat Nov 2 23:10:57 2019 +0100
Unit tests.
---
.../commons/numbers/fraction/BigFractionTest.java | 60 +++++++++++++++++-----
.../commons/numbers/fraction/CommonTestCases.java | 8 +++
.../commons/numbers/fraction/FractionTest.java | 10 ++++
3 files changed, 64 insertions(+), 14 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 2efa0f6..24f3f46 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
@@ -482,14 +482,14 @@ public class BigFractionTest {
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.divide(f2));
}
- BigFraction f1 = BigFraction.of(3, 5);
- BigFraction f2 = BigFraction.ZERO;
- try {
- f1.divide(f2);
- Assertions.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {
+ {
+ Assertions.assertThrows(FractionException.class,
+ () -> BigFraction.of(1, 2).divide(BigInteger.ZERO));
}
+ BigFraction f1;
+ BigFraction f2;
+
f1 = BigFraction.of(0, 5);
f2 = BigFraction.of(2, 7);
BigFraction f = f1.divide(f2);
@@ -536,12 +536,17 @@ public class BigFractionTest {
Assertions.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
Assertions.assertEquals(1, f.getDenominatorAsInt());
- try {
- f.multiply((BigFraction) null);
- Assertions.fail("expecting NullPointerException");
- } catch (NullPointerException ex) {
+ {
+ Assertions.assertThrows(NullPointerException.class,
+ () -> BigFraction.ONE.multiply((BigFraction) null));
}
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(BigInteger.ONE));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(BigInteger.ZERO));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(1));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(0));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ZERO.multiply(1L));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.multiply(0L));
}
@Test
@@ -553,11 +558,16 @@ public class BigFractionTest {
}
BigFraction f = BigFraction.of(1, 1);
- try {
- f.subtract((BigFraction) null);
- Assertions.fail("expecting NullPointerException");
- } catch (NullPointerException ex) {
+ {
+ Assertions.assertThrows(NullPointerException.class,
+ () -> f.subtract((BigFraction) null));
}
+
+ Assertions.assertEquals(BigFraction.ONE, BigFraction.ONE.subtract(BigInteger.ZERO));
+ Assertions.assertEquals(BigFraction.of(-1), BigFraction.ZERO.subtract(BigInteger.ONE));
+ Assertions.assertEquals(BigFraction.of(-2), BigFraction.ZERO.subtract(2));
+ Assertions.assertEquals(BigFraction.of(-123), BigFraction.ZERO.subtract(123L));
+ Assertions.assertEquals(BigFraction.of(-7, 3), BigFraction.of(2, 3).subtract(BigInteger.valueOf(3)));
}
@Test
@@ -603,6 +613,10 @@ public class BigFractionTest {
Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13));
Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13l));
Assertions.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(BigInteger.valueOf(-13l)));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(123));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(123L));
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.of(0, 5).pow(new BigInteger("112233445566778899")));
+ Assertions.assertEquals(Math.sqrt(2d / 3), BigFraction.of(2, 3).pow(0.5), 1e-15);
}
@Test
@@ -628,6 +642,24 @@ public class BigFractionTest {
}
@Test
+ public void testAdditiveNeutral() {
+ Assertions.assertEquals(BigFraction.ZERO, BigFraction.ONE.zero());
+ }
+ @Test
+ public void testMultiplicativeNeutral() {
+ Assertions.assertEquals(BigFraction.ONE, BigFraction.ZERO.one());
+ }
+
+ @Test
+ public void testToString() {
+ Assertions.assertEquals("0", BigFraction.of(0, 3).toString());
+ Assertions.assertEquals("3", BigFraction.of(6, 2).toString());
+ Assertions.assertEquals("2 / 3", BigFraction.of(18, 27).toString());
+ Assertions.assertEquals("-10 / 11", BigFraction.of(-10, 11).toString());
+ Assertions.assertEquals("10 / -11", BigFraction.of(10, -11).toString());
+ }
+
+ @Test
public void testParse() {
String[] validExpressions = new String[] {
"3",
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 6753282..d359550 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
@@ -212,6 +212,9 @@ class CommonTestCases {
testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 7, 6));
testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 7, 6));
testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 4, 3));
+ testCases.add(new BinaryOperatorTestCase(2, 3, 0, 5, 2, 3));
+ testCases.add(new BinaryOperatorTestCase(0, 7, 2, 3, 2, 3));
+ testCases.add(new BinaryOperatorTestCase(2, 3, -2, 3, 0, 1));
testCases.add(new BinaryOperatorTestCase(
-1, 13*13*2*2,
@@ -256,6 +259,7 @@ class CommonTestCases {
testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 3, 4));
testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 4, 3));
testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 1, 1));
+ testCases.add(new BinaryOperatorTestCase(0, 3, 2, 3, 0, 1));
testCases.add(new BinaryOperatorTestCase(
2, 7,
@@ -285,6 +289,8 @@ class CommonTestCases {
testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, 1, 3));
testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 1, 3));
testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 4, 9));
+ testCases.add(new BinaryOperatorTestCase(0, 3, 2, 3, 0, 1));
+ testCases.add(new BinaryOperatorTestCase(2, 3, 0, 3, 0, 1));
testCases.add(new BinaryOperatorTestCase(
Integer.MAX_VALUE, 1,
@@ -306,6 +312,8 @@ class CommonTestCases {
testCases.add(new BinaryOperatorTestCase(1, 2, 2, 3, -1, 6));
testCases.add(new BinaryOperatorTestCase(2, 3, 1, 2, 1, 6));
testCases.add(new BinaryOperatorTestCase(2, 3, 2, 3, 0, 1));
+ testCases.add(new BinaryOperatorTestCase(0, 3, 1, 5, -1, 5));
+ testCases.add(new BinaryOperatorTestCase(2, 3, 0, 5, 2, 3));
// if this fraction is subtracted naively, it will overflow the int range.
// check that it doesn't.
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 aea3d01..6d7c27a 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
@@ -478,6 +478,16 @@ public class FractionTest {
Assertions.assertEquals(minusOne2, minusOne);
Assertions.assertEquals(minusOne, minusOne2);
}
+
+ @Test
+ public void testAdditiveNeutral() {
+ Assertions.assertEquals(Fraction.ZERO, Fraction.ONE.zero());
+ }
+ @Test
+ public void testMultiplicativeNeutral() {
+ Assertions.assertEquals(Fraction.ONE, Fraction.ZERO.one());
+ }
+
@Test
public void testToString() {
Assertions.assertEquals("0", Fraction.of(0, 3).toString());