]> git.lizzy.rs Git - rust.git/blob - src/test/ui/threads-sendsync/task-comm-16.rs
Merge commit 'e18101137866b79045fee0ef996e696e68c920b4' into clippyup
[rust.git] / src / test / ui / threads-sendsync / task-comm-16.rs
1 // run-pass
2 #![allow(unused_mut)]
3 #![allow(unused_parens)]
4 #![allow(non_camel_case_types)]
5
6 use std::sync::mpsc::channel;
7 use std::cmp;
8
9 // Tests of ports and channels on various types
10 fn test_rec() {
11     struct R {val0: isize, val1: u8, val2: char}
12
13     let (tx, rx) = channel();
14     let r0: R = R {val0: 0, val1: 1, val2: '2'};
15     tx.send(r0).unwrap();
16     let mut r1: R;
17     r1 = rx.recv().unwrap();
18     assert_eq!(r1.val0, 0);
19     assert_eq!(r1.val1, 1);
20     assert_eq!(r1.val2, '2');
21 }
22
23 fn test_vec() {
24     let (tx, rx) = channel();
25     let v0: Vec<isize> = vec![0, 1, 2];
26     tx.send(v0).unwrap();
27     let v1 = rx.recv().unwrap();
28     assert_eq!(v1[0], 0);
29     assert_eq!(v1[1], 1);
30     assert_eq!(v1[2], 2);
31 }
32
33 fn test_str() {
34     let (tx, rx) = channel();
35     let s0 = "test".to_string();
36     tx.send(s0).unwrap();
37     let s1 = rx.recv().unwrap();
38     assert_eq!(s1.as_bytes()[0], 't' as u8);
39     assert_eq!(s1.as_bytes()[1], 'e' as u8);
40     assert_eq!(s1.as_bytes()[2], 's' as u8);
41     assert_eq!(s1.as_bytes()[3], 't' as u8);
42 }
43
44 #[derive(Debug)]
45 enum t {
46     tag1,
47     tag2(isize),
48     tag3(isize, u8, char)
49 }
50
51 impl cmp::PartialEq for t {
52     fn eq(&self, other: &t) -> bool {
53         match *self {
54             t::tag1 => {
55                 match (*other) {
56                     t::tag1 => true,
57                     _ => false
58                 }
59             }
60             t::tag2(e0a) => {
61                 match (*other) {
62                     t::tag2(e0b) => e0a == e0b,
63                     _ => false
64                 }
65             }
66             t::tag3(e0a, e1a, e2a) => {
67                 match (*other) {
68                     t::tag3(e0b, e1b, e2b) =>
69                         e0a == e0b && e1a == e1b && e2a == e2b,
70                     _ => false
71                 }
72             }
73         }
74     }
75     fn ne(&self, other: &t) -> bool { !(*self).eq(other) }
76 }
77
78 fn test_tag() {
79     let (tx, rx) = channel();
80     tx.send(t::tag1).unwrap();
81     tx.send(t::tag2(10)).unwrap();
82     tx.send(t::tag3(10, 11, 'A')).unwrap();
83     let mut t1: t;
84     t1 = rx.recv().unwrap();
85     assert_eq!(t1, t::tag1);
86     t1 = rx.recv().unwrap();
87     assert_eq!(t1, t::tag2(10));
88     t1 = rx.recv().unwrap();
89     assert_eq!(t1, t::tag3(10, 11, 'A'));
90 }
91
92 fn test_chan() {
93     let (tx1, rx1) = channel();
94     let (tx2, rx2) = channel();
95     tx1.send(tx2).unwrap();
96     let tx2 = rx1.recv().unwrap();
97     // Does the transmitted channel still work?
98
99     tx2.send(10).unwrap();
100     let mut i: isize;
101     i = rx2.recv().unwrap();
102     assert_eq!(i, 10);
103 }
104
105 pub fn main() {
106     test_rec();
107     test_vec();
108     test_str();
109     test_tag();
110     test_chan();
111 }