1 // Copyright 2012 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.
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.
11 use std::sync::mpsc::channel;
14 // Tests of ports and channels on various types
16 struct R {val0: isize, val1: u8, val2: char}
18 let (tx, rx) = channel();
19 let r0: R = R {val0: 0, val1: 1, val2: '2'};
22 r1 = rx.recv().unwrap();
23 assert_eq!(r1.val0, 0);
24 assert_eq!(r1.val1, 1);
25 assert_eq!(r1.val2, '2');
29 let (tx, rx) = channel();
30 let v0: Vec<isize> = vec!(0, 1, 2);
32 let v1 = rx.recv().unwrap();
39 let (tx, rx) = channel();
40 let s0 = "test".to_string();
42 let s1 = rx.recv().unwrap();
43 assert_eq!(s1.as_bytes()[0], 't' as u8);
44 assert_eq!(s1.as_bytes()[1], 'e' as u8);
45 assert_eq!(s1.as_bytes()[2], 's' as u8);
46 assert_eq!(s1.as_bytes()[3], 't' as u8);
56 impl cmp::PartialEq for t {
57 fn eq(&self, other: &t) -> bool {
67 t::tag2(e0b) => e0a == e0b,
71 t::tag3(e0a, e1a, e2a) => {
73 t::tag3(e0b, e1b, e2b) =>
74 e0a == e0b && e1a == e1b && e2a == e2b,
80 fn ne(&self, other: &t) -> bool { !(*self).eq(other) }
84 let (tx, rx) = channel();
85 tx.send(t::tag1).unwrap();
86 tx.send(t::tag2(10)).unwrap();
87 tx.send(t::tag3(10, 11, 'A')).unwrap();
89 t1 = rx.recv().unwrap();
90 assert_eq!(t1, t::tag1);
91 t1 = rx.recv().unwrap();
92 assert_eq!(t1, t::tag2(10));
93 t1 = rx.recv().unwrap();
94 assert_eq!(t1, t::tag3(10, 11, 'A'));
98 let (tx1, rx1) = channel();
99 let (tx2, rx2) = channel();
100 tx1.send(tx2).unwrap();
101 let tx2 = rx1.recv().unwrap();
102 // Does the transmitted channel still work?
104 tx2.send(10).unwrap();
106 i = rx2.recv().unwrap();