]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-fasta.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / bench / shootout-fasta.rs
1 // Copyright 2012-2013 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 /* -*- mode: rust; indent-tabs-mode: nil -*-
12  * Implementation of 'fasta' benchmark from
13  * Computer Language Benchmarks Game
14  * http://shootout.alioth.debian.org/
15  */
16
17 use std::io;
18 use std::io::{BufferedWriter, File};
19 use std::cmp::min;
20 use std::os;
21 use std::vec_ng::Vec;
22
23 static LINE_LENGTH: uint = 60;
24 static IM: u32 = 139968;
25
26 struct MyRandom {
27     last: u32
28 }
29 impl MyRandom {
30     fn new() -> MyRandom { MyRandom { last: 42 } }
31     fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
32     fn gen(&mut self) -> u32 {
33         self.last = (self.last * 3877 + 29573) % IM;
34         self.last
35     }
36 }
37
38 struct AAGen<'a> {
39     rng: &'a mut MyRandom,
40     data: Vec<(u32, u8)> }
41 impl<'a> AAGen<'a> {
42     fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
43         let mut cum = 0.;
44         let data = aa.iter()
45             .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
46             .collect();
47         AAGen { rng: rng, data: data }
48     }
49 }
50 impl<'a> Iterator<u8> for AAGen<'a> {
51     fn next(&mut self) -> Option<u8> {
52         let r = self.rng.gen();
53         self.data.iter()
54             .skip_while(|pc| pc.val0() < r)
55             .map(|&(_, c)| c)
56             .next()
57     }
58 }
59
60 fn make_fasta<W: Writer, I: Iterator<u8>>(
61     wr: &mut W, header: &str, mut it: I, mut n: uint)
62 {
63     wr.write(header.as_bytes());
64     let mut line = [0u8, .. LINE_LENGTH + 1];
65     while n > 0 {
66         let nb = min(LINE_LENGTH, n);
67         for i in range(0, nb) {
68             line[i] = it.next().unwrap();
69         }
70         n -= nb;
71         line[nb] = '\n' as u8;
72         wr.write(line.slice_to(nb + 1));
73     }
74 }
75
76 fn run<W: Writer>(writer: &mut W) {
77     let args = os::args();
78     let n = if os::getenv("RUST_BENCH").is_some() {
79         25000000
80     } else if args.len() <= 1u {
81         1000
82     } else {
83         from_str(args[1]).unwrap()
84     };
85
86     let rng = &mut MyRandom::new();
87     let alu =
88         "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
89         GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
90         CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
91         ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
92         GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
93         AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
94         AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
95     let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
96                 ('t', 0.27), ('B', 0.02), ('D', 0.02),
97                 ('H', 0.02), ('K', 0.02), ('M', 0.02),
98                 ('N', 0.02), ('R', 0.02), ('S', 0.02),
99                 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
100     let homosapiens = &[('a', 0.3029549426680),
101                         ('c', 0.1979883004921),
102                         ('g', 0.1975473066391),
103                         ('t', 0.3015094502008)];
104
105     make_fasta(writer, ">ONE Homo sapiens alu\n",
106                alu.as_bytes().iter().cycle().map(|c| *c), n * 2);
107     make_fasta(writer, ">TWO IUB ambiguity codes\n",
108                AAGen::new(rng, iub), n * 3);
109     make_fasta(writer, ">THREE Homo sapiens frequency\n",
110                AAGen::new(rng, homosapiens), n * 5);
111
112     writer.flush();
113 }
114
115 fn main() {
116     if os::getenv("RUST_BENCH").is_some() {
117         let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
118         run(&mut file);
119     } else {
120         run(&mut io::stdout());
121     }
122 }