Add more tests on streams and move relevant code from old iterators tests.

This commit is contained in:
Ilya Ryzhenkov
2014-09-01 20:03:22 +04:00
parent c5c1ecf6c0
commit 3e4cc87c87
2 changed files with 71 additions and 41 deletions
@@ -17,28 +17,4 @@ class IteratorsJVMTest {
assertEquals(15, sum)
}
test fun flatMapAndTakeExtractTheTransformedElements() {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i)!!
var index = 0
stream<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayListOf(
'0', // fibonacci(0) = 0
'1', // fibonacci(1) = 1
'1', // fibonacci(2) = 1
'1', '0', // fibonacci(3) = 2
'1', '1', // fibonacci(4) = 3
'1', '0', '1' // fibonacci(5) = 5
)
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
}
test fun flatMapOnStream() {
val result = listOf(1, 2).stream().flatMap<Int, Int> { (0..it).stream() }
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
}
}