2011-06-15 13:19:50 -05:00
|
|
|
// -*- rust -*-
|
2011-03-21 20:13:08 -05:00
|
|
|
|
2011-08-06 12:07:39 -05:00
|
|
|
use std;
|
|
|
|
import std::comm;
|
|
|
|
import std::comm::mk_port;
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
// 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};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-06 12:07:39 -05:00
|
|
|
let po = comm::mk_port();
|
|
|
|
let ch = po.mk_chan();
|
2011-07-27 07:19:39 -05:00
|
|
|
let r0: r = {val0: 0, val1: 1u8, val2: '2'};
|
2011-08-06 12:07:39 -05:00
|
|
|
ch.send(r0);
|
2011-07-27 07:19:39 -05:00
|
|
|
let r1: r;
|
2011-08-06 12:07:39 -05:00
|
|
|
r1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (r1.val0 == 0);
|
|
|
|
assert (r1.val1 == 1u8);
|
|
|
|
assert (r1.val2 == '2');
|
2011-03-21 20:13:08 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_vec() {
|
2011-08-06 12:07:39 -05:00
|
|
|
let po = comm::mk_port();
|
|
|
|
let ch = po.mk_chan();
|
2011-07-18 10:41:35 -05:00
|
|
|
let v0: [int] = ~[0, 1, 2];
|
2011-08-06 12:07:39 -05:00
|
|
|
ch.send(v0);
|
2011-07-18 10:41:35 -05:00
|
|
|
let v1: [int];
|
2011-08-06 12:07:39 -05:00
|
|
|
v1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (v1.(0) == 0);
|
|
|
|
assert (v1.(1) == 1);
|
|
|
|
assert (v1.(2) == 2);
|
2011-03-21 20:13:08 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_str() {
|
2011-08-06 12:07:39 -05:00
|
|
|
let po = comm::mk_port();
|
|
|
|
let ch = po.mk_chan();
|
2011-07-27 07:19:39 -05:00
|
|
|
let s0: str = "test";
|
2011-08-06 12:07:39 -05:00
|
|
|
ch.send(s0);
|
2011-07-27 07:19:39 -05:00
|
|
|
let s1: str;
|
2011-08-06 12:07:39 -05:00
|
|
|
s1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (s1.(0) as u8 == 't' as u8);
|
|
|
|
assert (s1.(1) as u8 == 'e' as u8);
|
|
|
|
assert (s1.(2) as u8 == 's' as u8);
|
|
|
|
assert (s1.(3) as u8 == 't' as u8);
|
2011-03-21 21:25:34 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_tag() {
|
2011-06-15 13:19:50 -05:00
|
|
|
tag t { tag1; tag2(int); tag3(int, u8, char); }
|
2011-08-06 12:07:39 -05:00
|
|
|
let po = comm::mk_port();
|
|
|
|
let ch = po.mk_chan();
|
|
|
|
ch.send(tag1);
|
|
|
|
ch.send(tag2(10));
|
|
|
|
ch.send(tag3(10, 11u8, 'A'));
|
|
|
|
// FIXME: Do port semantics really guarantee these happen in order?
|
2011-07-27 07:19:39 -05:00
|
|
|
let t1: t;
|
2011-08-06 12:07:39 -05:00
|
|
|
t1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (t1 == tag1);
|
2011-08-06 12:07:39 -05:00
|
|
|
t1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (t1 == tag2(10));
|
2011-08-06 12:07:39 -05:00
|
|
|
t1 = po.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (t1 == tag3(10, 11u8, 'A'));
|
2011-03-21 20:13:08 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_chan() {
|
2011-08-06 12:07:39 -05:00
|
|
|
let po = comm::mk_port();
|
|
|
|
let ch = po.mk_chan();
|
|
|
|
let po0 = comm::mk_port();
|
|
|
|
let ch0 = po0.mk_chan();
|
2011-08-08 12:48:21 -05:00
|
|
|
ch.send(ch0.unsafe_ptr());
|
|
|
|
let pch1;
|
|
|
|
pch1 = po.recv();
|
|
|
|
let ch1 = comm::chan_from_unsafe_ptr(pch1);
|
2011-06-15 13:19:50 -05:00
|
|
|
// Does the transmitted channel still work?
|
|
|
|
|
2011-08-06 12:07:39 -05:00
|
|
|
ch1.send(10);
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: int;
|
2011-08-06 12:07:39 -05:00
|
|
|
i = po0.recv();
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (i == 10);
|
2011-03-21 20:13:08 -05:00
|
|
|
}
|
|
|
|
|
2011-08-08 12:05:00 -05:00
|
|
|
fn main() {
|
|
|
|
test_rec();
|
|
|
|
test_vec();
|
|
|
|
test_str();
|
|
|
|
test_tag();
|
2011-08-08 12:48:21 -05:00
|
|
|
test_chan();
|
2011-07-18 10:41:35 -05:00
|
|
|
}
|