tests: Do not rely on Kotlin JVM impl in sort tests
Some sort tests had an order of equal objects in a sorted list hardcoded. It caused failures because an approach to placing equal objects differs in Kotlin JVM and Kotlin Native sort implementations. With this patch we check if all objects in a list are sorted istead of checking an exact order of them.
This commit is contained in:
+31
-4
@@ -1,14 +1,41 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.comparisons.*
|
||||
|
||||
fun <T: Comparable<T>> assertSorted(list: List<out T>, message: String = "") = assertSorted(list, message, { it })
|
||||
|
||||
inline fun <T, R : Comparable<R>> assertSorted(list: List<out T>, message: String = "", crossinline selector: (T) -> R) {
|
||||
if (list.isEmpty() || list.size == 1) {
|
||||
return
|
||||
}
|
||||
val it = list.iterator()
|
||||
var prev = selector(it.next())
|
||||
while(it.hasNext()) {
|
||||
val cur = selector(it.next())
|
||||
assertTrue(prev.compareTo(cur) <= 0, message)
|
||||
prev = cur
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T, R : Comparable<R>> assertSortedDescending(list: List<out T>, message: String = "", crossinline selector: (T) -> R) {
|
||||
if (list.isEmpty() || list.size == 1) {
|
||||
return
|
||||
}
|
||||
val it = list.iterator()
|
||||
var prev = selector(it.next())
|
||||
while(it.hasNext()) {
|
||||
val cur = selector(it.next())
|
||||
assertTrue(prev.compareTo(cur) >= 0, message)
|
||||
prev = cur
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
val data = arrayListOf("aa" to 20, "ab" to 3, "aa" to 3)
|
||||
data.sortBy { it.second }
|
||||
assertEquals(listOf("ab" to 3, "aa" to 3, "aa" to 20), data)
|
||||
assertSorted(data) { it.second }
|
||||
|
||||
data.sortBy { it.first }
|
||||
assertEquals(listOf("aa" to 3, "aa" to 20, "ab" to 3), data)
|
||||
assertSorted(data) { it.first }
|
||||
|
||||
data.sortByDescending { (it.first + it.second).length }
|
||||
assertEquals(listOf("aa" to 20, "aa" to 3, "ab" to 3), data)
|
||||
assertSortedDescending(data) { (it.first + it.second).length }
|
||||
}
|
||||
|
||||
+19
-3
@@ -1,11 +1,27 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.comparisons.*
|
||||
|
||||
fun <T> assertSorted(list: List<T>, cmp: Comparator<in T>, message: String = "") {
|
||||
if (list.isEmpty() || list.size == 1) {
|
||||
return
|
||||
}
|
||||
val it = list.iterator()
|
||||
var prev = it.next()
|
||||
while(it.hasNext()) {
|
||||
val cur = it.next()
|
||||
assert(cmp.compare(prev, cur) <= 0)
|
||||
prev = cur
|
||||
}
|
||||
}
|
||||
|
||||
fun box() {
|
||||
fun String.nullIfEmpty() = if (isEmpty()) null else this
|
||||
listOf(null, "", "a").let {
|
||||
expect(listOf(null, "", "a")) { it.sortedWith(nullsFirst(compareBy { it })) }
|
||||
expect(listOf("a", "", null)) { it.sortedWith(nullsLast(compareByDescending { it })) }
|
||||
expect(listOf(null, "a", "")) { it.sortedWith(nullsFirst(compareByDescending { it.nullIfEmpty() })) }
|
||||
assertSorted(it.sortedWith(nullsFirst(compareBy { it })), nullsFirst(compareBy { it }))
|
||||
assertSorted(it.sortedWith(nullsLast(compareByDescending { it })), compareByDescending { it })
|
||||
assertSorted(
|
||||
it.sortedWith(nullsFirst(compareByDescending { it.nullIfEmpty() })),
|
||||
nullsFirst(compareByDescending { it.nullIfEmpty() })
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user