]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-binarytrees.rs
librustc: Update the serializer to work properly with INHTWAMA, removing mutable...
[rust.git] / src / test / bench / shootout-binarytrees.rs
1 // xfail-test
2
3 // Broken due to arena API problems.
4
5 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
6 // file at the top-level directory of this distribution and at
7 // http://rust-lang.org/COPYRIGHT.
8 //
9 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
10 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
11 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
12 // option. This file may not be copied, modified, or distributed
13 // except according to those terms.
14
15 extern mod std;
16 use std::arena;
17
18 enum tree<'self> {
19     nil,
20     node(&'self tree<'self>, &'self tree<'self>, int),
21 }
22
23 fn item_check(t: &tree) -> int {
24     match *t {
25       nil => { return 0; }
26       node(left, right, item) => {
27         return item + item_check(left) - item_check(right);
28       }
29     }
30 }
31
32 fn bottom_up_tree<'r>(arena: &'r mut arena::Arena, item: int, depth: int)
33                    -> &'r tree<'r> {
34     if depth > 0 {
35         return arena.alloc(
36             || node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
37                     bottom_up_tree(arena, 2 * item, depth - 1),
38                     item));
39     }
40     return arena.alloc(|| nil);
41 }
42
43 fn main() {
44     let args = os::args();
45     let args = if os::getenv(~"RUST_BENCH").is_some() {
46         ~[~"", ~"17"]
47     } else if args.len() <= 1u {
48         ~[~"", ~"8"]
49     } else {
50         args
51     };
52
53     let n = int::from_str(args[1]).get();
54     let min_depth = 4;
55     let mut max_depth;
56     if min_depth + 2 > n {
57         max_depth = min_depth + 2;
58     } else {
59         max_depth = n;
60     }
61
62     let mut stretch_arena = arena::Arena();
63     let stretch_depth = max_depth + 1;
64     let stretch_tree = bottom_up_tree(&mut stretch_arena, 0, stretch_depth);
65
66     io::println(fmt!("stretch tree of depth %d\t check: %d",
67                           stretch_depth,
68                           item_check(stretch_tree)));
69
70     let mut long_lived_arena = arena::Arena();
71     let long_lived_tree = bottom_up_tree(&mut long_lived_arena, 0, max_depth);
72     let mut depth = min_depth;
73     while depth <= max_depth {
74         let iterations = int::pow(2, (max_depth - depth + min_depth) as uint);
75         let mut chk = 0;
76         let mut i = 1;
77         while i <= iterations {
78             let mut temp_tree = bottom_up_tree(&mut long_lived_arena, i, depth);
79             chk += item_check(temp_tree);
80             temp_tree = bottom_up_tree(&mut long_lived_arena, -i, depth);
81             chk += item_check(temp_tree);
82             i += 1;
83         }
84         io::println(fmt!("%d\t trees of depth %d\t check: %d",
85                          iterations * 2, depth,
86                          chk));
87         depth += 2;
88     }
89     io::println(fmt!("long lived trees of depth %d\t check: %d",
90                      max_depth,
91                      item_check(long_lived_tree)));
92 }