From 64f1cea134dba00c78b35d0d469b529f1685c6b5 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 13 Apr 2017 19:35:25 +0700 Subject: [PATCH] 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. --- .../CollectionTest/sortByInPlace.kt | 35 ++++++++++++++++--- .../CollectionTest/sortedNullableBy.kt | 22 ++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt b/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt index 36b53c7f096..d0b473256a1 100644 --- a/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt +++ b/backend.native/tests/external/stdlib/collections/CollectionTest/sortByInPlace.kt @@ -1,14 +1,41 @@ import kotlin.test.* -import kotlin.comparisons.* + +fun > assertSorted(list: List, message: String = "") = assertSorted(list, message, { it }) + +inline fun > assertSorted(list: List, 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 > assertSortedDescending(list: List, 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 } } diff --git a/backend.native/tests/external/stdlib/collections/CollectionTest/sortedNullableBy.kt b/backend.native/tests/external/stdlib/collections/CollectionTest/sortedNullableBy.kt index 47e18cc16af..ecdd17499da 100644 --- a/backend.native/tests/external/stdlib/collections/CollectionTest/sortedNullableBy.kt +++ b/backend.native/tests/external/stdlib/collections/CollectionTest/sortedNullableBy.kt @@ -1,11 +1,27 @@ import kotlin.test.* import kotlin.comparisons.* +fun assertSorted(list: List, cmp: Comparator, 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() }) + ) } }