added more iterator based tests to the JS compilation; exposes some issue with enums...
This commit is contained in:
@@ -27,8 +27,8 @@ public class StdLibTestToJSTest extends StdLibTestSupport {
|
||||
"dom/DomTest.kt",
|
||||
"js/MapTest.kt",
|
||||
"js/JsDomTest.kt",
|
||||
//"iterators/FunctionIteratorTest.kt",
|
||||
//"iterators/IteratorsTest.kt",
|
||||
"iterators/FunctionIteratorTest.kt",
|
||||
"iterators/IteratorsTest.kt",
|
||||
"GetOrElseTest.kt",
|
||||
"ListTest.kt",
|
||||
"SetTest.kt",
|
||||
|
||||
@@ -115,8 +115,8 @@ public abstract class Config {
|
||||
"/kotlin/JLangIterables.kt",
|
||||
"/kotlin/JLangIterablesLazy.kt",
|
||||
"/kotlin/JLangIterablesSpecial.kt",
|
||||
// TODO "/generated/JUtilIteratorsFromJLangIterables.kt",
|
||||
// TODO "/generated/JUtilIterablesFromJUtilCollections.kt",
|
||||
"/generated/JUtilIteratorsFromJLangIterables.kt",
|
||||
"/generated/JUtilIterablesFromJUtilCollections.kt",
|
||||
"/kotlin/support/AbstractIterator.kt",
|
||||
"/kotlin/Standard.kt",
|
||||
"/kotlin/Strings.kt",
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user