Skip to content

Commit

Permalink
Add tests for ConvolutionFFT (TheAlgorithms#5767)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardvan authored Oct 15, 2024
1 parent f1aceea commit 9eff71b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
* [ConvolutionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionTest.java)
* [CrossCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CrossCorrelationTest.java)
* [DeterminantOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DeterminantOfMatrixTest.java)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/thealgorithms/maths/FFT.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ public Complex divide(double n) {
temp.img = this.img / n;
return temp;
}

public double real() {
return real;
}

public double imaginary() {
return img;
}
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class ConvolutionFFTTest {

/**
* Helper method to create a complex signal from an array of doubles.
*/
private ArrayList<FFT.Complex> createComplexSignal(double[] values) {
ArrayList<FFT.Complex> signal = new ArrayList<>();
for (double value : values) {
signal.add(new FFT.Complex(value, 0));
}
return signal;
}

/**
* Helper method to compare two complex signals for equality within a small margin of error.
*/
private void assertComplexArrayEquals(List<FFT.Complex> expected, List<FFT.Complex> result, double delta) {
assertEquals(expected.size(), result.size(), "Signal lengths are not equal.");
for (int i = 0; i < expected.size(); i++) {
FFT.Complex expectedValue = expected.get(i);
FFT.Complex resultValue = result.get(i);
assertEquals(expectedValue.real(), resultValue.real(), delta, "Real part mismatch at index " + i);
assertEquals(expectedValue.imaginary(), resultValue.imaginary(), delta, "Imaginary part mismatch at index " + i);
}
}

@ParameterizedTest(name = "Test case {index}: {3}")
@MethodSource("provideTestCases")
public void testConvolutionFFT(double[] a, double[] b, double[] expectedOutput, String testDescription) {
ArrayList<FFT.Complex> signalA = createComplexSignal(a);
ArrayList<FFT.Complex> signalB = createComplexSignal(b);

ArrayList<FFT.Complex> expected = createComplexSignal(expectedOutput);
ArrayList<FFT.Complex> result = ConvolutionFFT.convolutionFFT(signalA, signalB);

assertComplexArrayEquals(expected, result, 1e-9); // Allow small margin of error
}

private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of(new double[] {1, 2, 3}, new double[] {4, 5, 6}, new double[] {4, 13, 28, 27, 18}, "Basic test"), Arguments.of(new double[] {0, 0, 0}, new double[] {1, 2, 3}, new double[] {0, 0, 0, 0, 0}, "Test with zero elements"),
Arguments.of(new double[] {1, 2}, new double[] {3, 4, 5}, new double[] {3, 10, 13, 10}, "Test with different sizes"), Arguments.of(new double[] {5}, new double[] {2}, new double[] {10}, "Test with single element"),
Arguments.of(new double[] {1, -2, 3}, new double[] {-1, 2, -3}, new double[] {-1, 4, -10, 12, -9}, "Test with negative values"));
}
}

0 comments on commit 9eff71b

Please sign in to comment.