Repository: TheAlgorithms/Java
Pull request #5590 (2024-10-06 04:16:56)

Add tests for `IIRFilter.java`, fix bug in `setCoeffs` method
<!--
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/5590?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 54.73%. Comparing base [(`f34fe4d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f34fe4d8408ac35cef900cfae022ffadfc007ea3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`0980285`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/09802859e44b03de7c2ed4e6fecef4d04211c1f3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

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


```diff
@@             Coverage Diff              @@
##             master    #5590      +/-   ##
============================================
+ Coverage     54.56%   54.73%   +0.17%     
- Complexity     3413     3422       +9     
============================================
  Files           535      535              
  Lines         15383    15383              
  Branches       2935     2935              
============================================
+ Hits           8393     8420      +27     
+ Misses         6637     6608      -29     
- Partials        353      355       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5590?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).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 23544efb..19ebe5c4 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -602,6 +602,8 @@
     * java
       * com
         * thealgorithms
+          * audiofilters
+            * [IIRFilterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java)
           * backtracking
             * [AllPathsFromSourceToTargetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java)
             * [ArrayCombinationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java)
diff --git a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
index c211cd08..fbc09590 100644
--- a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
+++ b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
@@ -58,7 +58,7 @@ public class IIRFilter {
             throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
         }
 
-        for (int i = 0; i <= order; i++) {
+        for (int i = 0; i < order; i++) {
             coeffsA[i] = aCoeffs[i];
             coeffsB[i] = bCoeffs[i];
         }


Test patch:
diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java
new file mode 100644
index 00000000..66d7d60c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java
@@ -0,0 +1,83 @@
+package com.thealgorithms.audiofilters;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+public class IIRFilterTest {
+
+    @Test
+    void testConstructorValidOrder() {
+        // Test a valid filter creation
+        IIRFilter filter = new IIRFilter(2);
+        assertNotNull(filter, "Filter should be instantiated correctly");
+    }
+
+    @Test
+    void testConstructorInvalidOrder() {
+        // Test an invalid filter creation (order <= 0)
+        assertThrows(IllegalArgumentException.class, () -> { new IIRFilter(0); }, "Order must be greater than zero");
+    }
+
+    @Test
+    void testSetCoeffsInvalidLengthA() {
+        IIRFilter filter = new IIRFilter(2);
+
+        // Invalid 'aCoeffs' length
+        double[] aCoeffs = {1.0}; // too short
+        double[] bCoeffs = {1.0, 0.5};
+        assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs must be of size 2");
+    }
+
+    @Test
+    void testSetCoeffsInvalidLengthB() {
+        IIRFilter filter = new IIRFilter(2);
+
+        // Invalid 'bCoeffs' length
+        double[] aCoeffs = {1.0, 0.5};
+        double[] bCoeffs = {1.0}; // too short
+        assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "bCoeffs must be of size 2");
+    }
+
+    @Test
+    void testSetCoeffsInvalidACoeffZero() {
+        IIRFilter filter = new IIRFilter(2);
+
+        // Invalid 'aCoeffs' where aCoeffs[0] == 0.0
+        double[] aCoeffs = {0.0, 0.5}; // aCoeffs[0] must not be zero
+        double[] bCoeffs = {1.0, 0.5};
+        assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs[0] must not be zero");
+    }
+
+    @Test
+    void testProcessWithNoCoeffsSet() {
+        // Test process method with default coefficients (sane defaults)
+        IIRFilter filter = new IIRFilter(2);
+        double inputSample = 0.5;
+        double result = filter.process(inputSample);
+
+        // Since default coeffsA[0] and coeffsB[0] are 1.0, expect output = input
+        assertEquals(inputSample, result, 1e-6, "Process should return the same value as input with default coefficients");
+    }
+
+    @Test
+    void testProcessWithCoeffsSet() {
+        // Test process method with set coefficients
+        IIRFilter filter = new IIRFilter(2);
+
+        double[] aCoeffs = {1.0, 0.5};
+        double[] bCoeffs = {1.0, 0.5};
+        filter.setCoeffs(aCoeffs, bCoeffs);
+
+        // Process a sample
+        double inputSample = 0.5;
+        double result = filter.process(inputSample);
+
+        // Expected output can be complex to calculate in advance;
+        // check if the method runs and returns a result within reasonable bounds
+        assertTrue(result >= -1.0 && result <= 1.0, "Processed result should be in the range [-1, 1]");
+    }
+}