rust/src/test/run-pass/generator/smoke.rs

182 lines
4.0 KiB
Rust
Raw Normal View History

2017-07-07 18:12:44 -05:00
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.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.
2017-08-28 01:37:50 -05:00
// ignore-emscripten
2017-07-07 18:12:44 -05:00
// compile-flags: --test
#![feature(generators, generator_trait)]
2017-07-21 21:20:46 -05:00
use std::ops::{GeneratorState, Generator};
2017-07-07 18:12:44 -05:00
use std::thread;
#[test]
fn simple() {
let mut foo = || {
if false {
yield;
}
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn return_capture() {
let a = String::from("foo");
let mut foo = || {
if false {
yield;
}
a
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(ref s) if *s == "foo" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn simple_yield() {
let mut foo = || {
yield;
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn yield_capture() {
let b = String::from("foo");
let mut foo = || {
yield b;
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(ref s) if *s == "foo" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn simple_yield_value() {
let mut foo = || {
yield String::from("bar");
return String::from("foo")
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(ref s) if *s == "bar" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(ref s) if *s == "foo" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn return_after_yield() {
let a = String::from("foo");
let mut foo = || {
yield;
return a
};
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(ref s) if *s == "foo" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}
#[test]
fn send_and_sync() {
assert_send_sync(|| {
yield
});
assert_send_sync(|| {
yield String::from("foo");
});
assert_send_sync(|| {
yield;
return String::from("foo");
});
let a = 3;
assert_send_sync(|| {
yield a;
return
});
let a = 3;
assert_send_sync(move || {
yield a;
return
});
let a = String::from("a");
assert_send_sync(|| {
yield ;
drop(a);
return
});
let a = String::from("a");
assert_send_sync(move || {
yield ;
drop(a);
return
});
fn assert_send_sync<T: Send + Sync>(_: T) {}
}
#[test]
fn send_over_threads() {
let mut foo = || { yield };
thread::spawn(move || {
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}).join().unwrap();
let a = String::from("a");
let mut foo = || { yield a };
thread::spawn(move || {
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Yielded(ref s) if *s == "a" => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
2017-07-11 14:57:05 -05:00
match foo.resume() {
2017-07-21 21:20:46 -05:00
GeneratorState::Complete(()) => {}
2017-07-07 18:12:44 -05:00
s => panic!("bad state: {:?}", s),
}
}).join().unwrap();
}