diff --git a/compiler/testData/codegen/jdk-annotations/collections.kt b/compiler/testData/codegen/jdk-annotations/collections.kt new file mode 100644 index 00000000000..eec3ec9623c --- /dev/null +++ b/compiler/testData/codegen/jdk-annotations/collections.kt @@ -0,0 +1,150 @@ +package collections + +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue +import java.security.KeyPair + + +fun testCollectionSize(c: Collection) = assertEquals(0, c.size()) +fun testCollectionIsEmpty(c: Collection) = assertTrue(c.isEmpty()) +fun testCollectionContains(c: Collection) = assertTrue(c.contains(1)) +fun testCollectionIterator(c: Collection) { + val it = c.iterator() + while (it.hasNext()) { + assertEquals(1, it.next()) + } +} +fun testCollectionToArray(c: Collection) = assertEquals(1, c.toArray()[0]) +fun testCollectionContainsAll(c: Collection) = assertTrue(c.containsAll(c)) +fun testMutableCollectionAdd(c: MutableCollection, t: T) { + c.add(t) + assertEquals(1, c.size()) + assertTrue(c.contains(t)) +} +fun testMutableCollectionRemove(c: MutableCollection, t: T) { + c.remove(t) + assertEquals(0, c.size()) + assertFalse(c.contains(t)) +} +fun testMutableCollectionIterator(c: MutableCollection, t: T) { + c.add(t) + val it = c.iterator() + while (it.hasNext()) { + it.next() + it.remove() + } + assertEquals(0, c.size()) +} +fun testMutableCollectionAddAll(c: MutableCollection, t1: T, t2: T) { + c.addAll(arrayList(t1, t2)) + assertEquals(arrayList(t1, t2), c) +} +fun testMutableCollectionRemoveAll(c: MutableCollection, t1: T, t2: T) { + c.addAll(arrayList(t1, t2)) + c.removeAll(arrayList(t1)) + assertEquals(arrayList(t2), c) +} +fun testMutableCollectionRetainAll(c: MutableCollection, t1: T, t2: T) { + c.addAll(arrayList(t1, t2)) + c.retainAll(arrayList(t1)) + assertEquals(arrayList(t1), c) +} +fun testMutableCollectionClear(c: MutableCollection) { + c.clear() + assertTrue(c.isEmpty()) +} + +fun testCollection() { + testCollectionSize(arrayList()) + testCollectionIsEmpty(arrayList()) + testCollectionContains(arrayList(1)) + testCollectionIterator(arrayList(1)) + testCollectionToArray(arrayList(1)) + testCollectionContainsAll(arrayList("a", "b")) + + testMutableCollectionAdd(arrayList(), "") + testMutableCollectionRemove(arrayList("a"), "a") + testMutableCollectionIterator(arrayList(), 1) + testMutableCollectionAddAll(arrayList(), 1, 2) + testMutableCollectionRemoveAll(arrayList(), 1, 2) + testMutableCollectionRetainAll(arrayList(), 1, 2) + testMutableCollectionClear(arrayList(1, 2)) +} + + +fun testListGet(l: List, t: T) = assertEquals(t, l.get(0)) +fun testListIndexOf(l: List, t: T) = assertEquals(0, l.indexOf(t)) +fun testListIterator(l: List, t1 : T, t2 : T) { + val indexes = arrayList() + val result = arrayList() + val it = l.listIterator() + while (it.hasNext()) { + indexes.add(it.nextIndex()) + result.add(it.next()) + } + while (it.hasPrevious()) { + indexes.add(it.previousIndex()) + result.add(it.previous()) + } + assertEquals(arrayList(0, 1, 1, 0), indexes) + assertEquals(arrayList(t1, t2, t2, t1), result) +} +fun testListSublist(l: List, t: T) = assertEquals(arrayList(t), l.subList(0, 1)) + +fun testMutableListSet(l: MutableList, t: T) { + l.set(0, t) + assertEquals(arrayList(t), l) +} +fun testMutableListIterator(l: MutableList, t1: T, t2: T, t3: T) { + val it = l.listIterator() + while (it.hasNext()) { + it.next() + it.add(t3) + } + assertEquals(arrayList(t1, t3, t2, t3), l) +} + +fun testList() { + testListGet(arrayList(1), 1) + testListIndexOf(arrayList(1), 1) + testListIterator(arrayList("a", "b"), "a", "b") + testListSublist(arrayList(1, 2), 1) + + testMutableListSet(arrayList(2), 4) + testMutableListIterator(arrayList(1, 2), 1, 2, 3) +} + +fun testMapContainsKey(map: Map, k: K) = assertTrue(map.containsKey(k)) +fun testMapKeys(map: Map, k1: K, k2: K) = assertEqualCollections(hashSet(k1, k2), map.keySet()) +fun testMapValues(map: Map, v1: V, v2: V) = assertEqualCollections(hashSet(v1, v2), map.values()) +fun testMapEntrySet(map: Map, k : K, v: V) { + for (entry in map.entrySet()) { + assertEquals(k, entry.key) + assertEquals(v, entry.value) + } +} +fun testMutableMapEntry(map: MutableMap, k1 : K, v: V) { + for (entry in map.entrySet()) { + entry.setValue(v) + } + assertEquals(hashMap(k1 to v), map) +} + + +fun testMap() { + testMapContainsKey(hashMap(1 to 'a', 2 to 'b'), 2) + testMapKeys(hashMap(1 to 'a', 2 to 'b'), 1, 2) + testMapValues(hashMap(1 to 'a', 2 to 'b'), 'a', 'b') + testMapEntrySet(hashMap(1 to 'a'), 1, 'a') + testMutableMapEntry(hashMap(1 to 'a'), 1, 'b') +} + +fun box() : String { + testCollection() + testList() + testMap() + return "OK" +} + +fun assertEqualCollections(c1: Collection, c2: Collection) = assertEquals(c1.toCollection(hashSet()), c2.toCollection(hashSet())) diff --git a/compiler/testData/diagnostics/tests/library/Collections.kt b/compiler/testData/diagnostics/tests/library/Collections.kt new file mode 100644 index 00000000000..e90aa8f39e0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/library/Collections.kt @@ -0,0 +1,118 @@ +package collections + +fun testCollection(c: Collection, t: T) { + c.size() + c.isEmpty() + c.contains(1) + val iterator: Iterator = c.iterator() + val arrayOfAnys: Array = c.toArray() + val arrayOfTs: Array = c.toArray(array()) + c.containsAll(c) + + val mutableIterator: MutableIterator = c.iterator() + c.add(t) + c.remove(1) + c.addAll(c) + c.removeAll(c) + c.retainAll(c) + c.clear() + +} +fun testMutableCollection(c: MutableCollection, t: T) { + c.size() + c.isEmpty() + c.contains(1) + val iterator: Iterator = c.iterator() + val arrayOfAnys: Array = c.toArray() + val arrayOfTs: Array = c.toArray(array()) + c.containsAll(c) + + + val mutableIterator: MutableIterator = c.iterator() + c.add(t) + c.remove(1) + c.addAll(c) + c.removeAll(c) + c.retainAll(c) + c.clear() +} + +fun testList(l: List, t: T) { + val t: T = l.get(1) + val i: Int = l.indexOf(1) + val i1: Int = l.lastIndexOf(1) + val listIterator: ListIterator = l.listIterator() + val listIterator1: ListIterator = l.listIterator(1) + val list: List = l.subList(1, 2) + + val value: T = l.set(1, t) + l.add(1, t) + l.remove(1) + val mutableListIterator: MutableListIterator = l.listIterator() + val mutableListIterator1: MutableListIterator = l.listIterator(1) + val mutableList: MutableList = l.subList(1, 2) +} + +fun testMutableList(l: MutableList, t: T) { + val value: T = l.set(1, t) + l.add(1, t) + l.remove(1) + val mutableListIterator: MutableListIterator = l.listIterator() + val mutableListIterator1: MutableListIterator = l.listIterator(1) + val mutableList: MutableList = l.subList(1, 2) +} + +fun testSet(s: Set, t: T) { + s.size() + s.isEmpty() + s.contains(1) + val iterator: Iterator = s.iterator() + val arrayOfAnys: Array = s.toArray() + val arrayOfTs: Array = s.toArray(array()) + s.containsAll(s) + + val mutableIterator: MutableIterator = s.iterator() + s.add(t) + s.remove(1) + s.addAll(s) + s.removeAll(s) + s.retainAll(s) + s.clear() + +} +fun testMutableSet(s: MutableSet, t: T) { + s.size() + s.isEmpty() + s.contains(1) + val iterator: Iterator = s.iterator() + val arrayOfAnys: Array = s.toArray() + val arrayOfTs: Array = s.toArray(array()) + s.containsAll(s) + + + val mutableIterator: MutableIterator = s.iterator() + s.add(t) + s.remove(1) + s.addAll(s) + s.removeAll(s) + s.retainAll(s) + s.clear() +} + +fun testMap(m: Map) { + val set: Set = m.keySet() + val collection: Collection = m.values() + val set1: Set> = m.entrySet() + + val mutableSet: MutableSet = m.keySet() + val mutableCollection: MutableCollection = m.values() + val mutableSet1: MutableSet> = m.entrySet() +} + +fun testMutableMap(m: MutableMap) { + val mutableSet: MutableSet = m.keySet() + val mutableCollection: MutableCollection = m.values() + val mutableSet1: MutableSet> = m.entrySet() +} + +fun array(vararg t: T): Array {} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index b5a48369cd4..b3db96dcd88 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1995,6 +1995,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve", new File("compiler/testData/diagnostics/tests/library"), "kt", true); } + @TestMetadata("Collections.kt") + public void testCollections() throws Exception { + doTest("compiler/testData/diagnostics/tests/library/Collections.kt"); + } + @TestMetadata("kt828.kt") public void testKt828() throws Exception { doTest("compiler/testData/diagnostics/tests/library/kt828.kt"); diff --git a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java index 7f26e004cf7..ec14cdf0b78 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/StdlibTest.java @@ -377,4 +377,8 @@ public class StdlibTest extends CodegenTestCase { public void testKt2596() { blackBoxFile("regressions/kt2596.kt"); } + + public void testCollections() { + blackBoxFile("jdk-annotations/collections.kt"); + } }