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;
|
2011-12-13 18:25:51 -06:00
|
|
|
import comm;
|
|
|
|
import comm::send;
|
|
|
|
import comm::port;
|
|
|
|
import comm::recv;
|
|
|
|
import comm::chan;
|
2011-08-06 12:07:39 -05:00
|
|
|
|
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-25 13:20:43 -05:00
|
|
|
let po = comm::port();
|
|
|
|
let ch = chan(po);
|
2011-07-27 07:19:39 -05:00
|
|
|
let r0: r = {val0: 0, val1: 1u8, val2: '2'};
|
2011-08-12 19:36:52 -05:00
|
|
|
send(ch, r0);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut r1: r;
|
2011-08-25 13:20:43 -05:00
|
|
|
r1 = recv(po);
|
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-25 13:20:43 -05:00
|
|
|
let po = port();
|
|
|
|
let ch = chan(po);
|
2012-06-25 22:00:46 -05:00
|
|
|
let v0: [int]/~ = [0, 1, 2]/~;
|
2011-08-12 19:36:52 -05:00
|
|
|
send(ch, v0);
|
2011-08-25 13:20:43 -05:00
|
|
|
let v1 = recv(po);
|
2011-08-19 17:16:48 -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() {
|
2012-02-05 18:50:54 -06:00
|
|
|
let po = port();
|
|
|
|
let ch = chan(po);
|
|
|
|
let s0 = "test";
|
|
|
|
send(ch, s0);
|
|
|
|
let s1 = recv(po);
|
|
|
|
assert (s1[0] == 't' as u8);
|
|
|
|
assert (s1[1] == 'e' as u8);
|
|
|
|
assert (s1[2] == 's' as u8);
|
|
|
|
assert (s1[3] == 't' as u8);
|
2011-03-21 21:25:34 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_tag() {
|
2012-01-19 20:31:08 -06:00
|
|
|
enum t { tag1, tag2(int), tag3(int, u8, char), }
|
2011-08-25 13:20:43 -05:00
|
|
|
let po = port();
|
|
|
|
let ch = chan(po);
|
2011-08-12 19:36:52 -05:00
|
|
|
send(ch, tag1);
|
|
|
|
send(ch, tag2(10));
|
|
|
|
send(ch, tag3(10, 11u8, 'A'));
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut t1: t;
|
2011-08-25 13:20:43 -05:00
|
|
|
t1 = recv(po);
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (t1 == tag1);
|
2011-08-25 13:20:43 -05:00
|
|
|
t1 = recv(po);
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (t1 == tag2(10));
|
2011-08-25 13:20:43 -05:00
|
|
|
t1 = recv(po);
|
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-25 13:20:43 -05:00
|
|
|
let po = port();
|
|
|
|
let ch = chan(po);
|
|
|
|
let po0 = port();
|
|
|
|
let ch0 = chan(po0);
|
2011-08-12 19:36:52 -05:00
|
|
|
send(ch, ch0);
|
2011-08-25 13:20:43 -05:00
|
|
|
let ch1 = recv(po);
|
2011-06-15 13:19:50 -05:00
|
|
|
// Does the transmitted channel still work?
|
|
|
|
|
2011-08-12 19:36:52 -05:00
|
|
|
send(ch1, 10);
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i: int;
|
2011-08-25 13:20:43 -05:00
|
|
|
i = recv(po0);
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (i == 10);
|
2011-03-21 20:13:08 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
fn main() { test_rec(); test_vec(); test_str(); test_tag(); test_chan(); }
|