Skip to content

Commit 6fcdf04

Browse files
committed
Improve SciJava Ops Image Type Extractor tests
1 parent f07a6c4 commit 6fcdf04

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

scijava-ops-image/src/test/java/org/scijava/ops/image/OpRegressionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class OpRegressionTest {
4242

4343
@Test
4444
public void testOpDiscoveryRegression() {
45-
long expected = 1942;
45+
long expected = 1940;
4646
long actual = ops.infos().size();
4747
assertEquals(expected, actual);
4848
}

scijava-ops-image/src/test/java/org/scijava/ops/image/types/TypeExtractorTests.java

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,68 +29,100 @@
2929

3030
package org.scijava.ops.image.types;
3131

32-
import java.util.function.BiFunction;
33-
import java.util.function.Function;
34-
35-
import org.scijava.ops.image.AbstractOpTest;
3632
import net.imglib2.RandomAccessibleInterval;
33+
import net.imglib2.histogram.Histogram1d;
3734
import net.imglib2.img.Img;
35+
import net.imglib2.img.array.ArrayImg;
3836
import net.imglib2.img.array.ArrayImgs;
37+
import net.imglib2.img.basictypeaccess.array.DoubleArray;
3938
import net.imglib2.outofbounds.OutOfBoundsConstantValueFactory;
4039
import net.imglib2.outofbounds.OutOfBoundsFactory;
4140
import net.imglib2.outofbounds.OutOfBoundsRandomValueFactory;
4241
import net.imglib2.type.numeric.integer.UnsignedByteType;
43-
42+
import net.imglib2.type.numeric.real.DoubleType;
43+
import org.junit.jupiter.api.Assertions;
4444
import org.junit.jupiter.api.Test;
45+
import org.scijava.ops.image.AbstractOpTest;
46+
import org.scijava.types.Nil;
4547
import org.scijava.types.extract.TypeExtractor;
4648

49+
import java.lang.reflect.ParameterizedType;
50+
import java.lang.reflect.Type;
51+
4752
/**
4853
* Tests various {@link TypeExtractor}s.
4954
*
5055
* @author Gabriel Selzer
5156
*/
5257
public class TypeExtractorTests extends AbstractOpTest {
5358

54-
/**
55-
* @input oobf the {@link OutOfBoundsConstantValueFactory}
56-
* @output some {@link String}
57-
* @implNote op names='test.oobcvfTypeExtractor'
58-
*/
59-
public final Function<OutOfBoundsConstantValueFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>>, String> func =
60-
(oobf) -> "oobcvf";
61-
6259
@Test
6360
public void testOutOfBoundsConstantValueFactoryTypeExtractors() {
6461
OutOfBoundsFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>> oobf =
6562
new OutOfBoundsConstantValueFactory<>(new UnsignedByteType(5));
66-
67-
String output = (String) ops.op("test.oobcvfTypeExtractor").input(oobf)
68-
.apply();
69-
// make sure that output matches the return from the Op above, specific to
70-
// the
71-
// type of OOBF we passed through.
72-
assert output.equals("oobcvf");
63+
// Get the generic type (indirectly using the Type reification system through ops)
64+
Type objType = ops.genericType(oobf);
65+
Assertions.assertInstanceOf(ParameterizedType.class, objType);
66+
ParameterizedType pType = (ParameterizedType) objType;
67+
// Assert raw type
68+
Assertions.assertEquals(OutOfBoundsConstantValueFactory.class, pType.getRawType());
69+
// Assert first type parameter
70+
Assertions.assertEquals(UnsignedByteType.class, pType.getActualTypeArguments()[0]);
71+
// Assert second type parameter
72+
ParameterizedType secondArgType = (ParameterizedType) pType.getActualTypeArguments()[1];
73+
Assertions.assertEquals(RandomAccessibleInterval.class, secondArgType.getRawType());
74+
Assertions.assertEquals(UnsignedByteType.class, secondArgType.getActualTypeArguments()[0]);
7375
}
7476

75-
// Test Op returns a string different from the one above
76-
/**
77-
* @input oobf the {@link OutOfBoundsRandomValueFactory}
78-
* @input rai the {@link RandomAccessibleInterval}
79-
* @output some {@link String}
80-
* @implNote op names='test.oobrvfTypeExtractor'
81-
*/
82-
public final BiFunction<OutOfBoundsRandomValueFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>>, RandomAccessibleInterval<UnsignedByteType>, String> funcRandom =
83-
(oobf, rai) -> "oobrvf";
84-
8577
@Test
8678
public void testOutOfBoundsRandomValueFactoryTypeExtractors() {
8779
OutOfBoundsFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>> oobf =
8880
new OutOfBoundsRandomValueFactory<>(new UnsignedByteType(7), 7, 7);
89-
Img<UnsignedByteType> img = ArrayImgs.unsignedBytes(new long[] { 10, 10 });
90-
String output = (String) ops.op("test.oobrvfTypeExtractor").input(oobf, img)
91-
.apply(); // make sure that output matches the return from the
92-
// Op above, specific to the // type of OOBF we passed
93-
// through.
94-
assert output.equals("oobrvf");
81+
// Get the generic type (indirectly using the Type reification system through ops)
82+
Type objType = ops.genericType(oobf);
83+
Assertions.assertInstanceOf(ParameterizedType.class, objType);
84+
ParameterizedType pType = (ParameterizedType) objType;
85+
// Assert raw type
86+
Assertions.assertEquals(OutOfBoundsRandomValueFactory.class, pType.getRawType());
87+
// Assert first type parameter
88+
Assertions.assertEquals(UnsignedByteType.class, pType.getActualTypeArguments()[0]);
89+
// Assert second type parameter
90+
ParameterizedType secondArgType = (ParameterizedType) pType.getActualTypeArguments()[1];
91+
Assertions.assertEquals(RandomAccessibleInterval.class, secondArgType.getRawType());
92+
Assertions.assertEquals(UnsignedByteType.class, secondArgType.getActualTypeArguments()[0]);
93+
}
94+
95+
@Test
96+
public void testRAITypeExtractor() {
97+
Img<DoubleType> data = ArrayImgs.doubles(20, 20);
98+
// Get the generic type (indirectly using the Type reification system through ops)
99+
Type objType = ops.genericType(data);
100+
Assertions.assertInstanceOf(ParameterizedType.class, objType);
101+
ParameterizedType pType = (ParameterizedType) objType;
102+
// Assert raw type
103+
Assertions.assertEquals(ArrayImg.class, pType.getRawType());
104+
// Assert type parameters
105+
Assertions.assertArrayEquals( //
106+
new Type[] {DoubleType.class, DoubleArray.class}, //
107+
pType.getActualTypeArguments() //
108+
);
109+
}
110+
111+
@Test
112+
public void testHistogram1dTypeExtractor() {
113+
Img<DoubleType> data = ArrayImgs.doubles(20, 20);
114+
// Use an Op to get a histogram
115+
Histogram1d<DoubleType> doubles = ops.op("image.histogram") //
116+
.input(data) //
117+
.outType(new Nil<Histogram1d<DoubleType>>() {}) //
118+
.apply();
119+
// Get the generic type (indirectly using the Type reification system through ops)
120+
Type objType = ops.genericType(doubles);
121+
Assertions.assertInstanceOf(ParameterizedType.class, objType);
122+
ParameterizedType pType = (ParameterizedType) objType;
123+
// Assert raw type
124+
Assertions.assertEquals(Histogram1d.class, pType.getRawType());
125+
// Assert first type parameter
126+
Assertions.assertArrayEquals(new Type[] {DoubleType.class}, pType.getActualTypeArguments());
95127
}
96128
}

0 commit comments

Comments
 (0)