diff --git a/libraries/stdlib/test/CollectionJVMTest.kt b/libraries/stdlib/test/CollectionJVMTest.kt new file mode 100644 index 00000000000..e2cc1196226 --- /dev/null +++ b/libraries/stdlib/test/CollectionJVMTest.kt @@ -0,0 +1,124 @@ +package test.collections + +import kotlin.test.* + +import java.util.* + +import org.junit.Test as test + +class CollectionJVMTest { + + test fun flatMap() { + val data = arrayList("", "foo", "bar", "x", "") + val characters = data.flatMap{ it.toCharList() } + println("Got list of characters ${characters}") + assertEquals(7, characters.size()) + val text = characters.makeString("") + assertEquals("foobarx", text) + } + + + // TODO would be nice to avoid the + test fun filterIntoLinkedList() { + val data = arrayList("foo", "bar") + val foo = data.filterTo(linkedList()){it.startsWith("f")} + + assertTrue { + foo.all{it.startsWith("f")} + } + assertEquals(1, foo.size) + assertEquals(linkedList("foo"), foo) + + assertTrue { + foo is LinkedList + } + } + + // TODO would be nice to avoid the + test fun filterNotIntoLinkedList() { + val data = arrayList("foo", "bar") + val foo = data.filterNotTo(linkedList()){it.startsWith("f")} + + assertTrue { + foo.all{!it.startsWith("f")} + } + assertEquals(1, foo.size) + assertEquals(linkedList("bar"), foo) + + assertTrue { + foo is LinkedList + } + } + + // TODO would be nice to avoid the + test fun filterNotNullIntoLinkedList() { + val data = arrayList(null, "foo", null, "bar") + val foo = data.filterNotNullTo(linkedList()) + + assertEquals(2, foo.size) + assertEquals(linkedList("foo", "bar"), foo) + + assertTrue { + foo is LinkedList + } + } + + + // TODO would be nice to avoid the + test fun filterIntoSortedSet() { + val data = arrayList("foo", "bar") + val sorted = data.filterTo(sortedSet()){it.length == 3} + assertEquals(2, sorted.size) + assertEquals(sortedSet("bar", "foo"), sorted) + assertTrue { + sorted is TreeSet + } + } + + //todo after KT-1873 the name might be returned to 'last' + test fun lastElement() { + val data = arrayList("foo", "bar") + assertEquals("bar", data.last()) + assertEquals(25, arrayList(15, 19, 20, 25).last()) + assertEquals('a', linkedList('a').last()) + } + + test fun lastException() { + fails { linkedList().last() } + } + + test fun contains() { + assertTrue(linkedList(15, 19, 20).contains(15)) + } + + test fun sortBy() { + expect(arrayList("two" to 2, "three" to 3)) { + arrayList("three" to 3, "two" to 2).sortBy { it._2 } + } + expect(arrayList("three" to 3, "two" to 2)) { + arrayList("three" to 3, "two" to 2).sortBy { it._1 } + } + expect(arrayList("two" to 2, "three" to 3)) { + arrayList("three" to 3, "two" to 2).sortBy { it._1.length } + } + } + + + test fun sortFunctionShouldReturnSortedCopyForList() { + // TODO fixme Some sort of in/out variance thing - or an issue with Java interop? + todo { +// val list : List = arrayList(2, 3, 1) +// expect(arrayList(1, 2, 3)) { list.sort() } +// expect(arrayList(2, 3, 1)) { list } + } + } + + test fun sortFunctionShouldReturnSortedCopyForIterable() { + // TODO fixme Some sort of in/out variance thing - or an issue with Java interop? + todo { +// val list : java.lang.Iterable = arrayList(2, 3, 1) +// expect(arrayList(1, 2, 3)) { list.sort() } +// expect(arrayList(2, 3, 1)) { list } + } + } +} diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index 36e8cf751ca..73348e95b20 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -52,6 +52,14 @@ class CollectionTest { assertEquals(arrayList("foo"), foo) } + test fun filterReturnsList() { + val data = arrayList("foo", "bar") + val foo = data.filter{it.startsWith("f")} + assertTrue { + foo is List + } + } + test fun filterNot() { val data = arrayList("foo", "bar") val foo = data.filterNot{it.startsWith("b")} @@ -63,57 +71,12 @@ class CollectionTest { assertEquals(arrayList("foo"), foo) } - // TODO would be nice to avoid the - test fun filterIntoLinkedList() { - val data = arrayList("foo", "bar") - val foo = data.filterTo(linkedList()){it.startsWith("f")} - - assertTrue { - foo.all{it.startsWith("f")} - } - assertEquals(1, foo.size) - assertEquals(linkedList("foo"), foo) - - assertTrue { - foo is LinkedList - } - } - - // TODO would be nice to avoid the - test fun filterNotIntoLinkedList() { - val data = arrayList("foo", "bar") - val foo = data.filterNotTo(linkedList()){it.startsWith("f")} - - assertTrue { - foo.all{!it.startsWith("f")} - } - assertEquals(1, foo.size) - assertEquals(linkedList("bar"), foo) - - assertTrue { - foo is LinkedList - } - } - - // TODO would be nice to avoid the - test fun filterNotNullIntoLinkedList() { - val data = arrayList(null, "foo", null, "bar") - val foo = data.filterNotNullTo(linkedList()) - - assertEquals(2, foo.size) - assertEquals(linkedList("foo", "bar"), foo) - - assertTrue { - foo is LinkedList - } - } - test fun filterNotNull() { val data = arrayList(null, "foo", null, "bar") val foo = data.filterNotNull() assertEquals(2, foo.size) - assertEquals(linkedList("foo", "bar"), foo) + assertEquals(arrayList("foo", "bar"), foo) assertTrue { foo is List @@ -136,39 +99,16 @@ class CollectionTest { } } - // TODO would be nice to avoid the - test fun filterIntoSortedSet() { - val data = arrayList("foo", "bar") - val sorted = data.filterTo(sortedSet()){it.length == 3} - assertEquals(2, sorted.size) - assertEquals(sortedSet("bar", "foo"), sorted) - assertTrue { - sorted is TreeSet - } - } - test fun find() { val data = arrayList("foo", "bar") val x = data.find{it.startsWith("x")} assertNull(x) - fails { - x.sure() - } val f = data.find{it.startsWith("f")} f.sure() assertEquals("foo", f) } - test fun flatMap() { - val data = arrayList("", "foo", "bar", "x", "") - val characters = data.flatMap{ it.toCharList() } - println("Got list of characters ${characters}") - assertEquals(7, characters.size()) - val text = characters.makeString("") - assertEquals("foobarx", text) - } - test fun forEach() { val data = arrayList("foo", "bar") var count = 0 @@ -337,23 +277,6 @@ class CollectionTest { expect(arrayList(2, 3, 1)) { iterable } } - test fun sortFunctionShouldReturnSortedCopyForList() { - // TODO fixme Some sort of in/out variance thing - or an issue with Java interop? - todo { -// val list : List = arrayList(2, 3, 1) -// expect(arrayList(1, 2, 3)) { list.sort() } -// expect(arrayList(2, 3, 1)) { list } - } - } - - test fun sortFunctionShouldReturnSortedCopyForIterable() { - // TODO fixme Some sort of in/out variance thing - or an issue with Java interop? - todo { -// val list : java.lang.Iterable = arrayList(2, 3, 1) -// expect(arrayList(1, 2, 3)) { list.sort() } -// expect(arrayList(2, 3, 1)) { list } - } - } test fun drop() { val coll = arrayList("foo", "bar", "abc") @@ -382,6 +305,7 @@ class CollectionTest { val data = arrayList("foo", "bar") val arr = data.toArray() println("Got array ${arr}") + assertEquals(2, arr.size) todo { assertTrue { arr is Array @@ -401,7 +325,7 @@ class CollectionTest { val data = arrayList("foo", "bar") assertEquals("bar", data.last()) assertEquals(25, arrayList(15, 19, 20, 25).last()) - assertEquals('a', linkedList('a').last()) + assertEquals('a', arrayList('a').last()) } // TODO // assertEquals(19, TreeSet(arrayList(90, 47, 19)).first()) @@ -409,7 +333,6 @@ class CollectionTest { test fun lastException() { fails { arrayList().last() } - fails { linkedList().last() } fails { hashSet().last() } } @@ -456,24 +379,12 @@ class CollectionTest { // assertFalse(IterableWrapper(data).contains("some")) assertFalse(hashSet().contains(12)) - assertTrue(linkedList(15, 19, 20).contains(15)) + assertTrue(arrayList(15, 19, 20).contains(15)) // assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14)) // assertFalse(IterableWrapper(linkedList()).contains(15)) } - test fun sortBy() { - expect(arrayList("two" to 2, "three" to 3)) { - arrayList("three" to 3, "two" to 2).sortBy { it._2 } - } - expect(arrayList("three" to 3, "two" to 2)) { - arrayList("three" to 3, "two" to 2).sortBy { it._1 } - } - expect(arrayList("two" to 2, "three" to 3)) { - arrayList("three" to 3, "two" to 2).sortBy { it._1.length } - } - } - class IterableWrapper(collection : java.lang.Iterable) : java.lang.Iterable { private val collection = collection diff --git a/libraries/stdlib/test/SetTest.kt b/libraries/stdlib/test/SetTest.kt index 57211c46892..c0aeab663d3 100644 --- a/libraries/stdlib/test/SetTest.kt +++ b/libraries/stdlib/test/SetTest.kt @@ -32,6 +32,7 @@ class SetTest { foo.all{it.startsWith("f")} } assertEquals(1, foo.size) + assertEquals(hashSet("foo"), foo) assertTrue("Filter on a Set should return a Set") { @@ -42,12 +43,8 @@ class SetTest { Test fun find() { val x = data.find{it.startsWith("x")} assertNull(x) - fails { - x.sure() - } val f = data.find{it.startsWith("f")} - f.sure() assertEquals("foo", f) }