Provide minWith and maxWith to find maximum and minimum values according to the given comparator.

#KT-9002 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-30 19:57:42 +03:00
parent b5e637bed5
commit 055c71e8d0
11 changed files with 433 additions and 1 deletions
@@ -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 } })