[stdlib] Use merge sort for comparables too
This commit is contained in:
@@ -1468,8 +1468,12 @@ task sort1(type: RunKonanTest) {
|
||||
source = "runtime/collections/sort1.kt"
|
||||
}
|
||||
|
||||
task sortWith(type: RunKonanTest) {
|
||||
// TODO: Enable devirtualization and make the test not standalone
|
||||
// when KT-26315 is fixed.
|
||||
task sortWith(type: RunStandaloneKonanTest) {
|
||||
source = "runtime/collections/SortWith.kt"
|
||||
flags = ['-tr', '--disable', 'devirtualization']
|
||||
arguments = ['--ktest_logger=SILENT']
|
||||
}
|
||||
|
||||
task if_else(type: RunKonanTest) {
|
||||
|
||||
@@ -24,8 +24,18 @@ fun Array<Int>.assertSorted(cmp: Comparator<Int>, message: String = "") {
|
||||
}
|
||||
}
|
||||
|
||||
fun Array<MyComparable>.assertSorted(message: String = "") {
|
||||
for (i in 1 until size) {
|
||||
assertTrue(this[i - 1] <= this[i], message)
|
||||
}
|
||||
}
|
||||
|
||||
data class ComparatorInfo(val name: String, val comparator: Comparator<Int>, val isCorrect: Boolean)
|
||||
|
||||
class MyComparable (val value: Int, val comparator: Comparator<Int>): Comparable<MyComparable> {
|
||||
override fun compareTo(other: MyComparable): Int = comparator.compare(value, other.value)
|
||||
}
|
||||
|
||||
// Assert that the array is sorted in terms of a comparator only for correct/partially correct cases
|
||||
val comparators = listOf<ComparatorInfo>(
|
||||
ComparatorInfo("Correct increasing", correctIncreasing , true),
|
||||
@@ -68,8 +78,8 @@ val arrays = listOf<Array<Int>>(
|
||||
@Test fun runTest() {
|
||||
arrays.forEach { array ->
|
||||
comparators.forEach {
|
||||
// Test with custom comparator
|
||||
val arrayUnderTest = array.copyOf()
|
||||
|
||||
arrayUnderTest.sortWith(it.comparator)
|
||||
if (it.isCorrect) {
|
||||
arrayUnderTest.assertSorted(it.comparator, """
|
||||
@@ -78,6 +88,20 @@ val arrays = listOf<Array<Int>>(
|
||||
Array after sorting: ${arrayUnderTest.joinToString()}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
// Test of a custom comparable
|
||||
val comparableArrayUnderTest = Array(array.size) { i ->
|
||||
MyComparable(array[i], it.comparator)
|
||||
}
|
||||
comparableArrayUnderTest.sort()
|
||||
|
||||
if (it.isCorrect) {
|
||||
comparableArrayUnderTest.assertSorted("""
|
||||
Assert sorted failed for Comparable: "${it.name}"
|
||||
Array: ${array.joinToString()}
|
||||
Array after sorting: ${comparableArrayUnderTest.joinToString()}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user