Skip to content

Commit a15bc0e

Browse files
committed
Test converters work in practice
1 parent 6977314 commit a15bc0e

3 files changed

Lines changed: 113 additions & 21 deletions

File tree

scijava-ops-image/src/main/java/org/scijava/ops/image/convert/NumbersToNativeTypes.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.scijava.ops.image.convert;
3030

3131
import net.imglib2.type.NativeType;
32+
import net.imglib2.type.numeric.IntegerType;
3233
import net.imglib2.type.numeric.RealType;
3334
import net.imglib2.type.numeric.integer.*;
3435
import net.imglib2.type.numeric.real.DoubleType;
@@ -42,7 +43,7 @@
4243
*
4344
* @author Gabriel Selzer
4445
*/
45-
public class NumbersToNativeTypes<N extends Number, T extends RealType<T>> {
46+
public class NumbersToNativeTypes<N extends Number, T extends RealType<T>, I extends IntegerType<I>> {
4647

4748
// -- Numbers to RealTypes -- //
4849

@@ -95,54 +96,93 @@ public class NumbersToNativeTypes<N extends Number, T extends RealType<T>> {
9596
num -> new FloatType(num.floatValue());
9697

9798
/**
99+
* NB This converter wins against those above in requests for e.g.
100+
* {@code Function<N, NativeType<T>>}
101+
*
98102
* @input num the {@link Number} to convert
99103
* @output a {@link DoubleType} containing the information in {@code num}
100-
* @implNote op names='engine.convert, convert.float64'
104+
* @implNote op names='engine.convert, convert.float64', priority=100
101105
*/
102106
public final Function<N, DoubleType> numberToDoubleType = //
103107
num -> new DoubleType(num.doubleValue());
104108

105109
// -- RealTypes to Numbers -- //
106110

107111
/**
112+
* @input realType the {@link ByteType} to convert
113+
* @output the {@link Byte}, converted from {@code realType}
114+
* @implNote op names='engine.convert, convert.int8', priority="10"
115+
*/
116+
public final Function<I, Byte> integerTypeToByte = i -> (byte) i.getIntegerLong();
117+
118+
/**
119+
* NB potentially lossy, so lower priority
120+
*
108121
* @input realType the {@link ByteType} to convert
109122
* @output the {@link Byte}, converted from {@code realType}
110123
* @implNote op names='engine.convert, convert.int8'
111124
*/
112-
public final Function<ByteType, Byte> byteTypeToByte = ByteType::get;
125+
public final Function<T, Byte> realTypeToByte = i -> (byte) i.getRealDouble();
113126

114127
/**
128+
* @input realType the {@link ShortType} to convert
129+
* @output the {@link Short}, converted from {@code realType}
130+
* @implNote op names='engine.convert, convert.int16', priority="10"
131+
*/
132+
public final Function<I, Short> integerTypeToShort = i -> (short) i.getInteger();
133+
134+
/**
135+
* NB potentially lossy, so lower priority
136+
*
115137
* @input realType the {@link ShortType} to convert
116138
* @output the {@link Short}, converted from {@code realType}
117139
* @implNote op names='engine.convert, convert.int16'
118140
*/
119-
public final Function<ShortType, Short> shortTypeToShort = ShortType::get;
141+
public final Function<T, Short> realTypeToShort = i -> (short) i.getRealDouble();
120142

121143
/**
122144
* @input realType the {@link IntType} to convert
123145
* @output the {@link Integer}, converted from {@code realType}
146+
* @implNote op names='engine.convert, convert.int32', priority="10"
147+
*/
148+
public final Function<I, Integer> integerTypeToInteger = IntegerType::getInteger;
149+
150+
/**
151+
* NB potentially lossy, so lower priority
152+
*
153+
* @input realType the {@link RealType} to convert
154+
* @output the {@link Integer}, converted from {@code realType}
124155
* @implNote op names='engine.convert, convert.int32'
125156
*/
126-
public final Function<IntType, Integer> intTypeToInteger = IntType::get;
157+
public final Function<T, Integer> realTypeToInteger = i -> (int) i.getRealDouble();
158+
159+
/**
160+
* @input integerType the {@link IntegerType} to convert
161+
* @output the {@link Long}, converted from {@code integerType}
162+
* @implNote op names='engine.convert, convert.int64', priority="10"
163+
*/
164+
public final Function<I, Long> integerTypeToLong = IntegerType::getIntegerLong;
127165

128166
/**
129-
* @input realType the {@link LongType} to convert
167+
* NB potentially lossy, so lower priority
168+
*
169+
* @input realType the {@link RealType} to convert
130170
* @output the {@link Long}, converted from {@code realType}
131171
* @implNote op names='engine.convert, convert.int64'
132172
*/
133-
public final Function<LongType, Long> longTypeToLong = LongType::get;
173+
public final Function<T, Long> realTypeToLong = i -> (long) i.getRealDouble();
134174

135175
/**
136-
* @input num the {@link Number} to convert
137-
* @output a {@link DoubleType} containing the information in {@code num}
176+
* @input realType the {@link RealType} to convert
177+
* @output the {@link Long}, converted from {@code realType}
138178
* @implNote op names='engine.convert, convert.float32'
139179
*/
140-
public final Function<FloatType, Float> floatTypeToFloat = FloatType::get;
180+
public final Function<T, Float> realTypeToFloat = RealType::getRealFloat;
141181

142182
/**
143-
* @input num the {@link Number} to convert
144-
* @output a {@link DoubleType} containing the information in {@code num}
183+
* @input realType the {@link RealType} to convert
184+
* @output the {@link Long}, converted from {@code realType}
145185
* @implNote op names='engine.convert, convert.float64'
146186
*/
147-
public final Function<DoubleType, Double> doubleTypeToDouble = DoubleType::get;
187+
public final Function<T, Double> realTypeToDouble = RealType::getRealDouble;
148188
}

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 = 1956;
45+
long expected = 1962;
4646
long actual = ops.infos().size();
4747
assertEquals(expected, actual);
4848
}

scijava-ops-image/src/test/java/org/scijava/ops/image/convert/TestConvertRealTypeNumbers.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@
2828
*/
2929
package org.scijava.ops.image.convert;
3030

31-
import net.imglib2.type.numeric.integer.ByteType;
32-
import net.imglib2.type.numeric.integer.IntType;
33-
import net.imglib2.type.numeric.integer.LongType;
34-
import net.imglib2.type.numeric.integer.ShortType;
31+
import net.imglib2.type.numeric.RealType;
32+
import net.imglib2.type.numeric.integer.*;
3533
import net.imglib2.type.numeric.real.DoubleType;
3634
import net.imglib2.type.numeric.real.FloatType;
3735
import org.junit.jupiter.api.Assertions;
3836
import org.junit.jupiter.api.Test;
3937
import org.scijava.ops.image.AbstractOpTest;
4038

41-
import java.util.function.Function;
42-
4339
public class TestConvertRealTypeNumbers extends AbstractOpTest {
4440

4541
public static final Class<?>[] REAL_TYPES = {
@@ -62,7 +58,6 @@ public class TestConvertRealTypeNumbers extends AbstractOpTest {
6258

6359
@Test
6460
public void testConversion() {
65-
Function<?, ?> f;
6661
for(Class<?> rt: REAL_TYPES){
6762
for (Class<?> n: NUMBERS) {
6863
// rt -> n
@@ -85,4 +80,61 @@ public void testConversion() {
8580
}
8681

8782
}
83+
84+
/**
85+
* Creates a five.
86+
*
87+
* @param input some unused input
88+
* @return five
89+
* @implNote op names="create.five"
90+
*/
91+
public static <T extends RealType<T>> LongType createRealTypeFive(T input) {
92+
return new LongType(5L);
93+
}
94+
95+
/**
96+
* Creates a five.
97+
*
98+
* @param input some unused input
99+
* @return five
100+
* @implNote op names="create.five"
101+
*/
102+
public static <N extends Number> LongType createNumberFive(N input) {
103+
return new LongType(5L);
104+
}
105+
106+
/**
107+
* Test that in practice the converters work
108+
*/
109+
@Test
110+
public void testExecution() {
111+
Object[] numbers = { //
112+
(byte) 5, //
113+
(short) 5, //
114+
5, //
115+
5L, //
116+
5f, //
117+
5d //
118+
};
119+
for (var i: numbers) {
120+
ops.op("create.five").input(i).outType(LongType.class).apply();
121+
}
122+
123+
Object[] realTypes = { //
124+
new ByteType((byte) 5), //
125+
new UnsignedByteType( 5), //
126+
new ShortType((short) 5), //
127+
new UnsignedShortType( 5), //
128+
new IntType(5), //
129+
new UnsignedIntType( 5), //
130+
new LongType(5), //
131+
new UnsignedLongType( 5), //
132+
new FloatType( 5f), //
133+
new DoubleType( 5d), //
134+
};
135+
for (var i: realTypes) {
136+
ops.op("create.five").input(i).outType(LongType.class).apply();
137+
}
138+
}
139+
88140
}

0 commit comments

Comments
 (0)