PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/jruderman/rust
Rust | 48 lines | 42 code | 6 blank | 0 comment | 3 complexity | 4cf0dd5ac1d469f6835a91571221998e MD5 | raw file
 1use std;
 2import task;
 3
 4fn main() { test00(); }
 5
 6fn test00_start(c: pipes::chan<int>, start: int, number_of_messages: int) {
 7    let mut i: int = 0;
 8    while i < number_of_messages { c.send(start + i); i += 1; }
 9}
10
11fn test00() {
12    let mut r: int = 0;
13    let mut sum: int = 0;
14    let p = pipes::port_set();
15    let number_of_messages: int = 10;
16
17    let c = p.chan();
18    do task::spawn {
19        test00_start(c, number_of_messages * 0, number_of_messages);
20    }
21    let c = p.chan();
22    do task::spawn {
23        test00_start(c, number_of_messages * 1, number_of_messages);
24    }
25    let c = p.chan();
26    do task::spawn {
27        test00_start(c, number_of_messages * 2, number_of_messages);
28    }
29    let c = p.chan();
30    do task::spawn {
31        test00_start(c, number_of_messages * 3, number_of_messages);
32    }
33
34    let mut i: int = 0;
35    while i < number_of_messages {
36        r = p.recv();
37        sum += r;
38        r = p.recv();
39        sum += r;
40        r = p.recv();
41        sum += r;
42        r = p.recv();
43        sum += r;
44        i += 1;
45    }
46
47    assert (sum == number_of_messages * 4 * (number_of_messages * 4 - 1) / 2);
48}