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