Skip to content

Commit 63b4ea9

Browse files
committed
ConvolverTest: reduce code duplication
1 parent b14b020 commit 63b4ea9

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

src/test/java/net/imglib2/algorithm/convolution/kernel/ConvolverTest.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,51 @@
22

33
import static org.junit.Assert.assertArrayEquals;
44

5+
import net.imglib2.RandomAccess;
56
import net.imglib2.img.array.ArrayImgs;
67
import net.imglib2.type.numeric.real.DoubleType;
78

89
import org.junit.Test;
910

1011
/**
12+
* Tests {@link ConvolverNativeType}, {@link ConvolverNumericType},
13+
* {@link DoubleConvolverRealType} and {@link FloatConvolverRealType}.
14+
*
1115
* @author Tobias Pietzsch
1216
*/
1317
public class ConvolverTest
1418
{
15-
private final double[] kernel = { 1.0, 2.0, 3.0, 4.0 };
16-
private final int center = 2;
17-
18-
private final double[] in = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 };
19-
2019
@Test
2120
public void testConvolverNativeType()
2221
{
23-
final double[] out = new double[ kernel.length ];
24-
final ConvolverNativeType< DoubleType > convolver = new ConvolverNativeType<>(
25-
Kernel1D.asymmetric( kernel, center ),
26-
ArrayImgs.doubles( in, in.length ).randomAccess(),
27-
ArrayImgs.doubles( out, out.length ).randomAccess(),
28-
0,
29-
out.length );
30-
convolver.run();
31-
assertArrayEquals( kernel, out, 0 );
22+
testConvolver( ConvolverNativeType::new );
3223
}
3324

3425
@Test
3526
public void testConvolverNumericType()
3627
{
37-
final double[] out = new double[ kernel.length ];
38-
final ConvolverNumericType< DoubleType > convolver = new ConvolverNumericType<>(
39-
Kernel1D.asymmetric( kernel, center ),
40-
ArrayImgs.doubles( in, in.length ).randomAccess(),
41-
ArrayImgs.doubles( out, out.length ).randomAccess(),
42-
0,
43-
out.length );
44-
convolver.run();
45-
assertArrayEquals( kernel, out, 0 );
28+
testConvolver( ConvolverNumericType::new );
4629
}
4730

4831
@Test
4932
public void testDoubleConvolverRealType()
5033
{
51-
final double[] out = new double[ kernel.length ];
52-
final DoubleConvolverRealType convolver = new DoubleConvolverRealType(
53-
Kernel1D.asymmetric( kernel, center ),
54-
ArrayImgs.doubles( in, in.length ).randomAccess(),
55-
ArrayImgs.doubles( out, out.length ).randomAccess(),
56-
0,
57-
out.length );
58-
convolver.run();
59-
assertArrayEquals( kernel, out, 0 );
34+
testConvolver( DoubleConvolverRealType::new );
6035
}
6136

6237
@Test
6338
public void testFloatConvolverRealType()
6439
{
40+
testConvolver( FloatConvolverRealType::new );
41+
}
42+
43+
private void testConvolver( ConvolverConstructor< DoubleType > constructor )
44+
{
45+
final double[] kernel = { 1.0, 2.0, 3.0, 4.0 };
46+
final int center = 2;
47+
final double[] in = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 };
6548
final double[] out = new double[ kernel.length ];
66-
final FloatConvolverRealType convolver = new FloatConvolverRealType(
49+
final Runnable convolver = constructor.create(
6750
Kernel1D.asymmetric( kernel, center ),
6851
ArrayImgs.doubles( in, in.length ).randomAccess(),
6952
ArrayImgs.doubles( out, out.length ).randomAccess(),
@@ -72,4 +55,9 @@ public void testFloatConvolverRealType()
7255
convolver.run();
7356
assertArrayEquals( kernel, out, 0 );
7457
}
58+
59+
private interface ConvolverConstructor< T >
60+
{
61+
Runnable create( final Kernel1D kernel, final RandomAccess< ? extends T > in, final RandomAccess< ? extends T > out, final int d, final long lineLength );
62+
}
7563
}

0 commit comments

Comments
 (0)