Verify index expectations for reduceIndexed/reduceRightIndexed
This commit is contained in:
@@ -223,6 +223,15 @@ class CollectionTest {
|
||||
list.reduceIndexed { index, a, b -> if (index == 3) a else a + b }
|
||||
}
|
||||
|
||||
expect(5) {
|
||||
listOf(2, 3).reduceIndexed { index, acc: Number, e ->
|
||||
assertEquals(1, index)
|
||||
assertEquals(2, acc)
|
||||
assertEquals(3, e)
|
||||
acc.toInt() + e
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
arrayListOf<Int>().reduceIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
@@ -234,6 +243,15 @@ class CollectionTest {
|
||||
list.reduceRightIndexed { index, a, b -> if (index == 0) b else a + b }
|
||||
}
|
||||
|
||||
expect(1) {
|
||||
listOf(2, 3).reduceRightIndexed { index, e, acc: Number ->
|
||||
assertEquals(0, index)
|
||||
assertEquals(3, acc)
|
||||
assertEquals(2, e)
|
||||
acc.toInt() - e
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
arrayListOf<Int>().reduceRightIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
|
||||
@@ -863,6 +863,15 @@ class StringTest {
|
||||
// get the 3rd character
|
||||
assertEquals('c', arg1("bacfd").reduceIndexed { index, v, c -> if (index == 2) c else v })
|
||||
|
||||
expect('c') {
|
||||
"ab".reduceIndexed { index, acc, e ->
|
||||
assertEquals(1, index)
|
||||
assertEquals('a', acc)
|
||||
assertEquals('b', e)
|
||||
e + (e - acc)
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
arg1("").reduceIndexed { index, a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
@@ -872,6 +881,15 @@ class StringTest {
|
||||
// get the 3rd character
|
||||
assertEquals('c', arg1("bacfd").reduceRightIndexed { index, c, v -> if (index == 2) c else v })
|
||||
|
||||
expect('c') {
|
||||
"ab".reduceRightIndexed { index, e, acc ->
|
||||
assertEquals(0, index)
|
||||
assertEquals('b', acc)
|
||||
assertEquals('a', e)
|
||||
acc + (acc - e)
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
arg1("").reduceRightIndexed { index, a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
|
||||
Reference in New Issue
Block a user