Repository: TheAlgorithms/Java
Pull request #5767 (2024-10-13 10:24:12)

Add tests for `ConvolutionFFT`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5767?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 64.28%. Comparing base [(`596c614`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/596c6147af0af91d1ca8990ed91820df1fe3b8ea?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`0df3a58`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0df3a584cf8db5179ecdab6bb17500a7bbc5f6d4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 4 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5767      +/-   ##
============================================
+ Coverage     64.05%   64.28%   +0.23%     
- Complexity     4177     4214      +37     
============================================
  Files           584      587       +3     
  Lines         16332    16392      +60     
  Branches       3152     3163      +11     
============================================
+ Hits          10461    10538      +77     
+ Misses         5445     5428      -17     
  Partials        426      426              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5767?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

Hardvan: I have resolved the conflicts @alxkm .

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index d0de7071..8d66f77d 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -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)
diff --git a/src/main/java/com/thealgorithms/maths/FFT.java b/src/main/java/com/thealgorithms/maths/FFT.java
index 47605f01..91754bd1 100644
--- a/src/main/java/com/thealgorithms/maths/FFT.java
+++ b/src/main/java/com/thealgorithms/maths/FFT.java
@@ -165,6 +165,14 @@ public final class FFT {
             temp.img = this.img / n;
             return temp;
         }
+
+        public double real() {
+            return real;
+        }
+
+        public double imaginary() {
+            return img;
+        }
     }
 
     /**


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java b/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java
new file mode 100644
index 00000000..4d627f93
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java
@@ -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"));
+    }
+}