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