- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathConvolutionFFTTest.java
55 lines (46 loc) · 2.68 KB
/
ConvolutionFFTTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
packagecom.thealgorithms.maths;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.stream.Stream;
importorg.junit.jupiter.params.ParameterizedTest;
importorg.junit.jupiter.params.provider.Arguments;
importorg.junit.jupiter.params.provider.MethodSource;
publicclassConvolutionFFTTest {
/**
* Helper method to create a complex signal from an array of doubles.
*/
privateArrayList<FFT.Complex> createComplexSignal(double[] values) {
ArrayList<FFT.Complex> signal = newArrayList<>();
for (doublevalue : values) {
signal.add(newFFT.Complex(value, 0));
}
returnsignal;
}
/**
* Helper method to compare two complex signals for equality within a small margin of error.
*/
privatevoidassertComplexArrayEquals(List<FFT.Complex> expected, List<FFT.Complex> result, doubledelta) {
assertEquals(expected.size(), result.size(), "Signal lengths are not equal.");
for (inti = 0; i < expected.size(); i++) {
FFT.ComplexexpectedValue = expected.get(i);
FFT.ComplexresultValue = 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")
publicvoidtestConvolutionFFT(double[] a, double[] b, double[] expectedOutput, StringtestDescription) {
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
}
privatestaticStream<Arguments> provideTestCases() {
returnStream.of(Arguments.of(newdouble[] {1, 2, 3}, newdouble[] {4, 5, 6}, newdouble[] {4, 13, 28, 27, 18}, "Basic test"), Arguments.of(newdouble[] {0, 0, 0}, newdouble[] {1, 2, 3}, newdouble[] {0, 0, 0, 0, 0}, "Test with zero elements"),
Arguments.of(newdouble[] {1, 2}, newdouble[] {3, 4, 5}, newdouble[] {3, 10, 13, 10}, "Test with different sizes"), Arguments.of(newdouble[] {5}, newdouble[] {2}, newdouble[] {10}, "Test with single element"),
Arguments.of(newdouble[] {1, -2, 3}, newdouble[] {-1, 2, -3}, newdouble[] {-1, 4, -10, 12, -9}, "Test with negative values"));
}
}