Skip to content

Commit 6a856ff

Browse files
Treiblesschorlegselzer
authored andcommitted
Add op matcher tests
1 parent 6168f62 commit 6a856ff

1 file changed

Lines changed: 83 additions & 48 deletions

File tree

‎src/test/java/org/scijava/ops/OpsTest.java‎

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,122 +43,159 @@
4343
import org.scijava.types.Nil;
4444

4545
public class OpsTest extends AbstractTestEnvironment {
46-
47-
46+
4847
@Test
48+
@SuppressWarnings("unchecked")
4949
public void unaryFunction() {
50+
// Look up a function type safe
5051
Class<Double> c = Double.class;
5152
Function<Double, Double> sqrtFunction = ops().findOp( //
52-
MathSqrtOp.class,
53-
new Nil<Function<Double, Double>>() {
53+
MathSqrtOp.class, new Nil<Function<Double, Double>>() {
5454
}, //
5555
new Type[] { c }, //
5656
c//
5757
);
58-
// execute the function
5958
double answer = sqrtFunction.apply(16.0);
6059
assert 4.0 == answer;
60+
61+
// Look up a function unsafe
62+
MathSqrtOp sqrtOp = ops().findOp(MathSqrtOp.class, new Type[] { c }, c);
63+
sqrtFunction = (Function<Double, Double>) sqrtOp;
64+
assert 4.0 == sqrtFunction.apply(16.0);
6165
}
62-
66+
6367
@Test
68+
@SuppressWarnings("unchecked")
6469
public void binaryFunction() {
6570
Class<Double> c = Double.class;
6671
// look up a function: Double result = math.add(Double v1, Double v2)
67-
BiFunction<Double, Double, Double> function = ops().findOp( //
68-
MathAddOp.class,
69-
new Nil<BiFunction<Double, Double, Double>>() {
72+
BiFunction<Double, Double, Double> addFunction = ops().findOp( //
73+
MathAddOp.class, new Nil<BiFunction<Double, Double, Double>>() {
7074
}, //
7175
new Type[] { c, c }, //
7276
c//
7377
);
74-
// execute the function
75-
double answer = function.apply(1.0, 2.0);
76-
assert 3.0 == answer;
78+
assert 3.0 == addFunction.apply(1.0, 2.0);
79+
80+
// Look up a function unsafe
81+
MathAddOp addOp = ops().findOp(MathAddOp.class, new Type[] { c, c }, c);
82+
addFunction = (BiFunction<Double, Double, Double>) addOp;
83+
assert 86.0 == addFunction.apply(10.0, 76.0);
7784

7885
// look up a specific implementation
79-
function = ops().findOp( //
80-
MathAddDoublesFunction.class,
81-
new Nil<BiFunction<Double, Double, Double>>() {
86+
addFunction = ops().findOp( //
87+
MathAddDoublesFunction.class, new Nil<BiFunction<Double, Double, Double>>() {
8288
}, //
8389
new Type[] { c, c }, //
8490
c//
8591
);
86-
answer = function.apply(10.0, 76.0);
87-
assert 86.0 == answer;
92+
assert 86.0 == addFunction.apply(10.0, 76.0);
93+
94+
// Look up a function unsafe
95+
MathAddDoublesFunction addDoublesOp = ops().findOp(MathAddDoublesFunction.class, new Type[] { c, c }, c);
96+
addFunction = (BiFunction<Double, Double, Double>) addDoublesOp;
97+
assert 86.0 == addFunction.apply(10.0, 76.0);
8898
}
89-
99+
90100
@Test
101+
@SuppressWarnings("unchecked")
91102
public void unaryComputer() {
92103
Class<double[]> cArray = double[].class;
93104
Computer<double[], double[]> sqrtComputer = ops().findOp( //
94-
MathSqrtOp.class,
95-
new Nil<Computer<double[], double[]>>() {
105+
MathSqrtOp.class, new Nil<Computer<double[], double[]>>() {
96106
}, //
97107
new Type[] { cArray, cArray }, //
98108
cArray//
99109
);
100-
// execute the computer
101110
double[] result = new double[2];
102111
sqrtComputer.compute(new double[] { 16.0, 81.0 }, result);
103112
assert arrayEquals(result, 4.0, 9.0);
113+
114+
MathSqrtOp sqrtOp = ops().findOp(MathSqrtOp.class, new Type[] { cArray, cArray }, cArray);
115+
sqrtComputer = (Computer<double[], double[]>) sqrtOp;
116+
result = new double[2];
117+
sqrtComputer.compute(new double[] { 16.0, 81.0 }, result);
118+
assert arrayEquals(result, 4.0, 9.0);
104119
}
105-
120+
106121
@Test
122+
@SuppressWarnings("unchecked")
107123
public void binaryComputer() {
108-
// look up a computer: math.add(BOTH double[] result, double[] v1,
109-
// double[] v2)
110124
Class<double[]> cArray = double[].class;
111-
final BiComputer<double[], double[], double[]> computer = ops().findOp( //
112-
MathAddOp.class,
113-
new Nil<BiComputer<double[], double[], double[]>>() {
125+
BiComputer<double[], double[], double[]> computer = ops().findOp( //
126+
MathAddOp.class, new Nil<BiComputer<double[], double[], double[]>>() {
114127
}, //
115128
new Type[] { cArray, cArray, cArray }, //
116129
cArray//
117130
);
118-
final double[] a1 = { 3, 5, 7 };
119-
final double[] a2 = { 2, 4, 9 };
120-
final double[] result = new double[a2.length];
131+
double[] a1 = { 3, 5, 7 };
132+
double[] a2 = { 2, 4, 9 };
133+
double[] result = new double[a2.length];
134+
computer.compute(a1, a2, result);
135+
assert arrayEquals(result, 5.0, 9.0, 16.0);
136+
137+
MathAddOp addOp = ops().findOp(MathAddOp.class, new Type[] { cArray, cArray, cArray }, //
138+
cArray);
139+
computer = (BiComputer<double[], double[], double[]>) addOp;
140+
a1 = new double[] { 3, 5, 7 };
141+
a2 = new double[] { 2, 4, 9 };
142+
result = new double[a2.length];
121143
computer.compute(a1, a2, result);
122144
assert arrayEquals(result, 5.0, 9.0, 16.0);
123145
}
124-
146+
125147
@Test
148+
@SuppressWarnings("unchecked")
126149
public void unaryInplace() {
127150
Class<double[]> cArray = double[].class;
128-
final Inplace<double[]> inplaceSqrt = ops().findOp( //
129-
MathSqrtOp.class,
130-
new Nil<Inplace<double[]>>() {
151+
Inplace<double[]> inplaceSqrt = ops().findOp( //
152+
MathSqrtOp.class, new Nil<Inplace<double[]>>() {
131153
}, //
132154
new Type[] { cArray }, //
133155
cArray//
134156
);
135-
final double[] a1 = { 4, 100, 36 };
157+
double[] a1 = { 4, 100, 36 };
158+
inplaceSqrt.mutate(a1);
159+
assert arrayEquals(a1, 2.0, 10.0, 6.0);
160+
161+
MathSqrtOp sqrtOp = ops().findOp(MathSqrtOp.class, new Type[] { cArray }, //
162+
cArray);
163+
inplaceSqrt = (Inplace<double[]>) sqrtOp;
164+
a1 = new double[] { 4, 100, 36 };
136165
inplaceSqrt.mutate(a1);
137166
assert arrayEquals(a1, 2.0, 10.0, 6.0);
138167
}
139-
168+
140169
@Test
170+
@SuppressWarnings("unchecked")
141171
public void binaryInplace() {
142172
Class<double[]> cArray = double[].class;
143-
final BiInplace1<double[], double[]> inplaceAdd = ops().findOp( //
144-
MathAddOp.class,
145-
new Nil<BiInplace1<double[], double[]>>() {
173+
BiInplace1<double[], double[]> inplaceAdd = ops().findOp( //
174+
MathAddOp.class, new Nil<BiInplace1<double[], double[]>>() {
146175
}, //
147176
new Type[] { cArray, cArray }, //
148177
cArray//
149178
);
150-
final double[] a1 = { 3, 5, 7 };
151-
final double[] a2 = { 2, 4, 9 };
179+
double[] a1 = { 3, 5, 7 };
180+
double[] a2 = { 2, 4, 9 };
152181
inplaceAdd.mutate(a1, a2);
153182
assert arrayEquals(a1, 5.0, 9.0, 16.0);
183+
184+
MathAddOp addOp = ops().findOp(MathAddOp.class, new Type[] { cArray, cArray }, //
185+
cArray);
186+
try {
187+
// will fail as 'addOp' will be matched to a function
188+
inplaceAdd = (BiInplace1<double[], double[]>) addOp;
189+
} catch (ClassCastException e) {
190+
// expected as the add double arrays function has a higher priority than the inplace
191+
}
154192
}
155-
193+
156194
@Test
157195
public void testSecondaryInputs() {
158196
Class<Double> c = Double.class;
159197
StructInstance<Function<Double, Double>> powerConstantFunctionStructInstance = ops().findOpInstance( //
160-
MathPowerOp.class,
161-
new Nil<Function<Double, Double>>() {
198+
MathPowerOp.class, new Nil<Function<Double, Double>>() {
162199
}, //
163200
new Type[] { c, c }, //
164201
c//
@@ -168,8 +205,7 @@ public void testSecondaryInputs() {
168205
assert power3.apply(2.0).equals(8.0);
169206

170207
BiFunction<Double, Double, Double> powerFunction = ops().findOp( //
171-
MathPowerOp.class,
172-
new Nil<BiFunction<Double, Double, Double>>() {
208+
MathPowerOp.class, new Nil<BiFunction<Double, Double, Double>>() {
173209
}, //
174210
new Type[] { c, c }, //
175211
c//
@@ -178,8 +214,7 @@ public void testSecondaryInputs() {
178214

179215
Class<double[]> cArray = double[].class;
180216
StructInstance<Computer<double[], double[]>> powerConstantComputerStructInstance = ops().findOpInstance( //
181-
MathPowerOp.class,
182-
new Nil<Computer<double[], double[]>>() {
217+
MathPowerOp.class, new Nil<Computer<double[], double[]>>() {
183218
}, //
184219
new Type[] { cArray, cArray, c }, //
185220
cArray//

0 commit comments

Comments
 (0)