GeneratorSequence: do not get first value, until it's requested by iterator, allow initialValue to be null and return EmptySequence in this case.
#KT-9153 Fixed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test as test
|
||||
import org.junit.Test
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
@@ -233,6 +234,24 @@ public class SequenceTest {
|
||||
assertEquals(expected, values.toList(), "Iterating sequence second time yields the same result")
|
||||
}
|
||||
|
||||
@Test fun sequenceFromFunctionWithLazyInitialValue() {
|
||||
var start = 3
|
||||
val values = sequence({ start }, { n -> if (n > 0) n - 1 else null })
|
||||
val expected = listOf(3, 2, 1, 0)
|
||||
assertEquals(expected, values.toList())
|
||||
assertEquals(expected, values.toList(), "Iterating sequence second time yields the same result")
|
||||
|
||||
start = 2
|
||||
assertEquals(expected.drop(1), values.toList(), "Initial value function is called on each iterator request")
|
||||
|
||||
// does not throw on construction
|
||||
val errorValues = sequence<Int>({ (throw IllegalStateException()) }, { null })
|
||||
// does not throw on iteration
|
||||
val iterator = errorValues.iterator()
|
||||
// throws on advancing
|
||||
assertFails { iterator.next() }
|
||||
}
|
||||
|
||||
|
||||
@test fun sequenceFromIterator() {
|
||||
val list = listOf(3, 2, 1, 0)
|
||||
|
||||
Reference in New Issue
Block a user