]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-16.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / task-comm-16.rs
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.
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
11 use std::sync::mpsc::channel;
12 use std::cmp;
13
14 // Tests of ports and channels on various types
15 fn test_rec() {
16     struct R {val0: isize, val1: u8, val2: char}
17
18     let (tx, rx) = channel();
19     let r0: R = R {val0: 0, val1: 1, val2: '2'};
20     tx.send(r0).unwrap();
21     let mut r1: R;
22     r1 = rx.recv().unwrap();
23     assert_eq!(r1.val0, 0);
24     assert_eq!(r1.val1, 1);
25     assert_eq!(r1.val2, '2');
26 }
27
28 fn test_vec() {
29     let (tx, rx) = channel();
30     let v0: Vec<isize> = vec!(0, 1, 2);
31     tx.send(v0).unwrap();
32     let v1 = rx.recv().unwrap();
33     assert_eq!(v1[0], 0);
34     assert_eq!(v1[1], 1);
35     assert_eq!(v1[2], 2);
36 }
37
38 fn test_str() {
39     let (tx, rx) = channel();
40     let s0 = "test".to_string();
41     tx.send(s0).unwrap();
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);
47 }
48
49 #[derive(Debug)]
50 enum t {
51     tag1,
52     tag2(isize),
53     tag3(isize, u8, char)
54 }
55
56 impl cmp::PartialEq for t {
57     fn eq(&self, other: &t) -> bool {
58         match *self {
59             t::tag1 => {
60                 match (*other) {
61                     t::tag1 => true,
62                     _ => false
63                 }
64             }
65             t::tag2(e0a) => {
66                 match (*other) {
67                     t::tag2(e0b) => e0a == e0b,
68                     _ => false
69                 }
70             }
71             t::tag3(e0a, e1a, e2a) => {
72                 match (*other) {
73                     t::tag3(e0b, e1b, e2b) =>
74                         e0a == e0b && e1a == e1b && e2a == e2b,
75                     _ => false
76                 }
77             }
78         }
79     }
80     fn ne(&self, other: &t) -> bool { !(*self).eq(other) }
81 }
82
83 fn test_tag() {
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();
88     let mut t1: t;
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'));
95 }
96
97 fn test_chan() {
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?
103
104     tx2.send(10).unwrap();
105     let mut i: isize;
106     i = rx2.recv().unwrap();
107     assert_eq!(i, 10);
108 }
109
110 pub fn main() {
111     test_rec();
112     test_vec();
113     test_str();
114     test_tag();
115     test_chan();
116 }