|
| 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.types; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +import java.lang.reflect.Type; |
| 36 | +import java.math.BigInteger; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests {@link TypeTools}. |
| 44 | + * |
| 45 | + * @author Gabe Selzer |
| 46 | + */ |
| 47 | +public class TypeToolsTest { |
| 48 | + |
| 49 | + /** Tests {@link TypeTools#satisfies(Type[], Type[])} for raw classes. */ |
| 50 | + @Test |
| 51 | + public void testSatisfiesRaw() { |
| 52 | + // f(Number, Integer) |
| 53 | + final Type[] dest = { Number.class, Integer.class }; |
| 54 | + |
| 55 | + // f(Double, Integer) |
| 56 | + // [OK] Double -> Number |
| 57 | + final Type[] srcOK = { Double.class, Integer.class }; |
| 58 | + assertTrue(TypeTools.satisfies(srcOK, dest)); |
| 59 | + |
| 60 | + // f(String, Integer) |
| 61 | + // [MISS] String is not assignable to Number |
| 62 | + final Type[] srcMiss = { String.class, Integer.class }; |
| 63 | + assertFalse(TypeTools.satisfies(srcMiss, dest)); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + /** Tests {@link TypeTools#satisfies(Type[], Type[])} for single arguments. */ |
| 68 | + @Test |
| 69 | + public <T extends Number, U extends BigInteger> void testSatisfiesSingle() { |
| 70 | + // <T extends Number> f(T) |
| 71 | + final Type t = new Nil<T>() {}.getType(); |
| 72 | + final Type u = new Nil<U>() {}.getType(); |
| 73 | + final Type[] tDest = { t }; |
| 74 | + |
| 75 | + assertTrue(TypeTools.satisfies(new Type[] { Double.class }, tDest)); |
| 76 | + assertTrue(TypeTools.satisfies(new Type[] { Number.class }, tDest)); |
| 77 | + assertTrue(TypeTools.satisfies(new Type[] { t }, tDest)); |
| 78 | + assertTrue(TypeTools.satisfies(new Type[] { u }, tDest)); |
| 79 | + // String does not extend Number |
| 80 | + assertFalse(TypeTools.satisfies(new Type[] { String.class }, tDest)); |
| 81 | + |
| 82 | + // -SINGLY RECURSIVE CALLS- |
| 83 | + |
| 84 | + // <T extends Number> f(List<T>) |
| 85 | + final Type listT = new Nil<List<T>>() {}.getType(); |
| 86 | + final Type[] listTDest = { listT }; |
| 87 | + // <U extends BigInteger> f(List<U>) |
| 88 | + final Type listU = new Nil<List<U>>() {}.getType(); |
| 89 | + final Type[] listUDest = { listU }; |
| 90 | + // f(List<Double>) |
| 91 | + final Type listDouble = new Nil<List<Double>>() {}.getType(); |
| 92 | + final Type[] listDoubleDest = { listDouble }; |
| 93 | + // f(List<? super Number>) |
| 94 | + final Type listSuperNumber = new Nil<List<? super Number>>() {}.getType(); |
| 95 | + final Type[] listSuperNumberDest = { listSuperNumber }; |
| 96 | + // f(List<? extends Number>) |
| 97 | + final Type listExtendsNumber = new Nil<List<? extends Number>>() {} |
| 98 | + .getType(); |
| 99 | + final Type[] listExtendsNumberDest = { listExtendsNumber }; |
| 100 | + |
| 101 | + assertTrue(TypeTools.satisfies(new Type[] { listT }, listTDest)); |
| 102 | + assertTrue(TypeTools.satisfies(listUDest, listTDest)); |
| 103 | + // not all Numbers are BigIntegers. |
| 104 | + assertFalse(TypeTools.satisfies(listTDest, listUDest)); |
| 105 | + assertTrue(TypeTools.satisfies(listTDest, listExtendsNumberDest)); |
| 106 | + assertTrue(TypeTools.satisfies(listUDest, listExtendsNumberDest)); |
| 107 | + assertTrue(TypeTools.satisfies(listTDest, listSuperNumberDest)); |
| 108 | + // BigInteger extends Number, not the other way around. |
| 109 | + assertFalse(TypeTools.satisfies(listUDest, listSuperNumberDest)); |
| 110 | + assertTrue(TypeTools.satisfies(listDoubleDest, listExtendsNumberDest)); |
| 111 | + // Double extends Number, not the other way around. |
| 112 | + assertFalse(TypeTools.satisfies(listDoubleDest, listSuperNumberDest)); |
| 113 | + |
| 114 | + // -MULTIPLY RECURSIVE CALLS- |
| 115 | + |
| 116 | + final Type MapListTT = new Nil<Map<List<T>, T>>() {}.getType(); |
| 117 | + final Type MapListTU = new Nil<Map<List<T>, U>>() {}.getType(); |
| 118 | + final Type MapListUU = new Nil<Map<List<U>, U>>() {}.getType(); |
| 119 | + final Type MapListTDouble = new Nil<Map<List<T>, Double>>() {}.getType(); |
| 120 | + final Type MapListDoubleDouble = new Nil<Map<List<Double>, Double>>() {} |
| 121 | + .getType(); |
| 122 | + final Type MapListDoubleString = new Nil<Map<List<Double>, String>>() {} |
| 123 | + .getType(); |
| 124 | + final Type MapListDoubleNumber = new Nil<Map<List<Double>, Number>>() {} |
| 125 | + .getType(); |
| 126 | + final Type MapListNumberDouble = new Nil<Map<List<Number>, Double>>() {} |
| 127 | + .getType(); |
| 128 | + |
| 129 | + // T might not always extend BigInteger(U) |
| 130 | + assertFalse(TypeTools.satisfies(new Type[] { MapListTT }, new Type[] { |
| 131 | + MapListTU })); |
| 132 | + // T might not always be the same as U |
| 133 | + assertFalse(TypeTools.satisfies(new Type[] { MapListTU }, new Type[] { |
| 134 | + MapListTT })); |
| 135 | + assertTrue(TypeTools.satisfies(new Type[] { MapListUU }, new Type[] { |
| 136 | + MapListTT })); |
| 137 | + // T might not always extend BigInteger(U) |
| 138 | + assertFalse(TypeTools.satisfies(new Type[] { MapListTT }, new Type[] { |
| 139 | + MapListUU })); |
| 140 | + // T might not always be Double |
| 141 | + assertFalse(TypeTools.satisfies(new Type[] { MapListTDouble }, new Type[] { |
| 142 | + MapListTT })); |
| 143 | + // T does not extend String. |
| 144 | + assertFalse(TypeTools.satisfies(new Type[] { MapListDoubleString }, |
| 145 | + new Type[] { MapListTT })); |
| 146 | + assertTrue(TypeTools.satisfies(new Type[] { MapListDoubleDouble }, |
| 147 | + new Type[] { MapListTT })); |
| 148 | + // T is already fixed to Double (in a parameterized Map), cannot accomodate |
| 149 | + // Nubmer. |
| 150 | + assertFalse(TypeTools.satisfies(new Type[] { MapListNumberDouble }, |
| 151 | + new Type[] { MapListTT })); |
| 152 | + // T is already fixed to Double (in a parameterized List) , cannot |
| 153 | + // accomodate Number |
| 154 | + assertFalse(TypeTools.satisfies(new Type[] { MapListDoubleNumber }, |
| 155 | + new Type[] { MapListTT })); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public <T extends Number, U extends String, V extends BigInteger> void |
| 160 | + testSatisfiesGenericArrays() |
| 161 | + { |
| 162 | + // generic arrays |
| 163 | + final Type arrayT = new Nil<T[]>() {}.getType(); |
| 164 | + final Type arrayU = new Nil<U[]>() {}.getType(); |
| 165 | + final Type arrayV = new Nil<V[]>() {}.getType(); |
| 166 | + final Type arrayDouble = new Nil<Double[]>() {}.getType(); |
| 167 | + |
| 168 | + assertTrue(TypeTools.satisfies(new Type[] { arrayDouble }, new Type[] { |
| 169 | + arrayT })); |
| 170 | + // Double does not extend String |
| 171 | + assertFalse(TypeTools.satisfies(new Type[] { arrayDouble }, new Type[] { |
| 172 | + arrayU })); |
| 173 | + assertTrue(TypeTools.satisfies(new Type[] { arrayT }, new Type[] { |
| 174 | + arrayT })); |
| 175 | + assertTrue(TypeTools.satisfies(new Type[] { arrayV }, new Type[] { |
| 176 | + arrayT })); |
| 177 | + // Number does not extend BigInteger |
| 178 | + assertFalse(TypeTools.satisfies(new Type[] { arrayT }, new Type[] { |
| 179 | + arrayV })); |
| 180 | + |
| 181 | + // generic multi-dimensional arrays |
| 182 | + final Type arrayT2D = new Nil<T[][]>() {}.getType(); |
| 183 | + final Type arrayV2D = new Nil<V[][]>() {}.getType(); |
| 184 | + final Type arrayDouble2D = new Nil<Double[][]>() {}.getType(); |
| 185 | + |
| 186 | + assertTrue(TypeTools.satisfies(new Type[] { arrayDouble2D }, new Type[] { |
| 187 | + arrayT2D })); |
| 188 | + assertTrue(TypeTools.satisfies(new Type[] { arrayV2D }, new Type[] { |
| 189 | + arrayT2D })); |
| 190 | + // A 2D array does not satisfy a 1D array |
| 191 | + assertFalse(TypeTools.satisfies(new Type[] { arrayT2D }, new Type[] { |
| 192 | + arrayT })); |
| 193 | + // A 1D array does not satisfy a 2D array |
| 194 | + assertFalse(TypeTools.satisfies(new Type[] { arrayT }, new Type[] { |
| 195 | + arrayT2D })); |
| 196 | + |
| 197 | + // generic parameterized type arrays |
| 198 | + final Type arrayListT = new Nil<List<T>[]>() {}.getType(); |
| 199 | + final Type arrayListDouble = new Nil<List<Double>[]>() {}.getType(); |
| 200 | + final Type arrayListString = new Nil<List<String>[]>() {}.getType(); |
| 201 | + |
| 202 | + assertTrue(TypeTools.satisfies(new Type[] { arrayListDouble }, new Type[] { |
| 203 | + arrayListT })); |
| 204 | + // String does not extend Number |
| 205 | + assertFalse(TypeTools.satisfies(new Type[] { arrayListString }, new Type[] { |
| 206 | + arrayListT })); |
| 207 | + // Number does not extend BigInteger |
| 208 | + assertFalse(TypeTools.satisfies(new Type[] { arrayListT }, new Type[] { |
| 209 | + arrayU })); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public <S, T extends Thing<S>, U extends IntegerThing, V extends RecursiveThing<V>, W extends RecursiveThing<W> & Loop, X extends Thing<S> & Loop> |
| 214 | + void testSatisfiesTypeVariables() |
| 215 | + { |
| 216 | + final Type t = new Nil<T>() {}.getType(); |
| 217 | + final Type u = new Nil<U>() {}.getType(); |
| 218 | + final Type thingInt = new Nil<Thing<Integer>>() {}.getType(); |
| 219 | + final Type numberThingInt = new Nil<NumberThing<Integer>>() {}.getType(); |
| 220 | + final Type numberThingDouble = new Nil<NumberThing<Double>>() {}.getType(); |
| 221 | + final Type strangeThingDouble = new Nil<StrangeThing<Double>>() {} |
| 222 | + .getType(); |
| 223 | + final Type strangerThingString = new Nil<StrangerThing<String>>() {} |
| 224 | + .getType(); |
| 225 | + final Type integerThing = new Nil<IntegerThing>() {}.getType(); |
| 226 | + |
| 227 | + assertTrue(TypeTools.satisfies(new Type[] { thingInt, thingInt, |
| 228 | + numberThingInt, integerThing }, new Type[] { t, t, t, t })); |
| 229 | + assertTrue(TypeTools.satisfies(new Type[] { thingInt, numberThingInt, |
| 230 | + strangerThingString }, new Type[] { t, t, t })); |
| 231 | + assertTrue(TypeTools.satisfies(new Type[] { thingInt, numberThingInt, |
| 232 | + integerThing }, new Type[] { t, t, t })); |
| 233 | + assertTrue(TypeTools.satisfies(new Type[] { numberThingInt, |
| 234 | + strangeThingDouble }, new Type[] { t, t })); |
| 235 | + // S cannot accommodate a Double since S is already locked to Integer from |
| 236 | + // the first argument. |
| 237 | + assertFalse(TypeTools.satisfies(new Type[] { thingInt, numberThingInt, |
| 238 | + numberThingDouble }, new Type[] { t, t, t })); |
| 239 | + assertTrue(TypeTools.satisfies(new Type[] { u }, new Type[] { t })); |
| 240 | + |
| 241 | + // recursive Type Variables |
| 242 | + final Type circularThing = new Nil<CircularThing>() {}.getType(); |
| 243 | + final Type loopingThing = new Nil<LoopingThing>() {}.getType(); |
| 244 | + final Type recursiveThingCircular = |
| 245 | + new Nil<RecursiveThing<CircularThing>>() |
| 246 | + {}.getType(); |
| 247 | + final Type v = new Nil<V>() {}.getType(); |
| 248 | + final Type w = new Nil<W>() {}.getType(); |
| 249 | + final Type x = new Nil<X>() {}.getType(); |
| 250 | + |
| 251 | + assertTrue(TypeTools.satisfies(new Type[] { circularThing, circularThing, |
| 252 | + loopingThing }, new Type[] { t, t, t })); |
| 253 | + // V cannot accommodate LoopingThing since V is already locked to |
| 254 | + // CircularThing |
| 255 | + assertFalse(TypeTools.satisfies(new Type[] { circularThing, circularThing, |
| 256 | + loopingThing }, new Type[] { v, v, v })); |
| 257 | + // V cannot accommodate RecursiveThing since V is already locked to |
| 258 | + // CircularThing (V has to extend RecursiveThing<itself>, not |
| 259 | + // RecursiveThing<not itself>). |
| 260 | + assertFalse(TypeTools.satisfies(new Type[] { circularThing, circularThing, |
| 261 | + recursiveThingCircular }, new Type[] { v, v, v })); |
| 262 | + // V cannot accommodate RecursiveThing<CircularThing> since V must extend |
| 263 | + // RecursiveThing<V> (it cannot extend RecursiveThing<not V>) |
| 264 | + assertFalse(TypeTools.satisfies(new Type[] { recursiveThingCircular, |
| 265 | + recursiveThingCircular, recursiveThingCircular }, new Type[] { v, v, |
| 266 | + v })); |
| 267 | + assertTrue(TypeTools.satisfies(new Type[] { recursiveThingCircular, |
| 268 | + recursiveThingCircular, recursiveThingCircular }, new Type[] { t, t, |
| 269 | + t })); |
| 270 | + assertTrue(TypeTools.satisfies(new Type[] { circularThing, circularThing, |
| 271 | + circularThing }, new Type[] { w, w, w })); |
| 272 | + // W cannot accommodate LoopingThing since W is already |
| 273 | + // fixed to CircularThing |
| 274 | + assertFalse(TypeTools.satisfies(new Type[] { circularThing, loopingThing, |
| 275 | + circularThing }, new Type[] { w, w, w })); |
| 276 | + assertTrue(TypeTools.satisfies(new Type[] { circularThing, loopingThing, |
| 277 | + circularThing }, new Type[] { x, x, x })); |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * Tests {@link TypeTools#satisfies(Type[], Type[])} when the same type |
| 282 | + * parameter appears across multiple destination types. |
| 283 | + */ |
| 284 | + @Test |
| 285 | + public <T> void testSatisfiesMatchingT() { |
| 286 | + // <T> f(List<T>, List<T>) |
| 287 | + final Type[] params = { // |
| 288 | + new Nil<List<T>>() |
| 289 | + {}.getType(), // |
| 290 | + new Nil<List<T>>() |
| 291 | + {}.getType(), // |
| 292 | + }; |
| 293 | + |
| 294 | + // f(List<Integer>, List<Integer>) |
| 295 | + // [OK] T -> Integer |
| 296 | + final Type[] argsOK = { // |
| 297 | + new Nil<List<Integer>>() |
| 298 | + {}.getType(), // |
| 299 | + new Nil<List<Integer>>() |
| 300 | + {}.getType() }; |
| 301 | + assertTrue(TypeTools.satisfies(argsOK, params)); |
| 302 | + |
| 303 | + // f(List<String>, List<Number>) |
| 304 | + // [MISS] T cannot be both String and Number |
| 305 | + final Type[] argsMiss = { // |
| 306 | + new Nil<List<Double>>() |
| 307 | + {}.getType(), // |
| 308 | + new Nil<List<Number>>() |
| 309 | + {}.getType() // |
| 310 | + }; |
| 311 | + assertFalse(TypeTools.satisfies(argsMiss, params)); |
| 312 | + } |
| 313 | + |
| 314 | + // -- Helper classes -- |
| 315 | + |
| 316 | + private static class Thing<T> { |
| 317 | + |
| 318 | + @SuppressWarnings("unused") |
| 319 | + private T thing; |
| 320 | + } |
| 321 | + |
| 322 | + private static class NumberThing<N extends Number> extends Thing<N> { |
| 323 | + // NB: No implementation needed. |
| 324 | + } |
| 325 | + |
| 326 | + private static class IntegerThing extends NumberThing<Integer> { |
| 327 | + // NB: No implementation needed. |
| 328 | + } |
| 329 | + |
| 330 | + private static class StrangeThing<S extends Number> extends Thing<Integer> { |
| 331 | + // NB: No implementation needed. |
| 332 | + } |
| 333 | + |
| 334 | + private static class StrangerThing<R extends String> extends |
| 335 | + StrangeThing<Double> |
| 336 | + { |
| 337 | + // NB: No implementation needed. |
| 338 | + } |
| 339 | + |
| 340 | + private static class RecursiveThing<T extends RecursiveThing<T>> extends |
| 341 | + Thing<Integer> |
| 342 | + { |
| 343 | + // NB: No implementation needed. |
| 344 | + } |
| 345 | + |
| 346 | + private static interface Loop { |
| 347 | + // NB: No implementation needed. |
| 348 | + } |
| 349 | + |
| 350 | + private static class CircularThing extends RecursiveThing<CircularThing> |
| 351 | + implements Loop |
| 352 | + { |
| 353 | + // NB: No implementation needed. |
| 354 | + } |
| 355 | + |
| 356 | + private static class LoopingThing extends RecursiveThing<LoopingThing> |
| 357 | + implements Loop |
| 358 | + { |
| 359 | + // NB: No implementation needed. |
| 360 | + } |
| 361 | +} |
0 commit comments