rust/src/test/run-pass/task-comm-16.rs

111 lines
2.2 KiB
Rust
Raw Normal View History

// -*- rust -*-
use std;
2012-07-25 16:05:06 -05:00
import pipes;
import pipes::send;
2012-08-28 13:11:15 -05:00
import pipes::Port;
2012-07-25 16:05:06 -05:00
import pipes::recv;
2012-08-28 13:11:15 -05:00
import pipes::Chan;
// Tests of ports and channels on various types
fn test_rec() {
2011-07-27 07:19:39 -05:00
type r = {val0: int, val1: u8, val2: char};
2012-07-25 16:05:06 -05:00
let (ch, po) = pipes::stream();
2011-07-27 07:19:39 -05:00
let r0: r = {val0: 0, val1: 1u8, val2: '2'};
2012-07-25 16:05:06 -05:00
ch.send(r0);
let mut r1: r;
2012-07-25 16:05:06 -05:00
r1 = po.recv();
assert (r1.val0 == 0);
assert (r1.val1 == 1u8);
assert (r1.val2 == '2');
}
2011-04-19 15:35:49 -05:00
fn test_vec() {
2012-07-25 16:05:06 -05:00
let (ch, po) = pipes::stream();
let v0: ~[int] = ~[0, 1, 2];
2012-07-25 16:05:06 -05:00
ch.send(v0);
let v1 = po.recv();
assert (v1[0] == 0);
assert (v1[1] == 1);
assert (v1[2] == 2);
}
2011-04-19 15:35:49 -05:00
fn test_str() {
2012-07-25 16:05:06 -05:00
let (ch, po) = pipes::stream();
2012-08-03 14:48:14 -05:00
let s0 = ~"test";
2012-07-25 16:05:06 -05:00
ch.send(s0);
let s1 = po.recv();
2012-02-05 18:50:54 -06:00
assert (s1[0] == 't' as u8);
assert (s1[1] == 'e' as u8);
assert (s1[2] == 's' as u8);
assert (s1[3] == 't' as u8);
}
2012-08-27 18:26:35 -05:00
enum t {
tag1,
tag2(int),
tag3(int, u8, char)
}
impl t : cmp::Eq {
pure fn eq(&&other: t) -> bool {
match self {
tag1 => {
match other {
tag1 => true,
_ => false
}
}
tag2(e0a) => {
match other {
tag2(e0b) => e0a == e0b,
_ => false
}
}
tag3(e0a, e1a, e2a) => {
match other {
tag3(e0b, e1b, e2b) =>
e0a == e0b && e1a == e1b && e2a == e2b,
_ => false
}
}
}
}
}
2011-04-19 15:35:49 -05:00
fn test_tag() {
2012-07-25 16:05:06 -05:00
let (ch, po) = pipes::stream();
ch.send(tag1);
ch.send(tag2(10));
ch.send(tag3(10, 11u8, 'A'));
let mut t1: t;
2012-07-25 16:05:06 -05:00
t1 = po.recv();
assert (t1 == tag1);
2012-07-25 16:05:06 -05:00
t1 = po.recv();
assert (t1 == tag2(10));
2012-07-25 16:05:06 -05:00
t1 = po.recv();
assert (t1 == tag3(10, 11u8, 'A'));
}
2011-04-19 15:35:49 -05:00
fn test_chan() {
2012-07-25 16:05:06 -05:00
let (ch, po) = pipes::stream();
let (ch0, po0) = pipes::stream();
ch.send(ch0);
let ch1 = po.recv();
// Does the transmitted channel still work?
2012-07-25 16:05:06 -05:00
ch1.send(10);
let mut i: int;
2012-07-25 16:05:06 -05:00
i = po0.recv();
assert (i == 10);
}
2012-07-25 16:05:06 -05:00
fn main() {
test_rec();
test_vec();
test_str();
test_tag();
test_chan();
}