Skip to content

Commit b35e57b

Browse files
committed
Test Op Adaptation Priority Functionality
We ensure that higher-priority Op Adaptations are attempted before lower-priority Op Adaptations
1 parent 6ed7003 commit b35e57b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2019 SciJava Ops developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops.adapt;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotEquals;
34+
35+
import java.util.function.Function;
36+
37+
import org.junit.Test;
38+
import org.scijava.ops.AbstractTestEnvironment;
39+
import org.scijava.ops.OpField;
40+
import org.scijava.ops.adapt.functional.ComputersToFunctionsViaFunction;
41+
import org.scijava.ops.adapt.functional.ComputersToFunctionsViaSource;
42+
import org.scijava.ops.core.OpCollection;
43+
import org.scijava.ops.core.builder.OpBuilder;
44+
import org.scijava.ops.function.Computers;
45+
import org.scijava.ops.function.Producer;
46+
import org.scijava.param.Parameter;
47+
import org.scijava.plugin.Plugin;
48+
import org.scijava.struct.ItemIO;
49+
50+
/**
51+
* Ensures that higher-priority adapt Ops are used over lower-priority adapt
52+
* Ops. For reference we are using {@link ComputersToFunctionsViaSource} and
53+
* {@link ComputersToFunctionsViaFunction}
54+
*
55+
* @author Gabriel Selzer
56+
*/
57+
@Plugin(type = OpCollection.class)
58+
public class OpAdaptationPriorityTest extends AbstractTestEnvironment {
59+
60+
public static class PriorityThing {
61+
62+
double priority;
63+
64+
public PriorityThing(double priority) {
65+
this.priority = priority;
66+
}
67+
68+
public void increasePriority(double newPriority) {
69+
priority += newPriority;
70+
}
71+
72+
public double getPriority() {
73+
return priority;
74+
}
75+
}
76+
77+
@OpField(names = "test.priorityOp")
78+
@Parameter(key = "input")
79+
@Parameter(key = "output", itemIO = ItemIO.BOTH)
80+
public static final Computers.Arity1<Double, PriorityThing> priorityOp = (in,
81+
out) -> {
82+
out.increasePriority(in);
83+
};
84+
85+
@OpField(names = "create")
86+
@Parameter(key = "output", itemIO = ItemIO.OUTPUT)
87+
public static final Producer<PriorityThing> priorityThingProducer =
88+
() -> new PriorityThing(10000);
89+
90+
@OpField(names = "create")
91+
@Parameter(key = "input")
92+
@Parameter(key = "output", itemIO = ItemIO.OUTPUT)
93+
public static final Function<Double, PriorityThing> priorityThingFunction = (
94+
in) -> new PriorityThing(in);
95+
96+
@Test
97+
public void testPriority() {
98+
PriorityThing pThing = new OpBuilder(ops, "test.priorityOp").input(
99+
new Double(10)).outType(PriorityThing.class).apply();
100+
assertEquals(20, pThing.getPriority(), 0.);
101+
// This would be the value of pThing if it were created using
102+
// PriorityThingProducer
103+
assertNotEquals(10010, pThing.getPriority(), 0.);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)