]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/task-comm-3.rs
rollup merge of #17355 : gamazeps/issue17210
[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 // no-pretty-expanded FIXME #15189
12
13 extern crate debug;
14
15 use std::task;
16
17 pub fn main() { println!("===== WITHOUT THREADS ====="); test00(); }
18
19 fn test00_start(ch: &Sender<int>, message: int, count: int) {
20     println!("Starting test00_start");
21     let mut i: int = 0;
22     while i < count {
23         println!("Sending Message");
24         ch.send(message + 0);
25         i = i + 1;
26     }
27     println!("Ending test00_start");
28 }
29
30 fn test00() {
31     let number_of_tasks: int = 16;
32     let number_of_messages: int = 4;
33
34     println!("Creating tasks");
35
36     let (tx, rx) = channel();
37
38     let mut i: int = 0;
39
40     // Create and spawn tasks...
41     let mut results = Vec::new();
42     while i < number_of_tasks {
43         let tx = tx.clone();
44         results.push(task::try_future({
45             let i = i;
46             proc() {
47                 test00_start(&tx, i, number_of_messages)
48             }
49         }));
50         i = i + 1;
51     }
52
53     // Read from spawned tasks...
54     let mut sum = 0;
55     for _r in results.iter() {
56         i = 0;
57         while i < number_of_messages {
58             let value = rx.recv();
59             sum += value;
60             i = i + 1;
61         }
62     }
63
64     // Join spawned tasks...
65     for r in results.iter_mut() { r.get_ref(); }
66
67     println!("Completed: Final number is: ");
68     println!("{:?}", sum);
69     // assert (sum == (((number_of_tasks * (number_of_tasks - 1)) / 2) *
70     //       number_of_messages));
71     assert_eq!(sum, 480);
72 }