2016-07-17 11:11:33 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
2016-08-31 06:26:42 +08:00
|
|
|
// Copyright (c) whitequark <whitequark@whitequark.org>,
|
|
|
|
// edef <edef@edef.eu>
|
2016-08-21 05:45:01 +08:00
|
|
|
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
|
|
|
|
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
|
|
|
|
// http://opensource.org/licenses/MIT>, at your option. This file may not be
|
|
|
|
// copied, modified, or distributed except according to those terms.
|
2016-07-17 11:11:33 +08:00
|
|
|
extern crate fringe;
|
|
|
|
|
2016-09-07 20:56:32 +08:00
|
|
|
use fringe::{SliceStack, OwnedStack, OsStack};
|
2016-08-19 21:58:45 +08:00
|
|
|
use fringe::generator::{Generator, Yielder};
|
|
|
|
|
2016-09-16 16:34:49 +08:00
|
|
|
fn add_one_fn(yielder: &Yielder<i32, i32>, mut input: i32) {
|
2016-08-19 21:58:45 +08:00
|
|
|
loop {
|
|
|
|
if input == 0 { break }
|
|
|
|
input = yielder.suspend(input + 1)
|
|
|
|
}
|
|
|
|
}
|
2016-07-17 11:11:33 +08:00
|
|
|
|
2016-09-15 03:18:31 +08:00
|
|
|
fn new_add_one() -> Generator<'static, i32, i32, OsStack> {
|
2016-07-17 11:11:33 +08:00
|
|
|
let stack = OsStack::new(0).unwrap();
|
2016-08-19 21:58:45 +08:00
|
|
|
Generator::new(stack, add_one_fn)
|
2016-08-12 05:04:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generator() {
|
|
|
|
let mut add_one = new_add_one();
|
|
|
|
assert_eq!(add_one.resume(1), Some(2));
|
|
|
|
assert_eq!(add_one.resume(2), Some(3));
|
|
|
|
assert_eq!(add_one.resume(0), None);
|
2016-07-17 11:11:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_after_new() {
|
2016-08-12 05:04:40 +08:00
|
|
|
let mut add_one = new_add_one();
|
|
|
|
assert_eq!(add_one.resume(1), Some(2));
|
2016-07-17 11:11:33 +08:00
|
|
|
|
|
|
|
#[inline(never)]
|
2016-08-12 05:04:40 +08:00
|
|
|
fn run_moved(mut add_one: Generator<i32, i32, OsStack>) {
|
|
|
|
assert_eq!(add_one.resume(2), Some(3));
|
|
|
|
assert_eq!(add_one.resume(3), Some(4));
|
|
|
|
assert_eq!(add_one.resume(0), None);
|
2016-07-17 11:11:33 +08:00
|
|
|
}
|
2016-08-12 05:04:40 +08:00
|
|
|
run_moved(add_one);
|
2016-07-17 11:11:33 +08:00
|
|
|
}
|
2016-08-11 10:16:13 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn panic_safety() {
|
|
|
|
struct Wrapper {
|
2016-09-15 03:18:31 +08:00
|
|
|
gen: Generator<'static, (), (), OsStack>
|
2016-08-11 10:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Wrapper {
|
|
|
|
fn drop(&mut self) {
|
2016-08-12 05:04:40 +08:00
|
|
|
self.gen.resume(());
|
2016-08-11 10:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stack = OsStack::new(4 << 20).unwrap();
|
2016-08-12 05:04:40 +08:00
|
|
|
let gen = Generator::new(stack, move |_yielder, ()| {
|
2016-08-11 10:16:13 +08:00
|
|
|
panic!("foo")
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut wrapper = Wrapper { gen: gen };
|
2016-08-12 05:04:40 +08:00
|
|
|
wrapper.gen.resume(());
|
2016-08-11 10:16:13 +08:00
|
|
|
}
|
2016-08-19 17:18:46 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_slice_stack() {
|
|
|
|
let mut memory = [0; 1024];
|
2016-09-17 12:17:28 +08:00
|
|
|
let stack = SliceStack::new(&mut memory);
|
2016-08-19 21:58:45 +08:00
|
|
|
let mut add_one = unsafe { Generator::unsafe_new(stack, add_one_fn) };
|
|
|
|
assert_eq!(add_one.resume(1), Some(2));
|
|
|
|
assert_eq!(add_one.resume(2), Some(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn with_owned_stack() {
|
|
|
|
let stack = OwnedStack::new(1024);
|
|
|
|
let mut add_one = unsafe { Generator::unsafe_new(stack, add_one_fn) };
|
2016-08-19 17:18:46 +08:00
|
|
|
assert_eq!(add_one.resume(1), Some(2));
|
|
|
|
assert_eq!(add_one.resume(2), Some(3));
|
|
|
|
}
|
2016-08-31 06:26:42 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn forget_yielded() {
|
2017-03-06 03:34:04 +08:00
|
|
|
use std::cell::Cell;
|
|
|
|
struct Dropper<'a>(&'a Cell<bool>);
|
2016-08-31 06:26:42 +08:00
|
|
|
|
2017-03-06 03:34:04 +08:00
|
|
|
impl<'a> Drop for Dropper<'a> {
|
2016-08-31 06:26:42 +08:00
|
|
|
fn drop(&mut self) {
|
2017-03-06 03:34:04 +08:00
|
|
|
if self.0.get() {
|
|
|
|
panic!("double drop!")
|
2016-08-31 06:26:42 +08:00
|
|
|
}
|
2017-03-06 03:34:04 +08:00
|
|
|
self.0.set(true);
|
2016-08-31 06:26:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stack = fringe::OsStack::new(1<<16).unwrap();
|
2017-03-06 03:34:04 +08:00
|
|
|
let flag = Cell::new(false);
|
2016-08-31 06:26:42 +08:00
|
|
|
let mut generator = Generator::new(stack, |yielder, ()| {
|
2017-03-06 03:34:04 +08:00
|
|
|
yielder.suspend(Dropper(&flag));
|
2016-08-31 06:26:42 +08:00
|
|
|
});
|
|
|
|
generator.resume(());
|
|
|
|
generator.resume(());
|
|
|
|
}
|
2017-02-25 22:16:05 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwrap_returned() {
|
|
|
|
let stack = OsStack::new(0).unwrap();
|
|
|
|
let mut generator = Generator::new(stack, |_, ()| {});
|
|
|
|
assert_eq!(generator.resume(()), None::<()>);
|
|
|
|
generator.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwrap_panicked() {
|
|
|
|
use std::panic;
|
|
|
|
let stack = OsStack::new(4 << 20).unwrap();
|
|
|
|
let mut generator: Generator<(), (), OsStack> = Generator::new(stack, |_, ()| panic!("unwind"));
|
|
|
|
{
|
|
|
|
let closure = panic::AssertUnwindSafe(|| generator.resume(()));
|
|
|
|
assert!(panic::catch_unwind(closure).is_err());
|
|
|
|
}
|
|
|
|
generator.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "Argh! Bastard! Don't touch that!")]
|
|
|
|
fn unwrap_running() {
|
|
|
|
let mut add_one = new_add_one();
|
|
|
|
assert_eq!(add_one.resume(1), Some(2));
|
|
|
|
add_one.unwrap();
|
|
|
|
}
|