Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 247 lines (214 sloc) 7.305 kB
13013d8 @TeXitoi Relicense shootout-chameneos-redux.rs to the shootout license.
TeXitoi authored
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
d1affff @graydon Reliciense makefiles and testsuite. Yup.
graydon authored
3 //
13013d8 @TeXitoi Relicense shootout-chameneos-redux.rs to the shootout license.
TeXitoi authored
4 // contributed by the Rust Project Developers
5
6 // Copyright (c) 2012-2014 The Rust Project Developers
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 // - Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // - Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in
19 // the documentation and/or other materials provided with the
20 // distribution.
21 //
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 // the name of "The Computer Language Shootout Benchmarks" nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
27 //
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
d1affff @graydon Reliciense makefiles and testsuite. Yup.
graydon authored
40
2e2160b @alexcrichton core: Update all tests for fmt movement
alexcrichton authored
41 // no-pretty-expanded
42
3dcd215 @sfackler Switch to purely namespaced enums
sfackler authored
43 use self::Color::{Red, Yellow, Blue};
e921e3f @alexcrichton Rollup test fixes and rebase conflicts
alexcrichton authored
44 use std::sync::mpsc::{channel, Sender, Receiver};
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
45 use std::fmt;
d0de2b4 @aturon Fallout from stabilization
aturon authored
46 use std::thread;
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
47
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
48 fn print_complements() {
cbad1da @thestinger vec: remove eachi
thestinger authored
49 let all = [Blue, Red, Yellow];
d5d7e65 @japaric `for x in xs.iter()` -> `for x in &xs`
japaric authored
50 for aa in &all {
51 for bb in &all {
a640008 @alexcrichton More test fixes
alexcrichton authored
52 println!("{:?} + {:?} -> {:?}", *aa, *bb, transform(*aa, *bb));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
53 }
54 }
55 }
56
890ed5c @nikomatsakis Fallout in tests
nikomatsakis authored
57 #[derive(Copy, Clone)]
096a286 @nikomatsakis librustc: Make `Copy` opt-in.
nikomatsakis authored
58 enum Color {
59 Red,
60 Yellow,
61 Blue,
62 }
63
788181d @japaric s/Show/Debug/g
japaric authored
64 impl fmt::Debug for Color {
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
65 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 let str = match *self {
67 Red => "red",
68 Yellow => "yellow",
69 Blue => "blue",
70 };
2e2160b @alexcrichton core: Update all tests for fmt movement
alexcrichton authored
71 write!(f, "{}", str)
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
72 }
73 }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
74
890ed5c @nikomatsakis Fallout in tests
nikomatsakis authored
75 #[derive(Copy, Clone)]
fc9650b @catamorphism testsuite: De-record most bench tests
catamorphism authored
76 struct CreatureInfo {
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
77 name: usize,
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
78 color: Color
fc9650b @catamorphism testsuite: De-record most bench tests
catamorphism authored
79 }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
80
5530745 @richo core: rename strbuf::StrBuf to string::String
richo authored
81 fn show_color_list(set: Vec<Color>) -> String {
82 let mut out = String::new();
d5d7e65 @japaric `for x in xs.iter()` -> `for x in &xs`
japaric authored
83 for col in &set {
5653b4d @TeXitoi remove shootout warnings
TeXitoi authored
84 out.push(' ');
17bc7d8 @japaric cleanup: replace `as[_mut]_slice()` calls with deref coercions
japaric authored
85 out.push_str(&format!("{:?}", col));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
86 }
def90f4 @huonw Fix tests. Add Vec<u8> conversion to StrBuf.
huonw authored
87 out
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
88 }
89
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
90 fn show_digit(nn: usize) -> &'static str {
39713b8 @huonw Remove unnecessary parentheses.
huonw authored
91 match nn {
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
92 0 => {" zero"}
93 1 => {" one"}
94 2 => {" two"}
95 3 => {" three"}
96 4 => {" four"}
97 5 => {" five"}
98 6 => {" six"}
99 7 => {" seven"}
100 8 => {" eight"}
101 9 => {" nine"}
7828c3d @steveklabnik Rename fail! to panic!
steveklabnik authored
102 _ => {panic!("expected digits from 0 to 9...")}
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
103 }
104 }
105
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
106 struct Number(usize);
788181d @japaric s/Show/Debug/g
japaric authored
107 impl fmt::Debug for Number {
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
108 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109 let mut out = vec![];
110 let Number(mut num) = *self;
111 if num == 0 { out.push(show_digit(0)) };
112
113 while num != 0 {
114 let dig = num % 10;
115 num = num / 10;
116 let s = show_digit(dig);
117 out.push(s);
118 }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
119
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
120 for s in out.iter().rev() {
2e2160b @alexcrichton core: Update all tests for fmt movement
alexcrichton authored
121 try!(write!(f, "{}", s))
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
122 }
123 Ok(())
def90f4 @huonw Fix tests. Add Vec<u8> conversion to StrBuf.
huonw authored
124 }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
125 }
126
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
127 fn transform(aa: Color, bb: Color) -> Color {
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
128 match (aa, bb) {
025d866 @brson Switch alts to use arrows
brson authored
129 (Red, Red ) => { Red }
130 (Red, Yellow) => { Blue }
131 (Red, Blue ) => { Yellow }
132 (Yellow, Red ) => { Blue }
133 (Yellow, Yellow) => { Yellow }
134 (Yellow, Blue ) => { Red }
135 (Blue, Red ) => { Yellow }
136 (Blue, Yellow) => { Red }
137 (Blue, Blue ) => { Blue }
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
138 }
139 }
140
141 fn creature(
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
142 name: usize,
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
143 mut color: Color,
144 from_rendezvous: Receiver<CreatureInfo>,
7858065 @alexcrichton std: Rename Chan/Port types and constructor
alexcrichton authored
145 to_rendezvous: Sender<CreatureInfo>,
5530745 @richo core: rename strbuf::StrBuf to string::String
richo authored
146 to_rendezvous_log: Sender<String>
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
147 ) {
e646708 @eddyb Remove integer suffixes where the types in compiled code are identical.
eddyb authored
148 let mut creatures_met = 0;
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
149 let mut evil_clones_met = 0;
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
150 let mut rendezvous = from_rendezvous.iter();
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
151
152 loop {
153 // ask for a pairing
e921e3f @alexcrichton Rollup test fixes and rebase conflicts
alexcrichton authored
154 to_rendezvous.send(CreatureInfo {name: name, color: color}).unwrap();
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
155
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
156 // log and change, or quit
157 match rendezvous.next() {
158 Some(other_creature) => {
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
159 color = transform(color, other_creature.color);
160
161 // track some statistics
162 creatures_met += 1;
163 if other_creature.name == name {
164 evil_clones_met += 1;
165 }
166 }
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
167 None => break
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
168 }
169 }
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
170 // log creatures met and evil clones of self
a640008 @alexcrichton More test fixes
alexcrichton authored
171 let report = format!("{}{:?}", creatures_met, Number(evil_clones_met));
e921e3f @alexcrichton Rollup test fixes and rebase conflicts
alexcrichton authored
172 to_rendezvous_log.send(report).unwrap();
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
173 }
174
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
175 fn rendezvous(nn: usize, set: Vec<Color>) {
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
176 // these ports will allow us to hear from the creatures
7858065 @alexcrichton std: Rename Chan/Port types and constructor
alexcrichton authored
177 let (to_rendezvous, from_creatures) = channel::<CreatureInfo>();
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
178
179 // these channels will be passed to the creatures so they can talk to us
5530745 @richo core: rename strbuf::StrBuf to string::String
richo authored
180 let (to_rendezvous_log, from_creatures_log) = channel::<String>();
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
181
182 // these channels will allow us to talk to each creature by 'name'/index
0f4ee2d @tshepang bench: fix a few compiler warnings
tshepang authored
183 let to_creature: Vec<Sender<CreatureInfo>> =
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
184 set.iter().enumerate().map(|(ii, &col)| {
3d59ac3 @nikomatsakis De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi
nikomatsakis authored
185 // create each creature as a listener with a port, and
186 // give us a channel to talk to each
02e907b @brson Remove oldcomm from the test suite
brson authored
187 let to_rendezvous = to_rendezvous.clone();
188 let to_rendezvous_log = to_rendezvous_log.clone();
7858065 @alexcrichton std: Rename Chan/Port types and constructor
alexcrichton authored
189 let (to_creature, from_rendezvous) = channel();
d0de2b4 @aturon Fallout from stabilization
aturon authored
190 thread::spawn(move|| {
786dea2 @pcwalton libextra: Another round of de-`Cell`-ing.
pcwalton authored
191 creature(ii,
192 col,
193 from_rendezvous,
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
194 to_rendezvous,
195 to_rendezvous_log);
caca9b2 @aturon Fallout from stabilization
aturon authored
196 });
02e907b @brson Remove oldcomm from the test suite
brson authored
197 to_creature
c0a20d2 @huonw Remove vec::{map, mapi, zip_map} and the methods, except for .map, si…
huonw authored
198 }).collect();
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
199
200 let mut creatures_met = 0;
201
202 // set up meetings...
7d661af @japaric `for x in range(a, b)` -> `for x in a..b`
japaric authored
203 for _ in 0..nn {
e921e3f @alexcrichton Rollup test fixes and rebase conflicts
alexcrichton authored
204 let fst_creature = from_creatures.recv().unwrap();
205 let snd_creature = from_creatures.recv().unwrap();
638ee23 @killerswan Add improvements suggested by erickt and bblum
killerswan authored
206
207 creatures_met += 2;
208
e921e3f @alexcrichton Rollup test fixes and rebase conflicts
alexcrichton authored
209 to_creature[fst_creature.name].send(snd_creature).unwrap();
210 to_creature[snd_creature.name].send(fst_creature).unwrap();
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
211 }
212
213 // tell each creature to stop
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
214 drop(to_creature);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
215
216 // print each color in the set
4fc0452 @bjz Remove re-exports of std::io::stdio::{print, println} in the prelude.
bjz authored
217 println!("{}", show_color_list(set));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
218
219 // print each creature's stats
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
220 drop(to_rendezvous_log);
221 for rep in from_creatures_log.iter() {
222 println!("{}", rep);
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
223 }
224
225 // print the total number of creatures met
a640008 @alexcrichton More test fixes
alexcrichton authored
226 println!("{:?}\n", Number(creatures_met));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
227 }
228
edc317b @brson Remove arg vectors from main functions. Stop supporting them.
brson authored
229 fn main() {
220bf41 @nagisa Cleanup getenv from tests and benchmarks
nagisa authored
230 let nn = if std::env::var_os("RUST_BENCH").is_some() {
0a0e2c3 @TeXitoi fix and improve shootout-chameneos-redux
TeXitoi authored
231 200000
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
232 } else {
7d941fa @nagisa Replace some uses of deprecated os functions
nagisa authored
233 std::env::args()
234 .nth(1)
0cdde6e @alexcrichton std: Stabilize FromStr and parse
alexcrichton authored
235 .and_then(|arg| arg.parse().ok())
362d713 @nikomatsakis Fix remaining bench/debuginfo tests (and a few stragglers)
nikomatsakis authored
236 .unwrap_or(600)
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
237 };
238
239 print_complements();
4fc0452 @bjz Remove re-exports of std::io::stdio::{print, println} in the prelude.
bjz authored
240 println!("");
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
241
579eb24 @pcwalton test: Automatically remove all `~[T]` from tests.
pcwalton authored
242 rendezvous(nn, vec!(Blue, Red, Yellow));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
243
244 rendezvous(nn,
579eb24 @pcwalton test: Automatically remove all `~[T]` from tests.
pcwalton authored
245 vec!(Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue));
2bd482e @killerswan Add the Alioth chameneos-redux benchmark
killerswan authored
246 }
Something went wrong with that request. Please try again.