added sample comparator code and tidied up some test cases & converted them to the cleaner JUnit 4 style
This commit is contained in:
@@ -53,7 +53,7 @@ inline fun <in T: java.lang.Comparable<T>> List<T>.sort() : List<T> {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <in T: java.lang.Comparable<T>> List<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
inline fun <in T> List<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
||||||
Collections.sort(this, comparator)
|
Collections.sort(this, comparator)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,9 +58,7 @@ private class FunctionComparator<T>(val functions: Array<Function1<T,Any?>>): C
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun compare(o1: T?, o2: T?): Int {
|
override fun compare(o1: T?, o2: T?): Int {
|
||||||
// TODO compile error
|
return compareBy<T>(o1, o2, *functions)
|
||||||
//return compareBy<T>(o1, o2, functions)
|
|
||||||
throw UnsupportedOperationException("TODO")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun equals(obj: Any?): Boolean {
|
override fun equals(obj: Any?): Boolean {
|
||||||
|
|||||||
@@ -2,15 +2,11 @@ package test.collections
|
|||||||
|
|
||||||
import kotlin.test.*
|
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 java.util.*
|
||||||
import junit.framework.TestCase
|
|
||||||
|
|
||||||
class CollectionTest() : TestCase() {
|
import org.junit.Test
|
||||||
|
|
||||||
|
class CollectionTest {
|
||||||
|
|
||||||
class IterableWrapper<T>(collection : java.lang.Iterable<T>) : java.lang.Iterable<T> {
|
class IterableWrapper<T>(collection : java.lang.Iterable<T>) : java.lang.Iterable<T> {
|
||||||
private val collection = collection
|
private val collection = collection
|
||||||
@@ -23,7 +19,7 @@ class CollectionTest() : TestCase() {
|
|||||||
|
|
||||||
val data = arrayList("foo", "bar")
|
val data = arrayList("foo", "bar")
|
||||||
|
|
||||||
fun testAny() {
|
Test fun any() {
|
||||||
assertTrue {
|
assertTrue {
|
||||||
data.any{it.startsWith("f")}
|
data.any{it.startsWith("f")}
|
||||||
}
|
}
|
||||||
@@ -32,7 +28,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testAll() {
|
Test fun all() {
|
||||||
assertTrue {
|
assertTrue {
|
||||||
data.all{it.length == 3}
|
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(1, data.count{it.startsWith("b")})
|
||||||
assertEquals(2, data.count{it.size == 3})
|
assertEquals(2, data.count{it.size == 3})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFilter() {
|
Test fun filter() {
|
||||||
val foo = data.filter{it.startsWith("f")}
|
val foo = data.filter{it.startsWith("f")}
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
@@ -56,7 +52,7 @@ class CollectionTest() : TestCase() {
|
|||||||
assertEquals(arrayList("foo"), foo)
|
assertEquals(arrayList("foo"), foo)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFilterNot() {
|
Test fun filterNot() {
|
||||||
val foo = data.filterNot{it.startsWith("b")}
|
val foo = data.filterNot{it.startsWith("b")}
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
@@ -66,7 +62,7 @@ class CollectionTest() : TestCase() {
|
|||||||
assertEquals(arrayList("foo"), foo)
|
assertEquals(arrayList("foo"), foo)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFilterIntoLinkedList() {
|
Test fun filterIntoLinkedList() {
|
||||||
// TODO would be nice to avoid the <String>
|
// TODO would be nice to avoid the <String>
|
||||||
val foo = data.filterTo(linkedList<String>()){it.startsWith("f")}
|
val foo = data.filterTo(linkedList<String>()){it.startsWith("f")}
|
||||||
|
|
||||||
@@ -81,7 +77,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFilterIntoSet() {
|
Test fun filterIntoSet() {
|
||||||
// TODO would be nice to avoid the <String>
|
// TODO would be nice to avoid the <String>
|
||||||
val foo = data.filterTo(hashSet<String>()){it.startsWith("f")}
|
val foo = data.filterTo(hashSet<String>()){it.startsWith("f")}
|
||||||
|
|
||||||
@@ -96,7 +92,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFilterIntoSortedSet() {
|
Test fun filterIntoSortedSet() {
|
||||||
// TODO would be nice to avoid the <String>
|
// TODO would be nice to avoid the <String>
|
||||||
val sorted = data.filterTo(sortedSet<String>()){it.length == 3}
|
val sorted = data.filterTo(sortedSet<String>()){it.length == 3}
|
||||||
assertEquals(2, sorted.size)
|
assertEquals(2, sorted.size)
|
||||||
@@ -106,7 +102,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFind() {
|
Test fun find() {
|
||||||
val x = data.find{it.startsWith("x")}
|
val x = data.find{it.startsWith("x")}
|
||||||
assertNull(x)
|
assertNull(x)
|
||||||
fails {
|
fails {
|
||||||
@@ -118,7 +114,7 @@ class CollectionTest() : TestCase() {
|
|||||||
assertEquals("foo", f)
|
assertEquals("foo", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFlatMap() {
|
Test fun flatMap() {
|
||||||
val characters = arrayList('f', 'o', 'o', 'b', 'a', 'r')
|
val characters = arrayList('f', 'o', 'o', 'b', 'a', 'r')
|
||||||
// TODO figure out how to get a line like this to compile :)
|
// 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
|
var count = 0
|
||||||
data.forEach{ count += it.length }
|
data.forEach{ count += it.length }
|
||||||
assertEquals(6, count)
|
assertEquals(6, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFold() {
|
Test fun fold() {
|
||||||
expect(10) {
|
expect(10) {
|
||||||
val numbers = arrayList(1, 2, 3, 4)
|
val numbers = arrayList(1, 2, 3, 4)
|
||||||
|
|
||||||
@@ -162,7 +158,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFoldRight() {
|
Test fun foldRight() {
|
||||||
expect("4321") {
|
expect("4321") {
|
||||||
val numbers = arrayList(1, 2, 3, 4)
|
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")
|
val words = arrayList("a", "ab", "abc", "def", "abcd")
|
||||||
/*
|
/*
|
||||||
TODO inference engine should not need this type info?
|
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("-", "<", ">")
|
val text = data.join("-", "<", ">")
|
||||||
assertEquals("<foo-bar>", text)
|
assertEquals("<foo-bar>", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testMap() {
|
Test fun map() {
|
||||||
/**
|
/**
|
||||||
TODO compiler bug
|
TODO compiler bug
|
||||||
we should be able to remove the explicit type on the function
|
we should be able to remove the explicit type on the function
|
||||||
@@ -210,12 +206,12 @@ class CollectionTest() : TestCase() {
|
|||||||
assertEquals(arrayList(3, 3), lengths)
|
assertEquals(arrayList(3, 3), lengths)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testReverse() {
|
Test fun reverse() {
|
||||||
val rev = data.reverse()
|
val rev = data.reverse()
|
||||||
assertEquals(arrayList("bar", "foo"), rev)
|
assertEquals(arrayList("bar", "foo"), rev)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testSort() {
|
Test fun sort() {
|
||||||
val coll: List<String> = arrayList("foo", "bar", "abc")
|
val coll: List<String> = arrayList("foo", "bar", "abc")
|
||||||
|
|
||||||
// TODO fixme
|
// TODO fixme
|
||||||
@@ -228,7 +224,7 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testToArray() {
|
Test fun toArray() {
|
||||||
val arr = data.toArray()
|
val arr = data.toArray()
|
||||||
println("Got array ${arr}")
|
println("Got array ${arr}")
|
||||||
todo {
|
todo {
|
||||||
@@ -238,26 +234,26 @@ class CollectionTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testSimpleCount() {
|
Test fun simpleCount() {
|
||||||
assertEquals(2, data.count())
|
assertEquals(2, data.count())
|
||||||
assertEquals(3, hashSet(12, 14, 15).count())
|
assertEquals(3, hashSet(12, 14, 15).count())
|
||||||
assertEquals(0, ArrayList<Double>().count())
|
assertEquals(0, ArrayList<Double>().count())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testLast() {
|
Test fun last() {
|
||||||
assertEquals("bar", data.last())
|
assertEquals("bar", data.last())
|
||||||
assertEquals(25, arrayList(15, 19, 20, 25).last())
|
assertEquals(25, arrayList(15, 19, 20, 25).last())
|
||||||
// assertEquals(19, TreeSet(arrayList(90, 47, 19)).first())
|
// assertEquals(19, TreeSet(arrayList(90, 47, 19)).first())
|
||||||
assertEquals('a', linkedList('a').last())
|
assertEquals('a', linkedList('a').last())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testLastException() {
|
Test fun lastException() {
|
||||||
fails { arrayList<Int>().last() }
|
fails { arrayList<Int>().last() }
|
||||||
fails { linkedList<String>().last() }
|
fails { linkedList<String>().last() }
|
||||||
fails { hashSet<Char>().last() }
|
fails { hashSet<Char>().last() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testSubscript() {
|
Test fun subscript() {
|
||||||
val list = arrayList("foo", "bar")
|
val list = arrayList("foo", "bar")
|
||||||
assertEquals("foo", list[0])
|
assertEquals("foo", list[0])
|
||||||
assertEquals("bar", list[1])
|
assertEquals("bar", list[1])
|
||||||
@@ -280,7 +276,7 @@ class CollectionTest() : TestCase() {
|
|||||||
assertEquals(arrayList("new", "thing", "works"), list)
|
assertEquals(arrayList("new", "thing", "works"), list)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testIndices() {
|
Test fun indices() {
|
||||||
val indices = data.indices
|
val indices = data.indices
|
||||||
assertEquals(0, indices.start)
|
assertEquals(0, indices.start)
|
||||||
assertEquals(1, indices.end)
|
assertEquals(1, indices.end)
|
||||||
@@ -288,7 +284,7 @@ class CollectionTest() : TestCase() {
|
|||||||
assertFalse(indices.isReversed)
|
assertFalse(indices.isReversed)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testContains() {
|
Test fun contains() {
|
||||||
assertTrue(data.contains("foo"))
|
assertTrue(data.contains("foo"))
|
||||||
assertTrue(data.contains("bar"))
|
assertTrue(data.contains("bar"))
|
||||||
assertFalse(data.contains("some"))
|
assertFalse(data.contains("some"))
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package test
|
package test.compare
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
import kotlin.test.*
|
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 {
|
class CompareTest {
|
||||||
val v1 = Item("wine", 9)
|
val v1 = Item("wine", 9)
|
||||||
val v2 = Item("beer", 10)
|
val v2 = Item("beer", 10)
|
||||||
@@ -28,13 +28,16 @@ class CompareTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Test fun createComparator() {
|
Test fun createComparator() {
|
||||||
val c = comparator({(i: Item) -> i.rating}, {(i: Item) -> i.name})
|
val c = comparator<Item>({(i: Item) -> i.rating}, {(i: Item) -> i.name})
|
||||||
println("Created comparator $c")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,18 +2,12 @@ package test.collections
|
|||||||
|
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
// TODO can we avoid importing all this stuff by default I wonder?
|
import org.junit.Test
|
||||||
// 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 ListTest() : TestCase() {
|
class ListTest {
|
||||||
val data = arrayList("foo", "bar")
|
val data = arrayList("foo", "bar")
|
||||||
|
|
||||||
fun testHeadAndTail() {
|
Test fun headAndTail() {
|
||||||
val h = data.head
|
val h = data.head
|
||||||
assertEquals("foo", h)
|
assertEquals("foo", h)
|
||||||
|
|
||||||
@@ -21,7 +15,7 @@ class ListTest() : TestCase() {
|
|||||||
assertEquals("bar", t)
|
assertEquals("bar", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFirstAndLast() {
|
Test fun firstAndLast() {
|
||||||
val h = data.first
|
val h = data.first
|
||||||
assertEquals("foo", h)
|
assertEquals("foo", h)
|
||||||
|
|
||||||
@@ -29,7 +23,7 @@ class ListTest() : TestCase() {
|
|||||||
assertEquals("bar", t)
|
assertEquals("bar", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testWithIndices() {
|
Test fun withIndices() {
|
||||||
val withIndices = data.withIndices()
|
val withIndices = data.withIndices()
|
||||||
var index = 0
|
var index = 0
|
||||||
for (withIndex in withIndices) {
|
for (withIndex in withIndices) {
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
package test.collections
|
package test.collections
|
||||||
|
|
||||||
import kotlin.*
|
|
||||||
import kotlin.io.*
|
|
||||||
import kotlin.util.*
|
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import junit.framework.TestCase
|
import org.junit.Test
|
||||||
|
|
||||||
class SetTest() : TestCase() {
|
class SetTest {
|
||||||
val data = hashSet("foo", "bar")
|
val data = hashSet("foo", "bar")
|
||||||
|
|
||||||
fun testAny() {
|
Test fun any() {
|
||||||
assertTrue {
|
assertTrue {
|
||||||
data.any{it.startsWith("f")}
|
data.any{it.startsWith("f")}
|
||||||
}
|
}
|
||||||
@@ -19,7 +16,7 @@ class SetTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testAll() {
|
Test fun all() {
|
||||||
assertTrue {
|
assertTrue {
|
||||||
data.all{it.length == 3}
|
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()
|
val foo = data.filter{it.startsWith("f")}.toSet()
|
||||||
|
|
||||||
assertTrue {
|
assertTrue {
|
||||||
@@ -42,7 +39,7 @@ class SetTest() : TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testFind() {
|
Test fun find() {
|
||||||
val x = data.find{it.startsWith("x")}
|
val x = data.find{it.startsWith("x")}
|
||||||
assertNull(x)
|
assertNull(x)
|
||||||
fails {
|
fails {
|
||||||
@@ -54,7 +51,7 @@ class SetTest() : TestCase() {
|
|||||||
assertEquals("foo", f)
|
assertEquals("foo", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testMap() {
|
Test fun map() {
|
||||||
/**
|
/**
|
||||||
TODO compiler bug
|
TODO compiler bug
|
||||||
we should be able to remove the explicit type on the function
|
we should be able to remove the explicit type on the function
|
||||||
|
|||||||
Reference in New Issue
Block a user