added tests for collections

This commit is contained in:
Svetlana Isakova
2012-09-20 14:42:33 +04:00
parent 45dbf36b53
commit b081615e59
4 changed files with 277 additions and 0 deletions
@@ -0,0 +1,150 @@
package collections
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import java.security.KeyPair
fun <T> testCollectionSize(c: Collection<T>) = assertEquals(0, c.size())
fun <T> testCollectionIsEmpty(c: Collection<T>) = assertTrue(c.isEmpty())
fun <T> testCollectionContains(c: Collection<T>) = assertTrue(c.contains(1))
fun <T> testCollectionIterator(c: Collection<T>) {
val it = c.iterator()
while (it.hasNext()) {
assertEquals(1, it.next())
}
}
fun <T> testCollectionToArray(c: Collection<T>) = assertEquals(1, c.toArray()[0])
fun <T> testCollectionContainsAll(c: Collection<T>) = assertTrue(c.containsAll(c))
fun <T> testMutableCollectionAdd(c: MutableCollection<T>, t: T) {
c.add(t)
assertEquals(1, c.size())
assertTrue(c.contains(t))
}
fun <T> testMutableCollectionRemove(c: MutableCollection<T>, t: T) {
c.remove(t)
assertEquals(0, c.size())
assertFalse(c.contains(t))
}
fun <T> testMutableCollectionIterator(c: MutableCollection<T>, t: T) {
c.add(t)
val it = c.iterator()
while (it.hasNext()) {
it.next()
it.remove()
}
assertEquals(0, c.size())
}
fun <T> testMutableCollectionAddAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
assertEquals(arrayList(t1, t2), c)
}
fun <T> testMutableCollectionRemoveAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
c.removeAll(arrayList(t1))
assertEquals(arrayList(t2), c)
}
fun <T> testMutableCollectionRetainAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
c.retainAll(arrayList(t1))
assertEquals(arrayList(t1), c)
}
fun <T> testMutableCollectionClear(c: MutableCollection<T>) {
c.clear()
assertTrue(c.isEmpty())
}
fun testCollection() {
testCollectionSize(arrayList<Int>())
testCollectionIsEmpty(arrayList<String>())
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 <T> testListGet(l: List<T>, t: T) = assertEquals(t, l.get(0))
fun <T> testListIndexOf(l: List<T>, t: T) = assertEquals(0, l.indexOf(t))
fun <T> testListIterator(l: List<T>, t1 : T, t2 : T) {
val indexes = arrayList<Int>()
val result = arrayList<T>()
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 <T> testListSublist(l: List<T>, t: T) = assertEquals(arrayList(t), l.subList(0, 1))
fun <T> testMutableListSet(l: MutableList<T>, t: T) {
l.set(0, t)
assertEquals(arrayList(t), l)
}
fun <T> testMutableListIterator(l: MutableList<T>, 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 <K, V> testMapContainsKey(map: Map<K, V>, k: K) = assertTrue(map.containsKey(k))
fun <K, V> testMapKeys(map: Map<K, V>, k1: K, k2: K) = assertEqualCollections(hashSet(k1, k2), map.keySet())
fun <K, V> testMapValues(map: Map<K, V>, v1: V, v2: V) = assertEqualCollections(hashSet(v1, v2), map.values())
fun <K, V> testMapEntrySet(map: Map<K, V>, k : K, v: V) {
for (entry in map.entrySet()) {
assertEquals(k, entry.key)
assertEquals(v, entry.value)
}
}
fun <K, V> testMutableMapEntry(map: MutableMap<K, V>, 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 <T> assertEqualCollections(c1: Collection<T>, c2: Collection<T>) = assertEquals(c1.toCollection(hashSet<T>()), c2.toCollection(hashSet<T>()))
@@ -0,0 +1,118 @@
package collections
fun <T> testCollection(c: Collection<T>, t: T) {
c.size()
c.isEmpty()
c.contains(1)
val <!UNUSED_VARIABLE!>iterator<!>: Iterator<T> = c.iterator()
val <!UNUSED_VARIABLE!>arrayOfAnys<!>: Array<Any?> = c.toArray()
val <!UNUSED_VARIABLE!>arrayOfTs<!>: Array<T> = c.toArray(array<T>())
c.containsAll(c)
val <!UNUSED_VARIABLE!>mutableIterator<!>: MutableIterator<T> = <!TYPE_MISMATCH!>c.iterator()<!>
c.<!UNRESOLVED_REFERENCE!>add<!>(t)
c.<!UNRESOLVED_REFERENCE!>remove<!>(1)
c.<!UNRESOLVED_REFERENCE!>addAll<!>(c)
c.<!UNRESOLVED_REFERENCE!>removeAll<!>(c)
c.<!UNRESOLVED_REFERENCE!>retainAll<!>(c)
c.<!UNRESOLVED_REFERENCE!>clear<!>()
}
fun <T> testMutableCollection(c: MutableCollection<T>, t: T) {
c.size()
c.isEmpty()
c.contains(1)
val <!UNUSED_VARIABLE!>iterator<!>: Iterator<T> = c.iterator()
val <!UNUSED_VARIABLE!>arrayOfAnys<!>: Array<Any?> = c.toArray()
val <!UNUSED_VARIABLE!>arrayOfTs<!>: Array<T> = c.toArray(array<T>())
c.containsAll(c)
val <!UNUSED_VARIABLE!>mutableIterator<!>: MutableIterator<T> = c.iterator()
c.add(t)
c.remove(1)
c.addAll(c)
c.removeAll(c)
c.retainAll(c)
c.clear()
}
fun <T> testList(l: List<T>, <!UNUSED_PARAMETER!>t<!>: T) {
val <!NAME_SHADOWING!>t<!>: T = l.get(1)
val <!UNUSED_VARIABLE!>i<!>: Int = l.indexOf(1)
val <!UNUSED_VARIABLE!>i1<!>: Int = l.lastIndexOf(1)
val <!UNUSED_VARIABLE!>listIterator<!>: ListIterator<T> = l.listIterator()
val <!UNUSED_VARIABLE!>listIterator1<!>: ListIterator<T> = l.listIterator(1)
val <!UNUSED_VARIABLE!>list<!>: List<T> = l.subList(1, 2)
val <!UNUSED_VARIABLE!>value<!>: T = l.<!UNRESOLVED_REFERENCE!>set<!>(1, t)
l.<!UNRESOLVED_REFERENCE!>add<!>(1, t)
l.<!UNRESOLVED_REFERENCE!>remove<!>(1)
val <!UNUSED_VARIABLE!>mutableListIterator<!>: MutableListIterator<T> = <!TYPE_MISMATCH!>l.listIterator()<!>
val <!UNUSED_VARIABLE!>mutableListIterator1<!>: MutableListIterator<T> = <!TYPE_MISMATCH!>l.listIterator(1)<!>
val <!UNUSED_VARIABLE!>mutableList<!>: MutableList<T> = <!TYPE_MISMATCH!>l.subList(1, 2)<!>
}
fun <T> testMutableList(l: MutableList<T>, t: T) {
val <!UNUSED_VARIABLE!>value<!>: T = l.set(1, t)
l.add(1, t)
l.remove(1)
val <!UNUSED_VARIABLE!>mutableListIterator<!>: MutableListIterator<T> = l.listIterator()
val <!UNUSED_VARIABLE!>mutableListIterator1<!>: MutableListIterator<T> = l.listIterator(1)
val <!UNUSED_VARIABLE!>mutableList<!>: MutableList<T> = l.subList(1, 2)
}
fun <T> testSet(s: Set<T>, t: T) {
s.size()
s.isEmpty()
s.contains(1)
val <!UNUSED_VARIABLE!>iterator<!>: Iterator<T> = s.iterator()
val <!UNUSED_VARIABLE!>arrayOfAnys<!>: Array<Any?> = s.toArray()
val <!UNUSED_VARIABLE!>arrayOfTs<!>: Array<T> = s.toArray(array<T>())
s.containsAll(s)
val <!UNUSED_VARIABLE!>mutableIterator<!>: MutableIterator<T> = <!TYPE_MISMATCH!>s.iterator()<!>
s.<!UNRESOLVED_REFERENCE!>add<!>(t)
s.<!UNRESOLVED_REFERENCE!>remove<!>(1)
s.<!UNRESOLVED_REFERENCE!>addAll<!>(s)
s.<!UNRESOLVED_REFERENCE!>removeAll<!>(s)
s.<!UNRESOLVED_REFERENCE!>retainAll<!>(s)
s.<!UNRESOLVED_REFERENCE!>clear<!>()
}
fun <T> testMutableSet(s: MutableSet<T>, t: T) {
s.size()
s.isEmpty()
s.contains(1)
val <!UNUSED_VARIABLE!>iterator<!>: Iterator<T> = s.iterator()
val <!UNUSED_VARIABLE!>arrayOfAnys<!>: Array<Any?> = s.toArray()
val <!UNUSED_VARIABLE!>arrayOfTs<!>: Array<T> = s.toArray(array<T>())
s.containsAll(s)
val <!UNUSED_VARIABLE!>mutableIterator<!>: MutableIterator<T> = s.iterator()
s.add(t)
s.remove(1)
s.addAll(s)
s.removeAll(s)
s.retainAll(s)
s.clear()
}
fun <K, V> testMap(m: Map<K, V>) {
val <!UNUSED_VARIABLE!>set<!>: Set<K> = m.keySet()
val <!UNUSED_VARIABLE!>collection<!>: Collection<V> = m.values()
val <!UNUSED_VARIABLE!>set1<!>: Set<Map.Entry<K, V>> = m.entrySet()
val <!UNUSED_VARIABLE!>mutableSet<!>: MutableSet<K> = <!TYPE_MISMATCH!>m.keySet()<!>
val <!UNUSED_VARIABLE!>mutableCollection<!>: MutableCollection<V> = <!TYPE_MISMATCH!>m.values()<!>
val <!UNUSED_VARIABLE!>mutableSet1<!>: MutableSet<MutableMap.MutableEntry<K, V>> = <!TYPE_MISMATCH!>m.entrySet()<!>
}
fun <K, V> testMutableMap(m: MutableMap<K, V>) {
val <!UNUSED_VARIABLE!>mutableSet<!>: MutableSet<K> = m.keySet()
val <!UNUSED_VARIABLE!>mutableCollection<!>: MutableCollection<V> = m.values()
val <!UNUSED_VARIABLE!>mutableSet1<!>: MutableSet<MutableMap.MutableEntry<K, V>> = m.entrySet()
}
fun <T> array(vararg <!UNUSED_PARAMETER!>t<!>: T): Array<T> {<!NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY!>}<!>
@@ -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");
@@ -377,4 +377,8 @@ public class StdlibTest extends CodegenTestCase {
public void testKt2596() {
blackBoxFile("regressions/kt2596.kt");
}
public void testCollections() {
blackBoxFile("jdk-annotations/collections.kt");
}
}