standard library: 'maxBy' and 'minBy' functions added (#KT-4267 fixed)

This commit is contained in:
nik
2013-11-30 00:22:40 +04:00
committed by Evgeny Gerashchenko
parent 45e9211943
commit ac6dc9fa54
16 changed files with 654 additions and 0 deletions
+32
View File
@@ -105,6 +105,38 @@ class ArraysJVMTest {
expect(3.0, { doubleArray(2.0, 3.0).max() })
}
test fun minBy() {
expect(null, { intArray().minBy { it } })
expect(1, { intArray(1).minBy { it } })
expect(3, { intArray(2, 3).minBy { -it } })
expect(2000000000000, { longArray(3000000000000, 2000000000000).minBy { it + 1 } })
expect(1, { byteArray(1, 3, 2).minBy { it * it } })
expect(3, { shortArray(3, 2).minBy { "a" } })
expect(2.0, { floatArray(3.0, 2.0).minBy { it.toString() } })
expect(2.0, { doubleArray(2.0, 3.0).minBy { Math.sqrt(it) } })
}
test fun minIndex() {
val a = intArray(1, 7, 9, -42, 54, 93)
expect(3, { a.indices.minBy { a[it] } })
}
test fun maxBy() {
expect(null, { intArray().maxBy { it } })
expect(1, { intArray(1).maxBy { it } })
expect(2, { intArray(2, 3).maxBy { -it } })
expect(3000000000000, { longArray(3000000000000, 2000000000000).maxBy { it + 1 } })
expect(3, { byteArray(1, 3, 2).maxBy { it * it } })
expect(3, { shortArray(3, 2).maxBy { "a" } })
expect(3.0, { floatArray(3.0, 2.0).maxBy { it.toString() } })
expect(3.0, { doubleArray(2.0, 3.0).maxBy { Math.sqrt(it) } })
}
test fun maxIndex() {
val a = intArray(1, 7, 9, 239, 54, 93)
expect(3, { a.indices.maxBy { a[it] } })
}
test fun sum() {
expect(0) { intArray().sum() }
expect(14) { intArray(2, 3, 9).sum() }
+28
View File
@@ -119,6 +119,34 @@ class ArraysTest {
expect("b", { array("a", "b").max() })
}
test fun minBy() {
expect(null, { array<Int>().minBy { it } })
expect(1, { array(1).minBy { it } })
expect(3, { array(2, 3).minBy { -it } })
expect('a', { array('a', 'b').minBy { "x$it" } })
expect("b", { array("b", "abc").minBy { it.length } })
}
test fun maxBy() {
expect(null, { array<Int>().maxBy { it } })
expect(1, { array(1).maxBy { it } })
expect(2, { array(2, 3).maxBy { -it } })
expect('b', { array('a', 'b').maxBy { "x$it" } })
expect("abc", { array("b", "abc").maxBy { it.length } })
}
test fun minByEvaluateOnce() {
var c = 0
expect(1, { array(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
}
test fun maxByEvaluateOnce() {
var c = 0
expect(5, { array(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
}
test fun sum() {
expect(0) { array<Int>().sum() }
expect(14) { array(2, 3, 9).sum() }
+38
View File
@@ -436,6 +436,44 @@ class CollectionTest {
expect(3, { listOf(2, 3).iterator().max() })
}
test fun minBy() {
expect(null, { listOf<Int>().minBy { it } })
expect(1, { listOf(1).minBy { it } })
expect(3, { listOf(2, 3).minBy { -it } })
expect('a', { listOf('a', 'b').minBy { "x$it" } })
expect("b", { listOf("b", "abc").minBy { it.length } })
expect(null, { listOf<Int>().iterator().minBy { it } })
expect(3, { listOf(2, 3).iterator().minBy { -it } })
}
test fun maxBy() {
expect(null, { listOf<Int>().maxBy { it } })
expect(1, { listOf(1).maxBy { it } })
expect(2, { listOf(2, 3).maxBy { -it } })
expect('b', { listOf('a', 'b').maxBy { "x$it" } })
expect("abc", { listOf("b", "abc").maxBy { it.length } })
expect(null, { listOf<Int>().iterator().maxBy { it } })
expect(2, { listOf(2, 3).iterator().maxBy { -it } })
}
test fun minByEvaluateOnce() {
var c = 0
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
c = 0
expect(1, { listOf(5, 4, 3, 2, 1).iterator().minBy { c++; it * it } })
assertEquals(5, c)
}
test fun maxByEvaluateOnce() {
var c = 0
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
c = 0
expect(5, { listOf(5, 4, 3, 2, 1).iterator().maxBy { c++; it * it } })
assertEquals(5, c)
}
test fun sum() {
expect(0) { ArrayList<Int>().sum() }
expect(14) { arrayListOf(2, 3, 9).sum() }