Skip to content

Commit a2457bd

Browse files
authored
Add tests for IIRFilter.java, fix bug in setCoeffs method (#5590)
1 parent fa7d357 commit a2457bd

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@
602602
* java
603603
* com
604604
* thealgorithms
605+
* audiofilters
606+
* [IIRFilterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java)
605607
* backtracking
606608
* [AllPathsFromSourceToTargetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/AllPathsFromSourceToTargetTest.java)
607609
* [ArrayCombinationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/ArrayCombinationTest.java)

src/main/java/com/thealgorithms/audiofilters/IIRFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgument
5858
throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
5959
}
6060

61-
for (int i = 0; i <= order; i++) {
61+
for (int i = 0; i < order; i++) {
6262
coeffsA[i] = aCoeffs[i];
6363
coeffsB[i] = bCoeffs[i];
6464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.audiofilters;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class IIRFilterTest {
11+
12+
@Test
13+
void testConstructorValidOrder() {
14+
// Test a valid filter creation
15+
IIRFilter filter = new IIRFilter(2);
16+
assertNotNull(filter, "Filter should be instantiated correctly");
17+
}
18+
19+
@Test
20+
void testConstructorInvalidOrder() {
21+
// Test an invalid filter creation (order <= 0)
22+
assertThrows(IllegalArgumentException.class, () -> { new IIRFilter(0); }, "Order must be greater than zero");
23+
}
24+
25+
@Test
26+
void testSetCoeffsInvalidLengthA() {
27+
IIRFilter filter = new IIRFilter(2);
28+
29+
// Invalid 'aCoeffs' length
30+
double[] aCoeffs = {1.0}; // too short
31+
double[] bCoeffs = {1.0, 0.5};
32+
assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs must be of size 2");
33+
}
34+
35+
@Test
36+
void testSetCoeffsInvalidLengthB() {
37+
IIRFilter filter = new IIRFilter(2);
38+
39+
// Invalid 'bCoeffs' length
40+
double[] aCoeffs = {1.0, 0.5};
41+
double[] bCoeffs = {1.0}; // too short
42+
assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "bCoeffs must be of size 2");
43+
}
44+
45+
@Test
46+
void testSetCoeffsInvalidACoeffZero() {
47+
IIRFilter filter = new IIRFilter(2);
48+
49+
// Invalid 'aCoeffs' where aCoeffs[0] == 0.0
50+
double[] aCoeffs = {0.0, 0.5}; // aCoeffs[0] must not be zero
51+
double[] bCoeffs = {1.0, 0.5};
52+
assertThrows(IllegalArgumentException.class, () -> { filter.setCoeffs(aCoeffs, bCoeffs); }, "aCoeffs[0] must not be zero");
53+
}
54+
55+
@Test
56+
void testProcessWithNoCoeffsSet() {
57+
// Test process method with default coefficients (sane defaults)
58+
IIRFilter filter = new IIRFilter(2);
59+
double inputSample = 0.5;
60+
double result = filter.process(inputSample);
61+
62+
// Since default coeffsA[0] and coeffsB[0] are 1.0, expect output = input
63+
assertEquals(inputSample, result, 1e-6, "Process should return the same value as input with default coefficients");
64+
}
65+
66+
@Test
67+
void testProcessWithCoeffsSet() {
68+
// Test process method with set coefficients
69+
IIRFilter filter = new IIRFilter(2);
70+
71+
double[] aCoeffs = {1.0, 0.5};
72+
double[] bCoeffs = {1.0, 0.5};
73+
filter.setCoeffs(aCoeffs, bCoeffs);
74+
75+
// Process a sample
76+
double inputSample = 0.5;
77+
double result = filter.process(inputSample);
78+
79+
// Expected output can be complex to calculate in advance;
80+
// check if the method runs and returns a result within reasonable bounds
81+
assertTrue(result >= -1.0 && result <= 1.0, "Processed result should be in the range [-1, 1]");
82+
}
83+
}

0 commit comments

Comments
 (0)