Skip to content

Add tests for IIRFilter.java, fix bug in setCoeffs method #5590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
coeffsA[i] = aCoeffs[i];
coeffsB[i] = bCoeffs[i];
}
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/com/thealgorithms/audiofilters/IIRFilterTest.java
Original file line number Diff line number Diff line change
@@ -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]");
}
}