Introduce filterIndexed

#KT-9502 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-07 17:55:33 +03:00
parent ee759c10c8
commit 008a8059cf
9 changed files with 310 additions and 27 deletions
@@ -736,6 +736,15 @@ class ArraysTest {
expect(listOf("b"), { arrayOf("a", "b").filter { it > "a" } })
}
@test fun filterIndexed() {
expect(listOf(), { intArrayOf().filterIndexed { i, v -> i > v } })
expect(listOf(2, 5, 8), { intArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == value % 2 } })
expect(listOf<Long>(2, 5, 8), { longArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } })
expect(listOf<Byte>(2, 5, 8), { byteArrayOf(2, 4, 3, 5, 8).filterIndexed { index, value -> index % 2 == (value % 2).toInt() } })
expect(listOf('9', 'e', 'a'), { charArrayOf('9', 'e', 'd', 'a').filterIndexed { index, c -> c == 'a' || index < 2 }})
expect(listOf("a", "c", "d"), { arrayOf("a", "b", "c", "d").filterIndexed { index, s -> s == "a" || index >= 2 } })
}
@test fun filterNot() {
expect(listOf(), { intArrayOf().filterNot { it > 2 } })
expect(listOf(1), { intArrayOf(1).filterNot { it > 2 } })
@@ -146,6 +146,11 @@ abstract class IterableTests<T : Iterable<String>>(val data: T, val empty: T) {
assertEquals(listOf("foo"), foo)
}
@Test fun filterIndexed() {
val result = data.filterIndexed { index, value -> value.first() == ('a' + index) }
assertEquals(listOf("bar"), result)
}
@Test
fun drop() {
val foo = data.drop(1)
@@ -45,6 +45,10 @@ public class SequenceTest {
}
}
@test fun filterIndexed() {
assertEquals(listOf(1, 2, 5, 13, 34), fibonacci().filterIndexed { index, value -> index % 2 == 1 }.take(5).toList())
}
@test fun filterNullable() {
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filter { it == null || it == "foo" }
+9
View File
@@ -746,6 +746,15 @@ class StringTest {
assertContentEquals("abcd", arg1("a1b2c3d4").filterNot { it.isAsciiDigit() })
}
@test fun filterIndexed() {
val data = "abedcf"
assertEquals("abdf", data.filterIndexed { index, c -> c == 'a' + index })
}
@test fun filterIndexedCharSequence() = withOneCharSequenceArg("abedcf") { data ->
assertContentEquals("abdf", data.filterIndexed { index, c -> c == 'a' + index })
}
@test fun all() = withOneCharSequenceArg("AbCd") { data ->
assertTrue {
data.all { it.isAsciiLetter() }