|
| 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.util; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +import java.lang.reflect.Array; |
| 36 | +import java.lang.reflect.Type; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.List; |
| 39 | +import java.util.function.Consumer; |
| 40 | +import java.util.function.Function; |
| 41 | +import java.util.function.Supplier; |
| 42 | + |
| 43 | +import org.junit.Test; |
| 44 | +import org.scijava.ops.core.Computer; |
| 45 | +import org.scijava.ops.transform.TypeModUtils; |
| 46 | +import org.scijava.ops.types.Nil; |
| 47 | + |
| 48 | +public class TypeModUtilsTest { |
| 49 | + |
| 50 | + @Test |
| 51 | + public <A, B, C> void replaceRaw() { |
| 52 | + Nil<Iterable<A>> n1 = new Nil<Iterable<A>>() { |
| 53 | + }; |
| 54 | + Nil<Function<Double, String>> n2 = new Nil<Function<Double, String>>() { |
| 55 | + }; |
| 56 | + Nil<A> n3 = new Nil<A>() { |
| 57 | + }; |
| 58 | + Nil<B[]> n4 = new Nil<B[]>() { |
| 59 | + }; |
| 60 | + Nil<Supplier<? extends Number>> n5 = new Nil<Supplier<? extends Number>>() { |
| 61 | + }; |
| 62 | + Nil<Consumer<C>> n6 = new Nil<Consumer<C>>() { |
| 63 | + }; |
| 64 | + |
| 65 | + Type[] types = new Type[] { Double.class, String.class, Iterable.class, String.class }; |
| 66 | + assertTrue(TypeModUtils.replaceRawTypes(types, String.class, Double.class)); |
| 67 | + assertTrue(Arrays.deepEquals(types, new Type[] { Double.class, Double.class, Iterable.class, Double.class })); |
| 68 | + |
| 69 | + types = new Type[] { Double.class, String.class, Iterable.class, String.class }; |
| 70 | + assertTrue(TypeModUtils.replaceRawTypes(types, String.class, Double.class, 1)); |
| 71 | + assertTrue(Arrays.deepEquals(types, new Type[] { Double.class, Double.class, Iterable.class, String.class })); |
| 72 | + |
| 73 | + types = new Type[] { Double.class, String.class, Iterable.class, String.class }; |
| 74 | + assertTrue(TypeModUtils.replaceRawTypes(types, String.class, Double.class, 1, 3)); |
| 75 | + assertTrue(Arrays.deepEquals(types, new Type[] { Double.class, Double.class, Iterable.class, Double.class })); |
| 76 | + |
| 77 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() }; |
| 78 | + assertTrue(TypeModUtils.replaceRawTypes(types, Function.class, Function.class)); |
| 79 | + assertTrue(Arrays.deepEquals(types, |
| 80 | + new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() })); |
| 81 | + |
| 82 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() }; |
| 83 | + assertTrue(TypeModUtils.replaceRawTypes(types, Function.class, Computer.class)); |
| 84 | + Nil<Computer<Double, String>> y2 = new Nil<Computer<Double, String>>() { |
| 85 | + }; |
| 86 | + assertTrue(Arrays.deepEquals(types, |
| 87 | + new Type[] { n1.getType(), y2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() })); |
| 88 | + |
| 89 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() }; |
| 90 | + assertTrue(TypeModUtils.replaceRawTypes(types, Supplier.class, Iterable.class)); |
| 91 | + Nil<Iterable<? extends Number>> y5 = new Nil<Iterable<? extends Number>>() { |
| 92 | + }; |
| 93 | + assertTrue(Arrays.deepEquals(types, |
| 94 | + new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), y5.getType(), n6.getType() })); |
| 95 | + |
| 96 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() }; |
| 97 | + assertTrue(TypeModUtils.replaceRawTypes(types, Consumer.class, List.class)); |
| 98 | + Nil<List<C>> y6 = new Nil<List<C>>() { |
| 99 | + }; |
| 100 | + assertTrue(Arrays.deepEquals(types, |
| 101 | + new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), y6.getType() })); |
| 102 | + |
| 103 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() }; |
| 104 | + assertFalse(TypeModUtils.replaceRawTypes(types, Integer.class, List.class)); |
| 105 | + assertTrue(Arrays.deepEquals(types, |
| 106 | + new Type[] { n1.getType(), n2.getType(), n3.getType(), n4.getType(), n5.getType(), n6.getType() })); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public <A, B, C> void unliftParameterized() { |
| 111 | + Nil<Computer<A, B[]>> n1 = new Nil<Computer<A, B[]>>() { |
| 112 | + }; |
| 113 | + Nil<Function<Double, String[]>> n2 = new Nil<Function<Double, String[]>>() { |
| 114 | + }; |
| 115 | + Nil<Function<C[], Iterable<String>>> n3 = new Nil<Function<C[], Iterable<String>>>() { |
| 116 | + }; |
| 117 | + Nil<Computer<C[], Function<String, Integer>>> n4 = new Nil<Computer<C[], Function<String, Integer>>>() { |
| 118 | + }; |
| 119 | + |
| 120 | + Nil<Function<Double, String>> y2 = new Nil<Function<Double, String>>() { |
| 121 | + }; |
| 122 | + Nil<Function<C, Iterable<String>>> y3 = new Nil<Function<C, Iterable<String>>>() { |
| 123 | + }; |
| 124 | + Nil<Function<C[], String>> y31 = new Nil<Function<C[], String>>() { |
| 125 | + }; |
| 126 | + |
| 127 | + Type[] types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 128 | + assertFalse(TypeModUtils.unliftParameterizedTypes(types, Computer.class, Iterable.class)); |
| 129 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), n2.getType(), n3.getType() })); |
| 130 | + |
| 131 | + types = new Type[] { n1.getType(), n2.getType(), n4.getType() }; |
| 132 | + assertFalse(TypeModUtils.unliftParameterizedTypes(types, Computer.class, Function.class)); |
| 133 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), n2.getType(), n4.getType() })); |
| 134 | + |
| 135 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 136 | + assertTrue(TypeModUtils.unliftParameterizedTypes(types, Function.class, Iterable.class)); |
| 137 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), n2.getType(), y31.getType() })); |
| 138 | + |
| 139 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 140 | + assertTrue(TypeModUtils.unliftParameterizedTypes(types, Function.class, Array.class)); |
| 141 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), y2.getType(), y3.getType() })); |
| 142 | + |
| 143 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 144 | + assertTrue(TypeModUtils.unliftParameterizedTypes(types, Function.class, Array.class, 1)); |
| 145 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), y2.getType(), n3.getType() })); |
| 146 | + |
| 147 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 148 | + assertTrue(TypeModUtils.unliftParameterizedTypes(types, Function.class, Array.class, 0)); |
| 149 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), n2.getType(), y3.getType() })); |
| 150 | + |
| 151 | + types = new Type[] { n1.getType(), n2.getType(), n3.getType() }; |
| 152 | + assertTrue(TypeModUtils.unliftParameterizedTypes(types, Function.class, Array.class, 0, 1)); |
| 153 | + assertTrue(Arrays.deepEquals(types, new Type[] { n1.getType(), y2.getType(), y3.getType() })); |
| 154 | + |
| 155 | + } |
| 156 | +} |
0 commit comments