[BAEL-19886] - moved another kotlin article to core-kotlin-modules/core-kotlin-2 module

This commit is contained in:
catalin-burcea
2020-01-22 22:27:02 +02:00
parent 0246bbf355
commit fb01be346b
2 changed files with 0 additions and 4 deletions

View File

@@ -0,0 +1,54 @@
package com.baeldung.sequeces
import org.junit.Test
import kotlin.test.assertEquals
import java.time.Instant
class SequencesTest {
@Test
fun shouldBuildSequenceWhenUsingFromElements() {
val seqOfElements = sequenceOf("first" ,"second", "third")
.toList()
assertEquals(3, seqOfElements.count())
}
@Test
fun shouldBuildSequenceWhenUsingFromFunction() {
val seqFromFunction = generateSequence(Instant.now()) {it.plusSeconds(1)}
.take(3)
.toList()
assertEquals(3, seqFromFunction.count())
}
@Test
fun shouldBuildSequenceWhenUsingFromChunks() {
val seqFromChunks = sequence {
yield(1)
yieldAll((2..5).toList())
}.toList()
assertEquals(5, seqFromChunks.count())
}
@Test
fun shouldBuildSequenceWhenUsingFromCollection() {
val seqFromIterable = (1..10)
.asSequence()
.toList()
assertEquals(10, seqFromIterable.count())
}
@Test
fun shouldShowNoCountDiffWhenUsingWithAndWithoutSequence() {
val withSequence = (1..10).asSequence()
.filter{it % 2 == 1}
.map { it * 2 }
.toList()
val withoutSequence = (1..10)
.filter{it % 2 == 1}
.map { it * 2 }
.toList()
assertEquals(withSequence.count(), withoutSequence.count())
}
}