]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-3.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / run-pass / task-comm-3.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-fast
12
13 use std::task;
14 use std::vec_ng::Vec;
15
16 pub fn main() { println!("===== WITHOUT THREADS ====="); test00(); }
17
18 fn test00_start(ch: &Sender<int>, message: int, count: int) {
19     println!("Starting test00_start");
20     let mut i: int = 0;
21     while i < count {
22         println!("Sending Message");
23         ch.send(message + 0);
24         i = i + 1;
25     }
26     println!("Ending test00_start");
27 }
28
29 fn test00() {
30     let number_of_tasks: int = 16;
31     let number_of_messages: int = 4;
32
33     println!("Creating tasks");
34
35     let (tx, rx) = channel();
36
37     let mut i: int = 0;
38
39     // Create and spawn tasks...
40     let mut results = Vec::new();
41     while i < number_of_tasks {
42         let tx = tx.clone();
43         let mut builder = task::task();
44         results.push(builder.future_result());
45         builder.spawn({
46             let i = i;
47             proc() {
48                 test00_start(&tx, i, number_of_messages)
49             }
50         });
51         i = i + 1;
52     }
53
54     // Read from spawned tasks...
55     let mut sum = 0;
56     for _r in results.iter() {
57         i = 0;
58         while i < number_of_messages {
59             let value = rx.recv();
60             sum += value;
61             i = i + 1;
62         }
63     }
64
65     // Join spawned tasks...
66     for r in results.iter() { r.recv(); }
67
68     println!("Completed: Final number is: ");
69     println!("{:?}", sum);
70     // assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) *
71     //       number_of_messages));
72     assert_eq!(sum, 480);
73 }