From 65922ddf6c2114068b8fc43b1aa5a570fb4e7cfd Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 09:46:41 +0530 Subject: [PATCH 1/7] Add tests for `IIRFilter.java`, fix bug in `setCoeffs` method --- .../thealgorithms/audiofilters/IIRFilter.java | 2 +- .../audiofilters/IIRFilterTest.java | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java diff --git a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java index c211cd08a501..10d828eb83d2 100644 --- a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java +++ b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java @@ -58,7 +58,7 @@ public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgument 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++) { // Corrected loop condition coeffsA[i] = aCoeffs[i]; coeffsB[i] = bCoeffs[i]; } 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 000000000000..d04829448844 --- /dev/null +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -0,0 +1,105 @@ +package com.thealgorithms.audiofilters; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +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 testSetCoeffsValid() { + IIRFilter filter = new IIRFilter(2); + + // Valid coefficients with correct length + double[] aCoeffs = {1.0, 0.5}; + double[] bCoeffs = {1.0, 0.5}; + assertDoesNotThrow(() -> filter.setCoeffs(aCoeffs, bCoeffs), + "Coefficients should be set without exceptions"); + } + + @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]"); + } +} From b7676cacabab9f467a6d7e0136f8ac87d04049b1 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 04:17:03 +0000 Subject: [PATCH 2/7] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..1fdb1e6f366c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -591,6 +591,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) From bdcc5ed2378f90ad7dff0f57088cd0596cc3ee68 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 09:51:23 +0530 Subject: [PATCH 3/7] Fix clang errors --- .../thealgorithms/audiofilters/IIRFilter.java | 2 +- .../audiofilters/IIRFilterTest.java | 27 ++++++------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java index 10d828eb83d2..fbc095909541 100644 --- a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java +++ b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java @@ -58,7 +58,7 @@ public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgument throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length); } - for (int i = 0; i < order; i++) { // Corrected loop condition + for (int i = 0; i < order; i++) { coeffsA[i] = aCoeffs[i]; coeffsB[i] = bCoeffs[i]; } diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index d04829448844..ddff08ae5c89 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -3,8 +3,8 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -20,9 +20,7 @@ void testConstructorValidOrder() { @Test void testConstructorInvalidOrder() { // Test an invalid filter creation (order <= 0) - assertThrows(IllegalArgumentException.class, () -> { - new IIRFilter(0); - }, "Order must be greater than zero"); + assertThrows(IllegalArgumentException.class, () -> { new IIRFilter(0); }, "Order must be greater than zero"); } @Test @@ -32,8 +30,7 @@ void testSetCoeffsValid() { // Valid coefficients with correct length double[] aCoeffs = {1.0, 0.5}; double[] bCoeffs = {1.0, 0.5}; - assertDoesNotThrow(() -> filter.setCoeffs(aCoeffs, bCoeffs), - "Coefficients should be set without exceptions"); + assertDoesNotThrow(() -> filter.setCoeffs(aCoeffs, bCoeffs), "Coefficients should be set without exceptions"); } @Test @@ -43,9 +40,7 @@ void testSetCoeffsInvalidLengthA() { // 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"); + assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs must be of size 2"); } @Test @@ -55,9 +50,7 @@ void testSetCoeffsInvalidLengthB() { // 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"); + assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "bCoeffs must be of size 2"); } @Test @@ -67,9 +60,7 @@ void testSetCoeffsInvalidACoeffZero() { // 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"); + assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs[0] must not be zero"); } @Test @@ -80,8 +71,7 @@ void testProcessWithNoCoeffsSet() { 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"); + assertEquals(inputSample, result, 1e-6, "Process should return the same value as input with default coefficients"); } @Test @@ -99,7 +89,6 @@ void testProcessWithCoeffsSet() { // 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]"); + assertTrue(result >= -1.0 && result <= 1.0, "Processed result should be in the range [-1, 1]"); } } From a72e43ad8d027c8aa12521af0efad01d6409aa38 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 10:12:32 +0530 Subject: [PATCH 4/7] Fix --- .../java/com/thealgorithms/audiofilters/IIRFilterTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index ddff08ae5c89..830607f30122 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -1,10 +1,6 @@ package com.thealgorithms.audiofilters; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -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 static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; From 601e9657867b7cf14329c40e1a40446606f90d99 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 10:14:38 +0530 Subject: [PATCH 5/7] Fix --- .../java/com/thealgorithms/audiofilters/IIRFilterTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index 830607f30122..ddff08ae5c89 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -1,6 +1,10 @@ package com.thealgorithms.audiofilters; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +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; From 29750df678376253cdb1a43367a8547d51bbc44c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 10:27:19 +0530 Subject: [PATCH 6/7] Fix --- .../java/com/thealgorithms/audiofilters/IIRFilterTest.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index ddff08ae5c89..830607f30122 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -1,10 +1,6 @@ package com.thealgorithms.audiofilters; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -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 static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; From 5c7547fb94dbba63aeae5d00e0e7d034ea3bad79 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 10:30:41 +0530 Subject: [PATCH 7/7] Fix --- .../thealgorithms/audiofilters/IIRFilterTest.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java index 830607f30122..66d7d60c501b 100644 --- a/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java +++ b/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java @@ -1,6 +1,9 @@ package com.thealgorithms.audiofilters; -import static org.junit.jupiter.api.Assertions.*; +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; @@ -19,16 +22,6 @@ void testConstructorInvalidOrder() { assertThrows(IllegalArgumentException.class, () -> { new IIRFilter(0); }, "Order must be greater than zero"); } - @Test - void testSetCoeffsValid() { - IIRFilter filter = new IIRFilter(2); - - // Valid coefficients with correct length - double[] aCoeffs = {1.0, 0.5}; - double[] bCoeffs = {1.0, 0.5}; - assertDoesNotThrow(() -> filter.setCoeffs(aCoeffs, bCoeffs), "Coefficients should be set without exceptions"); - } - @Test void testSetCoeffsInvalidLengthA() { IIRFilter filter = new IIRFilter(2);