Last active
September 2, 2015 16:04
-
-
Save nobeans/92b6ff4242a258c17136 to your computer and use it in GitHub Desktop.
各種コレクション型と四則演算
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static groovy.test.GroovyAssert.shouldFail | |
| Iterable i1 = new Iterable() { | |
| @Delegate | |
| Iterable l = [1, 2] | |
| } | |
| Iterable i2 = new Iterable() { | |
| @Delegate | |
| Iterable l = [2, 3] | |
| } | |
| assert (i1 + i2) == [1, 2, 2, 3] | |
| assert (i1 + i2) instanceof ArrayList | |
| assert (i1 - i2) == [1] | |
| assert (i1 - i2) instanceof ArrayList | |
| assert (i1 * 2) == [1, 2, 1, 2] | |
| assert (i1 * 2) instanceof ArrayList | |
| Iterator ittr1 = new Iterator() { | |
| @Delegate | |
| Iterator l = [1, 2].iterator() | |
| } | |
| Iterator ittr2 = new Iterator() { | |
| @Delegate | |
| Iterator l = [2, 3].iterator() | |
| } | |
| //assert (ittr1 + ittr2) == [1, 2, 2, 3] | |
| //assert (ittr1 + ittr2) instanceof ArrayList | |
| //assert (ittr1 - ittr2) == [1] | |
| //assert (ittr1 - ittr2) instanceof ArrayList | |
| //assert (ittr1 * 2) == [1, 2, 1, 2] | |
| //assert (ittr1 * 2) instanceof ArrayList | |
| Collection c1 = new Collection() { | |
| @Delegate | |
| Collection l = [1, 2] | |
| } | |
| Collection c2 = new Collection() { | |
| @Delegate | |
| Collection l = [2, 3] | |
| } | |
| assert (c1 + c2) == [1, 2, 2, 3] | |
| assert (c1 + c2) instanceof ArrayList | |
| assert (c1 - c2) == [1] | |
| assert (c1 - c2) instanceof ArrayList | |
| assert (c1 * 2) == [1, 2, 1, 2] | |
| assert (c1 * 2) instanceof ArrayList | |
| List l1 = [1, 2] | |
| List l2 = [2, 3] | |
| assert (l1 + l2) == [1, 2, 2, 3] | |
| assert (l1 + l2) instanceof ArrayList | |
| assert (l1 - l2) == [1] | |
| assert (l1 - l2) instanceof ArrayList | |
| assert (l1 * 2) == [1, 2, 1, 2] | |
| assert (l1 * 2) instanceof ArrayList | |
| assert ((l1 as LinkedList) * 2) == [1, 2, 1, 2] | |
| assert ((l1 as LinkedList) * 2) instanceof LinkedList | |
| assert ((l1 as Stack) * 2) == [1, 2, 1, 2] | |
| assert ((l1 as Stack) * 2) instanceof Stack | |
| Set s1 = [1, 2] as Set | |
| Set s2 = [2, 3] as Set | |
| assert (s1 + s2) == [1, 2, 3] as Set | |
| assert (s1 + s2) instanceof LinkedHashSet | |
| assert (s1 - s2) == [1] as Set | |
| assert (s1 - s2) instanceof LinkedHashSet | |
| assert (s1 * 2) == [1, 2] as Set | |
| assert (s1 * 2) instanceof LinkedHashSet | |
| assert ((s1 * 2) as HashSet) instanceof HashSet | |
| assert ((s1 * 2) as HashSet) instanceof LinkedHashSet == false | |
| assert ((s1 * 2) as TreeSet) instanceof TreeSet | |
| Object[] a1 = [1, 2] as Object[] | |
| Object[] a2 = [2, 3] as Object[] | |
| assert (a1 + a2) == [1, 2, 2, 3] as Object[] | |
| assert (a1 + a2) instanceof Object[] | |
| assert (a1 - a2) == [1] as Object[] | |
| assert (a1 - a2) instanceof Object[] | |
| shouldFail(groovy.lang.MissingMethodException) { | |
| (a1 * 2) | |
| } | |
| Map m1 = [a:1, b:2] as Map | |
| Map m2 = [b:2, c:3] as Map | |
| assert (m1 + m2) == [a:1, b:2, c:3] as Map | |
| assert (m1 + m2) instanceof LinkedHashMap | |
| def minus = (m1 - m2) | |
| assert (m1 - m2) == [a:1] as Map | |
| assert (m1 - m2) instanceof LinkedHashMap | |
| shouldFail(groovy.lang.MissingMethodException) { | |
| (m1 * 2) | |
| } | |
| assert ((m1 - m2) as TreeMap) instanceof TreeMap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment