package cz.softdeluxe.jlib.coroutine

import org.junit.Test
import kotlin.coroutines.experimental.buildSequence
import kotlin.test.assertEquals

/**
 * Created by balat on 3.2.2017.
 */

val fibonacci: Sequence<Long> = buildSequence {
    yield(1L) // first Fibonacci number
    var cur = 1L
    var next = 1L
    while (true) {
        yield(next) // next Fibonacci number
        val tmp = cur + next
        cur = next
        next = tmp
    }
}

class SequenceTest {

    @Test
    fun testFibonacci() {
        val fibList: List<Long> = fibonacci.take(60).toList()
        assertEquals(1, fibList[0])
        assertEquals(1, fibList[1])
        assertEquals(2, fibList[2])
        assertEquals(89, fibList[10])
        assertEquals(6765, fibList[19])
        assertEquals(139583862445, fibList[54])
    }

}