|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Java implementation of the SciJava Ops matching engine. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2016 - 2025 SciJava 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.engine.reduce; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | + |
| 34 | +import java.util.function.BiFunction; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.scijava.ops.engine.AbstractTestEnvironment; |
| 39 | +import org.scijava.ops.engine.conversionLoss.impl.IdentityLossReporter; |
| 40 | +import org.scijava.ops.engine.conversionLoss.impl.PrimitiveLossReporters; |
| 41 | +import org.scijava.ops.engine.copy.CopyOpCollection; |
| 42 | +import org.scijava.ops.engine.matcher.convert.IdentityCollection; |
| 43 | +import org.scijava.ops.engine.matcher.convert.PrimitiveConverters; |
| 44 | +import org.scijava.ops.engine.matcher.convert.UtilityConverters; |
| 45 | +import org.scijava.ops.engine.matcher.impl.LossReporterWrapper; |
| 46 | +import org.scijava.ops.spi.Nullable; |
| 47 | +import org.scijava.ops.spi.OpCollection; |
| 48 | +import org.scijava.ops.spi.OpMethod; |
| 49 | + |
| 50 | +/** |
| 51 | + * Tests ops with a multi-dimensional array parameter, whose reduced and |
| 52 | + * converted wrappers need a generated class name derived from the parameter |
| 53 | + * types ({@code double[][]} must give a valid identifier such as |
| 54 | + * {@code double_Arr_Arr}). |
| 55 | + * |
| 56 | + * @author Curtis Rueden |
| 57 | + * @author Claude Opus 5.5 |
| 58 | + */ |
| 59 | +public class NestedArrayParameterTest extends AbstractTestEnvironment |
| 60 | + implements OpCollection |
| 61 | +{ |
| 62 | + |
| 63 | + @BeforeAll |
| 64 | + public static void addNeededOps() { |
| 65 | + ops.register( // |
| 66 | + new CopyOpCollection<>(), // |
| 67 | + new IdentityCollection<>(), // |
| 68 | + new IdentityLossReporter<>(), // |
| 69 | + new LossReporterWrapper<>(), // |
| 70 | + new PrimitiveConverters<>(), // |
| 71 | + new PrimitiveLossReporters(), // |
| 72 | + new UtilityConverters(), // |
| 73 | + new NestedArrayParameterTest() // |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @OpMethod(names = "test.nestedArraySum", type = BiFunction.class) |
| 78 | + public static Double nestedArraySum(final double[][] values, |
| 79 | + @Nullable final Double scale) |
| 80 | + { |
| 81 | + double sum = 0; |
| 82 | + for (final double[] row : values) |
| 83 | + for (final double v : row) |
| 84 | + sum += v; |
| 85 | + return sum * (scale == null ? 1 : scale); |
| 86 | + } |
| 87 | + |
| 88 | + @OpMethod(names = "test.nestedArrayCount", type = BiFunction.class) |
| 89 | + public static Double nestedArrayCount(final double[][][] values, |
| 90 | + @Nullable final Double scale) |
| 91 | + { |
| 92 | + long count = 0; |
| 93 | + for (final double[][] plane : values) |
| 94 | + for (final double[] row : plane) |
| 95 | + count += row.length; |
| 96 | + return count * (scale == null ? 1 : scale); |
| 97 | + } |
| 98 | + |
| 99 | + private static final double[][] VALUES = { { 1, 2 }, { 3, 4 } }; |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testAllArguments() { |
| 103 | + final Double sum = ops.op("test.nestedArraySum").input(VALUES, 2.0) |
| 104 | + .outType(Double.class).apply(); |
| 105 | + assertEquals(20, sum, 0); |
| 106 | + } |
| 107 | + |
| 108 | + /** Omitting the optional parameter goes through a reduced wrapper. */ |
| 109 | + @Test |
| 110 | + public void testOptionalOmitted() { |
| 111 | + final Double sum = ops.op("test.nestedArraySum").input(VALUES).outType( |
| 112 | + Double.class).apply(); |
| 113 | + assertEquals(10, sum, 0); |
| 114 | + final Double count = ops.op("test.nestedArrayCount").input( |
| 115 | + new double[2][3][4]).outType(Double.class).apply(); |
| 116 | + assertEquals(24, count, 0); |
| 117 | + } |
| 118 | + |
| 119 | + /** An Integer scale goes through a converted wrapper. */ |
| 120 | + @Test |
| 121 | + public void testConverted() { |
| 122 | + final Double sum = ops.op("test.nestedArraySum").input(VALUES, 3) |
| 123 | + .outType(Double.class).apply(); |
| 124 | + assertEquals(30, sum, 0); |
| 125 | + final Integer rounded = ops.op("test.nestedArraySum").input(VALUES) |
| 126 | + .outType(Integer.class).apply(); |
| 127 | + assertEquals(10, rounded); |
| 128 | + } |
| 129 | +} |
0 commit comments