Provide minWith and maxWith to find maximum and minimum values according to the given comparator.
#KT-9002 Fixed
This commit is contained in:
@@ -10,6 +10,8 @@ data class Item(val name: String, val rating: Int) : Comparable<Item> {
|
||||
}
|
||||
}
|
||||
|
||||
val STRING_CASE_INSENSITIVE_ORDER: Comparator<String> = compareBy { it: String -> it.toUpperCase() }.thenBy { it.toLowerCase() }.thenBy { it }
|
||||
|
||||
class OrderingTest {
|
||||
val v1 = Item("wine", 9)
|
||||
val v2 = Item("beer", 10)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package test.collections
|
||||
|
||||
import test.collections.behaviors.listBehavior
|
||||
import test.compare.STRING_CASE_INSENSITIVE_ORDER
|
||||
import java.util.*
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
@@ -146,6 +147,28 @@ class ArraysTest {
|
||||
expect('b', { charArrayOf('a', 'b').max() })
|
||||
}
|
||||
|
||||
@test fun minWith() {
|
||||
assertEquals(null, arrayOf<Int>().minWith(naturalOrder()) )
|
||||
assertEquals("a", arrayOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@test fun minWithInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().minWith(naturalOrder()) })
|
||||
expect(1, { intArrayOf(1).minWith(naturalOrder()) })
|
||||
expect(4, { intArrayOf(2, 3, 4).minWith(compareBy { it % 4 }) })
|
||||
}
|
||||
|
||||
@test fun maxWith() {
|
||||
assertEquals(null, arrayOf<Int>().maxWith(naturalOrder()) )
|
||||
assertEquals("B", arrayOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@test fun maxWithInPrimitiveArrays() {
|
||||
expect(null, { intArrayOf().maxWith(naturalOrder()) })
|
||||
expect(1, { intArrayOf(1).maxWith(naturalOrder()) })
|
||||
expect(-4, { intArrayOf(2, 3, -4).maxWith(compareBy { it*it }) })
|
||||
}
|
||||
|
||||
@test fun minBy() {
|
||||
expect(null, { arrayOf<Int>().minBy { it } })
|
||||
expect(1, { arrayOf(1).minBy { it } })
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.*
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
import test.collections.behaviors.*
|
||||
import test.compare.STRING_CASE_INSENSITIVE_ORDER
|
||||
import java.io.Serializable
|
||||
|
||||
class CollectionTest {
|
||||
@@ -496,6 +497,20 @@ class CollectionTest {
|
||||
expect(3, { listOf(2, 3).asSequence().max() })
|
||||
}
|
||||
|
||||
@test fun minWith() {
|
||||
expect(null, { listOf<Int>().minWith(naturalOrder()) })
|
||||
expect(1, { listOf(1).minWith(naturalOrder()) })
|
||||
expect("a", { listOf("a", "B").minWith(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
expect("a", { listOf("a", "B").asSequence().minWith(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
}
|
||||
|
||||
@test fun maxWith() {
|
||||
expect(null, { listOf<Int>().maxWith(naturalOrder()) })
|
||||
expect(1, { listOf(1).maxWith(naturalOrder()) })
|
||||
expect("B", { listOf("a", "B").maxWith(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
expect("B", { listOf("a", "B").asSequence().maxWith(STRING_CASE_INSENSITIVE_ORDER) })
|
||||
}
|
||||
|
||||
@test fun minBy() {
|
||||
expect(null, { listOf<Int>().minBy { it } })
|
||||
expect(1, { listOf(1).minBy { it } })
|
||||
|
||||
Reference in New Issue
Block a user