Add foldIndexed and reduceIndexed groups of functions
- foldIndexed, foldRightIndexed, reduceIndexed and reduceRightIndexed have been added, in line with filterIndexed etc.; - Test cases added appropriately for the new functions.
This commit is contained in:
committed by
Ilya Gorbunov
parent
b3f390abe5
commit
d58efff974
@@ -619,6 +619,38 @@ class ArraysTest {
|
||||
|
||||
|
||||
|
||||
@test fun reduceIndexed() {
|
||||
expect(-1) { intArrayOf(1, 2, 3).reduceIndexed { index, a, b -> index + a - b } }
|
||||
expect(-1.toLong()) { longArrayOf(1, 2, 3).reduceIndexed { index, a, b -> index + a - b } }
|
||||
expect(-1F) { floatArrayOf(1F, 2F, 3F).reduceIndexed { index, a, b -> index + a - b } }
|
||||
expect(-1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceIndexed { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceIndexed { index, a, b -> if (a > b && index == 1) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceIndexed { index, a, b -> a && b || index == 2 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceIndexed { index, a, b -> a && b && index != 1 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceIndexed { index, a, b -> if (index != 2) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceIndexed { index, a, b -> if (index != 2) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
intArrayOf().reduceIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
|
||||
@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 } }
|
||||
expect(1F) { floatArrayOf(1F, 2F, 3F).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
expect(1.0) { doubleArrayOf(1.0, 2.0, 3.0).reduceRightIndexed { index, a, b -> index + a - b } }
|
||||
expect('2') { charArrayOf('1', '3', '2').reduceRightIndexed { index, a, b -> if (a > b && index == 0) a else b } }
|
||||
expect(true) { booleanArrayOf(true, true, false).reduceRightIndexed { index, a, b -> a && b || index == 1 } }
|
||||
expect(false) { booleanArrayOf(true, true).reduceRightIndexed { index, a, b -> a && b && index != 0 } }
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceRightIndexed { index, a, b -> if (index != 1) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceRightIndexed { index, a, b -> if (index != 1) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
intArrayOf().reduceRightIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
|
||||
@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 } }
|
||||
|
||||
Reference in New Issue
Block a user