]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generator/smoke.rs
Auto merge of #102655 - joboet:windows_tls_opt, r=ChrisDenton
[rust.git] / src / test / ui / generator / smoke.rs
1 // run-pass
2
3 // revisions: default nomiropt
4 //[nomiropt]compile-flags: -Z mir-opt-level=0
5
6 // ignore-emscripten no threads support
7 // compile-flags: --test
8
9 #![feature(generators, generator_trait)]
10
11 use std::ops::{GeneratorState, Generator};
12 use std::pin::Pin;
13 use std::thread;
14
15 #[test]
16 fn simple() {
17     let mut foo = || {
18         if false {
19             yield;
20         }
21     };
22
23     match Pin::new(&mut foo).resume(()) {
24         GeneratorState::Complete(()) => {}
25         s => panic!("bad state: {:?}", s),
26     }
27 }
28
29 #[test]
30 fn return_capture() {
31     let a = String::from("foo");
32     let mut foo = || {
33         if false {
34             yield;
35         }
36         a
37     };
38
39     match Pin::new(&mut foo).resume(()) {
40         GeneratorState::Complete(ref s) if *s == "foo" => {}
41         s => panic!("bad state: {:?}", s),
42     }
43 }
44
45 #[test]
46 fn simple_yield() {
47     let mut foo = || {
48         yield;
49     };
50
51     match Pin::new(&mut foo).resume(()) {
52         GeneratorState::Yielded(()) => {}
53         s => panic!("bad state: {:?}", s),
54     }
55     match Pin::new(&mut foo).resume(()) {
56         GeneratorState::Complete(()) => {}
57         s => panic!("bad state: {:?}", s),
58     }
59 }
60
61 #[test]
62 fn yield_capture() {
63     let b = String::from("foo");
64     let mut foo = || {
65         yield b;
66     };
67
68     match Pin::new(&mut foo).resume(()) {
69         GeneratorState::Yielded(ref s) if *s == "foo" => {}
70         s => panic!("bad state: {:?}", s),
71     }
72     match Pin::new(&mut foo).resume(()) {
73         GeneratorState::Complete(()) => {}
74         s => panic!("bad state: {:?}", s),
75     }
76 }
77
78 #[test]
79 fn simple_yield_value() {
80     let mut foo = || {
81         yield String::from("bar");
82         return String::from("foo")
83     };
84
85     match Pin::new(&mut foo).resume(()) {
86         GeneratorState::Yielded(ref s) if *s == "bar" => {}
87         s => panic!("bad state: {:?}", s),
88     }
89     match Pin::new(&mut foo).resume(()) {
90         GeneratorState::Complete(ref s) if *s == "foo" => {}
91         s => panic!("bad state: {:?}", s),
92     }
93 }
94
95 #[test]
96 fn return_after_yield() {
97     let a = String::from("foo");
98     let mut foo = || {
99         yield;
100         return a
101     };
102
103     match Pin::new(&mut foo).resume(()) {
104         GeneratorState::Yielded(()) => {}
105         s => panic!("bad state: {:?}", s),
106     }
107     match Pin::new(&mut foo).resume(()) {
108         GeneratorState::Complete(ref s) if *s == "foo" => {}
109         s => panic!("bad state: {:?}", s),
110     }
111 }
112
113 #[test]
114 fn send_and_sync() {
115     assert_send_sync(|| {
116         yield
117     });
118     assert_send_sync(|| {
119         yield String::from("foo");
120     });
121     assert_send_sync(|| {
122         yield;
123         return String::from("foo");
124     });
125     let a = 3;
126     assert_send_sync(|| {
127         yield a;
128         return
129     });
130     let a = 3;
131     assert_send_sync(move || {
132         yield a;
133         return
134     });
135     let a = String::from("a");
136     assert_send_sync(|| {
137         yield ;
138         drop(a);
139         return
140     });
141     let a = String::from("a");
142     assert_send_sync(move || {
143         yield ;
144         drop(a);
145         return
146     });
147
148     fn assert_send_sync<T: Send + Sync>(_: T) {}
149 }
150
151 #[test]
152 fn send_over_threads() {
153     let mut foo = || { yield };
154     thread::spawn(move || {
155         match Pin::new(&mut foo).resume(()) {
156             GeneratorState::Yielded(()) => {}
157             s => panic!("bad state: {:?}", s),
158         }
159         match Pin::new(&mut foo).resume(()) {
160             GeneratorState::Complete(()) => {}
161             s => panic!("bad state: {:?}", s),
162         }
163     }).join().unwrap();
164
165     let a = String::from("a");
166     let mut foo = || { yield a };
167     thread::spawn(move || {
168         match Pin::new(&mut foo).resume(()) {
169             GeneratorState::Yielded(ref s) if *s == "a" => {}
170             s => panic!("bad state: {:?}", s),
171         }
172         match Pin::new(&mut foo).resume(()) {
173             GeneratorState::Complete(()) => {}
174             s => panic!("bad state: {:?}", s),
175         }
176     }).join().unwrap();
177 }