added a couple of helper functions to make it easier to compare values, implement Comparable and create Comparators

This commit is contained in:
James Strachan
2012-03-26 10:53:28 +01:00
parent d0e7248ec1
commit d8c3a8f55c
3 changed files with 109 additions and 9 deletions
+40
View File
@@ -0,0 +1,40 @@
package test
import org.junit.Test
import kotlin.test.*
class Item(val name: String, val rating: Int)
/**
*/
class CompareTest {
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
Test fun compareByNameFirst() {
val diff = compareBy(v1, v2, {(i: Item) -> i.name}, {(i: Item) -> i.rating})
assertTrue(diff > 0)
}
Test fun compareByRatingFirst() {
val diff = compareBy(v1, v2, {(i: Item) -> i.rating}, {(i: Item) -> i.name})
assertTrue(diff < 0)
}
Test fun compareSameObjectsByRatingFirst() {
val diff = compareBy(v1, v1, {(i: Item) -> i.rating}, {(i: Item) -> i.name})
assertTrue(diff == 0)
}
Test fun createComparator() {
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")
*/
}
}