]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-3.rs
407939c9e657100c9032ae8ff84daa53b291317b
[rust.git] / src / test / run-pass / task-comm-3.rs
1 use std;
2 import task;
3 import comm;
4 import comm::chan;
5 import comm::send;
6 import comm::recv;
7
8 fn main() { #debug("===== WITHOUT THREADS ====="); test00(); }
9
10 fn test00_start(&&args: (chan<int>, int, int)) {
11     let (ch, message, count) = args;
12     #debug("Starting test00_start");
13     let i: int = 0;
14     while i < count {
15         #debug("Sending Message");
16         send(ch, message + 0);
17         i = i + 1;
18     }
19     #debug("Ending test00_start");
20 }
21
22 fn test00() {
23     let number_of_tasks: int = 16;
24     let number_of_messages: int = 4;
25
26     #debug("Creating tasks");
27
28     let po = comm::port();
29     let ch = chan(po);
30
31     let i: int = 0;
32
33     // Create and spawn tasks...
34     let tasks = [];
35     while i < number_of_tasks {
36         tasks += [task::spawn_joinable(
37             (ch, i, number_of_messages), test00_start)];
38         i = i + 1;
39     }
40
41     // Read from spawned tasks...
42     let sum = 0;
43     for t in tasks {
44         i = 0;
45         while i < number_of_messages {
46             let value = recv(po);
47             sum += value;
48             i = i + 1;
49         }
50     }
51
52     // Join spawned tasks...
53     for t in tasks { task::join(t); }
54
55     #debug("Completed: Final number is: ");
56     log(error, sum);
57     // assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) *
58     //       number_of_messages));
59     assert (sum == 480);
60 }