Files
jack/complex-numbers/ComplexNumberMain.java
T

427 lines
16 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 java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
public class ComplexNumberMain {
private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
@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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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")
@Disabled
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();
System.out.println("TEST PASSED");
} catch (Exception | AssertionError t) {
System.out.println("TEST FAILED: " + t.getMessage());
}
}
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 static void main(String[] args) {
new ComplexNumberMain().runTests();
}
/**
* Annotation to mark test methods. The name parameter is used to identify the
* test in the output.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
private @interface Test {
String name();
}
/**
* Annotation to mark test methods as disabled. Disabled tests will not be
* executed when running the tests.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
private @interface Disabled {
}
private void runTests() {
Method[] methods = this.getClass().getDeclaredMethods();
List<Method> allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class))
.collect(Collectors.toList());
List<Method> enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class))
.collect(Collectors.toList());
System.out
.println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n");
for (Method method : enabledTestMethods) {
try {
System.out.println("Running test: " + method.getAnnotation(Test.class).name());
method.invoke(this);
} catch (Exception e) {
System.out.println("Test " + method.getAnnotation(Test.class).name()
+ " failed with an unexpected exception: " + e.getCause());
}
}
System.out.println("Finished running tests.");
if (allTestMethods.size() != enabledTestMethods.size()) {
System.out.println("\nThe following tests are disabled and were not executed:");
for (Method method : allTestMethods) {
if (method.isAnnotationPresent(Disabled.class)) {
System.out.println("- " + method.getAnnotation(Test.class).name());
}
}
System.out.println(
"To enable these tests, remove the @Disabled annotation from the corresponding test methods.");
}
}
}