fn count_numbers(limit) {
    for n in 0..limit.unwrap_or(10) {
        yield n;
    }
}

#[test]
fn test_generators() {
    assert_eq!(count_numbers(None).iter().collect::<Vec>(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    assert_eq!(count_numbers(Some(2)).iter().collect::<Vec>(), [0, 1]);
}

#[test]
fn resume() {
    use std::ops::generator::GeneratorState;

    fn foo() {
        let a = yield 1;
        let b = yield a;
        b
    }

    let gen = foo();
    let result = 0;

    if let GeneratorState::Yielded(value) = gen.resume(()) {
        result += value;
    } else {
        panic!("unexpected");
    }

    if let GeneratorState::Yielded(value) = gen.resume(2) {
        result += value;
    } else {
        panic!("unexpected");
    }

    if let GeneratorState::Complete(value) = gen.resume(3) {
        result += value;
    } else {
        panic!("unexpected");
    }

    assert_eq!(result, 6);
}

#[test]
fn yields_next() {
    fn foo() {
        yield 1;
        yield 2;
        yield 3;
    }

    let gen = foo();
    let result = 0;

    while let Some(value) = gen.next() {
        result += value;
    }

    assert_eq!(result, 6);
}
