PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/jruderman/rust
Rust | 79 lines | 67 code | 9 blank | 3 comment | 14 complexity | 929b92dea327afd7932d7de94b525362 MD5 | raw file
 1// -*- rust -*-
 2
 3use std;
 4import pipes;
 5import pipes::send;
 6import pipes::port;
 7import pipes::recv;
 8import pipes::chan;
 9
10// Tests of ports and channels on various types
11fn test_rec() {
12    type r = {val0: int, val1: u8, val2: char};
13
14    let (ch, po) = pipes::stream();
15    let r0: r = {val0: 0, val1: 1u8, val2: '2'};
16    ch.send(r0);
17    let mut r1: r;
18    r1 = po.recv();
19    assert (r1.val0 == 0);
20    assert (r1.val1 == 1u8);
21    assert (r1.val2 == '2');
22}
23
24fn test_vec() {
25    let (ch, po) = pipes::stream();
26    let v0: ~[int] = ~[0, 1, 2];
27    ch.send(v0);
28    let v1 = po.recv();
29    assert (v1[0] == 0);
30    assert (v1[1] == 1);
31    assert (v1[2] == 2);
32}
33
34fn test_str() {
35    let (ch, po) = pipes::stream();
36    let s0 = ~"test";
37    ch.send(s0);
38    let s1 = po.recv();
39    assert (s1[0] == 't' as u8);
40    assert (s1[1] == 'e' as u8);
41    assert (s1[2] == 's' as u8);
42    assert (s1[3] == 't' as u8);
43}
44
45fn test_tag() {
46    enum t { tag1, tag2(int), tag3(int, u8, char), }
47    let (ch, po) = pipes::stream();
48    ch.send(tag1);
49    ch.send(tag2(10));
50    ch.send(tag3(10, 11u8, 'A'));
51    let mut t1: t;
52    t1 = po.recv();
53    assert (t1 == tag1);
54    t1 = po.recv();
55    assert (t1 == tag2(10));
56    t1 = po.recv();
57    assert (t1 == tag3(10, 11u8, 'A'));
58}
59
60fn test_chan() {
61    let (ch, po) = pipes::stream();
62    let (ch0, po0) = pipes::stream();
63    ch.send(ch0);
64    let ch1 = po.recv();
65    // Does the transmitted channel still work?
66
67    ch1.send(10);
68    let mut i: int;
69    i = po0.recv();
70    assert (i == 10);
71}
72
73fn main() {
74    test_rec();
75    test_vec();
76    test_str();
77    test_tag();
78    test_chan();
79}