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() }) + ) } }