|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ2 software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2022 ImageJ2 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 net.imagej.ops2.math; |
| 31 | + |
| 32 | +import net.imagej.ops2.AbstractOpTest; |
| 33 | +import net.imglib2.type.numeric.integer.IntType; |
| 34 | +import net.imglib2.type.numeric.real.DoubleType; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.scijava.ops.engine.math.MathOps; |
| 37 | + |
| 38 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests {@link BinaryNumericTypeMath}. |
| 42 | + * |
| 43 | + * @author Mark Hiner |
| 44 | + */ |
| 45 | +public class BinaryRealTypeMathTest extends AbstractOpTest { |
| 46 | + |
| 47 | + private static final DoubleType A = new DoubleType(12.0); |
| 48 | + private static final IntType B = new IntType(7); |
| 49 | + |
| 50 | + // TODO inplace would be nice |
| 51 | + private static String[] methods = new String[]{"function", "computer"}; |
| 52 | + |
| 53 | + // ADD |
| 54 | + @Test |
| 55 | + public void testAdd() { |
| 56 | + DoubleType e = A.copy(); |
| 57 | + e.set(e.get() + B.get()); |
| 58 | + test(MathOps.ADD, e.get()); |
| 59 | + } |
| 60 | + |
| 61 | + // SUB |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testSub() { |
| 65 | + DoubleType e = A.copy(); |
| 66 | + e.set(e.get() - B.get()); |
| 67 | + test(MathOps.SUB, e.get()); |
| 68 | + } |
| 69 | + |
| 70 | + // DIV |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testDiv() { |
| 74 | + DoubleType e = A.copy(); |
| 75 | + e.set(e.get() / B.get()); |
| 76 | + test(MathOps.DIV, e.get()); |
| 77 | + } |
| 78 | + |
| 79 | + // MUL |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testMul() { |
| 83 | + DoubleType e = A.copy(); |
| 84 | + e.set(e.get() * B.get()); |
| 85 | + test(MathOps.MUL, e.get()); |
| 86 | + } |
| 87 | + |
| 88 | + // POW |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testPow() { |
| 92 | + DoubleType e = A.copy(); |
| 93 | + e.set(Math.pow(e.get(), B.get())); |
| 94 | + test(MathOps.POW, e.get()); |
| 95 | + } |
| 96 | + |
| 97 | + // MOD |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testMod() { |
| 101 | + test(MathOps.MOD, A.get() % B.get()); |
| 102 | + } |
| 103 | + |
| 104 | + // OR |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testOr() { |
| 108 | + test("math.or", (int)((long) A.get() | (long) B.get())); |
| 109 | + } |
| 110 | + |
| 111 | + // XOR |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testXor() { |
| 115 | + test("math.xor", (int)((long)A.get() ^ (long)B.get())); |
| 116 | + } |
| 117 | + |
| 118 | + // AND |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testAnd() { |
| 122 | + test("math.and", (int)((long)A.get() & (long)B.get())); |
| 123 | + } |
| 124 | + |
| 125 | + // -- Helpers -- |
| 126 | + |
| 127 | + /** |
| 128 | + * Helper method to test that the given Op name run on {@link #A} and {@link #B} |
| 129 | + * produces the expected value when run using each method type in {@link #methods} |
| 130 | + * |
| 131 | + * @param opNames comma space-separated list of op names to test |
| 132 | + * @param expectedValue Expected value from op invocation |
| 133 | + */ |
| 134 | + private void test(String opNames, double expectedValue) { |
| 135 | + for (String opName : opNames.split(", ")) { |
| 136 | + for (String m : methods) { |
| 137 | + DoubleType c = new DoubleType(); |
| 138 | +// if (m.equals("function")) |
| 139 | +// c = ops.binary(opName).input(A, B).outType(DoubleType.class).apply(); |
| 140 | +// else if (m.equals("computer")) |
| 141 | + ops.binary(opName).input(A, B).output(c).compute(); |
| 142 | + assertEquals(expectedValue, c.get()); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments