summary refs log tree commit diff
path: root/src/test/run-pass/task-comm-16.rs
blob: a69b7b0c15bc8004b7ad64f7c925c5e160595ee2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// -*- rust -*-

extern mod std;
use pipes::send;
use pipes::Port;
use pipes::recv;
use pipes::Chan;

// Tests of ports and channels on various types
fn test_rec() {
    type r = {val0: int, val1: u8, val2: char};

    let (ch, po) = pipes::stream();
    let r0: r = {val0: 0, val1: 1u8, val2: '2'};
    ch.send(r0);
    let mut r1: r;
    r1 = po.recv();
    assert (r1.val0 == 0);
    assert (r1.val1 == 1u8);
    assert (r1.val2 == '2');
}

fn test_vec() {
    let (ch, po) = pipes::stream();
    let v0: ~[int] = ~[0, 1, 2];
    ch.send(v0);
    let v1 = po.recv();
    assert (v1[0] == 0);
    assert (v1[1] == 1);
    assert (v1[2] == 2);
}

fn test_str() {
    let (ch, po) = pipes::stream();
    let s0 = ~"test";
    ch.send(s0);
    let s1 = po.recv();
    assert (s1[0] == 't' as u8);
    assert (s1[1] == 'e' as u8);
    assert (s1[2] == 's' as u8);
    assert (s1[3] == 't' as u8);
}

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
                }
            }
        }
    }
    pure fn ne(other: &t) -> bool { !self.eq(other) }
}

fn test_tag() {
    let (ch, po) = pipes::stream();
    ch.send(tag1);
    ch.send(tag2(10));
    ch.send(tag3(10, 11u8, 'A'));
    let mut t1: t;
    t1 = po.recv();
    assert (t1 == tag1);
    t1 = po.recv();
    assert (t1 == tag2(10));
    t1 = po.recv();
    assert (t1 == tag3(10, 11u8, 'A'));
}

fn test_chan() {
    let (ch, po) = pipes::stream();
    let (ch0, po0) = pipes::stream();
    ch.send(ch0);
    let ch1 = po.recv();
    // Does the transmitted channel still work?

    ch1.send(10);
    let mut i: int;
    i = po0.recv();
    assert (i == 10);
}

fn main() {
    test_rec();
    test_vec();
    test_str();
    test_tag();
    test_chan();
}