2012-12-11 01:32:48 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-25 02:35:29 +00:00
|
|
|
use std::cmp;
|
2011-03-22 01:13:08 +00:00
|
|
|
|
2011-06-15 18:19:50 +00:00
|
|
|
// Tests of ports and channels on various types
|
|
|
|
fn test_rec() {
|
2013-01-26 06:46:32 +00:00
|
|
|
struct R {val0: int, val1: u8, val2: char}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2013-12-06 02:19:06 +00:00
|
|
|
let (po, ch) = Chan::new();
|
2013-01-26 06:46:32 +00:00
|
|
|
let r0: R = R {val0: 0, val1: 1u8, val2: '2'};
|
2012-07-25 21:05:06 +00:00
|
|
|
ch.send(r0);
|
2013-01-26 06:46:32 +00:00
|
|
|
let mut r1: R;
|
2012-07-25 21:05:06 +00:00
|
|
|
r1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(r1.val0, 0);
|
|
|
|
assert_eq!(r1.val1, 1u8);
|
|
|
|
assert_eq!(r1.val2, '2');
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_vec() {
|
2013-12-06 02:19:06 +00:00
|
|
|
let (po, ch) = Chan::new();
|
2012-06-29 23:26:56 +00:00
|
|
|
let v0: ~[int] = ~[0, 1, 2];
|
2012-07-25 21:05:06 +00:00
|
|
|
ch.send(v0);
|
|
|
|
let v1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(v1[0], 0);
|
|
|
|
assert_eq!(v1[1], 1);
|
|
|
|
assert_eq!(v1[2], 2);
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_str() {
|
2013-12-06 02:19:06 +00:00
|
|
|
let (po, ch) = Chan::new();
|
2012-08-03 19:48:14 +00:00
|
|
|
let s0 = ~"test";
|
2012-07-25 21:05:06 +00:00
|
|
|
ch.send(s0);
|
|
|
|
let s1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(s1[0], 't' as u8);
|
|
|
|
assert_eq!(s1[1], 'e' as u8);
|
|
|
|
assert_eq!(s1[2], 's' as u8);
|
|
|
|
assert_eq!(s1[3], 't' as u8);
|
2011-03-22 02:25:34 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 23:26:35 +00:00
|
|
|
enum t {
|
|
|
|
tag1,
|
|
|
|
tag2(int),
|
|
|
|
tag3(int, u8, char)
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl cmp::Eq for t {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn eq(&self, other: &t) -> bool {
|
2012-11-15 02:59:30 +00:00
|
|
|
match *self {
|
2012-08-27 23:26:35 +00:00
|
|
|
tag1 => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2012-08-27 23:26:35 +00:00
|
|
|
tag1 => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tag2(e0a) => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2012-08-27 23:26:35 +00:00
|
|
|
tag2(e0b) => e0a == e0b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tag3(e0a, e1a, e2a) => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2012-08-27 23:26:35 +00:00
|
|
|
tag3(e0b, e1b, e2b) =>
|
|
|
|
e0a == e0b && e1a == e1b && e2a == e2b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-22 18:23:21 +00:00
|
|
|
fn ne(&self, other: &t) -> bool { !(*self).eq(other) }
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_tag() {
|
2013-12-06 02:19:06 +00:00
|
|
|
let (po, ch) = Chan::new();
|
2012-07-25 21:05:06 +00:00
|
|
|
ch.send(tag1);
|
|
|
|
ch.send(tag2(10));
|
|
|
|
ch.send(tag3(10, 11u8, 'A'));
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut t1: t;
|
2012-07-25 21:05:06 +00:00
|
|
|
t1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(t1, tag1);
|
2012-07-25 21:05:06 +00:00
|
|
|
t1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(t1, tag2(10));
|
2012-07-25 21:05:06 +00:00
|
|
|
t1 = po.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(t1, tag3(10, 11u8, 'A'));
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_chan() {
|
2013-12-06 02:19:06 +00:00
|
|
|
let (po, ch) = Chan::new();
|
|
|
|
let (po0, ch0) = Chan::new();
|
2013-02-15 10:44:18 +00:00
|
|
|
ch.send(ch0);
|
2013-12-06 02:19:06 +00:00
|
|
|
let mut ch1 = po.recv();
|
2011-06-15 18:19:50 +00:00
|
|
|
// Does the transmitted channel still work?
|
|
|
|
|
2012-07-25 21:05:06 +00:00
|
|
|
ch1.send(10);
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut i: int;
|
2012-07-25 21:05:06 +00:00
|
|
|
i = po0.recv();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(i, 10);
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-07-25 21:05:06 +00:00
|
|
|
test_rec();
|
|
|
|
test_vec();
|
|
|
|
test_str();
|
|
|
|
test_tag();
|
|
|
|
test_chan();
|
|
|
|
}
|