Tests for sorting methods.
This commit is contained in:
@@ -402,12 +402,6 @@ class ArraysTest {
|
||||
assertEquals(listOf(true, false, true), arrayOf(true, false, true, true).slice(iter))
|
||||
}
|
||||
|
||||
test fun toSortedList() {
|
||||
assertTrue(arrayOf<Long>().toSortedList().none())
|
||||
assertEquals(listOf(1), arrayOf(1).toSortedList())
|
||||
assertEquals(listOf("aab", "aba", "ac"), arrayOf("ac", "aab", "aba").toSortedList())
|
||||
}
|
||||
|
||||
test fun asIterable() {
|
||||
val arr1 = intArrayOf(1, 2, 3, 4, 5)
|
||||
val iter1 = arr1.asIterable()
|
||||
@@ -788,4 +782,58 @@ class ArraysTest {
|
||||
assertArrayNotSameButEquals(arrayOf("80", "9", "Foo", "all"), strArr)
|
||||
}
|
||||
|
||||
test fun sorted() {
|
||||
assertTrue(arrayOf<Long>().sorted().none())
|
||||
assertEquals(listOf(1), arrayOf(1).sorted())
|
||||
|
||||
arrayOf("ac", "aD", "aba").let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
|
||||
intArrayOf(3, 7, 1).let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
|
||||
longArrayOf(1, Long.MIN_VALUE, Long.MAX_VALUE).let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
|
||||
charArrayOf('a', 'D', 'c').let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
|
||||
byteArrayOf(1, Byte.MAX_VALUE, Byte.MIN_VALUE).let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
|
||||
doubleArrayOf(Double.POSITIVE_INFINITY, 1.0, Double.MAX_VALUE).let {
|
||||
it.sorted().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortedBy() {
|
||||
val values = arrayOf("ac", "aD", "aba")
|
||||
val indices = values.indices.toList().toIntArray()
|
||||
|
||||
assertEquals(listOf(1, 2, 0), indices.sortedBy { values[it] })
|
||||
}
|
||||
|
||||
test fun sortedNullableBy() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
arrayOf(null, "").let {
|
||||
expect(listOf(null, "")) { it.sortedBy { it.orEmpty()}}
|
||||
expect(listOf("", null)) { it.sortedDescendingBy { it.orEmpty() }}
|
||||
expect(listOf("", null)) { it.sortedDescendingBy { it?.nullIfEmpty() }}
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortedWith() {
|
||||
assertEquals(listOf(3, 0, 4, 1, 5, 2), intArrayOf(0, 1, 2, 3, 4, 5).sortedWith( compareBy { it: Int -> it % 3 }.thenByDescending { it } ))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,18 +469,6 @@ class CollectionTest {
|
||||
assertTrue(IterableWrapper(hashSetOf(45, 14, 13)).contains(14))
|
||||
}
|
||||
|
||||
test fun sortForMutableIterable() {
|
||||
val list: MutableIterable<Int> = arrayListOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun sortForIterable() {
|
||||
val list: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun min() {
|
||||
expect(null, { listOf<Int>().min() })
|
||||
expect(1, { listOf(1).min() })
|
||||
@@ -571,28 +559,28 @@ class CollectionTest {
|
||||
expect(listOf(1)) { listOf(1) take 10 }
|
||||
}
|
||||
|
||||
test fun sortBy() {
|
||||
expect(listOf("two" to 2, "three" to 3)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.second }
|
||||
}
|
||||
expect(listOf("three" to 3, "two" to 2)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.first }
|
||||
}
|
||||
expect(listOf("two" to 2, "three" to 3)) {
|
||||
listOf("three" to 3, "two" to 2).sortBy { it.first.length() }
|
||||
test fun sorted() {
|
||||
assertEquals(listOf(1, 3, 7), listOf(3, 7, 1).sorted())
|
||||
assertEquals(listOf(7, 3, 1), listOf(3, 7, 1).sortedDescending())
|
||||
}
|
||||
|
||||
test fun sortedBy() {
|
||||
assertEquals(listOf("two" to 2, "three" to 3), listOf("three" to 3, "two" to 2).sortedBy { it.second })
|
||||
assertEquals(listOf("three" to 3, "two" to 2), listOf("three" to 3, "two" to 2).sortedBy { it.first })
|
||||
assertEquals(listOf("three", "two"), listOf("two", "three").sortedDescendingBy { it.length() })
|
||||
}
|
||||
|
||||
test fun sortedNullableBy() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
listOf(null, "").let {
|
||||
expect(listOf(null, "")) { it.sortedBy { it.orEmpty()}}
|
||||
expect(listOf("", null)) { it.sortedDescendingBy { it.orEmpty() }}
|
||||
expect(listOf("", null)) { it.sortedDescendingBy { it?.nullIfEmpty() }}
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortFunctionShouldReturnSortedCopyForList() {
|
||||
val list: List<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
test fun sortFunctionShouldReturnSortedCopyForIterable() {
|
||||
val list: Iterable<Int> = listOf(2, 3, 1)
|
||||
expect(listOf(1, 2, 3)) { list.sort() }
|
||||
expect(listOf(2, 3, 1)) { list }
|
||||
test fun sortedWith() {
|
||||
expect(listOf("BAD", "dad", "cat")) { listOf("cat", "dad", "BAD").sortedWith(compareBy { it.toUpperCase().reverse() }) }
|
||||
}
|
||||
|
||||
test fun decomposeFirst() {
|
||||
@@ -671,3 +659,17 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Iterable<T>.assertSorted(isInOrder: (T, T) -> Boolean): Unit { this.iterator().assertSorted(isInOrder) }
|
||||
fun <T> Iterator<T>.assertSorted(isInOrder: (T, T) -> Boolean) {
|
||||
if (!hasNext()) return
|
||||
var index = 0
|
||||
var prev = next()
|
||||
while (hasNext()) {
|
||||
index += 1
|
||||
val next = next()
|
||||
assertTrue(isInOrder(prev, next), "Not in order at position $index, element[${index-1}]: $prev, element[$index]: $next")
|
||||
prev = next
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -313,6 +313,30 @@ public class SequenceTest {
|
||||
assertEquals(listOf('a', 'b', 'c'), chars)
|
||||
}
|
||||
|
||||
test fun sorted() {
|
||||
sequenceOf(3, 7, 5).let {
|
||||
it.sorted().iterator().assertSorted { a, b -> a <= b }
|
||||
it.sortedDescending().iterator().assertSorted { a, b -> a >= b }
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortedBy() {
|
||||
sequenceOf("it", "greater", "less").let {
|
||||
it.sortedBy { it.length() }.iterator().assertSorted { a, b -> compareValuesBy(a, b) { it.length() } <= 0 }
|
||||
it.sortedDescendingBy { it.length() }.iterator().assertSorted { a, b -> compareValuesBy(a, b) { it.length() } >= 0 }
|
||||
}
|
||||
|
||||
sequenceOf('a', 'd', 'c', null).let {
|
||||
it.sortedBy {it}.iterator().assertSorted { a, b -> compareValues(a, b) <= 0 }
|
||||
it.sortedDescendingBy {it}.iterator().assertSorted { a, b -> compareValues(a, b) >= 0 }
|
||||
}
|
||||
}
|
||||
|
||||
test fun sortedWith() {
|
||||
val comparator = compareBy { s: String -> s.reverse() }
|
||||
assertEquals(listOf("act", "wast", "test"), sequenceOf("act", "test", "wast").sortedWith(comparator).toList())
|
||||
}
|
||||
|
||||
/*
|
||||
test fun pairIterator() {
|
||||
val pairStr = (fibonacci() zip fibonacci().map { i -> i*2 }).joinToString(limit = 10)
|
||||
|
||||
Reference in New Issue
Block a user