Skip to content

Commit f3fc619

Browse files
gselzerctrueden
authored andcommitted
Create TestOps for independent test ops
1 parent 743adf7 commit f3fc619

File tree

5 files changed

+171
-82
lines changed

5 files changed

+171
-82
lines changed

src/test/java/org/scijava/ops/AdaptersTest.java

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
package org.scijava.ops;
3131

3232
import java.util.function.BiFunction;
33-
import java.util.function.Function;
3433

3534
import org.junit.Test;
36-
import org.scijava.ops.core.OneToOneCommand;
3735
import org.scijava.ops.core.computer.BiComputer;
38-
import org.scijava.ops.core.computer.Computer;
3936
import org.scijava.ops.types.Nil;
4037
import org.scijava.ops.util.Adapt;
4138

@@ -47,39 +44,10 @@ public class AdaptersTest extends AbstractTestEnvironment {
4744
private static Nil<double[]> nilDoubleArray = new Nil<double[]>() {
4845
};
4946

50-
@Test
51-
public void testFunctionAsCommand() {
52-
Function<Double, Double> sqrtFunction = ops().findOp( //
53-
"math.sqrt", new Nil<Function<Double, Double>>() {
54-
}, //
55-
new Nil[] { nilDouble }, //
56-
nilDouble//
57-
);
58-
59-
OneToOneCommand<Double, Double> sqrtCommand = Adapt.Functions.asCommand(sqrtFunction, 25.0);
60-
sqrtCommand.run();
61-
assert sqrtCommand.get().equals(5.0);
62-
}
63-
64-
@Test
65-
public void testComputerAsCommand() {
66-
Computer<double[], double[]> sqrtComputer = ops().findOp( //
67-
"math.sqrt", new Nil<Computer<double[], double[]>>() {
68-
}, //
69-
new Nil[] { nilDoubleArray, nilDoubleArray }, //
70-
nilDoubleArray//
71-
);
72-
73-
OneToOneCommand<double[], double[]> sqrtCommand = Adapt.Computers.asCommand(sqrtComputer,
74-
new double[] { 25, 100, 4 }, new double[3]);
75-
sqrtCommand.run();
76-
assert arrayEquals(sqrtCommand.get(), 5.0, 10.0, 2.0);
77-
}
78-
7947
@Test
8048
public void testComputerAsFunction() {
8149
final BiComputer<double[], double[], double[]> computer = ops().findOp( //
82-
"math.add", new Nil<BiComputer<double[], double[], double[]>>() {
50+
"test.adapters", new Nil<BiComputer<double[], double[], double[]>>() {
8351
}, //
8452
new Nil[] { nilDoubleArray, nilDoubleArray, nilDoubleArray }, //
8553
nilDoubleArray//
@@ -100,7 +68,7 @@ public void testComputerAsFunction() {
10068
public void testFunctionAsComputer() {
10169
// look up a function: Double result = math.add(Double v1, Double v2)
10270
BiFunction<double[], double[], double[]> function = ops().findOp( //
103-
"math.add", new Nil<BiFunction<double[], double[], double[]>>() {
71+
"test.adapters", new Nil<BiFunction<double[], double[], double[]>>() {
10472
}, //
10573
new Nil[] { nilDoubleArray, nilDoubleArray }, //
10674
nilDoubleArray//

src/test/java/org/scijava/ops/AutoTransformTest.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,20 @@ public class AutoTransformTest extends AbstractTestEnvironment {
4646

4747
@Test
4848
public void autoLiftFunctionToIterables() {
49-
// There is no sqrt function for iterables in the system, however we can auto transform
49+
// There is no sqrt function for iterables in the system, however we can auto
50+
// transform
5051
// as there is a lifter for Functions to iterables
5152
Function<Iterable<Double>, Iterable<Double>> sqrtFunction = ops().findOp( //
5253
"math.sqrt", new Nil<Function<Iterable<Double>, Iterable<Double>>>() {
5354
}, //
5455
new Nil[] { nilIterableDouble }, //
5556
nilIterableDouble//
5657
);
57-
58+
5859
Iterable<Double> res = sqrtFunction.apply(Arrays.asList(0.0, 4.0, 16.0));
5960
arrayEquals(Streams.stream(res).mapToDouble(d -> d).toArray(), 0.0, 2.0, 4.0);
6061
}
61-
62+
6263
@Test
6364
public void autoFunctionToComputer() {
6465
Function<double[], double[]> sqrtArrayFunction = ops().findOp( //
@@ -67,10 +68,10 @@ public void autoFunctionToComputer() {
6768
new Nil[] { Nil.of(double[].class) }, //
6869
Nil.of(double[].class)//
6970
);
70-
double[] res = sqrtArrayFunction.apply(new double[]{4.0, 16.0});
71+
double[] res = sqrtArrayFunction.apply(new double[] { 4.0, 16.0 });
7172
arrayEquals(res, 2.0, 4.0);
7273
}
73-
74+
7475
@Test
7576
public void autoBiFunctionToBiComputer() {
7677
BiFunction<double[], double[], double[]> addArrayFunction = ops().findOp( //
@@ -79,52 +80,52 @@ public void autoBiFunctionToBiComputer() {
7980
new Nil[] { Nil.of(double[].class), Nil.of(double[].class) }, //
8081
Nil.of(double[].class)//
8182
);
82-
double[] res = addArrayFunction.apply(new double[]{4.0, 16.0}, new double[]{4.0, 16.0});
83+
double[] res = addArrayFunction.apply(new double[] { 4.0, 16.0 }, new double[] { 4.0, 16.0 });
8384
arrayEquals(res, 8.0, 32.0);
8485
}
85-
86+
8687
@Test
8788
public void autoLiftFuncToArray() {
8889
Function<Double[], Double[]> power3ArraysFunc = ops().findOp( //
89-
"math.pow", new Nil<Function<Double[], Double[]>>() {
90+
"test.secondaryInputsFunction", new Nil<Function<Double[], Double[]>>() {
9091
}, //
9192
new Nil[] { Nil.of(Double[].class), Nil.of(double.class) }, //
9293
Nil.of(Double[].class), //
9394
3.0//
9495
);
95-
96+
9697
Double[] result = power3ArraysFunc.apply(new Double[] { 1.0, 2.0, 3.0 });
9798
assert arrayEquals(Arrays.stream(result).mapToDouble(d -> d).toArray(), 1.0, 8.0, 27.0);
9899
}
99-
100+
100101
@Test
101102
public void autoTransformWithSecondaryArgs() {
102103
Computer<Double[], Double[]> power3Arrays = ops().findOp( //
103-
"math.pow", new Nil<Computer<Double[], Double[]>>() {
104+
"test.secondaryInputsFunction", new Nil<Computer<Double[], Double[]>>() {
104105
}, //
105106
new Nil[] { Nil.of(Double[].class), Nil.of(Double[].class), Nil.of(double.class) }, //
106107
Nil.of(Double[].class), //
107108
3.0//
108109
);
109-
110+
110111
Double[] result = new Double[3];
111112
power3Arrays.compute(new Double[] { 1.0, 2.0, 3.0 }, result);
112113
assert arrayEquals(Arrays.stream(result).mapToDouble(d -> d).toArray(), 1.0, 8.0, 27.0);
113114
}
114-
115+
115116
@Test
116117
public void autoCompToFuncAndLift() {
117118
Nil<List<double[]>> n = new Nil<List<double[]>>() {
118119
};
119-
120+
120121
Function<List<double[]>, List<double[]>> sqrtListFunction = ops().findOp( //
121-
"math.sqrt", new Nil<Function<List<double[]>, List<double[]>>>() {
122+
"test.liftSqrt", new Nil<Function<List<double[]>, List<double[]>>>() {
122123
}, //
123124
new Nil[] { n }, //
124125
n//
125126
);
126-
127-
List<double[]> res = sqrtListFunction.apply(Arrays.asList(new double[]{4.0}, new double[]{4.0, 25.0}));
127+
128+
List<double[]> res = sqrtListFunction.apply(Arrays.asList(new double[] { 4.0 }, new double[] { 4.0, 25.0 }));
128129
double[] resArray = res.stream().flatMapToDouble(ds -> Arrays.stream(ds)).toArray();
129130
arrayEquals(resArray, 2.0, 2.0, 5.0);
130131
}

src/test/java/org/scijava/ops/LiftTest.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,64 +29,59 @@
2929

3030
package org.scijava.ops;
3131

32+
import com.google.common.collect.Streams;
33+
3234
import java.util.ArrayList;
3335
import java.util.Arrays;
3436
import java.util.List;
3537
import java.util.function.Function;
3638

39+
import org.junit.Assert;
3740
import org.junit.Test;
3841
import org.scijava.ops.core.computer.Computer;
39-
import org.scijava.ops.util.Maps;
40-
import org.scijava.param.ValidityException;
4142
import org.scijava.ops.types.Nil;
42-
43-
import com.google.common.collect.Streams;
43+
import org.scijava.ops.util.Computers;
44+
import org.scijava.ops.util.Functions;
45+
import org.scijava.ops.util.Maps;
4446

4547
public class LiftTest extends AbstractTestEnvironment {
4648

47-
Nil<Double> nilDouble = new Nil<Double>() {};
49+
Nil<Double> nilDouble = new Nil<Double>() {
50+
};
4851

49-
Nil<double[]> nilDoubleArray = new Nil<double[]>() {};
52+
Nil<double[]> nilDoubleArray = new Nil<double[]>() {
53+
};
5054

5155
@Test
52-
public void testliftFunction() throws ValidityException {
53-
Function<Double, Double> powFunction = ops().findOp( //
54-
"math.pow", new Nil<Function<Double, Double>>() {
55-
}, //
56-
new Nil[] { nilDouble, nilDouble }, //
57-
nilDouble, 2.0//
58-
);
56+
public void testliftFunction(){
57+
Function<Double, Double> powFunction = Functions.unary(ops(), "test.liftFunction", nilDouble, nilDouble);
5958

6059
Function<Iterable<Double>, Iterable<Double>> liftedToIterable = Maps.Functions.Iterables.liftBoth(powFunction);
6160
Iterable<Double> res2 = liftedToIterable.apply(Arrays.asList(1.0, 2.0, 3.0, 4.0));
62-
arrayEquals(toArray(res2), 1.0, 4.0, 9.0, 16.0);
63-
61+
Assert.assertTrue(arrayEquals(toArray(res2), 2.0, 3.0, 4.0, 5.0));
62+
6463
Function<Double[], Double[]> liftedToArray = Maps.Functions.Arrays.liftBoth(powFunction, Double.class);
65-
Double[] res3 = liftedToArray.apply(new Double[]{1.0, 2.0, 3.0, 4.0});
66-
arrayEquals(Arrays.stream(res3).mapToDouble(d -> d).toArray(), 1.0, 4.0, 9.0, 16.0);
64+
Double[] res3 = liftedToArray.apply(new Double[] { 1.0, 2.0, 3.0, 4.0 });
65+
Assert.assertTrue(arrayEquals(Arrays.stream(res3).mapToDouble(d -> d).toArray(), 2.0, 3.0, 4.0, 5.0));
6766
}
6867

6968
private static double[] toArray(Iterable<Double> iter) {
7069
return Streams.stream(iter).mapToDouble(d -> d).toArray();
7170
}
7271

7372
@Test
74-
public void testliftComputer() throws ValidityException {
73+
public void testliftComputer() {
7574

76-
Computer<double[], double[]> powComputer = ops().findOp( //
77-
"math.pow", new Nil<Computer<double[], double[]>>() {
78-
}, //
79-
new Nil[] { nilDoubleArray, nilDoubleArray, nilDouble }, //
80-
nilDoubleArray, 2.0//
81-
);
75+
Computer<double[], double[]> powComputer = Computers.unary(ops(), "test.liftComputer", nilDoubleArray, nilDoubleArray);
8276

83-
Computer<Iterable<double[]>, Iterable<double[]>> liftedToIterable = Maps.Computers.Iterables.liftBoth(powComputer);
77+
Computer<Iterable<double[]>, Iterable<double[]>> liftedToIterable = Maps.Computers.Iterables
78+
.liftBoth(powComputer);
8479
Iterable<double[]> res = wrap(new double[4]);
85-
liftedToIterable.compute(wrap(new double[]{1.0, 2.0, 3.0, 4.0}), res);
86-
87-
arrayEquals(unwrap(res), 1.0, 4.0, 9.0, 16.0);
80+
liftedToIterable.compute(wrap(new double[] { 1.0, 2.0, 3.0, 4.0 }), res);
81+
82+
Assert.assertTrue(arrayEquals(unwrap(res), 2.0, 3.0, 4.0, 5.0));
8883
}
89-
84+
9085
private static double[] unwrap(Iterable<double[]> ds) {
9186
List<Double> wraps = new ArrayList<>();
9287
for (double[] d : ds) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void binaryInplace() {
147147
@Test
148148
public void testSecondaryInputs() {
149149
Function<Double, Double> power3 = ops().findOp( //
150-
"math.pow", new Nil<Function<Double, Double>>() {
150+
"test.secondaryInputsFunction", new Nil<Function<Double, Double>>() {
151151
}, //
152152
new Nil[] { nilDouble, nilDouble }, //
153153
nilDouble, //
@@ -164,7 +164,7 @@ public void testSecondaryInputs() {
164164
assert powerFunction.apply(2.0, 3.0).equals(8.0);
165165

166166
Computer<double[], double[]> power3Arrays = ops().findOp( //
167-
"math.pow", new Nil<Computer<double[], double[]>>() {
167+
"test.secondaryInputsComputer", new Nil<Computer<double[], double[]>>() {
168168
}, //
169169
new Nil[] { nilDoubleArray, nilDoubleArray, nilDouble }, //
170170
nilDoubleArray, //
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package org.scijava.ops;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
6+
import org.scijava.ops.core.Op;
7+
import org.scijava.ops.core.computer.BiComputer;
8+
import org.scijava.ops.core.computer.Computer;
9+
import org.scijava.param.Parameter;
10+
import org.scijava.plugin.Plugin;
11+
import org.scijava.struct.ItemIO;
12+
13+
/**
14+
* This class contains various ops used in various tests used to check framework
15+
* functionality. These Ops SHOULD NEVER be changed or used outside of the tests
16+
* that rely on them, however all should feel free to add more tests to this
17+
* class as needed.
18+
*
19+
* @author Gabriel Selzer
20+
*
21+
*/
22+
public class TestOps {
23+
24+
// -- Op Classes -- //
25+
26+
// OpsTest
27+
28+
@Plugin(type = Op.class, name = "test.secondaryInputsFunction")
29+
@Parameter(key = "number")
30+
@Parameter(key = "result", type = ItemIO.OUTPUT)
31+
public static class MathPowerDoublConstantFunction implements Function<Double, Double> {
32+
33+
@Parameter(type = ItemIO.INPUT)
34+
private double exponent;
35+
36+
@Override
37+
public Double apply(Double t) {
38+
return Math.pow(t, exponent);
39+
}
40+
}
41+
42+
@Plugin(type = Op.class, name = "test.secondaryInputsComputer")
43+
@Parameter(key = "array")
44+
@Parameter(key = "resultArray", type = ItemIO.BOTH)
45+
public static class MathPointwisePowerDoubleArrayComputer implements Computer<double[], double[]> {
46+
47+
@Parameter(type = ItemIO.INPUT)
48+
private Double exponent;
49+
50+
@Override
51+
public void compute(double[] in1, double[] out) {
52+
for (int i = 0; i < out.length; i++) {
53+
out[i] = Math.pow(in1[i], exponent);
54+
}
55+
}
56+
}
57+
58+
// AutoTransformTest
59+
60+
@Plugin(type = Op.class, name = "test.liftSqrt")
61+
@Parameter(key = "in")
62+
@Parameter(key = "out", type = ItemIO.BOTH)
63+
public static class LiftSqrt implements Computer<double[], double[]> {
64+
65+
@Override
66+
public void compute(double[] in, double[] out) {
67+
for (int i = 0; i < in.length; i++) {
68+
out[i] = Math.sqrt(in[i]);
69+
}
70+
}
71+
}
72+
73+
// AdaptersTest
74+
75+
@Plugin(type = Op.class, name = "test.adapters")
76+
@Parameter(key = "arr1")
77+
@Parameter(key = "arr2")
78+
@Parameter(key = "arrout", type = ItemIO.BOTH)
79+
public static class testAddTwoArraysComputer implements BiComputer<double[], double[], double[]> {
80+
@Override
81+
public void compute(double[] arr1, double[] arr2, double[] out) {
82+
for (int i = 0; i < out.length; i++)
83+
out[i] = arr1[i] + arr2[i];
84+
}
85+
}
86+
87+
@Plugin(type = Op.class, name = "test.adapters")
88+
@Parameter(key = "arr1")
89+
@Parameter(key = "arr2")
90+
@Parameter(key = "arrout", type = ItemIO.OUTPUT)
91+
public static class testAddTwoArraysFunction implements BiFunction<double[], double[], double[]> {
92+
@Override
93+
public double[] apply(double[] arr1, double[] arr2) {
94+
double[] out = new double[arr1.length];
95+
for (int i = 0; i < out.length; i++)
96+
out[i] = arr1[i] + arr2[i];
97+
return out;
98+
}
99+
}
100+
101+
// LiftTest
102+
103+
@Plugin(type = Op.class, name = "test.liftFunction")
104+
@Parameter(key = "input")
105+
@Parameter(key = "output", type = ItemIO.OUTPUT)
106+
public static class liftFunction implements Function<Double, Double> {
107+
@Override
108+
public Double apply(Double in) {
109+
return in + 1;
110+
}
111+
}
112+
113+
@Plugin(type = Op.class, name = "test.liftComputer")
114+
@Parameter(key = "input")
115+
@Parameter(key = "output", type = ItemIO.BOTH)
116+
public static class liftComputer implements Computer<double[], double[]> {
117+
@Override
118+
public void compute(double[] in, double[] out) {
119+
for (int i = 0; i < in.length; i++)
120+
out[i] = in[i] + 1;
121+
}
122+
}
123+
124+
// -- TODO: OpDependencies -- //
125+
}

0 commit comments

Comments
 (0)