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 2022/07/25 10:07:52 UTC
[commons-numbers] 03/03: Test code clean-up
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch complex-gsoc-2022
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 1b0bfa60cd8ae023fc75eac7f91e97986a887fa6
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Jul 25 10:59:35 2022 +0100
Test code clean-up
Remove unused assert methods (replace by assertions with
UnaryOperator<Complex> operation1 and
ComplexUnaryOperator<ComplexNumber> operation2).
Use final.
Correct typos.
---
.../commons/numbers/complex/CStandardTest.java | 106 ++++++++++-----------
.../numbers/complex/ComplexEdgeCaseTest.java | 45 +--------
.../commons/numbers/complex/ComplexTest.java | 6 +-
.../apache/commons/numbers/complex/TestUtils.java | 8 +-
4 files changed, 62 insertions(+), 103 deletions(-)
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java
index 2ac487b0..85bc5590 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java
@@ -658,8 +658,8 @@ class CStandardTest {
ToDoubleFunction<UniformRandomProvider> fy,
int samples) {
for (int i = 0; i < samples; i++) {
- double x = fx.applyAsDouble(rng);
- double y = fy.applyAsDouble(rng);
+ final double x = fx.applyAsDouble(rng);
+ final double y = fy.applyAsDouble(rng);
assertAbs(Complex.ofCartesian(x, y), 1);
}
}
@@ -1084,7 +1084,7 @@ class CStandardTest {
@Override
public double applyAsDouble(UniformRandomProvider rng) {
if (Double.isNaN(tmp)) {
- double u = rng.nextDouble() * Math.PI;
+ final double u = rng.nextDouble() * Math.PI;
tmp = Math.cos(u);
return Math.sin(u);
}
@@ -1109,23 +1109,23 @@ class CStandardTest {
assertComplex(negZeroZero, name, operation1, operation2, piTwoNegZero);
assertComplex(zeroNaN, name, operation1, operation2, piTwoNaN);
assertComplex(negZeroNaN, name, operation1, operation2, piTwoNaN);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, piTwoNegInf);
}
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(-inf, y), name, operation1, operation2, piNegInf);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, zeroNegInf);
}
assertComplex(negInfInf, name, operation1, operation2, threePiFourNegInf);
assertComplex(infInf, name, operation1, operation2, piFourNegInf);
assertComplex(infNaN, name, operation1, operation2, nanInf, UnspecifiedSign.IMAGINARY);
assertComplex(negInfNaN, name, operation1, operation2, nanNegInf, UnspecifiedSign.IMAGINARY);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(nanInf, name, operation1, operation2, nanNegInf);
@@ -1147,25 +1147,25 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertComplex(Complex.ZERO, name, operation1, operation2, zeroPiTwo);
assertComplex(negZeroZero, name, operation1, operation2, zeroPiTwo);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, infPiTwo);
}
assertComplex(zeroNaN, name, operation1, operation2, nanPiTwo);
assertComplex(negZeroNaN, name, operation1, operation2, nanPiTwo);
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(-inf, y), name, operation1, operation2, infPi);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, infZero);
}
assertComplex(negInfInf, name, operation1, operation2, infThreePiFour);
assertComplex(infInf, name, operation1, operation2, infPiFour);
assertComplex(infNaN, name, operation1, operation2, infNaN);
assertComplex(negInfNaN, name, operation1, operation2, infNaN);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(nanInf, name, operation1, operation2, infNaN);
@@ -1185,19 +1185,19 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertFunctionType(name, operation1, operation2, type);
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ZERO, type);
- for (double x : positiveFinite) {
+ for (final double x : positiveFinite) {
assertComplex(complex(x, inf), name, operation1, operation2, infPiTwo, type);
}
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN, type);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, infZero, type);
}
assertComplex(infInf, name, operation1, operation2, infPiFour, type);
assertComplex(infNaN, name, operation1, operation2, infNaN, type);
assertComplex(nanZero, name, operation1, operation2, nanZero, type);
- for (double y : nonZeroFinite) {
+ for (final double y : nonZeroFinite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN, type);
}
assertComplex(nanInf, name, operation1, operation2, infNaN, type, UnspecifiedSign.REAL);
@@ -1219,19 +1219,19 @@ class CStandardTest {
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ZERO, type);
assertComplex(zeroNaN, name, operation1, operation2, zeroNaN, type);
assertComplex(oneZero, name, operation1, operation2, infZero, type);
- for (double x : positiveFinite) {
+ for (final double x : positiveFinite) {
assertComplex(complex(x, inf), name, operation1, operation2, zeroPiTwo, type);
}
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN, type);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, zeroPiTwo, type);
}
assertComplex(infInf, name, operation1, operation2, zeroPiTwo, type);
assertComplex(infNaN, name, operation1, operation2, zeroNaN, type);
assertComplex(nanZero, name, operation1, operation2, NAN, type);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN, type);
}
assertComplex(nanInf, name, operation1, operation2, zeroPiTwo, type, UnspecifiedSign.REAL);
@@ -1253,20 +1253,20 @@ class CStandardTest {
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ONE, type);
assertComplex(zeroInf, name, operation1, operation2, nanZero, type, UnspecifiedSign.IMAGINARY);
assertComplex(zeroNaN, name, operation1, operation2, nanZero, type, UnspecifiedSign.IMAGINARY);
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, inf), name, operation1, operation2, NAN, type);
}
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN, type);
}
assertComplex(infZero, name, operation1, operation2, infZero, type);
- for (double y : nonZeroFinite) {
+ for (final double y : nonZeroFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, Complex.ofCis(y).multiply(inf), type);
}
assertComplex(infInf, name, operation1, operation2, infNaN, type, UnspecifiedSign.REAL);
assertComplex(infNaN, name, operation1, operation2, infNaN, type);
assertComplex(nanZero, name, operation1, operation2, nanZero, type, UnspecifiedSign.IMAGINARY);
- for (double y : nonZero) {
+ for (final double y : nonZero) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN, type);
}
assertComplex(NAN, name, operation1, operation2, NAN, type);
@@ -1287,21 +1287,21 @@ class CStandardTest {
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ZERO, type);
assertComplex(zeroInf, name, operation1, operation2, zeroNaN, type, UnspecifiedSign.REAL);
assertComplex(zeroNaN, name, operation1, operation2, zeroNaN, type, UnspecifiedSign.REAL);
- for (double x : nonZeroPositiveFinite) {
+ for (final double x : nonZeroPositiveFinite) {
assertComplex(complex(x, inf), name, operation1, operation2, NAN, type);
}
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN, type);
}
assertComplex(infZero, name, operation1, operation2, infZero, type);
// Note: Error in the ISO C99 reference to use positive finite y but the zero case is different
- for (double y : nonZeroFinite) {
+ for (final double y : nonZeroFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, Complex.ofCis(y).multiply(inf), type);
}
assertComplex(infInf, name, operation1, operation2, infNaN, type, UnspecifiedSign.REAL);
assertComplex(infNaN, name, operation1, operation2, infNaN, type, UnspecifiedSign.REAL);
assertComplex(nanZero, name, operation1, operation2, nanZero, type);
- for (double y : nonZero) {
+ for (final double y : nonZero) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN, type);
}
assertComplex(NAN, name, operation1, operation2, NAN, type);
@@ -1324,20 +1324,20 @@ class CStandardTest {
assertFunctionType(name, operation1, operation2, type);
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ZERO, type);
assertComplex(zeroInf, name, operation1, operation2, zeroNaN, type);
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, inf), name, operation1, operation2, NAN, type);
}
assertComplex(zeroNaN, name, operation1, operation2, zeroNaN, type);
- for (double x : nonZeroFinite) {
+ for (final double x : nonZeroFinite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN, type);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, complex(1.0, Math.copySign(0, Math.sin(2 * y))), type);
}
assertComplex(infInf, name, operation1, operation2, oneZero, type, UnspecifiedSign.IMAGINARY);
assertComplex(infNaN, name, operation1, operation2, oneZero, type, UnspecifiedSign.IMAGINARY);
assertComplex(nanZero, name, operation1, operation2, nanZero, type);
- for (double y : nonZero) {
+ for (final double y : nonZero) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN, type);
}
assertComplex(NAN, name, operation1, operation2, NAN, type);
@@ -1355,17 +1355,17 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertComplex(Complex.ZERO, name, operation1, operation2, oneZero);
assertComplex(negZeroZero, name, operation1, operation2, oneZero);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, NAN);
}
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
assertComplex(infZero, name, operation1, operation2, infZero);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(-inf, y), name, operation1, operation2, Complex.ofCis(y).multiply(0.0));
}
- for (double y : nonZeroFinite) {
+ for (final double y : nonZeroFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, Complex.ofCis(y).multiply(inf));
}
assertComplex(negInfInf, name, operation1, operation2, Complex.ZERO, UnspecifiedSign.REAL_IMAGINARY);
@@ -1373,7 +1373,7 @@ class CStandardTest {
assertComplex(negInfNaN, name, operation1, operation2, Complex.ZERO, UnspecifiedSign.REAL_IMAGINARY);
assertComplex(infNaN, name, operation1, operation2, infNaN, UnspecifiedSign.REAL);
assertComplex(nanZero, name, operation1, operation2, nanZero);
- for (double y : nonZero) {
+ for (final double y : nonZero) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(NAN, name, operation1, operation2, NAN);
@@ -1391,23 +1391,23 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertComplex(negZeroZero, name, operation1, operation2, negInfPi);
assertComplex(Complex.ZERO, name, operation1, operation2, negInfZero);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, infPiTwo);
}
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(-inf, y), name, operation1, operation2, infPi);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, infZero);
}
assertComplex(negInfInf, name, operation1, operation2, infThreePiFour);
assertComplex(infInf, name, operation1, operation2, infPiFour);
assertComplex(negInfNaN, name, operation1, operation2, infNaN);
assertComplex(infNaN, name, operation1, operation2, infNaN);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(nanInf, name, operation1, operation2, infNaN);
@@ -1427,23 +1427,23 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertComplex(negZeroZero, name, operation1, operation2, negInfPi);
assertComplex(Complex.ZERO, name, operation1, operation2, negInfZero);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, infPiTwo);
}
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(-inf, y), name, operation1, operation2, infPi);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, infZero);
}
assertComplex(negInfInf, name, operation1, operation2, infThreePiFour);
assertComplex(infInf, name, operation1, operation2, infPiFour);
assertComplex(negInfNaN, name, operation1, operation2, infNaN);
assertComplex(infNaN, name, operation1, operation2, infNaN);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(nanInf, name, operation1, operation2, infNaN);
@@ -1462,25 +1462,25 @@ class CStandardTest {
assertConjugateEquality(name, operation1, operation2);
assertComplex(negZeroZero, name, operation1, operation2, Complex.ZERO);
assertComplex(Complex.ZERO, name, operation1, operation2, Complex.ZERO);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, inf), name, operation1, operation2, infInf);
}
// Include infinity and nan for (x, inf).
assertComplex(infInf, name, operation1, operation2, infInf);
assertComplex(negInfInf, name, operation1, operation2, infInf);
assertComplex(nanInf, name, operation1, operation2, infInf);
- for (double x : finite) {
+ for (final double x : finite) {
assertComplex(complex(x, nan), name, operation1, operation2, NAN);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(-inf, y), name, operation1, operation2, zeroInf);
}
- for (double y : positiveFinite) {
+ for (final double y : positiveFinite) {
assertComplex(complex(inf, y), name, operation1, operation2, infZero);
}
assertComplex(negInfNaN, name, operation1, operation2, nanInf, UnspecifiedSign.IMAGINARY);
assertComplex(infNaN, name, operation1, operation2, infNaN);
- for (double y : finite) {
+ for (final double y : finite) {
assertComplex(complex(nan, y), name, operation1, operation2, NAN);
}
assertComplex(NAN, name, operation1, operation2, NAN);
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexEdgeCaseTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexEdgeCaseTest.java
index dfd53f45..72cc88d3 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexEdgeCaseTest.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexEdgeCaseTest.java
@@ -40,47 +40,6 @@ class ComplexEdgeCaseTest {
private static final double inf = Double.POSITIVE_INFINITY;
private static final double nan = Double.NaN;
- /**
- * Assert the operation on the complex number is equal to the expected value.
- *
- * <p>The results are are considered equal if there are no floating-point values between them.
- *
- * @param a Real part.
- * @param b Imaginary part.
- * @param name The operation name.
- * @param operation The operation.
- * @param x Expected real part.
- * @param y Expected imaginary part.
- */
- private static void assertComplex(double a, double b,
- String name, UnaryOperator<Complex> operation,
- double x, double y) {
- assertComplex(a, b, name, operation, x, y, 1);
- }
-
- /**
- * Assert the operation on the complex number is equal to the expected value.
- *
- * <p>The results are considered equal within the provided units of least
- * precision. The maximum count of numbers allowed between the two values is
- * {@code maxUlps - 1}.
- *
- * @param a Real part.
- * @param b Imaginary part.
- * @param name The operation name.
- * @param operation The operation.
- * @param x Expected real part.
- * @param y Expected imaginary part.
- * @param maxUlps the maximum units of least precision between the two values
- */
- private static void assertComplex(double a, double b,
- String name, UnaryOperator<Complex> operation,
- double x, double y, long maxUlps) {
- final Complex c = Complex.ofCartesian(a, b);
- final Complex e = Complex.ofCartesian(x, y);
- CReferenceTest.assertComplex(c, name, operation, e, maxUlps);
- }
-
/**
* Assert the operation on the complex number is equal to the expected value.
*
@@ -448,8 +407,8 @@ class ComplexEdgeCaseTest {
// im = 2 sin(2y) / e^2|x|
// This can be computed when e^2|x| only just overflows.
// Set a case where e^2|x| overflows but the imaginary can be computed
- double x = 709.783 / 2;
- double y = Math.PI / 4;
+ final double x = 709.783 / 2;
+ final double y = Math.PI / 4;
Assertions.assertEquals(1.0, Math.sin(2 * y), 1e-16);
Assertions.assertEquals(Double.POSITIVE_INFINITY, Math.exp(2 * x));
// As computed by GNU g++
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
index 35ebd50b..23665b71 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java
@@ -295,7 +295,7 @@ class ComplexTest {
@Test
void testAbs() {
final Complex input = Complex.ofCartesian(3.0, 4.0);
- double z = TestUtils.assertSame(input, "abs", Complex::abs, ComplexFunctions::abs);
+ final double z = TestUtils.assertSame(input, "abs", Complex::abs, ComplexFunctions::abs);
Assertions.assertEquals(5.0, z);
}
@@ -384,7 +384,7 @@ class ComplexTest {
@Test
void testNorm() {
final Complex z = Complex.ofCartesian(3.0, 4.0);
- double norm = TestUtils.assertSame(z, "norm", Complex::norm, ComplexFunctions::norm);
+ final double norm = TestUtils.assertSame(z, "norm", Complex::norm, ComplexFunctions::norm);
Assertions.assertEquals(25.0, norm);
}
@@ -1529,7 +1529,7 @@ class ComplexTest {
theta += pi / 12;
final Complex z = Complex.ofPolar(r, theta);
final Complex sqrtz = Complex.ofPolar(Math.sqrt(r), theta / 2);
- Complex actual = TestUtils.assertSame(z, "sqrt", Complex::sqrt, ComplexFunctions::sqrt);
+ final Complex actual = TestUtils.assertSame(z, "sqrt", Complex::sqrt, ComplexFunctions::sqrt);
TestUtils.assertEquals(sqrtz, actual, tol);
}
}
diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/TestUtils.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/TestUtils.java
index a601df6c..63556769 100644
--- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/TestUtils.java
+++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/TestUtils.java
@@ -61,7 +61,7 @@ public final class TestUtils {
/**
* The option for how to process test data lines flagged (prefixed)
- * with the {@code ;} character
+ * with the {@code ;} character.
*/
public enum TestDataFlagOption {
/** Ignore the line. */
@@ -449,7 +449,7 @@ public final class TestUtils {
final double r1 = operation1.applyAsDouble(c);
// Test operation2 produces the exact same result
final double r2 = operation2.applyAsDouble(c.real(), c.imag());
- Assertions.assertEquals(r1, r2, () -> "Double operator mismatch: " + name);
+ Assertions.assertEquals(r1, r2, () -> "ToDouble function mismatch: " + name);
return r1;
}
@@ -460,7 +460,7 @@ public final class TestUtils {
* @param c Input complex number.
* @param name Operation name.
* @param operation1 Condition operation on the Complex object.
- * @param operation2 Condition peration on the complex real and imaginary parts.
+ * @param operation2 Condition operation on the complex real and imaginary parts.
* @return Result boolean from the given operation.
*/
public static boolean assertSame(Complex c,
@@ -470,7 +470,7 @@ public final class TestUtils {
final boolean b1 = operation1.test(c);
// Test operation2 produces the exact same result
final boolean b2 = operation2.test(c.real(), c.imag());
- Assertions.assertEquals(b1, b2, () -> "Predicate operator mismatch: " + name);
+ Assertions.assertEquals(b1, b2, () -> "Predicate mismatch: " + name);
return b1;
}
}