]> git.lizzy.rs Git - rust.git/blob - src/libtest/lib.rs
Convert most code to new inner attribute syntax.
[rust.git] / src / libtest / lib.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 //! Support code for rustc's built in unit-test and micro-benchmarking
12 //! framework.
13 //!
14 //! Almost all user code will only be interested in `BenchHarness` and
15 //! `black_box`. All other interactions (such as writing tests and
16 //! benchmarks themselves) should be done via the `#[test]` and
17 //! `#[bench]` attributes.
18 //!
19 //! See the [Testing Guide](../guide-testing.html) for more details.
20
21 // Currently, not much of this is meant for users. It is intended to
22 // support the simplest interface possible for representing and
23 // running tests while providing a base that other test frameworks may
24 // build off of.
25
26 #![crate_id = "test#0.10-pre"]
27 #![comment = "Rust internal test library only used by rustc"]
28 #![license = "MIT/ASL2"]
29 #![crate_type = "rlib"]
30 #![crate_type = "dylib"]
31 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
32        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
33        html_root_url = "http://static.rust-lang.org/doc/master")]
34
35 #![feature(asm, macro_rules)]
36 #![deny(deprecated_owned_vector)]
37
38 extern crate collections;
39 extern crate getopts;
40 extern crate serialize;
41 extern crate term;
42 extern crate time;
43
44 use collections::TreeMap;
45 use stats::Stats;
46 use time::precise_time_ns;
47 use getopts::{OptGroup, optflag, optopt};
48 use serialize::{json, Decodable};
49 use serialize::json::{Json, ToJson};
50 use term::Terminal;
51 use term::color::{Color, RED, YELLOW, GREEN, CYAN};
52
53 use std::cmp;
54 use std::f64;
55 use std::fmt;
56 use std::from_str::FromStr;
57 use std::io::stdio::StdWriter;
58 use std::io::{File, ChanReader, ChanWriter};
59 use std::io;
60 use std::os;
61 use std::str;
62 use std::task;
63
64 // to be used by rustc to compile tests in libtest
65 pub mod test {
66     pub use {BenchHarness, TestName, TestResult, TestDesc,
67              TestDescAndFn, TestOpts, TrFailed, TrIgnored, TrOk,
68              Metric, MetricMap, MetricAdded, MetricRemoved,
69              MetricChange, Improvement, Regression, LikelyNoise,
70              StaticTestFn, StaticTestName, DynTestName, DynTestFn,
71              run_test, test_main, test_main_static, filter_tests,
72              parse_opts, StaticBenchFn};
73 }
74
75 pub mod stats;
76
77 // The name of a test. By convention this follows the rules for rust
78 // paths; i.e. it should be a series of identifiers separated by double
79 // colons. This way if some test runner wants to arrange the tests
80 // hierarchically it may.
81
82 #[deriving(Clone)]
83 pub enum TestName {
84     StaticTestName(&'static str),
85     DynTestName(~str)
86 }
87 impl fmt::Show for TestName {
88     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89         match *self {
90             StaticTestName(s) => f.buf.write_str(s),
91             DynTestName(ref s) => f.buf.write_str(s.as_slice()),
92         }
93     }
94 }
95
96 #[deriving(Clone)]
97 enum NamePadding { PadNone, PadOnLeft, PadOnRight }
98
99 impl TestDesc {
100     fn padded_name(&self, column_count: uint, align: NamePadding) -> ~str {
101         use std::num::Saturating;
102         let name = self.name.to_str();
103         let fill = column_count.saturating_sub(name.len());
104         let pad = " ".repeat(fill);
105         match align {
106             PadNone => name,
107             PadOnLeft => pad.append(name),
108             PadOnRight => name.append(pad),
109         }
110     }
111 }
112
113 /// Represents a benchmark function.
114 pub trait TDynBenchFn {
115     fn run(&self, harness: &mut BenchHarness);
116 }
117
118 // A function that runs a test. If the function returns successfully,
119 // the test succeeds; if the function fails then the test fails. We
120 // may need to come up with a more clever definition of test in order
121 // to support isolation of tests into tasks.
122 pub enum TestFn {
123     StaticTestFn(fn()),
124     StaticBenchFn(fn(&mut BenchHarness)),
125     StaticMetricFn(proc(&mut MetricMap)),
126     DynTestFn(proc:Send()),
127     DynMetricFn(proc(&mut MetricMap)),
128     DynBenchFn(~TDynBenchFn)
129 }
130
131 impl TestFn {
132     fn padding(&self) -> NamePadding {
133         match self {
134             &StaticTestFn(..)   => PadNone,
135             &StaticBenchFn(..)  => PadOnRight,
136             &StaticMetricFn(..) => PadOnRight,
137             &DynTestFn(..)      => PadNone,
138             &DynMetricFn(..)    => PadOnRight,
139             &DynBenchFn(..)     => PadOnRight,
140         }
141     }
142 }
143
144 /// Manager of the benchmarking runs.
145 ///
146 /// This is feed into functions marked with `#[bench]` to allow for
147 /// set-up & tear-down before running a piece of code repeatedly via a
148 /// call to `iter`.
149 pub struct BenchHarness {
150     priv iterations: u64,
151     priv ns_start: u64,
152     priv ns_end: u64,
153     bytes: u64
154 }
155
156 // The definition of a single test. A test runner will run a list of
157 // these.
158 #[deriving(Clone)]
159 pub struct TestDesc {
160     name: TestName,
161     ignore: bool,
162     should_fail: bool
163 }
164
165 pub struct TestDescAndFn {
166     desc: TestDesc,
167     testfn: TestFn,
168 }
169
170 #[deriving(Clone, Encodable, Decodable, Eq, Show)]
171 pub struct Metric {
172     priv value: f64,
173     priv noise: f64
174 }
175
176 impl Metric {
177     pub fn new(value: f64, noise: f64) -> Metric {
178         Metric {value: value, noise: noise}
179     }
180 }
181
182 #[deriving(Eq)]
183 pub struct MetricMap(TreeMap<~str,Metric>);
184
185 impl Clone for MetricMap {
186     fn clone(&self) -> MetricMap {
187         let MetricMap(ref map) = *self;
188         MetricMap(map.clone())
189     }
190 }
191
192 /// Analysis of a single change in metric
193 #[deriving(Eq, Show)]
194 pub enum MetricChange {
195     LikelyNoise,
196     MetricAdded,
197     MetricRemoved,
198     Improvement(f64),
199     Regression(f64)
200 }
201
202 pub type MetricDiff = TreeMap<~str,MetricChange>;
203
204 // The default console test runner. It accepts the command line
205 // arguments and a vector of test_descs.
206 pub fn test_main(args: &[~str], tests: Vec<TestDescAndFn> ) {
207     let opts =
208         match parse_opts(args) {
209             Some(Ok(o)) => o,
210             Some(Err(msg)) => fail!("{}", msg),
211             None => return
212         };
213     match run_tests_console(&opts, tests) {
214         Ok(true) => {}
215         Ok(false) => fail!("Some tests failed"),
216         Err(e) => fail!("io error when running tests: {}", e),
217     }
218 }
219
220 // A variant optimized for invocation with a static test vector.
221 // This will fail (intentionally) when fed any dynamic tests, because
222 // it is copying the static values out into a dynamic vector and cannot
223 // copy dynamic values. It is doing this because from this point on
224 // a ~[TestDescAndFn] is used in order to effect ownership-transfer
225 // semantics into parallel test runners, which in turn requires a ~[]
226 // rather than a &[].
227 pub fn test_main_static(args: &[~str], tests: &[TestDescAndFn]) {
228     let owned_tests = tests.iter().map(|t| {
229         match t.testfn {
230             StaticTestFn(f) =>
231             TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
232
233             StaticBenchFn(f) =>
234             TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
235
236             _ => {
237                 fail!("non-static tests passed to test::test_main_static");
238             }
239         }
240     }).collect();
241     test_main(args, owned_tests)
242 }
243
244 pub struct TestOpts {
245     filter: Option<~str>,
246     run_ignored: bool,
247     run_tests: bool,
248     run_benchmarks: bool,
249     ratchet_metrics: Option<Path>,
250     ratchet_noise_percent: Option<f64>,
251     save_metrics: Option<Path>,
252     test_shard: Option<(uint,uint)>,
253     logfile: Option<Path>
254 }
255
256 /// Result of parsing the options.
257 pub type OptRes = Result<TestOpts, ~str>;
258
259 fn optgroups() -> Vec<getopts::OptGroup> {
260     vec!(getopts::optflag("", "ignored", "Run ignored tests"),
261       getopts::optflag("", "test", "Run tests and not benchmarks"),
262       getopts::optflag("", "bench", "Run benchmarks instead of tests"),
263       getopts::optflag("h", "help", "Display this message (longer with --help)"),
264       getopts::optopt("", "save-metrics", "Location to save bench metrics",
265                      "PATH"),
266       getopts::optopt("", "ratchet-metrics",
267                      "Location to load and save metrics from. The metrics \
268                       loaded are cause benchmarks to fail if they run too \
269                       slowly", "PATH"),
270       getopts::optopt("", "ratchet-noise-percent",
271                      "Tests within N% of the recorded metrics will be \
272                       considered as passing", "PERCENTAGE"),
273       getopts::optopt("", "logfile", "Write logs to the specified file instead \
274                           of stdout", "PATH"),
275       getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
276                      "A.B"))
277 }
278
279 fn usage(binary: &str, helpstr: &str) {
280     let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
281     println!("{}", getopts::usage(message, optgroups().as_slice()));
282     println!("");
283     if helpstr == "help" {
284         println!("{}", "\
285 The FILTER is matched against the name of all tests to run, and if any tests
286 have a substring match, only those tests are run.
287
288 By default, all tests are run in parallel. This can be altered with the
289 RUST_TEST_TASKS environment variable when running tests (set it to 1).
290
291 Test Attributes:
292
293     #[test]        - Indicates a function is a test to be run. This function
294                      takes no arguments.
295     #[bench]       - Indicates a function is a benchmark to be run. This
296                      function takes one argument (test::BenchHarness).
297     #[should_fail] - This function (also labeled with #[test]) will only pass if
298                      the code causes a failure (an assertion failure or fail!)
299     #[ignore]      - When applied to a function which is already attributed as a
300                      test, then the test runner will ignore these tests during
301                      normal test runs. Running with --ignored will run these
302                      tests. This may also be written as #[ignore(cfg(...))] to
303                      ignore the test on certain configurations.");
304     }
305 }
306
307 // Parses command line arguments into test options
308 pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
309     let args_ = args.tail();
310     let matches =
311         match getopts::getopts(args_, optgroups().as_slice()) {
312           Ok(m) => m,
313           Err(f) => return Some(Err(f.to_err_msg()))
314         };
315
316     if matches.opt_present("h") { usage(args[0], "h"); return None; }
317     if matches.opt_present("help") { usage(args[0], "help"); return None; }
318
319     let filter =
320         if matches.free.len() > 0 {
321             Some((*matches.free.get(0)).clone())
322         } else {
323             None
324         };
325
326     let run_ignored = matches.opt_present("ignored");
327
328     let logfile = matches.opt_str("logfile");
329     let logfile = logfile.map(|s| Path::new(s));
330
331     let run_benchmarks = matches.opt_present("bench");
332     let run_tests = ! run_benchmarks ||
333         matches.opt_present("test");
334
335     let ratchet_metrics = matches.opt_str("ratchet-metrics");
336     let ratchet_metrics = ratchet_metrics.map(|s| Path::new(s));
337
338     let ratchet_noise_percent = matches.opt_str("ratchet-noise-percent");
339     let ratchet_noise_percent = ratchet_noise_percent.map(|s| from_str::<f64>(s).unwrap());
340
341     let save_metrics = matches.opt_str("save-metrics");
342     let save_metrics = save_metrics.map(|s| Path::new(s));
343
344     let test_shard = matches.opt_str("test-shard");
345     let test_shard = opt_shard(test_shard);
346
347     let test_opts = TestOpts {
348         filter: filter,
349         run_ignored: run_ignored,
350         run_tests: run_tests,
351         run_benchmarks: run_benchmarks,
352         ratchet_metrics: ratchet_metrics,
353         ratchet_noise_percent: ratchet_noise_percent,
354         save_metrics: save_metrics,
355         test_shard: test_shard,
356         logfile: logfile
357     };
358
359     Some(Ok(test_opts))
360 }
361
362 pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
363     match maybestr {
364         None => None,
365         Some(s) => {
366             let mut it = s.split('.');
367             match (it.next().and_then(from_str), it.next().and_then(from_str), it.next()) {
368                 (Some(a), Some(b), None) => Some((a, b)),
369                 _ => None,
370             }
371         }
372     }
373 }
374
375
376 #[deriving(Clone, Eq)]
377 pub struct BenchSamples {
378     priv ns_iter_summ: stats::Summary,
379     priv mb_s: uint
380 }
381
382 #[deriving(Clone, Eq)]
383 pub enum TestResult {
384     TrOk,
385     TrFailed,
386     TrIgnored,
387     TrMetrics(MetricMap),
388     TrBench(BenchSamples),
389 }
390
391 enum OutputLocation<T> {
392     Pretty(term::Terminal<T>),
393     Raw(T),
394 }
395
396 struct ConsoleTestState<T> {
397     log_out: Option<File>,
398     out: OutputLocation<T>,
399     use_color: bool,
400     total: uint,
401     passed: uint,
402     failed: uint,
403     ignored: uint,
404     measured: uint,
405     metrics: MetricMap,
406     failures: Vec<(TestDesc, Vec<u8> )> ,
407     max_name_len: uint, // number of columns to fill when aligning names
408 }
409
410 impl<T: Writer> ConsoleTestState<T> {
411     pub fn new(opts: &TestOpts,
412                _: Option<T>) -> io::IoResult<ConsoleTestState<StdWriter>> {
413         let log_out = match opts.logfile {
414             Some(ref path) => Some(try!(File::create(path))),
415             None => None
416         };
417         let out = match term::Terminal::new(io::stdio::stdout_raw()) {
418             Err(_) => Raw(io::stdio::stdout_raw()),
419             Ok(t) => Pretty(t)
420         };
421         Ok(ConsoleTestState {
422             out: out,
423             log_out: log_out,
424             use_color: use_color(),
425             total: 0u,
426             passed: 0u,
427             failed: 0u,
428             ignored: 0u,
429             measured: 0u,
430             metrics: MetricMap::new(),
431             failures: Vec::new(),
432             max_name_len: 0u,
433         })
434     }
435
436     pub fn write_ok(&mut self) -> io::IoResult<()> {
437         self.write_pretty("ok", term::color::GREEN)
438     }
439
440     pub fn write_failed(&mut self) -> io::IoResult<()> {
441         self.write_pretty("FAILED", term::color::RED)
442     }
443
444     pub fn write_ignored(&mut self) -> io::IoResult<()> {
445         self.write_pretty("ignored", term::color::YELLOW)
446     }
447
448     pub fn write_metric(&mut self) -> io::IoResult<()> {
449         self.write_pretty("metric", term::color::CYAN)
450     }
451
452     pub fn write_bench(&mut self) -> io::IoResult<()> {
453         self.write_pretty("bench", term::color::CYAN)
454     }
455
456     pub fn write_added(&mut self) -> io::IoResult<()> {
457         self.write_pretty("added", term::color::GREEN)
458     }
459
460     pub fn write_improved(&mut self) -> io::IoResult<()> {
461         self.write_pretty("improved", term::color::GREEN)
462     }
463
464     pub fn write_removed(&mut self) -> io::IoResult<()> {
465         self.write_pretty("removed", term::color::YELLOW)
466     }
467
468     pub fn write_regressed(&mut self) -> io::IoResult<()> {
469         self.write_pretty("regressed", term::color::RED)
470     }
471
472     pub fn write_pretty(&mut self,
473                         word: &str,
474                         color: term::color::Color) -> io::IoResult<()> {
475         match self.out {
476             Pretty(ref mut term) => {
477                 if self.use_color {
478                     try!(term.fg(color));
479                 }
480                 try!(term.write(word.as_bytes()));
481                 if self.use_color {
482                     try!(term.reset());
483                 }
484                 Ok(())
485             }
486             Raw(ref mut stdout) => stdout.write(word.as_bytes())
487         }
488     }
489
490     pub fn write_plain(&mut self, s: &str) -> io::IoResult<()> {
491         match self.out {
492             Pretty(ref mut term) => term.write(s.as_bytes()),
493             Raw(ref mut stdout) => stdout.write(s.as_bytes())
494         }
495     }
496
497     pub fn write_run_start(&mut self, len: uint) -> io::IoResult<()> {
498         self.total = len;
499         let noun = if len != 1 { &"tests" } else { &"test" };
500         self.write_plain(format!("\nrunning {} {}\n", len, noun))
501     }
502
503     pub fn write_test_start(&mut self, test: &TestDesc,
504                             align: NamePadding) -> io::IoResult<()> {
505         let name = test.padded_name(self.max_name_len, align);
506         self.write_plain(format!("test {} ... ", name))
507     }
508
509     pub fn write_result(&mut self, result: &TestResult) -> io::IoResult<()> {
510         try!(match *result {
511             TrOk => self.write_ok(),
512             TrFailed => self.write_failed(),
513             TrIgnored => self.write_ignored(),
514             TrMetrics(ref mm) => {
515                 try!(self.write_metric());
516                 self.write_plain(format!(": {}", fmt_metrics(mm)))
517             }
518             TrBench(ref bs) => {
519                 try!(self.write_bench());
520                 self.write_plain(format!(": {}", fmt_bench_samples(bs)))
521             }
522         });
523         self.write_plain("\n")
524     }
525
526     pub fn write_log(&mut self, test: &TestDesc,
527                      result: &TestResult) -> io::IoResult<()> {
528         match self.log_out {
529             None => Ok(()),
530             Some(ref mut o) => {
531                 let s = format!("{} {}\n", match *result {
532                         TrOk => ~"ok",
533                         TrFailed => ~"failed",
534                         TrIgnored => ~"ignored",
535                         TrMetrics(ref mm) => fmt_metrics(mm),
536                         TrBench(ref bs) => fmt_bench_samples(bs)
537                     }, test.name.to_str());
538                 o.write(s.as_bytes())
539             }
540         }
541     }
542
543     pub fn write_failures(&mut self) -> io::IoResult<()> {
544         try!(self.write_plain("\nfailures:\n"));
545         let mut failures = Vec::new();
546         let mut fail_out  = ~"";
547         for &(ref f, ref stdout) in self.failures.iter() {
548             failures.push(f.name.to_str());
549             if stdout.len() > 0 {
550                 fail_out.push_str(format!("---- {} stdout ----\n\t",
551                                   f.name.to_str()));
552                 let output = str::from_utf8_lossy(stdout.as_slice());
553                 fail_out.push_str(output.as_slice().replace("\n", "\n\t"));
554                 fail_out.push_str("\n");
555             }
556         }
557         if fail_out.len() > 0 {
558             try!(self.write_plain("\n"));
559             try!(self.write_plain(fail_out));
560         }
561
562         try!(self.write_plain("\nfailures:\n"));
563         failures.as_mut_slice().sort();
564         for name in failures.iter() {
565             try!(self.write_plain(format!("    {}\n", name.to_str())));
566         }
567         Ok(())
568     }
569
570     pub fn write_metric_diff(&mut self, diff: &MetricDiff) -> io::IoResult<()> {
571         let mut noise = 0;
572         let mut improved = 0;
573         let mut regressed = 0;
574         let mut added = 0;
575         let mut removed = 0;
576
577         for (k, v) in diff.iter() {
578             match *v {
579                 LikelyNoise => noise += 1,
580                 MetricAdded => {
581                     added += 1;
582                     try!(self.write_added());
583                     try!(self.write_plain(format!(": {}\n", *k)));
584                 }
585                 MetricRemoved => {
586                     removed += 1;
587                     try!(self.write_removed());
588                     try!(self.write_plain(format!(": {}\n", *k)));
589                 }
590                 Improvement(pct) => {
591                     improved += 1;
592                     try!(self.write_plain(format!(": {}", *k)));
593                     try!(self.write_improved());
594                     try!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
595                 }
596                 Regression(pct) => {
597                     regressed += 1;
598                     try!(self.write_plain(format!(": {}", *k)));
599                     try!(self.write_regressed());
600                     try!(self.write_plain(format!(" by {:.2f}%\n", pct as f64)));
601                 }
602             }
603         }
604         try!(self.write_plain(format!("result of ratchet: {} metrics added, \
605                                         {} removed, {} improved, {} regressed, \
606                                         {} noise\n",
607                                        added, removed, improved, regressed,
608                                        noise)));
609         if regressed == 0 {
610             try!(self.write_plain("updated ratchet file\n"));
611         } else {
612             try!(self.write_plain("left ratchet file untouched\n"));
613         }
614         Ok(())
615     }
616
617     pub fn write_run_finish(&mut self,
618                             ratchet_metrics: &Option<Path>,
619                             ratchet_pct: Option<f64>) -> io::IoResult<bool> {
620         assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
621
622         let ratchet_success = match *ratchet_metrics {
623             None => true,
624             Some(ref pth) => {
625                 try!(self.write_plain(format!("\nusing metrics ratchet: {}\n",
626                                         pth.display())));
627                 match ratchet_pct {
628                     None => (),
629                     Some(pct) =>
630                         try!(self.write_plain(format!("with noise-tolerance \
631                                                          forced to: {}%\n",
632                                                         pct)))
633                 }
634                 let (diff, ok) = self.metrics.ratchet(pth, ratchet_pct);
635                 try!(self.write_metric_diff(&diff));
636                 ok
637             }
638         };
639
640         let test_success = self.failed == 0u;
641         if !test_success {
642             try!(self.write_failures());
643         }
644
645         let success = ratchet_success && test_success;
646
647         try!(self.write_plain("\ntest result: "));
648         if success {
649             // There's no parallelism at this point so it's safe to use color
650             try!(self.write_ok());
651         } else {
652             try!(self.write_failed());
653         }
654         let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
655                         self.passed, self.failed, self.ignored, self.measured);
656         try!(self.write_plain(s));
657         return Ok(success);
658     }
659 }
660
661 pub fn fmt_metrics(mm: &MetricMap) -> ~str {
662     let MetricMap(ref mm) = *mm;
663     let v : Vec<~str> = mm.iter()
664         .map(|(k,v)| format!("{}: {} (+/- {})",
665                           *k,
666                           v.value as f64,
667                           v.noise as f64))
668         .collect();
669     v.connect(", ")
670 }
671
672 pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
673     if bs.mb_s != 0 {
674         format!("{:>9} ns/iter (+/- {}) = {} MB/s",
675              bs.ns_iter_summ.median as uint,
676              (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
677              bs.mb_s)
678     } else {
679         format!("{:>9} ns/iter (+/- {})",
680              bs.ns_iter_summ.median as uint,
681              (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
682     }
683 }
684
685 // A simple console test runner
686 pub fn run_tests_console(opts: &TestOpts,
687                          tests: Vec<TestDescAndFn> ) -> io::IoResult<bool> {
688     fn callback<T: Writer>(event: &TestEvent,
689                            st: &mut ConsoleTestState<T>) -> io::IoResult<()> {
690         match (*event).clone() {
691             TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
692             TeWait(ref test, padding) => st.write_test_start(test, padding),
693             TeResult(test, result, stdout) => {
694                 try!(st.write_log(&test, &result));
695                 try!(st.write_result(&result));
696                 match result {
697                     TrOk => st.passed += 1,
698                     TrIgnored => st.ignored += 1,
699                     TrMetrics(mm) => {
700                         let tname = test.name.to_str();
701                         let MetricMap(mm) = mm;
702                         for (k,v) in mm.iter() {
703                             st.metrics.insert_metric(tname + "." + *k,
704                                                      v.value, v.noise);
705                         }
706                         st.measured += 1
707                     }
708                     TrBench(bs) => {
709                         st.metrics.insert_metric(test.name.to_str(),
710                                                  bs.ns_iter_summ.median,
711                                                  bs.ns_iter_summ.max - bs.ns_iter_summ.min);
712                         st.measured += 1
713                     }
714                     TrFailed => {
715                         st.failed += 1;
716                         st.failures.push((test, stdout));
717                     }
718                 }
719                 Ok(())
720             }
721         }
722     }
723     let mut st = try!(ConsoleTestState::new(opts, None::<StdWriter>));
724     fn len_if_padded(t: &TestDescAndFn) -> uint {
725         match t.testfn.padding() {
726             PadNone => 0u,
727             PadOnLeft | PadOnRight => t.desc.name.to_str().len(),
728         }
729     }
730     match tests.iter().max_by(|t|len_if_padded(*t)) {
731         Some(t) => {
732             let n = t.desc.name.to_str();
733             st.max_name_len = n.len();
734         },
735         None => {}
736     }
737     try!(run_tests(opts, tests, |x| callback(&x, &mut st)));
738     match opts.save_metrics {
739         None => (),
740         Some(ref pth) => {
741             try!(st.metrics.save(pth));
742             try!(st.write_plain(format!("\nmetrics saved to: {}",
743                                           pth.display())));
744         }
745     }
746     return st.write_run_finish(&opts.ratchet_metrics, opts.ratchet_noise_percent);
747 }
748
749 #[test]
750 fn should_sort_failures_before_printing_them() {
751     use std::io::MemWriter;
752     use std::str;
753
754     let test_a = TestDesc {
755         name: StaticTestName("a"),
756         ignore: false,
757         should_fail: false
758     };
759
760     let test_b = TestDesc {
761         name: StaticTestName("b"),
762         ignore: false,
763         should_fail: false
764     };
765
766     let mut st = ConsoleTestState {
767         log_out: None,
768         out: Raw(MemWriter::new()),
769         use_color: false,
770         total: 0u,
771         passed: 0u,
772         failed: 0u,
773         ignored: 0u,
774         measured: 0u,
775         max_name_len: 10u,
776         metrics: MetricMap::new(),
777         failures: vec!((test_b, Vec::new()), (test_a, Vec::new()))
778     };
779
780     st.write_failures().unwrap();
781     let s = match st.out {
782         Raw(ref m) => str::from_utf8_lossy(m.get_ref()),
783         Pretty(_) => unreachable!()
784     };
785
786     let apos = s.as_slice().find_str("a").unwrap();
787     let bpos = s.as_slice().find_str("b").unwrap();
788     assert!(apos < bpos);
789 }
790
791 fn use_color() -> bool { return get_concurrency() == 1; }
792
793 #[deriving(Clone)]
794 enum TestEvent {
795     TeFiltered(Vec<TestDesc> ),
796     TeWait(TestDesc, NamePadding),
797     TeResult(TestDesc, TestResult, Vec<u8> ),
798 }
799
800 pub type MonitorMsg = (TestDesc, TestResult, Vec<u8> );
801
802 fn run_tests(opts: &TestOpts,
803              tests: Vec<TestDescAndFn> ,
804              callback: |e: TestEvent| -> io::IoResult<()>) -> io::IoResult<()> {
805     let filtered_tests = filter_tests(opts, tests);
806     let filtered_descs = filtered_tests.iter()
807                                        .map(|t| t.desc.clone())
808                                        .collect();
809
810     try!(callback(TeFiltered(filtered_descs)));
811
812     let (filtered_tests, filtered_benchs_and_metrics) =
813         filtered_tests.partition(|e| {
814             match e.testfn {
815                 StaticTestFn(_) | DynTestFn(_) => true,
816                 _ => false
817             }
818         });
819
820     // It's tempting to just spawn all the tests at once, but since we have
821     // many tests that run in other processes we would be making a big mess.
822     let concurrency = get_concurrency();
823
824     let mut remaining = filtered_tests;
825     remaining.reverse();
826     let mut pending = 0;
827
828     let (tx, rx) = channel::<MonitorMsg>();
829
830     while pending > 0 || !remaining.is_empty() {
831         while pending < concurrency && !remaining.is_empty() {
832             let test = remaining.pop().unwrap();
833             if concurrency == 1 {
834                 // We are doing one test at a time so we can print the name
835                 // of the test before we run it. Useful for debugging tests
836                 // that hang forever.
837                 try!(callback(TeWait(test.desc.clone(), test.testfn.padding())));
838             }
839             run_test(!opts.run_tests, test, tx.clone());
840             pending += 1;
841         }
842
843         let (desc, result, stdout) = rx.recv();
844         if concurrency != 1 {
845             try!(callback(TeWait(desc.clone(), PadNone)));
846         }
847         try!(callback(TeResult(desc, result, stdout)));
848         pending -= 1;
849     }
850
851     // All benchmarks run at the end, in serial.
852     // (this includes metric fns)
853     for b in filtered_benchs_and_metrics.move_iter() {
854         try!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
855         run_test(!opts.run_benchmarks, b, tx.clone());
856         let (test, result, stdout) = rx.recv();
857         try!(callback(TeResult(test, result, stdout)));
858     }
859     Ok(())
860 }
861
862 fn get_concurrency() -> uint {
863     use std::rt;
864     match os::getenv("RUST_TEST_TASKS") {
865         Some(s) => {
866             let opt_n: Option<uint> = FromStr::from_str(s);
867             match opt_n {
868                 Some(n) if n > 0 => n,
869                 _ => fail!("RUST_TEST_TASKS is `{}`, should be a positive integer.", s)
870             }
871         }
872         None => {
873             rt::default_sched_threads()
874         }
875     }
876 }
877
878 pub fn filter_tests(
879     opts: &TestOpts,
880     tests: Vec<TestDescAndFn> ) -> Vec<TestDescAndFn> {
881     let mut filtered = tests;
882
883     // Remove tests that don't match the test filter
884     filtered = if opts.filter.is_none() {
885         filtered
886     } else {
887         let filter_str = match opts.filter {
888           Some(ref f) => (*f).clone(),
889           None => ~""
890         };
891
892         fn filter_fn(test: TestDescAndFn, filter_str: &str) ->
893             Option<TestDescAndFn> {
894             if test.desc.name.to_str().contains(filter_str) {
895                 return Some(test);
896             } else {
897                 return None;
898             }
899         }
900
901         filtered.move_iter().filter_map(|x| filter_fn(x, filter_str)).collect()
902     };
903
904     // Maybe pull out the ignored test and unignore them
905     filtered = if !opts.run_ignored {
906         filtered
907     } else {
908         fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
909             if test.desc.ignore {
910                 let TestDescAndFn {desc, testfn} = test;
911                 Some(TestDescAndFn {
912                     desc: TestDesc {ignore: false, ..desc},
913                     testfn: testfn
914                 })
915             } else {
916                 None
917             }
918         };
919         filtered.move_iter().filter_map(|x| filter(x)).collect()
920     };
921
922     // Sort the tests alphabetically
923     filtered.sort_by(|t1, t2| t1.desc.name.to_str().cmp(&t2.desc.name.to_str()));
924
925     // Shard the remaining tests, if sharding requested.
926     match opts.test_shard {
927         None => filtered,
928         Some((a,b)) => {
929             filtered.move_iter().enumerate()
930             .filter(|&(i,_)| i % b == a)
931             .map(|(_,t)| t)
932             .collect()
933         }
934     }
935 }
936
937 pub fn run_test(force_ignore: bool,
938                 test: TestDescAndFn,
939                 monitor_ch: Sender<MonitorMsg>) {
940
941     let TestDescAndFn {desc, testfn} = test;
942
943     if force_ignore || desc.ignore {
944         monitor_ch.send((desc, TrIgnored, Vec::new()));
945         return;
946     }
947
948     #[allow(deprecated_owned_vector)]
949     fn run_test_inner(desc: TestDesc,
950                       monitor_ch: Sender<MonitorMsg>,
951                       testfn: proc:Send()) {
952         spawn(proc() {
953             let (tx, rx) = channel();
954             let mut reader = ChanReader::new(rx);
955             let stdout = ChanWriter::new(tx.clone());
956             let stderr = ChanWriter::new(tx);
957             let mut task = task::task().named(match desc.name {
958                 DynTestName(ref name) => name.clone().into_maybe_owned(),
959                 StaticTestName(name) => name.into_maybe_owned(),
960             });
961             task.opts.stdout = Some(~stdout as ~Writer:Send);
962             task.opts.stderr = Some(~stderr as ~Writer:Send);
963             let result_future = task.future_result();
964             task.spawn(testfn);
965
966             let stdout = reader.read_to_end().unwrap().move_iter().collect();
967             let task_result = result_future.recv();
968             let test_result = calc_result(&desc, task_result.is_ok());
969             monitor_ch.send((desc.clone(), test_result, stdout));
970         })
971     }
972
973     match testfn {
974         DynBenchFn(bencher) => {
975             let bs = ::bench::benchmark(|harness| bencher.run(harness));
976             monitor_ch.send((desc, TrBench(bs), Vec::new()));
977             return;
978         }
979         StaticBenchFn(benchfn) => {
980             let bs = ::bench::benchmark(|harness| benchfn(harness));
981             monitor_ch.send((desc, TrBench(bs), Vec::new()));
982             return;
983         }
984         DynMetricFn(f) => {
985             let mut mm = MetricMap::new();
986             f(&mut mm);
987             monitor_ch.send((desc, TrMetrics(mm), Vec::new()));
988             return;
989         }
990         StaticMetricFn(f) => {
991             let mut mm = MetricMap::new();
992             f(&mut mm);
993             monitor_ch.send((desc, TrMetrics(mm), Vec::new()));
994             return;
995         }
996         DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
997         StaticTestFn(f) => run_test_inner(desc, monitor_ch, proc() f())
998     }
999 }
1000
1001 fn calc_result(desc: &TestDesc, task_succeeded: bool) -> TestResult {
1002     if task_succeeded {
1003         if desc.should_fail { TrFailed }
1004         else { TrOk }
1005     } else {
1006         if desc.should_fail { TrOk }
1007         else { TrFailed }
1008     }
1009 }
1010
1011
1012 impl ToJson for Metric {
1013     fn to_json(&self) -> json::Json {
1014         let mut map = ~TreeMap::new();
1015         map.insert(~"value", json::Number(self.value));
1016         map.insert(~"noise", json::Number(self.noise));
1017         json::Object(map)
1018     }
1019 }
1020
1021 // FIXME: remove decode_ after snapshot
1022 #[cfg(stage0)]
1023 fn decode_(json: Json) -> MetricMap {
1024     let mut decoder = json::Decoder::new(json);
1025     MetricMap(Decodable::decode(&mut decoder))
1026 }
1027
1028 #[cfg(not(stage0))]
1029 fn decode_(json: Json) -> MetricMap {
1030     let mut decoder = json::Decoder::new(json);
1031     MetricMap(match Decodable::decode(&mut decoder) {
1032         Ok(t) => t,
1033         Err(e) => fail!("failure decoding JSON: {}", e)
1034     })
1035 }
1036
1037
1038 impl MetricMap {
1039
1040     pub fn new() -> MetricMap {
1041         MetricMap(TreeMap::new())
1042     }
1043
1044     /// Load MetricDiff from a file.
1045     ///
1046     /// # Failure
1047     ///
1048     /// This function will fail if the path does not exist or the path does not
1049     /// contain a valid metric map.
1050     pub fn load(p: &Path) -> MetricMap {
1051         assert!(p.exists());
1052         let mut f = File::open(p).unwrap();
1053         let value = json::from_reader(&mut f as &mut io::Reader).unwrap();
1054         decode_(value)
1055     }
1056
1057     /// Write MetricDiff to a file.
1058     pub fn save(&self, p: &Path) -> io::IoResult<()> {
1059         let mut file = try!(File::create(p));
1060         let MetricMap(ref map) = *self;
1061         map.to_json().to_pretty_writer(&mut file)
1062     }
1063
1064     /// Compare against another MetricMap. Optionally compare all
1065     /// measurements in the maps using the provided `noise_pct` as a
1066     /// percentage of each value to consider noise. If `None`, each
1067     /// measurement's noise threshold is independently chosen as the
1068     /// maximum of that measurement's recorded noise quantity in either
1069     /// map.
1070     pub fn compare_to_old(&self, old: &MetricMap,
1071                           noise_pct: Option<f64>) -> MetricDiff {
1072         let mut diff : MetricDiff = TreeMap::new();
1073         let MetricMap(ref selfmap) = *self;
1074         let MetricMap(ref old) = *old;
1075         for (k, vold) in old.iter() {
1076             let r = match selfmap.find(k) {
1077                 None => MetricRemoved,
1078                 Some(v) => {
1079                     let delta = v.value - vold.value;
1080                     let noise = match noise_pct {
1081                         None => vold.noise.abs().max(v.noise.abs()),
1082                         Some(pct) => vold.value * pct / 100.0
1083                     };
1084                     if delta.abs() <= noise {
1085                         LikelyNoise
1086                     } else {
1087                         let pct = delta.abs() / vold.value.max(f64::EPSILON) * 100.0;
1088                         if vold.noise < 0.0 {
1089                             // When 'noise' is negative, it means we want
1090                             // to see deltas that go up over time, and can
1091                             // only tolerate slight negative movement.
1092                             if delta < 0.0 {
1093                                 Regression(pct)
1094                             } else {
1095                                 Improvement(pct)
1096                             }
1097                         } else {
1098                             // When 'noise' is positive, it means we want
1099                             // to see deltas that go down over time, and
1100                             // can only tolerate slight positive movements.
1101                             if delta < 0.0 {
1102                                 Improvement(pct)
1103                             } else {
1104                                 Regression(pct)
1105                             }
1106                         }
1107                     }
1108                 }
1109             };
1110             diff.insert((*k).clone(), r);
1111         }
1112         let MetricMap(ref map) = *self;
1113         for (k, _) in map.iter() {
1114             if !diff.contains_key(k) {
1115                 diff.insert((*k).clone(), MetricAdded);
1116             }
1117         }
1118         diff
1119     }
1120
1121     /// Insert a named `value` (+/- `noise`) metric into the map. The value
1122     /// must be non-negative. The `noise` indicates the uncertainty of the
1123     /// metric, which doubles as the "noise range" of acceptable
1124     /// pairwise-regressions on this named value, when comparing from one
1125     /// metric to the next using `compare_to_old`.
1126     ///
1127     /// If `noise` is positive, then it means this metric is of a value
1128     /// you want to see grow smaller, so a change larger than `noise` in the
1129     /// positive direction represents a regression.
1130     ///
1131     /// If `noise` is negative, then it means this metric is of a value
1132     /// you want to see grow larger, so a change larger than `noise` in the
1133     /// negative direction represents a regression.
1134     pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
1135         let m = Metric {
1136             value: value,
1137             noise: noise
1138         };
1139         let MetricMap(ref mut map) = *self;
1140         map.insert(name.to_owned(), m);
1141     }
1142
1143     /// Attempt to "ratchet" an external metric file. This involves loading
1144     /// metrics from a metric file (if it exists), comparing against
1145     /// the metrics in `self` using `compare_to_old`, and rewriting the
1146     /// file to contain the metrics in `self` if none of the
1147     /// `MetricChange`s are `Regression`. Returns the diff as well
1148     /// as a boolean indicating whether the ratchet succeeded.
1149     pub fn ratchet(&self, p: &Path, pct: Option<f64>) -> (MetricDiff, bool) {
1150         let old = if p.exists() {
1151             MetricMap::load(p)
1152         } else {
1153             MetricMap::new()
1154         };
1155
1156         let diff : MetricDiff = self.compare_to_old(&old, pct);
1157         let ok = diff.iter().all(|(_, v)| {
1158             match *v {
1159                 Regression(_) => false,
1160                 _ => true
1161             }
1162         });
1163
1164         if ok {
1165             self.save(p).unwrap();
1166         }
1167         return (diff, ok)
1168     }
1169 }
1170
1171
1172 // Benchmarking
1173
1174 /// A function that is opaque to the optimizer, to allow benchmarks to
1175 /// pretend to use outputs to assist in avoiding dead-code
1176 /// elimination.
1177 ///
1178 /// This function is a no-op, and does not even read from `dummy`.
1179 pub fn black_box<T>(dummy: T) {
1180     // we need to "use" the argument in some way LLVM can't
1181     // introspect.
1182     unsafe {asm!("" : : "r"(&dummy))}
1183 }
1184
1185
1186 impl BenchHarness {
1187     /// Callback for benchmark functions to run in their body.
1188     pub fn iter<T>(&mut self, inner: || -> T) {
1189         self.ns_start = precise_time_ns();
1190         let k = self.iterations;
1191         for _ in range(0u64, k) {
1192             black_box(inner());
1193         }
1194         self.ns_end = precise_time_ns();
1195     }
1196
1197     pub fn ns_elapsed(&mut self) -> u64 {
1198         if self.ns_start == 0 || self.ns_end == 0 {
1199             0
1200         } else {
1201             self.ns_end - self.ns_start
1202         }
1203     }
1204
1205     pub fn ns_per_iter(&mut self) -> u64 {
1206         if self.iterations == 0 {
1207             0
1208         } else {
1209             self.ns_elapsed() / cmp::max(self.iterations, 1)
1210         }
1211     }
1212
1213     pub fn bench_n(&mut self, n: u64, f: |&mut BenchHarness|) {
1214         self.iterations = n;
1215         f(self);
1216     }
1217
1218     // This is a more statistics-driven benchmark algorithm
1219     pub fn auto_bench(&mut self, f: |&mut BenchHarness|) -> stats::Summary {
1220
1221         // Initial bench run to get ballpark figure.
1222         let mut n = 1_u64;
1223         self.bench_n(n, |x| f(x));
1224
1225         // Try to estimate iter count for 1ms falling back to 1m
1226         // iterations if first run took < 1ns.
1227         if self.ns_per_iter() == 0 {
1228             n = 1_000_000;
1229         } else {
1230             n = 1_000_000 / cmp::max(self.ns_per_iter(), 1);
1231         }
1232         // if the first run took more than 1ms we don't want to just
1233         // be left doing 0 iterations on every loop. The unfortunate
1234         // side effect of not being able to do as many runs is
1235         // automatically handled by the statistical analysis below
1236         // (i.e. larger error bars).
1237         if n == 0 { n = 1; }
1238
1239         let mut total_run = 0;
1240         let samples : &mut [f64] = [0.0_f64, ..50];
1241         loop {
1242             let loop_start = precise_time_ns();
1243
1244             for p in samples.mut_iter() {
1245                 self.bench_n(n, |x| f(x));
1246                 *p = self.ns_per_iter() as f64;
1247             };
1248
1249             stats::winsorize(samples, 5.0);
1250             let summ = stats::Summary::new(samples);
1251
1252             for p in samples.mut_iter() {
1253                 self.bench_n(5 * n, |x| f(x));
1254                 *p = self.ns_per_iter() as f64;
1255             };
1256
1257             stats::winsorize(samples, 5.0);
1258             let summ5 = stats::Summary::new(samples);
1259
1260             let now = precise_time_ns();
1261             let loop_run = now - loop_start;
1262
1263             // If we've run for 100ms and seem to have converged to a
1264             // stable median.
1265             if loop_run > 100_000_000 &&
1266                 summ.median_abs_dev_pct < 1.0 &&
1267                 summ.median - summ5.median < summ5.median_abs_dev {
1268                 return summ5;
1269             }
1270
1271             total_run += loop_run;
1272             // Longest we ever run for is 3s.
1273             if total_run > 3_000_000_000 {
1274                 return summ5;
1275             }
1276
1277             n *= 2;
1278         }
1279     }
1280 }
1281
1282 pub mod bench {
1283     use std::cmp;
1284     use super::{BenchHarness, BenchSamples};
1285
1286     pub fn benchmark(f: |&mut BenchHarness|) -> BenchSamples {
1287         let mut bs = BenchHarness {
1288             iterations: 0,
1289             ns_start: 0,
1290             ns_end: 0,
1291             bytes: 0
1292         };
1293
1294         let ns_iter_summ = bs.auto_bench(f);
1295
1296         let ns_iter = cmp::max(ns_iter_summ.median as u64, 1);
1297         let iter_s = 1_000_000_000 / ns_iter;
1298         let mb_s = (bs.bytes * iter_s) / 1_000_000;
1299
1300         BenchSamples {
1301             ns_iter_summ: ns_iter_summ,
1302             mb_s: mb_s as uint
1303         }
1304     }
1305 }
1306
1307 #[cfg(test)]
1308 mod tests {
1309     use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
1310                TestDesc, TestDescAndFn, TestOpts, run_test,
1311                Metric, MetricMap, MetricAdded, MetricRemoved,
1312                Improvement, Regression, LikelyNoise,
1313                StaticTestName, DynTestName, DynTestFn};
1314     use std::io::TempDir;
1315
1316     #[test]
1317     pub fn do_not_run_ignored_tests() {
1318         fn f() { fail!(); }
1319         let desc = TestDescAndFn {
1320             desc: TestDesc {
1321                 name: StaticTestName("whatever"),
1322                 ignore: true,
1323                 should_fail: false
1324             },
1325             testfn: DynTestFn(proc() f()),
1326         };
1327         let (tx, rx) = channel();
1328         run_test(false, desc, tx);
1329         let (_, res, _) = rx.recv();
1330         assert!(res != TrOk);
1331     }
1332
1333     #[test]
1334     pub fn ignored_tests_result_in_ignored() {
1335         fn f() { }
1336         let desc = TestDescAndFn {
1337             desc: TestDesc {
1338                 name: StaticTestName("whatever"),
1339                 ignore: true,
1340                 should_fail: false
1341             },
1342             testfn: DynTestFn(proc() f()),
1343         };
1344         let (tx, rx) = channel();
1345         run_test(false, desc, tx);
1346         let (_, res, _) = rx.recv();
1347         assert!(res == TrIgnored);
1348     }
1349
1350     #[test]
1351     fn test_should_fail() {
1352         fn f() { fail!(); }
1353         let desc = TestDescAndFn {
1354             desc: TestDesc {
1355                 name: StaticTestName("whatever"),
1356                 ignore: false,
1357                 should_fail: true
1358             },
1359             testfn: DynTestFn(proc() f()),
1360         };
1361         let (tx, rx) = channel();
1362         run_test(false, desc, tx);
1363         let (_, res, _) = rx.recv();
1364         assert!(res == TrOk);
1365     }
1366
1367     #[test]
1368     fn test_should_fail_but_succeeds() {
1369         fn f() { }
1370         let desc = TestDescAndFn {
1371             desc: TestDesc {
1372                 name: StaticTestName("whatever"),
1373                 ignore: false,
1374                 should_fail: true
1375             },
1376             testfn: DynTestFn(proc() f()),
1377         };
1378         let (tx, rx) = channel();
1379         run_test(false, desc, tx);
1380         let (_, res, _) = rx.recv();
1381         assert!(res == TrFailed);
1382     }
1383
1384     #[test]
1385     fn first_free_arg_should_be_a_filter() {
1386         let args = vec!(~"progname", ~"filter");
1387         let opts = match parse_opts(args.as_slice()) {
1388             Some(Ok(o)) => o,
1389             _ => fail!("Malformed arg in first_free_arg_should_be_a_filter")
1390         };
1391         assert!("filter" == opts.filter.clone().unwrap());
1392     }
1393
1394     #[test]
1395     fn parse_ignored_flag() {
1396         let args = vec!(~"progname", ~"filter", ~"--ignored");
1397         let opts = match parse_opts(args.as_slice()) {
1398             Some(Ok(o)) => o,
1399             _ => fail!("Malformed arg in parse_ignored_flag")
1400         };
1401         assert!((opts.run_ignored));
1402     }
1403
1404     #[test]
1405     pub fn filter_for_ignored_option() {
1406         // When we run ignored tests the test filter should filter out all the
1407         // unignored tests and flip the ignore flag on the rest to false
1408
1409         let opts = TestOpts {
1410             filter: None,
1411             run_ignored: true,
1412             logfile: None,
1413             run_tests: true,
1414             run_benchmarks: false,
1415             ratchet_noise_percent: None,
1416             ratchet_metrics: None,
1417             save_metrics: None,
1418             test_shard: None
1419         };
1420
1421         let tests = vec!(
1422             TestDescAndFn {
1423                 desc: TestDesc {
1424                     name: StaticTestName("1"),
1425                     ignore: true,
1426                     should_fail: false,
1427                 },
1428                 testfn: DynTestFn(proc() {}),
1429             },
1430             TestDescAndFn {
1431                 desc: TestDesc {
1432                     name: StaticTestName("2"),
1433                     ignore: false,
1434                     should_fail: false
1435                 },
1436                 testfn: DynTestFn(proc() {}),
1437             });
1438         let filtered = filter_tests(&opts, tests);
1439
1440         assert_eq!(filtered.len(), 1);
1441         assert_eq!(filtered.get(0).desc.name.to_str(), ~"1");
1442         assert!(filtered.get(0).desc.ignore == false);
1443     }
1444
1445     #[test]
1446     pub fn sort_tests() {
1447         let opts = TestOpts {
1448             filter: None,
1449             run_ignored: false,
1450             logfile: None,
1451             run_tests: true,
1452             run_benchmarks: false,
1453             ratchet_noise_percent: None,
1454             ratchet_metrics: None,
1455             save_metrics: None,
1456             test_shard: None
1457         };
1458
1459         let names =
1460             vec!(~"sha1::test", ~"int::test_to_str", ~"int::test_pow",
1461              ~"test::do_not_run_ignored_tests",
1462              ~"test::ignored_tests_result_in_ignored",
1463              ~"test::first_free_arg_should_be_a_filter",
1464              ~"test::parse_ignored_flag", ~"test::filter_for_ignored_option",
1465              ~"test::sort_tests");
1466         let tests =
1467         {
1468             fn testfn() { }
1469             let mut tests = Vec::new();
1470             for name in names.iter() {
1471                 let test = TestDescAndFn {
1472                     desc: TestDesc {
1473                         name: DynTestName((*name).clone()),
1474                         ignore: false,
1475                         should_fail: false
1476                     },
1477                     testfn: DynTestFn(testfn),
1478                 };
1479                 tests.push(test);
1480             }
1481             tests
1482         };
1483         let filtered = filter_tests(&opts, tests);
1484
1485         let expected =
1486             vec!(~"int::test_pow", ~"int::test_to_str", ~"sha1::test",
1487               ~"test::do_not_run_ignored_tests",
1488               ~"test::filter_for_ignored_option",
1489               ~"test::first_free_arg_should_be_a_filter",
1490               ~"test::ignored_tests_result_in_ignored",
1491               ~"test::parse_ignored_flag",
1492               ~"test::sort_tests");
1493
1494         for (a, b) in expected.iter().zip(filtered.iter()) {
1495             assert!(*a == b.desc.name.to_str());
1496         }
1497     }
1498
1499     #[test]
1500     pub fn test_metricmap_compare() {
1501         let mut m1 = MetricMap::new();
1502         let mut m2 = MetricMap::new();
1503         m1.insert_metric("in-both-noise", 1000.0, 200.0);
1504         m2.insert_metric("in-both-noise", 1100.0, 200.0);
1505
1506         m1.insert_metric("in-first-noise", 1000.0, 2.0);
1507         m2.insert_metric("in-second-noise", 1000.0, 2.0);
1508
1509         m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
1510         m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
1511
1512         m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
1513         m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
1514
1515         m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
1516         m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
1517
1518         m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
1519         m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
1520
1521         let diff1 = m2.compare_to_old(&m1, None);
1522
1523         assert_eq!(*(diff1.find(&~"in-both-noise").unwrap()), LikelyNoise);
1524         assert_eq!(*(diff1.find(&~"in-first-noise").unwrap()), MetricRemoved);
1525         assert_eq!(*(diff1.find(&~"in-second-noise").unwrap()), MetricAdded);
1526         assert_eq!(*(diff1.find(&~"in-both-want-downwards-but-regressed").unwrap()),
1527                    Regression(100.0));
1528         assert_eq!(*(diff1.find(&~"in-both-want-downwards-and-improved").unwrap()),
1529                    Improvement(50.0));
1530         assert_eq!(*(diff1.find(&~"in-both-want-upwards-but-regressed").unwrap()),
1531                    Regression(50.0));
1532         assert_eq!(*(diff1.find(&~"in-both-want-upwards-and-improved").unwrap()),
1533                    Improvement(100.0));
1534         assert_eq!(diff1.len(), 7);
1535
1536         let diff2 = m2.compare_to_old(&m1, Some(200.0));
1537
1538         assert_eq!(*(diff2.find(&~"in-both-noise").unwrap()), LikelyNoise);
1539         assert_eq!(*(diff2.find(&~"in-first-noise").unwrap()), MetricRemoved);
1540         assert_eq!(*(diff2.find(&~"in-second-noise").unwrap()), MetricAdded);
1541         assert_eq!(*(diff2.find(&~"in-both-want-downwards-but-regressed").unwrap()), LikelyNoise);
1542         assert_eq!(*(diff2.find(&~"in-both-want-downwards-and-improved").unwrap()), LikelyNoise);
1543         assert_eq!(*(diff2.find(&~"in-both-want-upwards-but-regressed").unwrap()), LikelyNoise);
1544         assert_eq!(*(diff2.find(&~"in-both-want-upwards-and-improved").unwrap()), LikelyNoise);
1545         assert_eq!(diff2.len(), 7);
1546     }
1547
1548     #[test]
1549     pub fn ratchet_test() {
1550
1551         let dpth = TempDir::new("test-ratchet").expect("missing test for ratchet");
1552         let pth = dpth.path().join("ratchet.json");
1553
1554         let mut m1 = MetricMap::new();
1555         m1.insert_metric("runtime", 1000.0, 2.0);
1556         m1.insert_metric("throughput", 50.0, 2.0);
1557
1558         let mut m2 = MetricMap::new();
1559         m2.insert_metric("runtime", 1100.0, 2.0);
1560         m2.insert_metric("throughput", 50.0, 2.0);
1561
1562         m1.save(&pth).unwrap();
1563
1564         // Ask for a ratchet that should fail to advance.
1565         let (diff1, ok1) = m2.ratchet(&pth, None);
1566         assert_eq!(ok1, false);
1567         assert_eq!(diff1.len(), 2);
1568         assert_eq!(*(diff1.find(&~"runtime").unwrap()), Regression(10.0));
1569         assert_eq!(*(diff1.find(&~"throughput").unwrap()), LikelyNoise);
1570
1571         // Check that it was not rewritten.
1572         let m3 = MetricMap::load(&pth);
1573         let MetricMap(m3) = m3;
1574         assert_eq!(m3.len(), 2);
1575         assert_eq!(*(m3.find(&~"runtime").unwrap()), Metric::new(1000.0, 2.0));
1576         assert_eq!(*(m3.find(&~"throughput").unwrap()), Metric::new(50.0, 2.0));
1577
1578         // Ask for a ratchet with an explicit noise-percentage override,
1579         // that should advance.
1580         let (diff2, ok2) = m2.ratchet(&pth, Some(10.0));
1581         assert_eq!(ok2, true);
1582         assert_eq!(diff2.len(), 2);
1583         assert_eq!(*(diff2.find(&~"runtime").unwrap()), LikelyNoise);
1584         assert_eq!(*(diff2.find(&~"throughput").unwrap()), LikelyNoise);
1585
1586         // Check that it was rewritten.
1587         let m4 = MetricMap::load(&pth);
1588         let MetricMap(m4) = m4;
1589         assert_eq!(m4.len(), 2);
1590         assert_eq!(*(m4.find(&~"runtime").unwrap()), Metric::new(1100.0, 2.0));
1591         assert_eq!(*(m4.find(&~"throughput").unwrap()), Metric::new(50.0, 2.0));
1592     }
1593 }