Implement reduceIndexedOrNull and reduceRightIndexedOrNull #KT-36866
This commit is contained in:
@@ -1016,6 +1016,20 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNull() {
|
||||
expect(-1) { intArrayOf(1, 2, 3).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1.toLong()) { longArrayOf(1, 2, 3).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1F) { floatArrayOf(1F, 2F, 3F).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(-1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceIndexedOrNull { index, a, b -> if (a > b && index == 1) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceIndexedOrNull { index, a, b -> a && b || index == 2 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceIndexedOrNull { index, a, b -> a && b && index != 1 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceIndexedOrNull { index, a, b -> if (index != 2) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceIndexedOrNull { index, a, b -> if (index != 2) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
expect(null, { intArrayOf().reduceIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexed() {
|
||||
expect(1) { intArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
expect(1.toLong()) { longArrayOf(1, 2, 3).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
@@ -1032,6 +1046,20 @@ class ArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexedOrNull() {
|
||||
expect(1) { intArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1.toLong()) { longArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1F) { floatArrayOf(1F, 2F, 3F).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect(1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceRightIndexedOrNull { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceRightIndexedOrNull { index, a, b -> if (a > b && index == 0) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceRightIndexedOrNull { index, a, b -> a && b || index == 1 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceRightIndexedOrNull { index, a, b -> a && b && index != 0 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, a, b -> if (index != 1) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, a, b -> if (index != 1) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
expect(null, { intArrayOf().reduceRightIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduce() {
|
||||
expect(-4) { intArrayOf(1, 2, 3).reduce { a, b -> a - b } }
|
||||
expect(-4.toLong()) { longArrayOf(1, 2, 3).reduce { a, b -> a - b } }
|
||||
|
||||
@@ -296,6 +296,24 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNull() {
|
||||
expect("123") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduceIndexedOrNull { index, a, b -> if (index == 3) a else a + b }
|
||||
}
|
||||
|
||||
expect(5) {
|
||||
listOf(2, 3).reduceIndexedOrNull { index, acc: Number, e ->
|
||||
assertEquals(1, index)
|
||||
assertEquals(2, acc)
|
||||
assertEquals(3, e)
|
||||
acc.toInt() + e
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arrayListOf<Int>().reduceIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexed() {
|
||||
expect("234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
@@ -316,6 +334,24 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun reduceRightIndexedOrNull() {
|
||||
expect("234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
list.reduceRightIndexedOrNull { index, a, b -> if (index == 0) b else a + b }
|
||||
}
|
||||
|
||||
expect(1) {
|
||||
listOf(2, 3).reduceRightIndexedOrNull { index, e, acc: Number ->
|
||||
assertEquals(0, index)
|
||||
assertEquals(3, acc)
|
||||
assertEquals(2, e)
|
||||
acc.toInt() - e
|
||||
}
|
||||
}
|
||||
|
||||
expect(null, { arrayListOf<Int>().reduceRightIndexedOrNull { index, a, b -> index + a + b } })
|
||||
}
|
||||
|
||||
@Test fun reduce() {
|
||||
expect("1234") {
|
||||
val list = listOf("1", "2", "3", "4")
|
||||
|
||||
@@ -426,6 +426,10 @@ public class SequenceTest {
|
||||
expect(null, { sequenceOf<Int>().reduceOrNull { acc, i -> acc + i } })
|
||||
}
|
||||
|
||||
@Test fun reduceIndexedOrNullOnEmpty() {
|
||||
expect(null, { sequenceOf<Int>().reduceIndexedOrNull { index, acc, i -> acc + i + index } })
|
||||
}
|
||||
|
||||
@Test fun minusElement() = testMinus(expected = listOf("foo", "bar")) { it - "bar" - "zoo" }
|
||||
@Test fun minusCollection() = testMinus { it - listOf("bar", "zoo") }
|
||||
@Test fun minusArray() = testMinus { it - arrayOf("bar", "zoo") }
|
||||
|
||||
@@ -548,6 +548,16 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceIndexedOrNull() {
|
||||
expect(1u) { ubyteArrayOf(3, 2, 1).reduceIndexedOrNull { index, acc, e -> if (index != 2) (e - acc).toUByte() else e } }
|
||||
expect(1u) { ushortArrayOf(3, 2, 1).reduceIndexedOrNull { index, acc, e -> if (index != 2) (e - acc).toUShort() else e } }
|
||||
expect(UInt.MAX_VALUE) { uintArrayOf(1, 2, 3).reduceIndexedOrNull { index, acc, e -> index.toUInt() + acc - e } }
|
||||
expect(ULong.MAX_VALUE) { ulongArrayOf(1, 2, 3).reduceIndexedOrNull { index, acc, e -> index.toULong() + acc - e } }
|
||||
|
||||
expect(null, { uintArrayOf().reduceIndexedOrNull { index, acc, e -> index.toUInt() + e + acc } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceRight() {
|
||||
expect(2u) { ubyteArrayOf(1, 2, 3).reduceRightOrNull { e, acc -> (e - acc).toUByte() } }
|
||||
@@ -582,6 +592,16 @@ class UnsignedArraysTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun reduceRightIndexedOrNull() {
|
||||
expect(1u) { ubyteArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, e, acc -> if (index != 1) (e - acc).toUByte() else e } }
|
||||
expect(1u) { ushortArrayOf(3, 2, 1).reduceRightIndexedOrNull { index, e, acc -> if (index != 1) (e - acc).toUShort() else e } }
|
||||
expect(1u) { uintArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, e, acc -> index.toUInt() + e - acc } }
|
||||
expect(1uL) { ulongArrayOf(1, 2, 3).reduceRightIndexedOrNull { index, e, acc -> index.toULong() + e - acc } }
|
||||
|
||||
expect(null, { uintArrayOf().reduceRightIndexedOrNull { index, e, acc -> index.toUInt() + e + acc } })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun forEach() {
|
||||
var i = 0
|
||||
|
||||
Reference in New Issue
Block a user