added more iterator based tests to the JS compilation; exposes some issue with enums...

This commit is contained in:
James Strachan
2012-07-04 09:19:03 +01:00
parent 64b00f0a19
commit bb8dc49692
5 changed files with 39 additions and 30 deletions
@@ -5,7 +5,7 @@ import java.util.NoSuchElementException
// TODO should not need this - its here for the JS stuff
import java.lang.UnsupportedOperationException
enum class State {
public enum class State {
Ready
NotReady
Done
@@ -0,0 +1,28 @@
package iterators
import kotlin.test.assertEquals
import org.junit.Test as test
import kotlin.test.failsWith
class IteratorsJVMTest {
test fun flatMapAndTakeExtractTheTransformedElements() {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i).sure()
var index = 0
iterate<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayList(
'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())
}
}
@@ -4,13 +4,13 @@ import kotlin.test.assertEquals
import org.junit.Test as test
import kotlin.test.failsWith
class IteratorsTest {
fun fibonacci(): java.util.Iterator<Int> {
// fibonacci terms
var index = 0; var a = 0; var b = 1
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
}
private fun fibonacci(): java.util.Iterator<Int> {
// fibonacci terms
var index = 0; var a = 0; var b = 1
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
}
class IteratorsTest {
test fun filterAndTakeWhileExtractTheElementsWithinRange() {
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
@@ -29,25 +29,6 @@ class IteratorsTest {
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
}
test fun flatMapAndTakeExtractTheTransformedElements() {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i).sure()
var index = 0
iterate<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayList(
'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 joinConcatenatesTheFirstNElementsAboveAThreshold() {
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
}