diff --git a/libraries/stdlib/src/JavaUtil.kt b/libraries/stdlib/src/JavaUtil.kt index aedb127fd14..bb9798f7b52 100644 --- a/libraries/stdlib/src/JavaUtil.kt +++ b/libraries/stdlib/src/JavaUtil.kt @@ -53,7 +53,7 @@ inline fun > List.sort() : List { return this } -inline fun > List.sort(comparator: java.util.Comparator) : List { +inline fun List.sort(comparator: java.util.Comparator) : List { Collections.sort(this, comparator) return this } diff --git a/libraries/stdlib/src/Ordering.kt b/libraries/stdlib/src/Ordering.kt index 071151890d8..5bb5ca62603 100644 --- a/libraries/stdlib/src/Ordering.kt +++ b/libraries/stdlib/src/Ordering.kt @@ -58,9 +58,7 @@ private class FunctionComparator(val functions: Array>): C } override fun compare(o1: T?, o2: T?): Int { - // TODO compile error - //return compareBy(o1, o2, functions) - throw UnsupportedOperationException("TODO") + return compareBy(o1, o2, *functions) } override fun equals(obj: Any?): Boolean { diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index d4a48566a14..51e04c7cd47 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -2,15 +2,11 @@ package test.collections import kotlin.test.* -// TODO can we avoid importing all this stuff by default I wonder? -// e.g. making println and the collection builder methods public by default? -import kotlin.* -import kotlin.io.* -import kotlin.util.* import java.util.* -import junit.framework.TestCase -class CollectionTest() : TestCase() { +import org.junit.Test + +class CollectionTest { class IterableWrapper(collection : java.lang.Iterable) : java.lang.Iterable { private val collection = collection @@ -23,7 +19,7 @@ class CollectionTest() : TestCase() { val data = arrayList("foo", "bar") - fun testAny() { + Test fun any() { assertTrue { data.any{it.startsWith("f")} } @@ -32,7 +28,7 @@ class CollectionTest() : TestCase() { } } - fun testAll() { + Test fun all() { assertTrue { data.all{it.length == 3} } @@ -41,12 +37,12 @@ class CollectionTest() : TestCase() { } } - fun testCount() { + Test fun count() { assertEquals(1, data.count{it.startsWith("b")}) assertEquals(2, data.count{it.size == 3}) } - fun testFilter() { + Test fun filter() { val foo = data.filter{it.startsWith("f")} assertTrue { @@ -56,7 +52,7 @@ class CollectionTest() : TestCase() { assertEquals(arrayList("foo"), foo) } - fun testFilterNot() { + Test fun filterNot() { val foo = data.filterNot{it.startsWith("b")} assertTrue { @@ -66,7 +62,7 @@ class CollectionTest() : TestCase() { assertEquals(arrayList("foo"), foo) } - fun testFilterIntoLinkedList() { + Test fun filterIntoLinkedList() { // TODO would be nice to avoid the val foo = data.filterTo(linkedList()){it.startsWith("f")} @@ -81,7 +77,7 @@ class CollectionTest() : TestCase() { } } - fun testFilterIntoSet() { + Test fun filterIntoSet() { // TODO would be nice to avoid the val foo = data.filterTo(hashSet()){it.startsWith("f")} @@ -96,7 +92,7 @@ class CollectionTest() : TestCase() { } } - fun testFilterIntoSortedSet() { + Test fun filterIntoSortedSet() { // TODO would be nice to avoid the val sorted = data.filterTo(sortedSet()){it.length == 3} assertEquals(2, sorted.size) @@ -106,7 +102,7 @@ class CollectionTest() : TestCase() { } } - fun testFind() { + Test fun find() { val x = data.find{it.startsWith("x")} assertNull(x) fails { @@ -118,7 +114,7 @@ class CollectionTest() : TestCase() { assertEquals("foo", f) } - fun testFlatMap() { + Test fun flatMap() { val characters = arrayList('f', 'o', 'o', 'b', 'a', 'r') // TODO figure out how to get a line like this to compile :) /* @@ -133,13 +129,13 @@ class CollectionTest() : TestCase() { } } - fun testForeach() { + Test fun foreach() { var count = 0 data.forEach{ count += it.length } assertEquals(6, count) } - fun testFold() { + Test fun fold() { expect(10) { val numbers = arrayList(1, 2, 3, 4) @@ -162,7 +158,7 @@ class CollectionTest() : TestCase() { } } - fun testFoldRight() { + Test fun foldRight() { expect("4321") { val numbers = arrayList(1, 2, 3, 4) @@ -173,7 +169,7 @@ class CollectionTest() : TestCase() { } - fun testGroupBy() { + Test fun groupBy() { val words = arrayList("a", "ab", "abc", "def", "abcd") /* TODO inference engine should not need this type info? @@ -191,12 +187,12 @@ class CollectionTest() : TestCase() { } - fun testJoin() { + Test fun join() { val text = data.join("-", "<", ">") assertEquals("", text) } - fun testMap() { + Test fun map() { /** TODO compiler bug we should be able to remove the explicit type on the function @@ -210,12 +206,12 @@ class CollectionTest() : TestCase() { assertEquals(arrayList(3, 3), lengths) } - fun testReverse() { + Test fun reverse() { val rev = data.reverse() assertEquals(arrayList("bar", "foo"), rev) } - fun testSort() { + Test fun sort() { val coll: List = arrayList("foo", "bar", "abc") // TODO fixme @@ -228,7 +224,7 @@ class CollectionTest() : TestCase() { } } - fun testToArray() { + Test fun toArray() { val arr = data.toArray() println("Got array ${arr}") todo { @@ -238,26 +234,26 @@ class CollectionTest() : TestCase() { } } - fun testSimpleCount() { + Test fun simpleCount() { assertEquals(2, data.count()) assertEquals(3, hashSet(12, 14, 15).count()) assertEquals(0, ArrayList().count()) } - fun testLast() { + Test fun last() { assertEquals("bar", data.last()) assertEquals(25, arrayList(15, 19, 20, 25).last()) // assertEquals(19, TreeSet(arrayList(90, 47, 19)).first()) assertEquals('a', linkedList('a').last()) } - fun testLastException() { + Test fun lastException() { fails { arrayList().last() } fails { linkedList().last() } fails { hashSet().last() } } - fun testSubscript() { + Test fun subscript() { val list = arrayList("foo", "bar") assertEquals("foo", list[0]) assertEquals("bar", list[1]) @@ -280,7 +276,7 @@ class CollectionTest() : TestCase() { assertEquals(arrayList("new", "thing", "works"), list) } - fun testIndices() { + Test fun indices() { val indices = data.indices assertEquals(0, indices.start) assertEquals(1, indices.end) @@ -288,7 +284,7 @@ class CollectionTest() : TestCase() { assertFalse(indices.isReversed) } - fun testContains() { + Test fun contains() { assertTrue(data.contains("foo")) assertTrue(data.contains("bar")) assertFalse(data.contains("some")) diff --git a/libraries/stdlib/test/CompareTest.kt b/libraries/stdlib/test/CompareTest.kt index 03d6c9d4c85..dde0dbbd4f8 100644 --- a/libraries/stdlib/test/CompareTest.kt +++ b/libraries/stdlib/test/CompareTest.kt @@ -1,13 +1,13 @@ -package test - -import org.junit.Test +package test.compare import kotlin.test.* +import java.util.* +import org.junit.Test -class Item(val name: String, val rating: Int) -/** - */ +class Item(val name: String, val rating: Int) { +} + class CompareTest { val v1 = Item("wine", 9) val v2 = Item("beer", 10) @@ -28,13 +28,16 @@ class CompareTest { } Test fun createComparator() { - val c = comparator({(i: Item) -> i.rating}, {(i: Item) -> i.name}) + val c = comparator({(i: Item) -> i.rating}, {(i: Item) -> i.name}) println("Created comparator $c") -/* - val items = arrayList(v1, v2) - items.sort(c) - println("Sorted list in rating order $items") -*/ + todo { + // TODO needs KT-729 before this code works + val diff = c.compare(v1, v2) + assertTrue(diff < 0) + val items = arrayList(v1, v2) + items.sort(c) + println("Sorted list in rating order $items") + } } } \ No newline at end of file diff --git a/libraries/stdlib/test/ListTest.kt b/libraries/stdlib/test/ListTest.kt index bb5c5b54fa2..c427d46f647 100644 --- a/libraries/stdlib/test/ListTest.kt +++ b/libraries/stdlib/test/ListTest.kt @@ -2,18 +2,12 @@ package test.collections import kotlin.test.* -// TODO can we avoid importing all this stuff by default I wonder? -// e.g. making println and the collection builder methods public by default? -import kotlin.* -import kotlin.io.* -import kotlin.util.* -import java.util.* -import junit.framework.TestCase +import org.junit.Test -class ListTest() : TestCase() { +class ListTest { val data = arrayList("foo", "bar") - fun testHeadAndTail() { + Test fun headAndTail() { val h = data.head assertEquals("foo", h) @@ -21,7 +15,7 @@ class ListTest() : TestCase() { assertEquals("bar", t) } - fun testFirstAndLast() { + Test fun firstAndLast() { val h = data.first assertEquals("foo", h) @@ -29,7 +23,7 @@ class ListTest() : TestCase() { assertEquals("bar", t) } - fun testWithIndices() { + Test fun withIndices() { val withIndices = data.withIndices() var index = 0 for (withIndex in withIndices) { diff --git a/libraries/stdlib/test/SetTest.kt b/libraries/stdlib/test/SetTest.kt index 4b29b65c417..57211c46892 100644 --- a/libraries/stdlib/test/SetTest.kt +++ b/libraries/stdlib/test/SetTest.kt @@ -1,16 +1,13 @@ package test.collections -import kotlin.* -import kotlin.io.* -import kotlin.util.* import kotlin.test.* import java.util.* -import junit.framework.TestCase +import org.junit.Test -class SetTest() : TestCase() { +class SetTest { val data = hashSet("foo", "bar") - fun testAny() { + Test fun any() { assertTrue { data.any{it.startsWith("f")} } @@ -19,7 +16,7 @@ class SetTest() : TestCase() { } } - fun testAll() { + Test fun all() { assertTrue { data.all{it.length == 3} } @@ -28,7 +25,7 @@ class SetTest() : TestCase() { } } - fun testFilter() { + Test fun filter() { val foo = data.filter{it.startsWith("f")}.toSet() assertTrue { @@ -42,7 +39,7 @@ class SetTest() : TestCase() { } } - fun testFind() { + Test fun find() { val x = data.find{it.startsWith("x")} assertNull(x) fails { @@ -54,7 +51,7 @@ class SetTest() : TestCase() { assertEquals("foo", f) } - fun testMap() { + Test fun map() { /** TODO compiler bug we should be able to remove the explicit type on the function