|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Operations: a framework for reusable algorithms. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2018 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; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.function.Function; |
| 33 | +import java.util.stream.IntStream; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertTrue; |
| 36 | + |
| 37 | +import org.junit.Test; |
| 38 | +import org.scijava.param.ValidityException; |
| 39 | +import org.scijava.ops.types.Nil; |
| 40 | + |
| 41 | +public class OptionalParametersTest extends AbstractTestEnvironment { |
| 42 | + |
| 43 | + Nil<Double> nilDouble = new Nil<Double>() { |
| 44 | + }; |
| 45 | + |
| 46 | + Nil<double[]> nilDoubleArray = new Nil<double[]>() { |
| 47 | + }; |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testSecondaryArgs() throws ValidityException { |
| 51 | + double min = 1.0; |
| 52 | + double max = 2.9; |
| 53 | + |
| 54 | + // Min and max set |
| 55 | + Function<double[], double[]> normFunction = ops().findOp( // |
| 56 | + "minmax", new Nil<Function<double[], double[]>>() { |
| 57 | + }, // |
| 58 | + new Nil[] { nilDoubleArray, nilDouble, nilDouble }, // |
| 59 | + nilDoubleArray, |
| 60 | + min, max// |
| 61 | + ); |
| 62 | + |
| 63 | + // Max is not required, one arg is give for required min, max will be default 1.0 |
| 64 | + double[] nums = IntStream.range(0, 10).mapToDouble(i -> i).toArray(); |
| 65 | + double[] res = normFunction.apply(nums); |
| 66 | + assertTrue(Math.abs(max - Arrays.stream(res).max().getAsDouble()) < 0.000001); |
| 67 | + assertTrue(Math.abs(min - Arrays.stream(res).min().getAsDouble()) < 0.000001); |
| 68 | + |
| 69 | + normFunction = ops().findOp( // |
| 70 | + "minmax", new Nil<Function<double[], double[]>>() { |
| 71 | + }, // |
| 72 | + new Nil[] { nilDoubleArray, nilDouble, nilDouble }, // |
| 73 | + nilDoubleArray, |
| 74 | + 0.5// |
| 75 | + ); |
| 76 | + |
| 77 | + res = normFunction.apply(nums); |
| 78 | + assertTrue(Math.abs(1.0 - Arrays.stream(res).max().getAsDouble()) < 0.000001); |
| 79 | + assertTrue(Math.abs(0.5 - Arrays.stream(res).min().getAsDouble()) < 0.000001); |
| 80 | + } |
| 81 | + |
| 82 | + @Test(expected = IllegalStateException.class) |
| 83 | + public void testRequiredMissing() throws ValidityException { |
| 84 | + // One required, but none are given |
| 85 | + ops().findOp( // |
| 86 | + "minmax", new Nil<Function<double[], double[]>>() { |
| 87 | + }, // |
| 88 | + new Nil[] { nilDoubleArray, nilDouble, nilDouble }, // |
| 89 | + nilDoubleArray |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expected = IllegalStateException.class) |
| 94 | + public void testTooMannyArgs() throws ValidityException { |
| 95 | + // One required, one optional, but three given |
| 96 | + ops().findOp( // |
| 97 | + "minmax", new Nil<Function<double[], double[]>>() { |
| 98 | + }, // |
| 99 | + new Nil[] { nilDoubleArray, nilDouble, nilDouble }, // |
| 100 | + nilDoubleArray, |
| 101 | + 1,2,3 |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments