|
| 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