|
29 | 29 |
|
30 | 30 | package org.scijava.ops.image.types; |
31 | 31 |
|
32 | | -import java.util.function.BiFunction; |
33 | | -import java.util.function.Function; |
34 | | - |
35 | | -import org.scijava.ops.image.AbstractOpTest; |
36 | 32 | import net.imglib2.RandomAccessibleInterval; |
| 33 | +import net.imglib2.histogram.Histogram1d; |
37 | 34 | import net.imglib2.img.Img; |
| 35 | +import net.imglib2.img.array.ArrayImg; |
38 | 36 | import net.imglib2.img.array.ArrayImgs; |
| 37 | +import net.imglib2.img.basictypeaccess.array.DoubleArray; |
39 | 38 | import net.imglib2.outofbounds.OutOfBoundsConstantValueFactory; |
40 | 39 | import net.imglib2.outofbounds.OutOfBoundsFactory; |
41 | 40 | import net.imglib2.outofbounds.OutOfBoundsRandomValueFactory; |
42 | 41 | import net.imglib2.type.numeric.integer.UnsignedByteType; |
43 | | - |
| 42 | +import net.imglib2.type.numeric.real.DoubleType; |
| 43 | +import org.junit.jupiter.api.Assertions; |
44 | 44 | import org.junit.jupiter.api.Test; |
| 45 | +import org.scijava.ops.image.AbstractOpTest; |
| 46 | +import org.scijava.types.Nil; |
45 | 47 | import org.scijava.types.extract.TypeExtractor; |
46 | 48 |
|
| 49 | +import java.lang.reflect.ParameterizedType; |
| 50 | +import java.lang.reflect.Type; |
| 51 | + |
47 | 52 | /** |
48 | 53 | * Tests various {@link TypeExtractor}s. |
49 | 54 | * |
50 | 55 | * @author Gabriel Selzer |
51 | 56 | */ |
52 | 57 | public class TypeExtractorTests extends AbstractOpTest { |
53 | 58 |
|
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 | | - |
62 | 59 | @Test |
63 | 60 | public void testOutOfBoundsConstantValueFactoryTypeExtractors() { |
64 | 61 | OutOfBoundsFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>> oobf = |
65 | 62 | 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]); |
73 | 75 | } |
74 | 76 |
|
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 | | - |
85 | 77 | @Test |
86 | 78 | public void testOutOfBoundsRandomValueFactoryTypeExtractors() { |
87 | 79 | OutOfBoundsFactory<UnsignedByteType, RandomAccessibleInterval<UnsignedByteType>> oobf = |
88 | 80 | 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()); |
95 | 127 | } |
96 | 128 | } |
0 commit comments