Files
jack/complex-numbers/ComplexNumberTest.java

356 lines
14 KiB
Java

/**
* MIT License
*
* Copyright (c) 2021 Exercism
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
public class ComplexNumberTest {
// Test helpers
private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
private static final int MAX_POINTS = 100;
private static final int TEST_CASES = 31;
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
private static final int FAILURE_POINTS = 0;
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
// Tests
@Test(name = "Real part of a purely real number")
public void testRealPartOfPurelyRealNumber() {
runTest(() -> {
double expected = 1.0;
double actual = new ComplexNumber(1.0, 0).getReal();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Real part of a purely imaginary number")
public void testRealPartOfPurelyImaginaryNumber() {
runTest(() -> {
double expected = 0.0;
double actual = new ComplexNumber(0, 1.0).getReal();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Real part of a number with real and imaginary part")
public void testRealPartOfNumberWithRealAndImaginaryParts() {
runTest(() -> {
double expected = 1.0;
double actual = new ComplexNumber(1.0, 2.0).getReal();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Imaginary part of a purely real number")
public void testImaginaryPartOfPurelyRealNumber() {
runTest(() -> {
double expected = 0.0;
double actual = new ComplexNumber(1.0, 0).getImaginary();
assertDoublesEqual(actual, expected, "imaginary");
});
}
@Test(name = "Imaginary part of a purely imaginary number")
public void testImaginaryPartOfPurelyImaginaryNumber() {
runTest(() -> {
double expected = 1.0;
double actual = new ComplexNumber(0, 1.0).getImaginary();
assertDoublesEqual(actual, expected, "imaginary");
});
}
@Test(name = "Imaginary part of a number with real and imaginary part")
public void testImaginaryPartOfNumberWithRealAndImaginaryParts() {
runTest(() -> {
double expected = 2.0;
double actual = new ComplexNumber(1.0, 2.0).getImaginary();
assertDoublesEqual(actual, expected, "imaginary");
});
}
@Test(name = "Imaginary unit")
public void testImaginaryUnitExhibitsDefiningProperty() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-1.0, 0);
ComplexNumber actual = new ComplexNumber(0, 1.0).multiply(new ComplexNumber(0, 1.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Add purely real numbers")
public void testAdditionWithPurelyRealNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(3.0, 0);
ComplexNumber actual = new ComplexNumber(1.0, 0).add(new ComplexNumber(2.0, 0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Add purely imaginary numbers")
public void testAdditionWithPurelyImaginaryNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0, 3.0);
ComplexNumber actual = new ComplexNumber(0, 1.0).add(new ComplexNumber(0, 2.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Add numbers with real and imaginary part")
public void testAdditionWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(4.0, 6.0);
ComplexNumber actual = new ComplexNumber(1.0, 2.0).add(new ComplexNumber(3.0, 4.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Subtract purely real numbers")
public void testSubtractionWithPurelyRealNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-1.0, 0.0);
ComplexNumber actual = new ComplexNumber(1.0, 0.0).subtract(new ComplexNumber(2.0, 0.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Subtract purely imaginary numbers")
public void testSubtractionWithPurelyImaginaryNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0, -1.0);
ComplexNumber actual = new ComplexNumber(0, 1.0).subtract(new ComplexNumber(0, 2.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Subtract numbers with real and imaginary part")
public void testSubtractionWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-2.0, -2.0);
ComplexNumber actual = new ComplexNumber(1.0, 2.0).subtract(new ComplexNumber(3.0, 4.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Multiply purely real numbers")
public void testMultiplicationWithPurelyRealNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(2.0, 0);
ComplexNumber actual = new ComplexNumber(1.0, 0).multiply(new ComplexNumber(2.0, 0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Multiply purely imaginary numbers")
public void testMultiplicationWithPurelyImaginaryNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-2.0, 0);
ComplexNumber actual = new ComplexNumber(0, 1.0).multiply(new ComplexNumber(0, 2.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Multiply numbers with real and imaginary part")
public void testMultiplicationWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-5.0, 10.0);
ComplexNumber actual = new ComplexNumber(1.0, 2.0).multiply(new ComplexNumber(3.0, 4.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Divide purely real numbers")
public void testDivisionWithPurelyRealNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0.5, 0);
ComplexNumber actual = new ComplexNumber(1.0, 0).divide(new ComplexNumber(2.0, 0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Divide purely imaginary numbers")
public void testDivisionWithPurelyImaginaryNumbers() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0.5, 0);
ComplexNumber actual = new ComplexNumber(0, 1.0).divide(new ComplexNumber(0, 2.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Divide numbers with real and imaginary part")
public void testDivisionWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0.44, 0.08);
ComplexNumber actual = new ComplexNumber(1.0, 2.0).divide(new ComplexNumber(3.0, 4.0));
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Absolute value of a positive purely real number")
public void testAbsoluteValueOfPositivePurelyRealNumber() {
runTest(() -> {
double expected = 5.0;
double actual = new ComplexNumber(5.0, 0).abs();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Absolute value of a negative purely real number")
public void testAbsoluteValueOfNegativePurelyRealNumber() {
runTest(() -> {
double expected = 5.0;
double actual = new ComplexNumber(-5.0, 0).abs();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Absolute value of a purely imaginary number with positive imaginary part")
public void testAbsoluteValueOfPurelyImaginaryNumberWithPositiveImaginaryPart() {
runTest(() -> {
double expected = 5.0;
double actual = new ComplexNumber(0, 5.0).abs();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Absolute value of a purely imaginary number with negative imaginary part")
public void testAbsoluteValueOfPurelyImaginaryNumberWithNegativeImaginaryPart() {
runTest(() -> {
double expected = 5.0;
double actual = new ComplexNumber(0, -5.0).abs();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Absolute value of a number with real and imaginary part")
public void testAbsoluteValueOfNumberWithRealAndImaginaryParts() {
runTest(() -> {
double expected = 5.0;
double actual = new ComplexNumber(3.0, 4.0).abs();
assertDoublesEqual(actual, expected, "real");
});
}
@Test(name = "Conjugate a purely real number")
public void testConjugationOfPurelyRealNumber() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(5.0, 0);
ComplexNumber actual = new ComplexNumber(5.0, 0).conjugate();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Conjugate a purely imaginary number")
public void testConjugationOfPurelyImaginaryNumber() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(0, -5.0);
ComplexNumber actual = new ComplexNumber(0, 5.0).conjugate();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Conjugate a number with real and imaginary part")
public void testConjugationOfNumberWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(1.0, -1.0);
ComplexNumber actual = new ComplexNumber(1.0, 1.0).conjugate();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Euler's identity/formula")
public void testExponentialOfPurelyImaginaryNumber() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(-1.0, 0);
ComplexNumber actual = new ComplexNumber(0, Math.PI).exponentialOf();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Exponential of 0")
public void testExponentialOfZero() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(1.0, 0);
ComplexNumber actual = new ComplexNumber(0, 0).exponentialOf();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Exponential of a purely real number")
public void testExponentialOfPurelyRealNumber() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(Math.E, 0);
ComplexNumber actual = new ComplexNumber(1.0, 0).exponentialOf();
assertComplexNumbersEqual(expected, actual);
});
}
@Test(name = "Exponential resulting in a number with real and imaginary part")
public void testExponentialOfNumberWithRealAndImaginaryParts() {
runTest(() -> {
ComplexNumber expected = new ComplexNumber(1, 1);
ComplexNumber actual = new ComplexNumber(Math.log(2.0) / 2, Math.PI / 4).exponentialOf();
assertComplexNumbersEqual(expected, actual);
});
}
private void runTest(Runnable body) {
try {
body.run();
testPoints.add(SUCCESS_POINTS);
} catch (Throwable t) {
TracingFramework.printError(t.getMessage());
testPoints.add(FAILURE_POINTS);
}
}
private void assertDoublesEqual(double d1, double d2, String numberPart) {
if (Double.isNaN(d1) && Double.isNaN(d2))
return;
double diff = Math.abs(d1 - d2);
if (diff > DOUBLE_EQUALITY_TOLERANCE) {
throw new AssertionError(
"While testing " + numberPart + " part of number: expected " + d2 + " but was " + d1);
}
}
private void assertComplexNumbersEqual(ComplexNumber c1, ComplexNumber c2) {
assertDoublesEqual(c1.getReal(), c2.getReal(), "real");
assertDoublesEqual(c1.getImaginary(), c2.getImaginary(), "imaginary");
}
public int getResult() {
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
}
}