Add onEachIndexed similar to forEachIndexed #KT-37161

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-01 02:52:25 +03:00
parent 57d390bab0
commit 4a7b1b210a
14 changed files with 368 additions and 17 deletions
@@ -994,6 +994,21 @@ class UnsignedArraysTest {
assertEquals(listOf<ULong>(1, 2, 3), mutableListOf<ULong>().apply { ulongArrayOf(1, 2, 3).onEach { add(it) } })
}
@Test
fun onEachIndexed() {
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { uintArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUInt() + e) } })
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { ubyteArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUByte() + e) } })
assertEquals(listOf<UInt>(1, 3, 5), mutableListOf<UInt>().apply { ushortArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toUShort() + e) } })
assertEquals(listOf<ULong>(1, 3, 5), mutableListOf<ULong>().apply { ulongArrayOf(1, 2, 3).onEachIndexed { i, e -> add(i.toULong() + e) } })
val empty = arrayOf<UInt>()
assertSame(empty, empty.onEachIndexed { i, e -> fail("Should be unreachable: $i, $e") })
// Identity equality for arguments of types ULongArray and ULongArray is forbidden
// val nonEmpty = ulongArrayOf(1, 2, 3)
// assertSame(nonEmpty, nonEmpty.onEachIndexed { _, _ -> })
}
@Test
fun drop() {
expect(listOf(1.toUByte())) { ubyteArrayOf(1).drop(0) }