]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/generator/smoke.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / generator / smoke.rs
CommitLineData
ea8adc8c
XL
1// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
abe05a73 11// ignore-emscripten no threads support
ea8adc8c
XL
12// compile-flags: --test
13
14#![feature(generators, generator_trait)]
15
16use std::ops::{GeneratorState, Generator};
17use std::thread;
18
19#[test]
20fn simple() {
21 let mut foo = || {
22 if false {
23 yield;
24 }
25 };
26
27 match foo.resume() {
28 GeneratorState::Complete(()) => {}
29 s => panic!("bad state: {:?}", s),
30 }
31}
32
33#[test]
34fn return_capture() {
35 let a = String::from("foo");
36 let mut foo = || {
37 if false {
38 yield;
39 }
40 a
41 };
42
43 match foo.resume() {
44 GeneratorState::Complete(ref s) if *s == "foo" => {}
45 s => panic!("bad state: {:?}", s),
46 }
47}
48
49#[test]
50fn simple_yield() {
51 let mut foo = || {
52 yield;
53 };
54
55 match foo.resume() {
56 GeneratorState::Yielded(()) => {}
57 s => panic!("bad state: {:?}", s),
58 }
59 match foo.resume() {
60 GeneratorState::Complete(()) => {}
61 s => panic!("bad state: {:?}", s),
62 }
63}
64
65#[test]
66fn yield_capture() {
67 let b = String::from("foo");
68 let mut foo = || {
69 yield b;
70 };
71
72 match foo.resume() {
73 GeneratorState::Yielded(ref s) if *s == "foo" => {}
74 s => panic!("bad state: {:?}", s),
75 }
76 match foo.resume() {
77 GeneratorState::Complete(()) => {}
78 s => panic!("bad state: {:?}", s),
79 }
80}
81
82#[test]
83fn simple_yield_value() {
84 let mut foo = || {
85 yield String::from("bar");
86 return String::from("foo")
87 };
88
89 match foo.resume() {
90 GeneratorState::Yielded(ref s) if *s == "bar" => {}
91 s => panic!("bad state: {:?}", s),
92 }
93 match foo.resume() {
94 GeneratorState::Complete(ref s) if *s == "foo" => {}
95 s => panic!("bad state: {:?}", s),
96 }
97}
98
99#[test]
100fn return_after_yield() {
101 let a = String::from("foo");
102 let mut foo = || {
103 yield;
104 return a
105 };
106
107 match foo.resume() {
108 GeneratorState::Yielded(()) => {}
109 s => panic!("bad state: {:?}", s),
110 }
111 match foo.resume() {
112 GeneratorState::Complete(ref s) if *s == "foo" => {}
113 s => panic!("bad state: {:?}", s),
114 }
115}
116
117#[test]
118fn send_and_sync() {
119 assert_send_sync(|| {
120 yield
121 });
122 assert_send_sync(|| {
123 yield String::from("foo");
124 });
125 assert_send_sync(|| {
126 yield;
127 return String::from("foo");
128 });
129 let a = 3;
130 assert_send_sync(|| {
131 yield a;
132 return
133 });
134 let a = 3;
135 assert_send_sync(move || {
136 yield a;
137 return
138 });
139 let a = String::from("a");
140 assert_send_sync(|| {
141 yield ;
142 drop(a);
143 return
144 });
145 let a = String::from("a");
146 assert_send_sync(move || {
147 yield ;
148 drop(a);
149 return
150 });
151
152 fn assert_send_sync<T: Send + Sync>(_: T) {}
153}
154
155#[test]
156fn send_over_threads() {
157 let mut foo = || { yield };
158 thread::spawn(move || {
159 match foo.resume() {
160 GeneratorState::Yielded(()) => {}
161 s => panic!("bad state: {:?}", s),
162 }
163 match foo.resume() {
164 GeneratorState::Complete(()) => {}
165 s => panic!("bad state: {:?}", s),
166 }
167 }).join().unwrap();
168
169 let a = String::from("a");
170 let mut foo = || { yield a };
171 thread::spawn(move || {
172 match foo.resume() {
173 GeneratorState::Yielded(ref s) if *s == "a" => {}
174 s => panic!("bad state: {:?}", s),
175 }
176 match foo.resume() {
177 GeneratorState::Complete(()) => {}
178 s => panic!("bad state: {:?}", s),
179 }
180 }).join().unwrap();
181}