]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
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 | ||
1a4d82fc | 11 | use std::sync::mpsc::channel; |
970d7e83 | 12 | use std::cmp; |
223e47cc LB |
13 | |
14 | // Tests of ports and channels on various types | |
15 | fn test_rec() { | |
c34b1796 | 16 | struct R {val0: isize, val1: u8, val2: char} |
223e47cc | 17 | |
1a4d82fc | 18 | let (tx, rx) = channel(); |
c34b1796 | 19 | let r0: R = R {val0: 0, val1: 1, val2: '2'}; |
1a4d82fc | 20 | tx.send(r0).unwrap(); |
223e47cc | 21 | let mut r1: R; |
1a4d82fc | 22 | r1 = rx.recv().unwrap(); |
970d7e83 | 23 | assert_eq!(r1.val0, 0); |
c34b1796 | 24 | assert_eq!(r1.val1, 1); |
970d7e83 | 25 | assert_eq!(r1.val2, '2'); |
223e47cc LB |
26 | } |
27 | ||
28 | fn test_vec() { | |
1a4d82fc | 29 | let (tx, rx) = channel(); |
c30ab7b3 | 30 | let v0: Vec<isize> = vec![0, 1, 2]; |
1a4d82fc JJ |
31 | tx.send(v0).unwrap(); |
32 | let v1 = rx.recv().unwrap(); | |
970d7e83 LB |
33 | assert_eq!(v1[0], 0); |
34 | assert_eq!(v1[1], 1); | |
35 | assert_eq!(v1[2], 2); | |
223e47cc LB |
36 | } |
37 | ||
38 | fn test_str() { | |
1a4d82fc JJ |
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); | |
223e47cc LB |
47 | } |
48 | ||
85aaf69f | 49 | #[derive(Debug)] |
223e47cc LB |
50 | enum t { |
51 | tag1, | |
c34b1796 AL |
52 | tag2(isize), |
53 | tag3(isize, u8, char) | |
223e47cc LB |
54 | } |
55 | ||
1a4d82fc | 56 | impl cmp::PartialEq for t { |
223e47cc LB |
57 | fn eq(&self, other: &t) -> bool { |
58 | match *self { | |
1a4d82fc | 59 | t::tag1 => { |
223e47cc | 60 | match (*other) { |
1a4d82fc | 61 | t::tag1 => true, |
223e47cc LB |
62 | _ => false |
63 | } | |
64 | } | |
1a4d82fc | 65 | t::tag2(e0a) => { |
223e47cc | 66 | match (*other) { |
1a4d82fc | 67 | t::tag2(e0b) => e0a == e0b, |
223e47cc LB |
68 | _ => false |
69 | } | |
70 | } | |
1a4d82fc | 71 | t::tag3(e0a, e1a, e2a) => { |
223e47cc | 72 | match (*other) { |
1a4d82fc | 73 | t::tag3(e0b, e1b, e2b) => |
223e47cc LB |
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() { | |
1a4d82fc JJ |
84 | let (tx, rx) = channel(); |
85 | tx.send(t::tag1).unwrap(); | |
86 | tx.send(t::tag2(10)).unwrap(); | |
c34b1796 | 87 | tx.send(t::tag3(10, 11, 'A')).unwrap(); |
223e47cc | 88 | let mut t1: t; |
1a4d82fc JJ |
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(); | |
c34b1796 | 94 | assert_eq!(t1, t::tag3(10, 11, 'A')); |
223e47cc LB |
95 | } |
96 | ||
97 | fn test_chan() { | |
1a4d82fc JJ |
98 | let (tx1, rx1) = channel(); |
99 | let (tx2, rx2) = channel(); | |
100 | tx1.send(tx2).unwrap(); | |
101 | let tx2 = rx1.recv().unwrap(); | |
223e47cc LB |
102 | // Does the transmitted channel still work? |
103 | ||
1a4d82fc | 104 | tx2.send(10).unwrap(); |
c34b1796 | 105 | let mut i: isize; |
1a4d82fc | 106 | i = rx2.recv().unwrap(); |
970d7e83 | 107 | assert_eq!(i, 10); |
223e47cc LB |
108 | } |
109 | ||
110 | pub fn main() { | |
111 | test_rec(); | |
112 | test_vec(); | |
113 | test_str(); | |
114 | test_tag(); | |
115 | test_chan(); | |
116 | } |