2014-02-14 09:49:11 +08:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-28 23:25:44 +11:00
|
|
|
//! Support code for rustc's built in unit-test and micro-benchmarking
|
|
|
|
//! framework.
|
|
|
|
//!
|
2014-04-01 09:16:35 +08:00
|
|
|
//! Almost all user code will only be interested in `Bencher` and
|
2014-02-28 23:25:44 +11:00
|
|
|
//! `black_box`. All other interactions (such as writing tests and
|
|
|
|
//! benchmarks themselves) should be done via the `#[test]` and
|
|
|
|
//! `#[bench]` attributes.
|
|
|
|
//!
|
2015-02-02 03:45:52 +05:30
|
|
|
//! See the [Testing Chapter](../book/testing.html) of the book for more details.
|
2014-02-28 23:25:44 +11:00
|
|
|
|
|
|
|
// Currently, not much of this is meant for users. It is intended to
|
|
|
|
// support the simplest interface possible for representing and
|
|
|
|
// running tests while providing a base that other test frameworks may
|
|
|
|
// build off of.
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2015-03-05 11:53:51 -05:00
|
|
|
// Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
|
|
|
|
#![cfg_attr(stage0, feature(custom_attribute))]
|
2014-07-09 10:57:01 -07:00
|
|
|
#![crate_name = "test"]
|
2015-08-13 10:21:36 -07:00
|
|
|
#![unstable(feature = "test", issue = "27812")]
|
Preliminary feature staging
This partially implements the feature staging described in the
[release channel RFC][rc]. It does not yet fully conform to the RFC as
written, but does accomplish its goals sufficiently for the 1.0 alpha
release.
It has three primary user-visible effects:
* On the nightly channel, use of unstable APIs generates a warning.
* On the beta channel, use of unstable APIs generates a warning.
* On the beta channel, use of feature gates generates a warning.
Code that does not trigger these warnings is considered 'stable',
modulo pre-1.0 bugs.
Disabling the warnings for unstable APIs continues to be done in the
existing (i.e. old) style, via `#[allow(...)]`, not that specified in
the RFC. I deem this marginally acceptable since any code that must do
this is not using the stable dialect of Rust.
Use of feature gates is itself gated with the new 'unstable_features'
lint, on nightly set to 'allow', and on beta 'warn'.
The attribute scheme used here corresponds to an older version of the
RFC, with the `#[staged_api]` crate attribute toggling the staging
behavior of the stability attributes, but the user impact is only
in-tree so I'm not concerned about having to make design changes later
(and I may ultimately prefer the scheme here after all, with the
`#[staged_api]` crate attribute).
Since the Rust codebase itself makes use of unstable features the
compiler and build system to a midly elaborate dance to allow it to
bootstrap while disobeying these lints (which would otherwise be
errors because Rust builds with `-D warnings`).
This patch includes one significant hack that causes a
regression. Because the `format_args!` macro emits calls to unstable
APIs it would trigger the lint. I added a hack to the lint to make it
not trigger, but this in turn causes arguments to `println!` not to be
checked for feature gates. I don't presently understand macro
expansion well enough to fix. This is bug #20661.
Closes #16678
[rc]: https://github.com/rust-lang/rfcs/blob/master/text/0507-release-channels.md
2015-01-06 06:26:08 -08:00
|
|
|
#![staged_api]
|
2014-03-21 18:05:05 -07:00
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![crate_type = "dylib"]
|
2015-08-09 14:15:05 -07:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 16:04:01 -07:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-08-09 14:15:05 -07:00
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2015-01-30 12:26:44 -08:00
|
|
|
|
2015-02-15 19:07:14 +05:30
|
|
|
#![feature(asm)]
|
2015-01-07 15:15:34 +01:00
|
|
|
#![feature(box_syntax)]
|
2015-04-28 11:40:04 -07:00
|
|
|
#![feature(duration_span)]
|
2015-06-09 11:52:41 -07:00
|
|
|
#![feature(fnbox)]
|
|
|
|
#![feature(iter_cmp)]
|
|
|
|
#![feature(libc)]
|
2015-01-22 18:22:03 -08:00
|
|
|
#![feature(rustc_private)]
|
2015-06-09 11:52:41 -07:00
|
|
|
#![feature(set_stdio)]
|
2015-01-30 12:26:44 -08:00
|
|
|
#![feature(staged_api)]
|
2014-02-14 09:49:11 +08:00
|
|
|
|
2014-02-14 10:10:06 -08:00
|
|
|
extern crate getopts;
|
2014-02-14 09:49:11 +08:00
|
|
|
extern crate serialize;
|
2015-03-24 18:13:54 -07:00
|
|
|
extern crate serialize as rustc_serialize;
|
2014-02-14 10:10:06 -08:00
|
|
|
extern crate term;
|
2015-03-11 15:24:14 -07:00
|
|
|
extern crate libc;
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::TestFn::*;
|
|
|
|
pub use self::ColorConfig::*;
|
|
|
|
pub use self::TestResult::*;
|
|
|
|
pub use self::TestName::*;
|
|
|
|
use self::TestEvent::*;
|
|
|
|
use self::NamePadding::*;
|
|
|
|
use self::OutputLocation::*;
|
|
|
|
|
2014-03-14 11:16:10 -07:00
|
|
|
use stats::Stats;
|
2014-02-14 09:49:11 +08:00
|
|
|
use getopts::{OptGroup, optflag, optopt};
|
2015-01-21 02:31:15 -08:00
|
|
|
use serialize::Encodable;
|
2015-04-01 11:12:30 -04:00
|
|
|
use std::boxed::FnBox;
|
2014-02-14 09:49:11 +08:00
|
|
|
use term::Terminal;
|
|
|
|
use term::color::{Color, RED, YELLOW, GREEN, CYAN};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-01-01 01:13:08 -05:00
|
|
|
use std::any::Any;
|
2014-02-06 02:34:33 -05:00
|
|
|
use std::cmp;
|
2014-12-22 09:04:23 -08:00
|
|
|
use std::collections::BTreeMap;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::env;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::fmt;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::fs::File;
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io;
|
2014-12-10 19:46:38 -08:00
|
|
|
use std::iter::repeat;
|
2015-03-28 02:23:20 -07:00
|
|
|
use std::path::PathBuf;
|
2014-12-23 11:53:35 -08:00
|
|
|
use std::sync::mpsc::{channel, Sender};
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::sync::{Arc, Mutex};
|
2015-02-17 15:24:34 -08:00
|
|
|
use std::thread;
|
2014-12-22 09:04:23 -08:00
|
|
|
use std::time::Duration;
|
2011-07-16 17:04:20 -07:00
|
|
|
|
2014-02-14 09:49:11 +08:00
|
|
|
// to be used by rustc to compile tests in libtest
|
|
|
|
pub mod test {
|
2014-04-01 09:16:35 +08:00
|
|
|
pub use {Bencher, TestName, TestResult, TestDesc,
|
2014-02-14 09:49:11 +08:00
|
|
|
TestDescAndFn, TestOpts, TrFailed, TrIgnored, TrOk,
|
2015-01-19 02:51:34 -08:00
|
|
|
Metric, MetricMap,
|
2014-02-14 09:49:11 +08:00
|
|
|
StaticTestFn, StaticTestName, DynTestName, DynTestFn,
|
|
|
|
run_test, test_main, test_main_static, filter_tests,
|
2015-01-31 15:08:25 -08:00
|
|
|
parse_opts, StaticBenchFn, ShouldPanic};
|
2014-02-14 09:49:11 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 11:16:10 -07:00
|
|
|
pub mod stats;
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// The name of a test. By convention this follows the rules for rust
|
2013-05-09 02:34:47 +09:00
|
|
|
// paths; i.e. it should be a series of identifiers separated by double
|
2011-07-09 16:08:03 -07:00
|
|
|
// colons. This way if some test runner wants to arrange the tests
|
2011-09-26 10:51:23 -07:00
|
|
|
// hierarchically it may.
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
2013-02-13 11:46:14 -08:00
|
|
|
pub enum TestName {
|
2013-03-14 11:22:51 -07:00
|
|
|
StaticTestName(&'static str),
|
2014-05-22 16:57:53 -07:00
|
|
|
DynTestName(String)
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2014-05-05 19:15:17 +10:00
|
|
|
impl TestName {
|
2015-09-08 00:36:29 +02:00
|
|
|
fn as_slice(&self) -> &str {
|
2014-02-19 18:56:33 -08:00
|
|
|
match *self {
|
2014-05-05 19:15:17 +10:00
|
|
|
StaticTestName(s) => s,
|
2015-02-01 21:53:25 -05:00
|
|
|
DynTestName(ref s) => s
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Display for TestName {
|
2014-05-05 19:15:17 +10:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-01-20 15:45:07 -08:00
|
|
|
fmt::Display::fmt(self.as_slice(), f)
|
2014-05-05 19:15:17 +10:00
|
|
|
}
|
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy)]
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
enum NamePadding {
|
|
|
|
PadNone,
|
|
|
|
PadOnRight,
|
|
|
|
}
|
|
|
|
|
2013-10-12 09:49:50 -04:00
|
|
|
impl TestDesc {
|
2015-03-25 17:06:52 -07:00
|
|
|
fn padded_name(&self, column_count: usize, align: NamePadding) -> String {
|
2015-06-08 16:55:35 +02:00
|
|
|
let mut name = String::from(self.name.as_slice());
|
2013-10-12 09:49:50 -04:00
|
|
|
let fill = column_count.saturating_sub(name.len());
|
2015-03-31 11:07:46 -07:00
|
|
|
let pad = repeat(" ").take(fill).collect::<String>();
|
2013-10-12 09:49:50 -04:00
|
|
|
match align {
|
2014-05-13 16:44:05 -07:00
|
|
|
PadNone => name,
|
2014-04-02 16:54:22 -07:00
|
|
|
PadOnRight => {
|
2015-02-01 21:53:25 -05:00
|
|
|
name.push_str(&pad);
|
2014-05-13 16:44:05 -07:00
|
|
|
name
|
2014-04-02 16:54:22 -07:00
|
|
|
}
|
2013-10-12 09:49:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 15:39:02 -08:00
|
|
|
/// Represents a benchmark function.
|
2015-05-02 13:38:51 +10:00
|
|
|
pub trait TDynBenchFn: Send {
|
2014-04-01 09:16:35 +08:00
|
|
|
fn run(&self, harness: &mut Bencher);
|
2013-11-18 15:39:02 -08:00
|
|
|
}
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// A function that runs a test. If the function returns successfully,
|
2014-10-09 15:17:22 -04:00
|
|
|
// the test succeeds; if the function panics then the test fails. We
|
2011-07-09 16:08:03 -07:00
|
|
|
// may need to come up with a more clever definition of test in order
|
2015-05-09 00:12:29 +09:00
|
|
|
// to support isolation of tests into threads.
|
2013-02-13 11:46:14 -08:00
|
|
|
pub enum TestFn {
|
2014-02-14 17:23:01 +13:00
|
|
|
StaticTestFn(fn()),
|
2014-04-01 09:16:35 +08:00
|
|
|
StaticBenchFn(fn(&mut Bencher)),
|
2014-11-26 08:12:18 -05:00
|
|
|
StaticMetricFn(fn(&mut MetricMap)),
|
2015-06-10 19:33:04 -07:00
|
|
|
DynTestFn(Box<FnBox() + Send>),
|
2015-04-01 11:12:30 -04:00
|
|
|
DynMetricFn(Box<FnBox(&mut MetricMap)+Send>),
|
2014-08-27 21:46:52 -04:00
|
|
|
DynBenchFn(Box<TDynBenchFn+'static>)
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
2013-10-12 09:49:50 -04:00
|
|
|
impl TestFn {
|
|
|
|
fn padding(&self) -> NamePadding {
|
2015-09-08 00:36:29 +02:00
|
|
|
match *self {
|
|
|
|
StaticTestFn(..) => PadNone,
|
|
|
|
StaticBenchFn(..) => PadOnRight,
|
|
|
|
StaticMetricFn(..) => PadOnRight,
|
|
|
|
DynTestFn(..) => PadNone,
|
|
|
|
DynMetricFn(..) => PadOnRight,
|
|
|
|
DynBenchFn(..) => PadOnRight,
|
2013-10-12 09:49:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:45:07 -08:00
|
|
|
impl fmt::Debug for TestFn {
|
2014-05-18 19:35:45 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-12-12 10:59:41 -08:00
|
|
|
f.write_str(match *self {
|
2014-05-18 19:35:45 -07:00
|
|
|
StaticTestFn(..) => "StaticTestFn(..)",
|
|
|
|
StaticBenchFn(..) => "StaticBenchFn(..)",
|
|
|
|
StaticMetricFn(..) => "StaticMetricFn(..)",
|
|
|
|
DynTestFn(..) => "DynTestFn(..)",
|
|
|
|
DynMetricFn(..) => "DynMetricFn(..)",
|
|
|
|
DynBenchFn(..) => "DynBenchFn(..)"
|
2014-12-12 10:59:41 -08:00
|
|
|
})
|
2014-05-18 19:35:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 23:25:44 +11:00
|
|
|
/// Manager of the benchmarking runs.
|
|
|
|
///
|
2015-01-19 03:14:36 +02:00
|
|
|
/// This is fed into functions marked with `#[bench]` to allow for
|
2014-02-28 23:25:44 +11:00
|
|
|
/// set-up & tear-down before running a piece of code repeatedly via a
|
|
|
|
/// call to `iter`.
|
2015-03-30 09:40:52 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2014-04-01 09:16:35 +08:00
|
|
|
pub struct Bencher {
|
2014-03-27 15:15:28 -07:00
|
|
|
iterations: u64,
|
2014-11-10 12:27:56 -08:00
|
|
|
dur: Duration,
|
2014-03-27 15:15:28 -07:00
|
|
|
pub bytes: u64,
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2015-01-31 15:08:25 -08:00
|
|
|
pub enum ShouldPanic {
|
2014-12-04 23:02:36 -08:00
|
|
|
No,
|
2015-07-30 08:53:22 -07:00
|
|
|
Yes,
|
|
|
|
YesWithMessage(&'static str)
|
2014-12-04 23:02:36 -08:00
|
|
|
}
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// The definition of a single test. A test runner will run a list of
|
|
|
|
// these.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
2013-01-08 14:00:45 -08:00
|
|
|
pub struct TestDesc {
|
2014-03-27 15:15:28 -07:00
|
|
|
pub name: TestName,
|
|
|
|
pub ignore: bool,
|
2015-01-31 15:08:25 -08:00
|
|
|
pub should_panic: ShouldPanic,
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2015-01-15 01:40:09 +01:00
|
|
|
unsafe impl Send for TestDesc {}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Debug)]
|
2013-01-31 17:12:29 -08:00
|
|
|
pub struct TestDescAndFn {
|
2014-03-27 15:15:28 -07:00
|
|
|
pub desc: TestDesc,
|
|
|
|
pub testfn: TestFn,
|
2013-01-31 17:12:29 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug, Copy)]
|
2013-07-10 16:17:41 -07:00
|
|
|
pub struct Metric {
|
2014-03-27 15:15:28 -07:00
|
|
|
value: f64,
|
|
|
|
noise: f64
|
2013-07-10 16:17:41 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 09:49:11 +08:00
|
|
|
impl Metric {
|
|
|
|
pub fn new(value: f64, noise: f64) -> Metric {
|
|
|
|
Metric {value: value, noise: noise}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq)]
|
2014-12-17 10:16:10 -05:00
|
|
|
pub struct MetricMap(BTreeMap<String,Metric>);
|
2013-07-10 16:17:41 -07:00
|
|
|
|
2013-07-17 15:15:34 -07:00
|
|
|
impl Clone for MetricMap {
|
2013-08-09 01:25:24 -07:00
|
|
|
fn clone(&self) -> MetricMap {
|
2013-11-01 18:06:31 -07:00
|
|
|
let MetricMap(ref map) = *self;
|
|
|
|
MetricMap(map.clone())
|
2013-07-17 15:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-09 16:08:03 -07:00
|
|
|
// The default console test runner. It accepts the command line
|
2013-02-13 11:46:14 -08:00
|
|
|
// arguments and a vector of test_descs.
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn test_main(args: &[String], tests: Vec<TestDescAndFn> ) {
|
2011-07-27 14:19:39 +02:00
|
|
|
let opts =
|
2012-08-06 12:34:08 -07:00
|
|
|
match parse_opts(args) {
|
2013-09-24 16:34:23 -07:00
|
|
|
Some(Ok(o)) => o,
|
2014-12-20 00:09:35 -08:00
|
|
|
Some(Err(msg)) => panic!("{:?}", msg),
|
2013-09-24 16:34:23 -07:00
|
|
|
None => return
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
2014-01-29 17:39:12 -08:00
|
|
|
match run_tests_console(&opts, tests) {
|
|
|
|
Ok(true) => {}
|
2015-08-17 12:58:19 -07:00
|
|
|
Ok(false) => std::process::exit(101),
|
2014-12-20 00:09:35 -08:00
|
|
|
Err(e) => panic!("io error when running tests: {:?}", e),
|
2014-01-29 17:39:12 -08:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 11:46:14 -08:00
|
|
|
// A variant optimized for invocation with a static test vector.
|
2014-10-09 15:17:22 -04:00
|
|
|
// This will panic (intentionally) when fed any dynamic tests, because
|
2013-02-13 11:46:14 -08:00
|
|
|
// it is copying the static values out into a dynamic vector and cannot
|
|
|
|
// copy dynamic values. It is doing this because from this point on
|
2015-05-02 16:25:49 -04:00
|
|
|
// a Vec<TestDescAndFn> is used in order to effect ownership-transfer
|
|
|
|
// semantics into parallel test runners, which in turn requires a Vec<>
|
2013-02-13 11:46:14 -08:00
|
|
|
// rather than a &[].
|
2015-07-30 08:53:22 -07:00
|
|
|
pub fn test_main_static(tests: &[TestDescAndFn]) {
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
2014-03-05 15:28:08 -08:00
|
|
|
let owned_tests = tests.iter().map(|t| {
|
2013-02-13 11:46:14 -08:00
|
|
|
match t.testfn {
|
2014-05-18 19:35:45 -07:00
|
|
|
StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: t.desc.clone() },
|
|
|
|
StaticBenchFn(f) => TestDescAndFn { testfn: StaticBenchFn(f), desc: t.desc.clone() },
|
2014-10-09 15:17:22 -04:00
|
|
|
_ => panic!("non-static tests passed to test::test_main_static")
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2014-03-05 15:28:08 -08:00
|
|
|
}).collect();
|
2015-02-09 16:33:19 -08:00
|
|
|
test_main(&args, owned_tests)
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:40:52 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2014-06-08 17:10:27 -07:00
|
|
|
pub enum ColorConfig {
|
|
|
|
AutoColor,
|
|
|
|
AlwaysColor,
|
|
|
|
NeverColor,
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
pub struct TestOpts {
|
2015-01-20 10:45:29 -08:00
|
|
|
pub filter: Option<String>,
|
2014-03-27 15:15:28 -07:00
|
|
|
pub run_ignored: bool,
|
|
|
|
pub run_tests: bool,
|
2015-05-02 13:38:51 +10:00
|
|
|
pub bench_benchmarks: bool,
|
2015-02-26 21:00:43 -08:00
|
|
|
pub logfile: Option<PathBuf>,
|
2014-04-23 09:38:46 -07:00
|
|
|
pub nocapture: bool,
|
2014-06-08 17:10:27 -07:00
|
|
|
pub color: ColorConfig,
|
2014-04-23 09:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestOpts {
|
|
|
|
#[cfg(test)]
|
|
|
|
fn new() -> TestOpts {
|
|
|
|
TestOpts {
|
|
|
|
filter: None,
|
|
|
|
run_ignored: false,
|
|
|
|
run_tests: false,
|
2015-05-02 13:38:51 +10:00
|
|
|
bench_benchmarks: false,
|
2014-04-23 09:38:46 -07:00
|
|
|
logfile: None,
|
|
|
|
nocapture: false,
|
2014-06-08 17:10:27 -07:00
|
|
|
color: AutoColor,
|
2014-04-23 09:38:46 -07:00
|
|
|
}
|
|
|
|
}
|
2013-01-22 08:44:24 -08:00
|
|
|
}
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2014-01-26 13:25:02 +11:00
|
|
|
/// Result of parsing the options.
|
2014-05-22 16:57:53 -07:00
|
|
|
pub type OptRes = Result<TestOpts, String>;
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
fn optgroups() -> Vec<getopts::OptGroup> {
|
|
|
|
vec!(getopts::optflag("", "ignored", "Run ignored tests"),
|
2014-02-03 19:14:40 -08:00
|
|
|
getopts::optflag("", "test", "Run tests and not benchmarks"),
|
|
|
|
getopts::optflag("", "bench", "Run benchmarks instead of tests"),
|
|
|
|
getopts::optflag("h", "help", "Display this message (longer with --help)"),
|
|
|
|
getopts::optopt("", "logfile", "Write logs to the specified file instead \
|
2013-08-23 15:30:23 -07:00
|
|
|
of stdout", "PATH"),
|
2014-04-23 09:38:46 -07:00
|
|
|
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
|
2014-06-08 17:10:27 -07:00
|
|
|
task, allow printing directly"),
|
|
|
|
getopts::optopt("", "color", "Configure coloring of output:
|
|
|
|
auto = colorize if stdout is a tty and tests are run on serially (default);
|
|
|
|
always = always colorize output;
|
2015-01-19 00:20:55 -08:00
|
|
|
never = never colorize output;", "auto|always|never"))
|
2013-07-16 20:08:01 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 22:44:07 +10:00
|
|
|
fn usage(binary: &str) {
|
2013-09-27 20:18:50 -07:00
|
|
|
let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
|
2014-12-04 23:02:36 -08:00
|
|
|
println!(r#"{usage}
|
2014-05-05 22:44:07 +10:00
|
|
|
|
|
|
|
The FILTER regex is tested against the name of all tests to run, and
|
2014-05-05 22:19:38 +10:00
|
|
|
only those tests that match are run.
|
2013-07-16 20:08:01 -07:00
|
|
|
|
|
|
|
By default, all tests are run in parallel. This can be altered with the
|
2015-03-29 17:22:21 -04:00
|
|
|
RUST_TEST_THREADS environment variable when running tests (set it to 1).
|
2013-07-16 20:08:01 -07:00
|
|
|
|
2014-04-23 09:38:46 -07:00
|
|
|
All tests have their standard output and standard error captured by default.
|
|
|
|
This can be overridden with the --nocapture flag or the RUST_TEST_NOCAPTURE=1
|
|
|
|
environment variable. Logging is not captured by default.
|
|
|
|
|
2013-07-16 20:08:01 -07:00
|
|
|
Test Attributes:
|
|
|
|
|
2014-06-14 11:03:34 -07:00
|
|
|
#[test] - Indicates a function is a test to be run. This function
|
2013-07-16 20:08:01 -07:00
|
|
|
takes no arguments.
|
2014-06-14 11:03:34 -07:00
|
|
|
#[bench] - Indicates a function is a benchmark to be run. This
|
2014-04-01 09:16:35 +08:00
|
|
|
function takes one argument (test::Bencher).
|
2015-01-31 15:08:25 -08:00
|
|
|
#[should_panic] - This function (also labeled with #[test]) will only pass if
|
|
|
|
the code causes a panic (an assertion failure or panic!)
|
2014-12-04 23:02:36 -08:00
|
|
|
A message may be provided, which the failure string must
|
2015-01-31 15:08:25 -08:00
|
|
|
contain: #[should_panic(expected = "foo")].
|
2014-06-14 11:03:34 -07:00
|
|
|
#[ignore] - When applied to a function which is already attributed as a
|
2013-07-16 20:08:01 -07:00
|
|
|
test, then the test runner will ignore these tests during
|
|
|
|
normal test runs. Running with --ignored will run these
|
2014-12-04 23:02:36 -08:00
|
|
|
tests."#,
|
2015-02-01 21:53:25 -05:00
|
|
|
usage = getopts::usage(&message, &optgroups()));
|
2013-07-16 20:08:01 -07:00
|
|
|
}
|
|
|
|
|
2011-07-14 16:05:33 -07:00
|
|
|
// Parses command line arguments into test options
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
2015-07-11 14:34:57 +03:00
|
|
|
let args_ = &args[1..];
|
2012-07-31 16:38:41 -07:00
|
|
|
let matches =
|
2015-02-01 21:53:25 -05:00
|
|
|
match getopts::getopts(args_, &optgroups()) {
|
2013-02-15 02:30:30 -05:00
|
|
|
Ok(m) => m,
|
2014-06-21 03:39:03 -07:00
|
|
|
Err(f) => return Some(Err(f.to_string()))
|
2011-07-27 14:19:39 +02:00
|
|
|
};
|
|
|
|
|
2015-02-01 21:53:25 -05:00
|
|
|
if matches.opt_present("h") { usage(&args[0]); return None; }
|
2013-07-16 20:08:01 -07:00
|
|
|
|
2015-03-24 16:54:09 -07:00
|
|
|
let filter = if !matches.free.is_empty() {
|
2015-01-20 10:45:29 -08:00
|
|
|
Some(matches.free[0].clone())
|
2014-05-05 22:19:38 +10:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2011-07-14 16:05:33 -07:00
|
|
|
|
2013-09-18 03:42:23 +02:00
|
|
|
let run_ignored = matches.opt_present("ignored");
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-09-18 03:42:23 +02:00
|
|
|
let logfile = matches.opt_str("logfile");
|
2015-03-18 09:14:54 -07:00
|
|
|
let logfile = logfile.map(|s| PathBuf::from(&s));
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2015-05-02 13:38:51 +10:00
|
|
|
let bench_benchmarks = matches.opt_present("bench");
|
|
|
|
let run_tests = ! bench_benchmarks ||
|
2013-09-18 03:42:23 +02:00
|
|
|
matches.opt_present("test");
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2014-04-23 09:38:46 -07:00
|
|
|
let mut nocapture = matches.opt_present("nocapture");
|
|
|
|
if !nocapture {
|
2015-02-11 11:47:53 -08:00
|
|
|
nocapture = env::var("RUST_TEST_NOCAPTURE").is_ok();
|
2014-04-23 09:38:46 -07:00
|
|
|
}
|
|
|
|
|
2015-02-01 21:53:25 -05:00
|
|
|
let color = match matches.opt_str("color").as_ref().map(|s| &**s) {
|
2014-06-08 17:10:27 -07:00
|
|
|
Some("auto") | None => AutoColor,
|
|
|
|
Some("always") => AlwaysColor,
|
|
|
|
Some("never") => NeverColor,
|
|
|
|
|
|
|
|
Some(v) => return Some(Err(format!("argument for --color must be \
|
|
|
|
auto, always, or never (was {})",
|
|
|
|
v))),
|
|
|
|
};
|
|
|
|
|
2013-01-22 08:44:24 -08:00
|
|
|
let test_opts = TestOpts {
|
|
|
|
filter: filter,
|
|
|
|
run_ignored: run_ignored,
|
2013-02-13 11:46:14 -08:00
|
|
|
run_tests: run_tests,
|
2015-05-02 13:38:51 +10:00
|
|
|
bench_benchmarks: bench_benchmarks,
|
2014-04-23 09:38:46 -07:00
|
|
|
logfile: logfile,
|
|
|
|
nocapture: nocapture,
|
2014-06-08 17:10:27 -07:00
|
|
|
color: color,
|
2013-01-22 08:44:24 -08:00
|
|
|
};
|
2011-07-11 16:33:21 -07:00
|
|
|
|
2013-09-24 16:34:23 -07:00
|
|
|
Some(Ok(test_opts))
|
2011-07-11 16:33:21 -07:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, PartialEq)]
|
2013-02-13 11:46:14 -08:00
|
|
|
pub struct BenchSamples {
|
2015-04-17 15:32:42 -07:00
|
|
|
ns_iter_summ: stats::Summary,
|
2015-03-25 17:06:52 -07:00
|
|
|
mb_s: usize,
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, PartialEq)]
|
2013-07-15 18:50:32 -07:00
|
|
|
pub enum TestResult {
|
|
|
|
TrOk,
|
|
|
|
TrFailed,
|
|
|
|
TrIgnored,
|
|
|
|
TrMetrics(MetricMap),
|
2013-07-02 12:47:32 -07:00
|
|
|
TrBench(BenchSamples),
|
2013-07-15 18:50:32 -07:00
|
|
|
}
|
2011-07-14 11:29:54 -07:00
|
|
|
|
2015-01-15 01:40:09 +01:00
|
|
|
unsafe impl Send for TestResult {}
|
|
|
|
|
2013-12-25 21:55:05 -08:00
|
|
|
enum OutputLocation<T> {
|
2014-06-25 18:18:13 -07:00
|
|
|
Pretty(Box<term::Terminal<term::WriterWrapper> + Send>),
|
2013-12-25 21:55:05 -08:00
|
|
|
Raw(T),
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:53:08 -05:00
|
|
|
struct ConsoleTestState<T> {
|
|
|
|
log_out: Option<File>,
|
2013-12-25 21:55:05 -08:00
|
|
|
out: OutputLocation<T>,
|
2013-02-04 16:48:52 -08:00
|
|
|
use_color: bool,
|
2015-03-25 17:06:52 -07:00
|
|
|
total: usize,
|
|
|
|
passed: usize,
|
|
|
|
failed: usize,
|
|
|
|
ignored: usize,
|
|
|
|
measured: usize,
|
2013-07-10 16:17:41 -07:00
|
|
|
metrics: MetricMap,
|
2014-03-05 15:28:08 -08:00
|
|
|
failures: Vec<(TestDesc, Vec<u8> )> ,
|
2015-03-25 17:06:52 -07:00
|
|
|
max_name_len: usize, // number of columns to fill when aligning names
|
2013-02-04 16:48:52 -08:00
|
|
|
}
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
impl<T: Write> ConsoleTestState<T> {
|
2014-01-29 17:39:12 -08:00
|
|
|
pub fn new(opts: &TestOpts,
|
2015-03-11 15:24:14 -07:00
|
|
|
_: Option<T>) -> io::Result<ConsoleTestState<io::Stdout>> {
|
2013-07-09 17:18:02 -07:00
|
|
|
let log_out = match opts.logfile {
|
2015-03-11 15:24:14 -07:00
|
|
|
Some(ref path) => Some(try!(File::create(path))),
|
2013-07-09 17:18:02 -07:00
|
|
|
None => None
|
|
|
|
};
|
2014-04-08 12:18:16 -04:00
|
|
|
let out = match term::stdout() {
|
2015-03-11 15:24:14 -07:00
|
|
|
None => Raw(io::stdout()),
|
2014-04-08 12:18:16 -04:00
|
|
|
Some(t) => Pretty(t)
|
2013-07-09 17:18:02 -07:00
|
|
|
};
|
2014-04-08 12:18:16 -04:00
|
|
|
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(ConsoleTestState {
|
2013-07-09 17:18:02 -07:00
|
|
|
out: out,
|
|
|
|
log_out: log_out,
|
2014-06-08 17:10:27 -07:00
|
|
|
use_color: use_color(opts),
|
2015-01-24 14:39:32 +00:00
|
|
|
total: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0,
|
|
|
|
ignored: 0,
|
|
|
|
measured: 0,
|
2013-07-10 16:17:41 -07:00
|
|
|
metrics: MetricMap::new(),
|
2014-03-05 15:28:08 -08:00
|
|
|
failures: Vec::new(),
|
2015-01-24 14:39:32 +00:00
|
|
|
max_name_len: 0,
|
2014-01-29 17:39:12 -08:00
|
|
|
})
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_ok(&mut self) -> io::Result<()> {
|
2014-01-29 17:39:12 -08:00
|
|
|
self.write_pretty("ok", term::color::GREEN)
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_failed(&mut self) -> io::Result<()> {
|
2014-01-29 17:39:12 -08:00
|
|
|
self.write_pretty("FAILED", term::color::RED)
|
2011-07-21 22:26:53 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_ignored(&mut self) -> io::Result<()> {
|
2014-01-29 17:39:12 -08:00
|
|
|
self.write_pretty("ignored", term::color::YELLOW)
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_metric(&mut self) -> io::Result<()> {
|
2014-01-29 17:39:12 -08:00
|
|
|
self.write_pretty("metric", term::color::CYAN)
|
2013-07-15 18:50:32 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_bench(&mut self) -> io::Result<()> {
|
2014-01-29 17:39:12 -08:00
|
|
|
self.write_pretty("bench", term::color::CYAN)
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2013-11-24 06:53:08 -05:00
|
|
|
pub fn write_pretty(&mut self,
|
2013-07-09 17:18:02 -07:00
|
|
|
word: &str,
|
2015-03-11 15:24:14 -07:00
|
|
|
color: term::color::Color) -> io::Result<()> {
|
2013-11-24 06:53:08 -05:00
|
|
|
match self.out {
|
2013-12-25 21:55:05 -08:00
|
|
|
Pretty(ref mut term) => {
|
2013-07-09 17:18:02 -07:00
|
|
|
if self.use_color {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(term.fg(color));
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2015-01-23 10:46:14 -08:00
|
|
|
try!(term.write_all(word.as_bytes()));
|
2013-07-09 17:18:02 -07:00
|
|
|
if self.use_color {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(term.reset());
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2015-02-26 15:31:24 +01:00
|
|
|
term.flush()
|
|
|
|
}
|
|
|
|
Raw(ref mut stdout) => {
|
|
|
|
try!(stdout.write_all(word.as_bytes()));
|
|
|
|
stdout.flush()
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2013-11-24 06:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_plain(&mut self, s: &str) -> io::Result<()> {
|
2013-11-24 06:53:08 -05:00
|
|
|
match self.out {
|
2015-02-26 15:31:24 +01:00
|
|
|
Pretty(ref mut term) => {
|
|
|
|
try!(term.write_all(s.as_bytes()));
|
|
|
|
term.flush()
|
|
|
|
},
|
|
|
|
Raw(ref mut stdout) => {
|
|
|
|
try!(stdout.write_all(s.as_bytes()));
|
|
|
|
stdout.flush()
|
|
|
|
},
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
pub fn write_run_start(&mut self, len: usize) -> io::Result<()> {
|
2013-07-09 17:18:02 -07:00
|
|
|
self.total = len;
|
2014-04-30 16:49:12 -07:00
|
|
|
let noun = if len != 1 { "tests" } else { "test" };
|
2015-02-01 21:53:25 -05:00
|
|
|
self.write_plain(&format!("\nrunning {} {}\n", len, noun))
|
2012-04-03 23:27:51 +08:00
|
|
|
}
|
|
|
|
|
2014-01-29 17:39:12 -08:00
|
|
|
pub fn write_test_start(&mut self, test: &TestDesc,
|
2015-03-11 15:24:14 -07:00
|
|
|
align: NamePadding) -> io::Result<()> {
|
2013-10-12 09:49:50 -04:00
|
|
|
let name = test.padded_name(self.max_name_len, align);
|
2015-02-01 21:53:25 -05:00
|
|
|
self.write_plain(&format!("test {} ... ", name))
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-07-11 11:19:32 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_result(&mut self, result: &TestResult) -> io::Result<()> {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(match *result {
|
2013-07-09 17:18:02 -07:00
|
|
|
TrOk => self.write_ok(),
|
|
|
|
TrFailed => self.write_failed(),
|
|
|
|
TrIgnored => self.write_ignored(),
|
2013-07-15 18:50:32 -07:00
|
|
|
TrMetrics(ref mm) => {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_metric());
|
2015-02-01 21:53:25 -05:00
|
|
|
self.write_plain(&format!(": {}", mm.fmt_metrics()))
|
2013-07-15 18:50:32 -07:00
|
|
|
}
|
2013-07-09 17:18:02 -07:00
|
|
|
TrBench(ref bs) => {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_bench());
|
2014-12-05 09:04:55 -08:00
|
|
|
|
2015-02-01 21:53:25 -05:00
|
|
|
try!(self.write_plain(&format!(": {}", fmt_bench_samples(bs))));
|
2014-12-05 09:04:55 -08:00
|
|
|
|
|
|
|
Ok(())
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2014-01-29 17:39:12 -08:00
|
|
|
});
|
|
|
|
self.write_plain("\n")
|
2011-07-11 11:19:32 -07:00
|
|
|
}
|
2011-07-14 11:29:54 -07:00
|
|
|
|
2014-01-29 17:39:12 -08:00
|
|
|
pub fn write_log(&mut self, test: &TestDesc,
|
2015-02-26 21:00:43 -08:00
|
|
|
result: &TestResult) -> io::Result<()> {
|
2013-07-09 17:18:02 -07:00
|
|
|
match self.log_out {
|
2014-01-29 17:39:12 -08:00
|
|
|
None => Ok(()),
|
2013-11-24 06:53:08 -05:00
|
|
|
Some(ref mut o) => {
|
2014-01-16 09:35:47 -06:00
|
|
|
let s = format!("{} {}\n", match *result {
|
2015-09-08 00:36:29 +02:00
|
|
|
TrOk => "ok".to_owned(),
|
|
|
|
TrFailed => "failed".to_owned(),
|
|
|
|
TrIgnored => "ignored".to_owned(),
|
2015-01-21 03:09:44 -08:00
|
|
|
TrMetrics(ref mm) => mm.fmt_metrics(),
|
2013-11-24 06:53:08 -05:00
|
|
|
TrBench(ref bs) => fmt_bench_samples(bs)
|
2015-02-01 21:53:25 -05:00
|
|
|
}, test.name);
|
2015-01-23 10:46:14 -08:00
|
|
|
o.write_all(s.as_bytes())
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-15 00:31:00 -07:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_failures(&mut self) -> io::Result<()> {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_plain("\nfailures:\n"));
|
2014-03-05 15:28:08 -08:00
|
|
|
let mut failures = Vec::new();
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut fail_out = String::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for &(ref f, ref stdout) in &self.failures {
|
2014-06-21 03:39:03 -07:00
|
|
|
failures.push(f.name.to_string());
|
2015-03-24 16:54:09 -07:00
|
|
|
if !stdout.is_empty() {
|
2015-02-01 21:53:25 -05:00
|
|
|
fail_out.push_str(&format!("---- {} stdout ----\n\t", f.name));
|
|
|
|
let output = String::from_utf8_lossy(stdout);
|
|
|
|
fail_out.push_str(&output);
|
2014-02-12 10:25:09 -08:00
|
|
|
fail_out.push_str("\n");
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 16:54:09 -07:00
|
|
|
if !fail_out.is_empty() {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_plain("\n"));
|
2015-02-01 21:53:25 -05:00
|
|
|
try!(self.write_plain(&fail_out));
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2014-02-12 10:25:09 -08:00
|
|
|
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_plain("\nfailures:\n"));
|
2014-11-27 19:00:21 -05:00
|
|
|
failures.sort();
|
2015-01-31 12:20:46 -05:00
|
|
|
for name in &failures {
|
2015-02-01 21:53:25 -05:00
|
|
|
try!(self.write_plain(&format!(" {}\n", name)));
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(())
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn write_run_finish(&mut self) -> io::Result<bool> {
|
2013-07-15 18:50:32 -07:00
|
|
|
assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
|
2013-07-10 16:17:41 -07:00
|
|
|
|
2015-01-24 14:39:32 +00:00
|
|
|
let success = self.failed == 0;
|
2015-01-21 01:45:24 -08:00
|
|
|
if !success {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_failures());
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
2013-07-09 17:18:02 -07:00
|
|
|
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_plain("\ntest result: "));
|
2013-07-09 17:18:02 -07:00
|
|
|
if success {
|
|
|
|
// There's no parallelism at this point so it's safe to use color
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_ok());
|
2013-07-09 17:18:02 -07:00
|
|
|
} else {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(self.write_failed());
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
2013-11-24 06:53:08 -05:00
|
|
|
let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
|
|
|
|
self.passed, self.failed, self.ignored, self.measured);
|
2015-02-01 21:53:25 -05:00
|
|
|
try!(self.write_plain(&s));
|
2014-01-29 17:39:12 -08:00
|
|
|
return Ok(success);
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
}
|
|
|
|
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
// Format a number with thousands separators
|
|
|
|
fn fmt_thousands_sep(mut n: usize, sep: char) -> String {
|
|
|
|
use std::fmt::Write;
|
|
|
|
let mut output = String::new();
|
2015-06-16 13:10:27 +02:00
|
|
|
let mut trailing = false;
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
for &pow in &[9, 6, 3, 0] {
|
|
|
|
let base = 10_usize.pow(pow);
|
2015-06-16 13:10:27 +02:00
|
|
|
if pow == 0 || trailing || n / base != 0 {
|
|
|
|
if !trailing {
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
output.write_fmt(format_args!("{}", n / base)).unwrap();
|
|
|
|
} else {
|
|
|
|
output.write_fmt(format_args!("{:03}", n / base)).unwrap();
|
|
|
|
}
|
|
|
|
if pow != 0 {
|
|
|
|
output.push(sep);
|
|
|
|
}
|
2015-06-16 13:10:27 +02:00
|
|
|
trailing = true;
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
}
|
|
|
|
n %= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn fmt_bench_samples(bs: &BenchSamples) -> String {
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
use std::fmt::Write;
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
let median = bs.ns_iter_summ.median as usize;
|
|
|
|
let deviation = (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as usize;
|
|
|
|
|
|
|
|
output.write_fmt(format_args!("{:>11} ns/iter (+/- {})",
|
|
|
|
fmt_thousands_sep(median, ','),
|
|
|
|
fmt_thousands_sep(deviation, ','))).unwrap();
|
2013-07-09 17:18:02 -07:00
|
|
|
if bs.mb_s != 0 {
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
output.write_fmt(format_args!(" = {} MB/s", bs.mb_s)).unwrap();
|
2013-03-16 11:11:31 -07:00
|
|
|
}
|
test: Display benchmark results with thousands separators
Example display:
```
running 9 tests
test a ... bench: 0 ns/iter (+/- 0)
test b ... bench: 52 ns/iter (+/- 0)
test c ... bench: 88 ns/iter (+/- 0)
test d ... bench: 618 ns/iter (+/- 111)
test e ... bench: 5,933 ns/iter (+/- 87)
test f ... bench: 59,280 ns/iter (+/- 1,052)
test g ... bench: 588,672 ns/iter (+/- 3,381)
test h ... bench: 5,894,227 ns/iter (+/- 303,489)
test i ... bench: 59,112,382 ns/iter (+/- 1,500,110)
```
Fixes #10953
Fixes #26109
2015-06-07 19:23:56 +02:00
|
|
|
output
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// A simple console test runner
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn> ) -> io::Result<bool> {
|
2014-05-18 19:35:45 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
fn callback<T: Write>(event: &TestEvent,
|
|
|
|
st: &mut ConsoleTestState<T>) -> io::Result<()> {
|
2013-07-02 12:47:32 -07:00
|
|
|
match (*event).clone() {
|
2013-07-09 17:18:02 -07:00
|
|
|
TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
|
2013-10-12 09:49:50 -04:00
|
|
|
TeWait(ref test, padding) => st.write_test_start(test, padding),
|
2014-02-12 10:25:09 -08:00
|
|
|
TeResult(test, result, stdout) => {
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(st.write_log(&test, &result));
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(st.write_result(&result));
|
2013-07-09 17:18:02 -07:00
|
|
|
match result {
|
|
|
|
TrOk => st.passed += 1,
|
|
|
|
TrIgnored => st.ignored += 1,
|
2013-07-15 18:50:32 -07:00
|
|
|
TrMetrics(mm) => {
|
2015-02-01 21:53:25 -05:00
|
|
|
let tname = test.name;
|
2013-11-01 18:06:31 -07:00
|
|
|
let MetricMap(mm) = mm;
|
2015-01-31 12:20:46 -05:00
|
|
|
for (k,v) in &mm {
|
2014-05-13 16:44:05 -07:00
|
|
|
st.metrics
|
2015-02-01 21:53:25 -05:00
|
|
|
.insert_metric(&format!("{}.{}",
|
|
|
|
tname,
|
|
|
|
k),
|
2014-05-13 16:44:05 -07:00
|
|
|
v.value,
|
|
|
|
v.noise);
|
2013-07-15 18:50:32 -07:00
|
|
|
}
|
|
|
|
st.measured += 1
|
|
|
|
}
|
2013-07-10 16:17:41 -07:00
|
|
|
TrBench(bs) => {
|
2014-05-05 19:15:17 +10:00
|
|
|
st.metrics.insert_metric(test.name.as_slice(),
|
2013-07-10 16:17:41 -07:00
|
|
|
bs.ns_iter_summ.median,
|
|
|
|
bs.ns_iter_summ.max - bs.ns_iter_summ.min);
|
2013-07-15 18:50:32 -07:00
|
|
|
st.measured += 1
|
2013-07-10 16:17:41 -07:00
|
|
|
}
|
2013-07-09 17:18:02 -07:00
|
|
|
TrFailed => {
|
|
|
|
st.failed += 1;
|
2014-02-12 10:25:09 -08:00
|
|
|
st.failures.push((test, stdout));
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(())
|
2013-07-09 17:18:02 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-12 17:31:03 -07:00
|
|
|
}
|
2014-05-18 19:35:45 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
let mut st = try!(ConsoleTestState::new(opts, None::<io::Stdout>));
|
2015-03-25 17:06:52 -07:00
|
|
|
fn len_if_padded(t: &TestDescAndFn) -> usize {
|
2013-10-14 15:45:57 -04:00
|
|
|
match t.testfn.padding() {
|
2015-01-24 14:39:32 +00:00
|
|
|
PadNone => 0,
|
2015-03-31 11:07:46 -07:00
|
|
|
PadOnRight => t.desc.name.as_slice().len(),
|
2013-10-14 15:45:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
match tests.iter().max_by(|t|len_if_padded(*t)) {
|
|
|
|
Some(t) => {
|
2014-05-05 19:15:17 +10:00
|
|
|
let n = t.desc.name.as_slice();
|
2015-03-18 09:14:54 -07:00
|
|
|
st.max_name_len = n.len();
|
2013-10-14 15:45:57 -04:00
|
|
|
},
|
2013-10-12 09:49:50 -04:00
|
|
|
None => {}
|
|
|
|
}
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(run_tests(opts, tests, |x| callback(&x, &mut st)));
|
2015-01-21 01:45:24 -08:00
|
|
|
return st.write_run_finish();
|
2012-03-12 17:31:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_sort_failures_before_printing_them() {
|
2013-10-13 18:48:47 -07:00
|
|
|
let test_a = TestDesc {
|
|
|
|
name: StaticTestName("a"),
|
|
|
|
ignore: false,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2013-10-13 18:48:47 -07:00
|
|
|
let test_b = TestDesc {
|
|
|
|
name: StaticTestName("b"),
|
|
|
|
ignore: false,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2013-11-24 06:53:08 -05:00
|
|
|
let mut st = ConsoleTestState {
|
2013-10-13 18:48:47 -07:00
|
|
|
log_out: None,
|
2014-11-11 16:01:29 -05:00
|
|
|
out: Raw(Vec::new()),
|
2013-10-13 18:48:47 -07:00
|
|
|
use_color: false,
|
2015-01-24 14:39:32 +00:00
|
|
|
total: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0,
|
|
|
|
ignored: 0,
|
|
|
|
measured: 0,
|
|
|
|
max_name_len: 10,
|
2013-10-13 18:48:47 -07:00
|
|
|
metrics: MetricMap::new(),
|
2014-03-05 15:28:08 -08:00
|
|
|
failures: vec!((test_b, Vec::new()), (test_a, Vec::new()))
|
2012-09-14 09:40:28 -07:00
|
|
|
};
|
2012-03-12 17:31:03 -07:00
|
|
|
|
2014-01-30 14:28:20 -08:00
|
|
|
st.write_failures().unwrap();
|
2013-11-24 06:53:08 -05:00
|
|
|
let s = match st.out {
|
2015-02-18 14:48:57 -05:00
|
|
|
Raw(ref m) => String::from_utf8_lossy(&m[..]),
|
2013-12-25 21:55:05 -08:00
|
|
|
Pretty(_) => unreachable!()
|
2013-11-24 06:53:08 -05:00
|
|
|
};
|
2013-10-13 18:48:47 -07:00
|
|
|
|
2015-02-28 20:07:05 +02:00
|
|
|
let apos = s.find("a").unwrap();
|
|
|
|
let bpos = s.find("b").unwrap();
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(apos < bpos);
|
2012-03-12 17:31:03 -07:00
|
|
|
}
|
|
|
|
|
2014-06-08 17:10:27 -07:00
|
|
|
fn use_color(opts: &TestOpts) -> bool {
|
|
|
|
match opts.color {
|
2015-05-26 23:46:55 +02:00
|
|
|
AutoColor => !opts.nocapture && stdout_isatty(),
|
2014-06-08 17:10:27 -07:00
|
|
|
AlwaysColor => true,
|
|
|
|
NeverColor => false,
|
|
|
|
}
|
2014-05-31 21:55:18 +02:00
|
|
|
}
|
2012-04-13 18:34:41 +08:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn stdout_isatty() -> bool {
|
|
|
|
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn stdout_isatty() -> bool {
|
2015-04-01 18:44:53 -07:00
|
|
|
const STD_OUTPUT_HANDLE: libc::DWORD = -11i32 as libc::DWORD;
|
2015-03-11 15:24:14 -07:00
|
|
|
extern "system" {
|
|
|
|
fn GetStdHandle(which: libc::DWORD) -> libc::HANDLE;
|
|
|
|
fn GetConsoleMode(hConsoleHandle: libc::HANDLE,
|
|
|
|
lpMode: libc::LPDWORD) -> libc::BOOL;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
let mut out = 0;
|
|
|
|
GetConsoleMode(handle, &mut out) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2012-09-04 18:05:57 -07:00
|
|
|
enum TestEvent {
|
2014-03-05 15:28:08 -08:00
|
|
|
TeFiltered(Vec<TestDesc> ),
|
2013-10-12 09:49:50 -04:00
|
|
|
TeWait(TestDesc, NamePadding),
|
2014-03-05 15:28:08 -08:00
|
|
|
TeResult(TestDesc, TestResult, Vec<u8> ),
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
pub type MonitorMsg = (TestDesc, TestResult, Vec<u8> );
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2014-12-09 17:00:29 -05:00
|
|
|
fn run_tests<F>(opts: &TestOpts,
|
|
|
|
tests: Vec<TestDescAndFn> ,
|
2015-03-11 15:24:14 -07:00
|
|
|
mut callback: F) -> io::Result<()> where
|
|
|
|
F: FnMut(TestEvent) -> io::Result<()>,
|
2014-12-09 17:00:29 -05:00
|
|
|
{
|
2015-05-02 13:38:51 +10:00
|
|
|
let mut filtered_tests = filter_tests(opts, tests);
|
|
|
|
if !opts.bench_benchmarks {
|
|
|
|
filtered_tests = convert_benchmarks_to_tests(filtered_tests);
|
|
|
|
}
|
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
let filtered_descs = filtered_tests.iter()
|
|
|
|
.map(|t| t.desc.clone())
|
|
|
|
.collect();
|
2013-05-09 13:27:24 -07:00
|
|
|
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(callback(TeFiltered(filtered_descs)));
|
2011-07-29 19:54:05 -07:00
|
|
|
|
2014-12-30 10:51:18 -08:00
|
|
|
let (filtered_tests, filtered_benchs_and_metrics): (Vec<_>, _) =
|
|
|
|
filtered_tests.into_iter().partition(|e| {
|
2013-11-20 15:46:49 -08:00
|
|
|
match e.testfn {
|
|
|
|
StaticTestFn(_) | DynTestFn(_) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
});
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2012-01-19 13:44:07 -08:00
|
|
|
// It's tempting to just spawn all the tests at once, but since we have
|
|
|
|
// many tests that run in other processes we would be making a big mess.
|
2011-07-29 19:54:05 -07:00
|
|
|
let concurrency = get_concurrency();
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
let mut remaining = filtered_tests;
|
2013-06-29 02:54:03 +10:00
|
|
|
remaining.reverse();
|
2013-01-31 17:12:29 -08:00
|
|
|
let mut pending = 0;
|
2011-07-29 19:54:05 -07:00
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel::<MonitorMsg>();
|
2012-01-19 14:36:11 -08:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
while pending > 0 || !remaining.is_empty() {
|
|
|
|
while pending < concurrency && !remaining.is_empty() {
|
2013-12-23 16:20:52 +01:00
|
|
|
let test = remaining.pop().unwrap();
|
2012-09-10 17:50:48 -07:00
|
|
|
if concurrency == 1 {
|
2012-02-18 16:30:07 -08:00
|
|
|
// We are doing one test at a time so we can print the name
|
|
|
|
// of the test before we run it. Useful for debugging tests
|
|
|
|
// that hang forever.
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(callback(TeWait(test.desc.clone(), test.testfn.padding())));
|
2012-02-18 16:30:07 -08:00
|
|
|
}
|
2014-04-23 09:38:46 -07:00
|
|
|
run_test(opts, !opts.run_tests, test, tx.clone());
|
2013-01-31 17:12:29 -08:00
|
|
|
pending += 1;
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
|
2014-12-23 11:53:35 -08:00
|
|
|
let (desc, result, stdout) = rx.recv().unwrap();
|
2012-09-10 17:50:48 -07:00
|
|
|
if concurrency != 1 {
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(callback(TeWait(desc.clone(), PadNone)));
|
2012-02-18 16:30:07 -08:00
|
|
|
}
|
2014-02-19 10:07:49 -08:00
|
|
|
try!(callback(TeResult(desc, result, stdout)));
|
2013-01-31 17:12:29 -08:00
|
|
|
pending -= 1;
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2015-05-02 13:38:51 +10:00
|
|
|
if opts.bench_benchmarks {
|
|
|
|
// All benchmarks run at the end, in serial.
|
|
|
|
// (this includes metric fns)
|
|
|
|
for b in filtered_benchs_and_metrics {
|
|
|
|
try!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
|
|
|
|
run_test(opts, false, b, tx.clone());
|
|
|
|
let (test, result, stdout) = rx.recv().unwrap();
|
|
|
|
try!(callback(TeResult(test, result, stdout)));
|
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(())
|
2011-07-29 19:54:05 -07:00
|
|
|
}
|
|
|
|
|
2015-03-19 18:38:16 -04:00
|
|
|
#[allow(deprecated)]
|
2015-03-25 17:06:52 -07:00
|
|
|
fn get_concurrency() -> usize {
|
2015-06-30 21:55:00 -07:00
|
|
|
return match env::var("RUST_TEST_THREADS") {
|
2015-01-27 12:20:58 -08:00
|
|
|
Ok(s) => {
|
2015-03-25 17:06:52 -07:00
|
|
|
let opt_n: Option<usize> = s.parse().ok();
|
2013-08-28 22:58:41 +10:00
|
|
|
match opt_n {
|
|
|
|
Some(n) if n > 0 => n,
|
2015-03-19 15:42:53 -04:00
|
|
|
_ => panic!("RUST_TEST_THREADS is `{}`, should be a positive integer.", s)
|
2013-08-28 22:58:41 +10:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 16:10:59 -07:00
|
|
|
Err(..) => num_cpus(),
|
2015-06-30 21:55:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
unsafe {
|
|
|
|
let mut sysinfo = std::mem::zeroed();
|
|
|
|
libc::GetSystemInfo(&mut sysinfo);
|
|
|
|
sysinfo.dwNumberOfProcessors as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
extern { fn rust_get_num_cpus() -> libc::uintptr_t; }
|
|
|
|
unsafe { rust_get_num_cpus() as usize }
|
2013-08-28 22:58:41 +10:00
|
|
|
}
|
2012-01-19 14:43:56 -08:00
|
|
|
}
|
2011-07-25 15:21:36 -07:00
|
|
|
|
2014-10-29 20:21:37 -05:00
|
|
|
pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
|
|
|
|
let mut filtered = tests;
|
|
|
|
|
|
|
|
// Remove tests that don't match the test filter
|
|
|
|
filtered = match opts.filter {
|
|
|
|
None => filtered,
|
2015-01-20 10:45:29 -08:00
|
|
|
Some(ref filter) => {
|
|
|
|
filtered.into_iter().filter(|test| {
|
2015-02-18 14:48:57 -05:00
|
|
|
test.desc.name.as_slice().contains(&filter[..])
|
2015-01-20 10:45:29 -08:00
|
|
|
}).collect()
|
2014-10-29 20:21:37 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Maybe pull out the ignored test and unignore them
|
|
|
|
filtered = if !opts.run_ignored {
|
|
|
|
filtered
|
|
|
|
} else {
|
|
|
|
fn filter(test: TestDescAndFn) -> Option<TestDescAndFn> {
|
|
|
|
if test.desc.ignore {
|
|
|
|
let TestDescAndFn {desc, testfn} = test;
|
|
|
|
Some(TestDescAndFn {
|
|
|
|
desc: TestDesc {ignore: false, ..desc},
|
|
|
|
testfn: testfn
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2015-09-08 00:36:29 +02:00
|
|
|
filtered.into_iter().filter_map(filter).collect()
|
2014-10-29 20:21:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Sort the tests alphabetically
|
|
|
|
filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));
|
|
|
|
|
2015-01-19 00:20:55 -08:00
|
|
|
filtered
|
2011-07-11 16:33:21 -07:00
|
|
|
}
|
2011-07-09 16:08:03 -07:00
|
|
|
|
2015-05-02 13:38:51 +10:00
|
|
|
pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
|
|
|
|
// convert benchmarks to tests, if we're not benchmarking them
|
|
|
|
tests.into_iter().map(|x| {
|
|
|
|
let testfn = match x.testfn {
|
|
|
|
DynBenchFn(bench) => {
|
|
|
|
DynTestFn(Box::new(move || bench::run_once(|b| bench.run(b))))
|
|
|
|
}
|
|
|
|
StaticBenchFn(benchfn) => {
|
|
|
|
DynTestFn(Box::new(move || bench::run_once(|b| benchfn(b))))
|
|
|
|
}
|
|
|
|
f => f
|
|
|
|
};
|
|
|
|
TestDescAndFn { desc: x.desc, testfn: testfn }
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
2014-04-23 09:38:46 -07:00
|
|
|
pub fn run_test(opts: &TestOpts,
|
|
|
|
force_ignore: bool,
|
2013-02-13 11:46:14 -08:00
|
|
|
test: TestDescAndFn,
|
2014-03-09 14:58:32 -07:00
|
|
|
monitor_ch: Sender<MonitorMsg>) {
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-01-31 17:12:29 -08:00
|
|
|
let TestDescAndFn {desc, testfn} = test;
|
|
|
|
|
2013-02-13 11:46:14 -08:00
|
|
|
if force_ignore || desc.ignore {
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc, TrIgnored, Vec::new())).unwrap();
|
2012-08-01 17:30:05 -07:00
|
|
|
return;
|
2011-11-01 10:31:23 -07:00
|
|
|
}
|
|
|
|
|
2013-02-13 11:46:14 -08:00
|
|
|
fn run_test_inner(desc: TestDesc,
|
2014-03-09 14:58:32 -07:00
|
|
|
monitor_ch: Sender<MonitorMsg>,
|
2014-04-23 09:38:46 -07:00
|
|
|
nocapture: bool,
|
2015-06-10 19:33:04 -07:00
|
|
|
testfn: Box<FnBox() + Send>) {
|
2015-03-11 15:24:14 -07:00
|
|
|
struct Sink(Arc<Mutex<Vec<u8>>>);
|
|
|
|
impl Write for Sink {
|
|
|
|
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
|
|
|
|
Write::write(&mut *self.0.lock().unwrap(), data)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:10:25 -08:00
|
|
|
thread::spawn(move || {
|
2015-03-11 15:24:14 -07:00
|
|
|
let data = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let data2 = data.clone();
|
|
|
|
let cfg = thread::Builder::new().name(match desc.name {
|
2015-09-08 00:36:29 +02:00
|
|
|
DynTestName(ref name) => name.clone(),
|
|
|
|
StaticTestName(name) => name.to_owned(),
|
2014-02-12 22:03:36 -08:00
|
|
|
});
|
2013-05-03 13:21:33 -07:00
|
|
|
|
2015-03-11 15:24:14 -07:00
|
|
|
let result_guard = cfg.spawn(move || {
|
|
|
|
if !nocapture {
|
2015-03-09 00:30:15 +02:00
|
|
|
io::set_print(box Sink(data2.clone()));
|
2015-03-11 15:24:14 -07:00
|
|
|
io::set_panic(box Sink(data2));
|
|
|
|
}
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn()
|
2015-03-11 15:24:14 -07:00
|
|
|
}).unwrap();
|
2014-12-06 18:34:37 -08:00
|
|
|
let test_result = calc_result(&desc, result_guard.join());
|
2015-03-11 15:24:14 -07:00
|
|
|
let stdout = data.lock().unwrap().to_vec();
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc.clone(), test_result, stdout)).unwrap();
|
2015-01-05 21:59:45 -08:00
|
|
|
});
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
match testfn {
|
2013-11-18 15:39:02 -08:00
|
|
|
DynBenchFn(bencher) => {
|
2014-02-14 09:49:11 +08:00
|
|
|
let bs = ::bench::benchmark(|harness| bencher.run(harness));
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc, TrBench(bs), Vec::new())).unwrap();
|
2013-02-13 11:46:14 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
StaticBenchFn(benchfn) => {
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
let bs = ::bench::benchmark(|harness| (benchfn.clone())(harness));
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc, TrBench(bs), Vec::new())).unwrap();
|
2013-02-13 11:46:14 -08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-15 18:50:32 -07:00
|
|
|
DynMetricFn(f) => {
|
|
|
|
let mut mm = MetricMap::new();
|
2015-04-01 16:03:33 -04:00
|
|
|
f.call_box((&mut mm,));
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc, TrMetrics(mm), Vec::new())).unwrap();
|
2013-07-15 18:50:32 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
StaticMetricFn(f) => {
|
|
|
|
let mut mm = MetricMap::new();
|
|
|
|
f(&mut mm);
|
2014-12-23 11:53:35 -08:00
|
|
|
monitor_ch.send((desc, TrMetrics(mm), Vec::new())).unwrap();
|
2013-07-15 18:50:32 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-04-23 09:38:46 -07:00
|
|
|
DynTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture, f),
|
|
|
|
StaticTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture,
|
2015-09-08 00:36:29 +02:00
|
|
|
Box::new(f))
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2011-07-14 11:29:54 -07:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:02:36 -08:00
|
|
|
fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any+Send>>) -> TestResult {
|
2015-01-31 15:08:25 -08:00
|
|
|
match (&desc.should_panic, task_result) {
|
|
|
|
(&ShouldPanic::No, Ok(())) |
|
2015-07-30 08:53:22 -07:00
|
|
|
(&ShouldPanic::Yes, Err(_)) => TrOk,
|
|
|
|
(&ShouldPanic::YesWithMessage(msg), Err(ref err))
|
2014-12-04 23:02:36 -08:00
|
|
|
if err.downcast_ref::<String>()
|
|
|
|
.map(|e| &**e)
|
|
|
|
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
|
|
|
|
.map(|e| e.contains(msg))
|
|
|
|
.unwrap_or(false) => TrOk,
|
|
|
|
_ => TrFailed,
|
2012-01-19 14:36:11 -08:00
|
|
|
}
|
2011-07-14 22:24:19 -07:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:17:41 -07:00
|
|
|
impl MetricMap {
|
|
|
|
|
2013-07-15 18:50:32 -07:00
|
|
|
pub fn new() -> MetricMap {
|
2014-12-17 10:16:10 -05:00
|
|
|
MetricMap(BTreeMap::new())
|
2013-07-10 16:17:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a named `value` (+/- `noise`) metric into the map. The value
|
|
|
|
/// must be non-negative. The `noise` indicates the uncertainty of the
|
|
|
|
/// metric, which doubles as the "noise range" of acceptable
|
|
|
|
/// pairwise-regressions on this named value, when comparing from one
|
|
|
|
/// metric to the next using `compare_to_old`.
|
|
|
|
///
|
|
|
|
/// If `noise` is positive, then it means this metric is of a value
|
|
|
|
/// you want to see grow smaller, so a change larger than `noise` in the
|
|
|
|
/// positive direction represents a regression.
|
|
|
|
///
|
|
|
|
/// If `noise` is negative, then it means this metric is of a value
|
|
|
|
/// you want to see grow larger, so a change larger than `noise` in the
|
|
|
|
/// negative direction represents a regression.
|
|
|
|
pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
|
|
|
|
let m = Metric {
|
|
|
|
value: value,
|
|
|
|
noise: noise
|
|
|
|
};
|
2013-11-01 18:06:31 -07:00
|
|
|
let MetricMap(ref mut map) = *self;
|
2015-09-08 00:36:29 +02:00
|
|
|
map.insert(name.to_owned(), m);
|
2013-07-10 16:17:41 -07:00
|
|
|
}
|
|
|
|
|
2015-01-21 03:09:44 -08:00
|
|
|
pub fn fmt_metrics(&self) -> String {
|
|
|
|
let MetricMap(ref mm) = *self;
|
|
|
|
let v : Vec<String> = mm.iter()
|
|
|
|
.map(|(k,v)| format!("{}: {} (+/- {})", *k,
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-20 17:15:27 +13:00
|
|
|
v.value, v.noise))
|
2015-01-21 03:09:44 -08:00
|
|
|
.collect();
|
2015-07-10 08:19:21 -04:00
|
|
|
v.join(", ")
|
2013-07-10 16:17:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Benchmarking
|
|
|
|
|
2014-02-17 22:53:45 +11:00
|
|
|
/// A function that is opaque to the optimizer, to allow benchmarks to
|
2014-02-08 17:59:23 +11:00
|
|
|
/// pretend to use outputs to assist in avoiding dead-code
|
|
|
|
/// elimination.
|
|
|
|
///
|
|
|
|
/// This function is a no-op, and does not even read from `dummy`.
|
2015-01-03 02:49:42 -06:00
|
|
|
pub fn black_box<T>(dummy: T) -> T {
|
2014-02-08 17:59:23 +11:00
|
|
|
// we need to "use" the argument in some way LLVM can't
|
|
|
|
// introspect.
|
|
|
|
unsafe {asm!("" : : "r"(&dummy))}
|
2015-01-03 02:49:42 -06:00
|
|
|
dummy
|
2014-02-08 17:59:23 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-01 09:16:35 +08:00
|
|
|
impl Bencher {
|
2013-06-14 18:21:47 -07:00
|
|
|
/// Callback for benchmark functions to run in their body.
|
2014-12-09 17:00:29 -05:00
|
|
|
pub fn iter<T, F>(&mut self, mut inner: F) where F: FnMut() -> T {
|
2014-11-10 12:27:56 -08:00
|
|
|
self.dur = Duration::span(|| {
|
|
|
|
let k = self.iterations;
|
2015-03-03 10:42:26 +02:00
|
|
|
for _ in 0..k {
|
2014-11-10 12:27:56 -08:00
|
|
|
black_box(inner());
|
|
|
|
}
|
|
|
|
});
|
2013-06-14 18:21:47 -07:00
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-06-14 18:21:47 -07:00
|
|
|
pub fn ns_elapsed(&mut self) -> u64 {
|
2015-07-05 23:20:00 -07:00
|
|
|
self.dur.as_secs() * 1_000_000_000 + (self.dur.subsec_nanos() as u64)
|
2013-06-14 18:21:47 -07:00
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-06-14 18:21:47 -07:00
|
|
|
pub fn ns_per_iter(&mut self) -> u64 {
|
|
|
|
if self.iterations == 0 {
|
|
|
|
0
|
|
|
|
} else {
|
2014-02-06 02:34:33 -05:00
|
|
|
self.ns_elapsed() / cmp::max(self.iterations, 1)
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2013-06-14 18:21:47 -07:00
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2014-12-09 17:00:29 -05:00
|
|
|
pub fn bench_n<F>(&mut self, n: u64, f: F) where F: FnOnce(&mut Bencher) {
|
2013-06-14 18:21:47 -07:00
|
|
|
self.iterations = n;
|
|
|
|
f(self);
|
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-07-10 16:17:41 -07:00
|
|
|
// This is a more statistics-driven benchmark algorithm
|
2015-04-17 15:32:42 -07:00
|
|
|
pub fn auto_bench<F>(&mut self, mut f: F) -> stats::Summary where F: FnMut(&mut Bencher) {
|
2013-06-14 18:21:47 -07:00
|
|
|
// Initial bench run to get ballpark figure.
|
2015-03-03 10:42:26 +02:00
|
|
|
let mut n = 1;
|
2013-06-21 20:08:35 -04:00
|
|
|
self.bench_n(n, |x| f(x));
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-07-10 16:17:41 -07:00
|
|
|
// Try to estimate iter count for 1ms falling back to 1m
|
|
|
|
// iterations if first run took < 1ns.
|
|
|
|
if self.ns_per_iter() == 0 {
|
|
|
|
n = 1_000_000;
|
|
|
|
} else {
|
2014-02-06 02:34:33 -05:00
|
|
|
n = 1_000_000 / cmp::max(self.ns_per_iter(), 1);
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2013-12-13 22:51:11 +11:00
|
|
|
// if the first run took more than 1ms we don't want to just
|
|
|
|
// be left doing 0 iterations on every loop. The unfortunate
|
|
|
|
// side effect of not being able to do as many runs is
|
|
|
|
// automatically handled by the statistical analysis below
|
|
|
|
// (i.e. larger error bars).
|
|
|
|
if n == 0 { n = 1; }
|
|
|
|
|
2015-04-28 11:40:04 -07:00
|
|
|
let mut total_run = Duration::new(0, 0);
|
2014-12-30 21:19:41 +13:00
|
|
|
let samples : &mut [f64] = &mut [0.0_f64; 50];
|
2013-06-14 18:21:47 -07:00
|
|
|
loop {
|
2014-11-10 12:27:56 -08:00
|
|
|
let mut summ = None;
|
|
|
|
let mut summ5 = None;
|
2013-06-14 18:21:47 -07:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
let loop_run = Duration::span(|| {
|
2013-06-14 18:21:47 -07:00
|
|
|
|
2015-01-31 20:02:00 -05:00
|
|
|
for p in &mut *samples {
|
2014-11-10 12:27:56 -08:00
|
|
|
self.bench_n(n, |x| f(x));
|
|
|
|
*p = self.ns_per_iter() as f64;
|
|
|
|
};
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
stats::winsorize(samples, 5.0);
|
|
|
|
summ = Some(stats::Summary::new(samples));
|
2013-07-10 16:17:41 -07:00
|
|
|
|
2015-01-31 20:02:00 -05:00
|
|
|
for p in &mut *samples {
|
2014-11-10 12:27:56 -08:00
|
|
|
self.bench_n(5 * n, |x| f(x));
|
|
|
|
*p = self.ns_per_iter() as f64;
|
|
|
|
};
|
2013-07-10 16:17:41 -07:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
stats::winsorize(samples, 5.0);
|
|
|
|
summ5 = Some(stats::Summary::new(samples));
|
|
|
|
});
|
|
|
|
let summ = summ.unwrap();
|
|
|
|
let summ5 = summ5.unwrap();
|
2013-07-07 15:43:31 -07:00
|
|
|
|
2013-12-13 22:51:11 +11:00
|
|
|
// If we've run for 100ms and seem to have converged to a
|
2013-07-10 16:17:41 -07:00
|
|
|
// stable median.
|
2015-04-28 11:40:04 -07:00
|
|
|
if loop_run > Duration::from_millis(100) &&
|
2013-07-10 16:17:41 -07:00
|
|
|
summ.median_abs_dev_pct < 1.0 &&
|
|
|
|
summ.median - summ5.median < summ5.median_abs_dev {
|
|
|
|
return summ5;
|
2013-07-07 15:43:31 -07:00
|
|
|
}
|
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
total_run = total_run + loop_run;
|
2013-07-15 20:34:11 -07:00
|
|
|
// Longest we ever run for is 3s.
|
2015-04-28 11:40:04 -07:00
|
|
|
if total_run > Duration::from_secs(3) {
|
2013-07-10 16:17:41 -07:00
|
|
|
return summ5;
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
2013-06-14 18:21:47 -07:00
|
|
|
|
2015-03-06 11:58:32 -08:00
|
|
|
// If we overflow here just return the results so far. We check a
|
|
|
|
// multiplier of 10 because we're about to multiply by 2 and the
|
|
|
|
// next iteration of the loop will also multiply by 5 (to calculate
|
|
|
|
// the summ5 result)
|
|
|
|
n = match n.checked_mul(10) {
|
|
|
|
Some(_) => n * 2,
|
|
|
|
None => return summ5,
|
|
|
|
};
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 18:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod bench {
|
2014-02-06 02:34:33 -05:00
|
|
|
use std::cmp;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::time::Duration;
|
2014-04-01 09:16:35 +08:00
|
|
|
use super::{Bencher, BenchSamples};
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2014-12-09 17:00:29 -05:00
|
|
|
pub fn benchmark<F>(f: F) -> BenchSamples where F: FnMut(&mut Bencher) {
|
2014-04-01 09:16:35 +08:00
|
|
|
let mut bs = Bencher {
|
2013-02-13 11:46:14 -08:00
|
|
|
iterations: 0,
|
2015-04-28 11:40:04 -07:00
|
|
|
dur: Duration::new(0, 0),
|
2013-02-13 11:46:14 -08:00
|
|
|
bytes: 0
|
|
|
|
};
|
|
|
|
|
2013-07-07 15:43:31 -07:00
|
|
|
let ns_iter_summ = bs.auto_bench(f);
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2014-02-06 02:34:33 -05:00
|
|
|
let ns_iter = cmp::max(ns_iter_summ.median as u64, 1);
|
2013-07-17 12:28:48 -07:00
|
|
|
let iter_s = 1_000_000_000 / ns_iter;
|
2013-02-13 11:46:14 -08:00
|
|
|
let mb_s = (bs.bytes * iter_s) / 1_000_000;
|
|
|
|
|
|
|
|
BenchSamples {
|
2013-07-07 15:43:31 -07:00
|
|
|
ns_iter_summ: ns_iter_summ,
|
2015-03-25 17:06:52 -07:00
|
|
|
mb_s: mb_s as usize
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
}
|
2015-05-02 13:38:51 +10:00
|
|
|
|
|
|
|
pub fn run_once<F>(f: F) where F: FnOnce(&mut Bencher) {
|
|
|
|
let mut bs = Bencher {
|
|
|
|
iterations: 0,
|
2015-04-28 11:40:04 -07:00
|
|
|
dur: Duration::new(0, 0),
|
2015-05-02 13:38:51 +10:00
|
|
|
bytes: 0
|
|
|
|
};
|
|
|
|
bs.bench_n(1, f);
|
|
|
|
}
|
2013-02-13 11:46:14 -08:00
|
|
|
}
|
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-31 17:12:29 -08:00
|
|
|
use test::{TrFailed, TrIgnored, TrOk, filter_tests, parse_opts,
|
2014-02-14 09:49:11 +08:00
|
|
|
TestDesc, TestDescAndFn, TestOpts, run_test,
|
2015-01-26 22:56:50 -05:00
|
|
|
MetricMap,
|
2015-01-31 15:08:25 -08:00
|
|
|
StaticTestName, DynTestName, DynTestFn, ShouldPanic};
|
2014-12-23 11:53:35 -08:00
|
|
|
use std::sync::mpsc::channel;
|
2012-12-27 18:24:18 -08:00
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn do_not_run_ignored_tests() {
|
2014-10-09 15:17:22 -04:00
|
|
|
fn f() { panic!(); }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("whatever"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: true,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-04-23 09:38:46 -07:00
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(res != TrOk);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn ignored_tests_result_in_ignored() {
|
2012-01-17 19:05:07 -08:00
|
|
|
fn f() { }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("whatever"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: true,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-04-23 09:38:46 -07:00
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res == TrIgnored);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 15:08:25 -08:00
|
|
|
fn test_should_panic() {
|
2014-10-09 15:17:22 -04:00
|
|
|
fn f() { panic!(); }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("whatever"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: false,
|
2015-07-30 08:53:22 -07:00
|
|
|
should_panic: ShouldPanic::Yes,
|
2014-12-04 23:02:36 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2014-12-04 23:02:36 -08:00
|
|
|
};
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2014-12-04 23:02:36 -08:00
|
|
|
assert!(res == TrOk);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 15:08:25 -08:00
|
|
|
fn test_should_panic_good_message() {
|
2014-12-04 23:02:36 -08:00
|
|
|
fn f() { panic!("an error message"); }
|
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: StaticTestName("whatever"),
|
|
|
|
ignore: false,
|
2015-07-30 08:53:22 -07:00
|
|
|
should_panic: ShouldPanic::YesWithMessage("error message"),
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-04-23 09:38:46 -07:00
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res == TrOk);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
2014-12-04 23:02:36 -08:00
|
|
|
#[test]
|
2015-01-31 15:08:25 -08:00
|
|
|
fn test_should_panic_bad_message() {
|
2014-12-04 23:02:36 -08:00
|
|
|
fn f() { panic!("an error message"); }
|
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
|
|
|
name: StaticTestName("whatever"),
|
|
|
|
ignore: false,
|
2015-07-30 08:53:22 -07:00
|
|
|
should_panic: ShouldPanic::YesWithMessage("foobar"),
|
2014-12-04 23:02:36 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2014-12-04 23:02:36 -08:00
|
|
|
};
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2014-12-04 23:02:36 -08:00
|
|
|
assert!(res == TrFailed);
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:05:07 -08:00
|
|
|
#[test]
|
2015-01-31 15:08:25 -08:00
|
|
|
fn test_should_panic_but_succeeds() {
|
2012-01-17 19:05:07 -08:00
|
|
|
fn f() { }
|
2013-01-31 17:12:29 -08:00
|
|
|
let desc = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("whatever"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: false,
|
2015-07-30 08:53:22 -07:00
|
|
|
should_panic: ShouldPanic::Yes,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| f())),
|
2012-01-17 19:05:07 -08:00
|
|
|
};
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-04-23 09:38:46 -07:00
|
|
|
run_test(&TestOpts::new(), false, desc, tx);
|
2014-12-23 11:53:35 -08:00
|
|
|
let (_, res, _) = rx.recv().unwrap();
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(res == TrFailed);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-16 01:08:52 +10:00
|
|
|
fn parse_ignored_flag() {
|
2014-05-25 03:17:19 -07:00
|
|
|
let args = vec!("progname".to_string(),
|
|
|
|
"filter".to_string(),
|
|
|
|
"--ignored".to_string());
|
2015-02-01 21:53:25 -05:00
|
|
|
let opts = match parse_opts(&args) {
|
2013-09-24 16:34:23 -07:00
|
|
|
Some(Ok(o)) => o,
|
2014-10-09 15:17:22 -04:00
|
|
|
_ => panic!("Malformed arg in parse_ignored_flag")
|
2012-08-03 19:59:04 -07:00
|
|
|
};
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!((opts.run_ignored));
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn filter_for_ignored_option() {
|
2012-01-17 19:05:07 -08:00
|
|
|
// When we run ignored tests the test filter should filter out all the
|
|
|
|
// unignored tests and flip the ignore flag on the rest to false
|
|
|
|
|
2014-04-23 09:38:46 -07:00
|
|
|
let mut opts = TestOpts::new();
|
|
|
|
opts.run_tests = true;
|
|
|
|
opts.run_ignored = true;
|
2013-01-22 08:44:24 -08:00
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
let tests = vec!(
|
2013-01-31 17:12:29 -08:00
|
|
|
TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("1"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: true,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| {})),
|
2013-01-22 08:44:24 -08:00
|
|
|
},
|
2013-01-31 17:12:29 -08:00
|
|
|
TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-02-13 11:46:14 -08:00
|
|
|
name: StaticTestName("2"),
|
2013-01-31 17:12:29 -08:00
|
|
|
ignore: false,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(move|| {})),
|
2014-03-05 15:28:08 -08:00
|
|
|
});
|
2012-09-19 18:51:35 -07:00
|
|
|
let filtered = filter_tests(&opts, tests);
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(filtered.len(), 1);
|
2014-09-22 19:30:06 +02:00
|
|
|
assert_eq!(filtered[0].desc.name.to_string(),
|
2014-11-27 19:55:37 -05:00
|
|
|
"1");
|
2014-09-22 19:30:06 +02:00
|
|
|
assert!(filtered[0].desc.ignore == false);
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-29 12:06:09 -08:00
|
|
|
pub fn sort_tests() {
|
2014-04-23 09:38:46 -07:00
|
|
|
let mut opts = TestOpts::new();
|
|
|
|
opts.run_tests = true;
|
2012-01-17 19:05:07 -08:00
|
|
|
|
|
|
|
let names =
|
2014-05-25 03:17:19 -07:00
|
|
|
vec!("sha1::test".to_string(),
|
2015-03-25 17:06:52 -07:00
|
|
|
"isize::test_to_str".to_string(),
|
|
|
|
"isize::test_pow".to_string(),
|
2014-05-25 03:17:19 -07:00
|
|
|
"test::do_not_run_ignored_tests".to_string(),
|
|
|
|
"test::ignored_tests_result_in_ignored".to_string(),
|
|
|
|
"test::first_free_arg_should_be_a_filter".to_string(),
|
|
|
|
"test::parse_ignored_flag".to_string(),
|
|
|
|
"test::filter_for_ignored_option".to_string(),
|
|
|
|
"test::sort_tests".to_string());
|
2012-01-17 19:05:07 -08:00
|
|
|
let tests =
|
|
|
|
{
|
2013-01-31 17:12:29 -08:00
|
|
|
fn testfn() { }
|
2014-03-05 15:28:08 -08:00
|
|
|
let mut tests = Vec::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for name in &names {
|
2013-01-31 17:12:29 -08:00
|
|
|
let test = TestDescAndFn {
|
|
|
|
desc: TestDesc {
|
2013-07-02 12:47:32 -07:00
|
|
|
name: DynTestName((*name).clone()),
|
2013-02-13 11:46:14 -08:00
|
|
|
ignore: false,
|
2015-01-31 15:08:25 -08:00
|
|
|
should_panic: ShouldPanic::No,
|
2013-01-31 17:12:29 -08:00
|
|
|
},
|
2015-04-01 11:12:30 -04:00
|
|
|
testfn: DynTestFn(Box::new(testfn)),
|
2013-01-31 17:12:29 -08:00
|
|
|
};
|
2013-02-15 02:30:30 -05:00
|
|
|
tests.push(test);
|
2012-09-18 21:41:37 -07:00
|
|
|
}
|
2013-02-15 02:30:30 -05:00
|
|
|
tests
|
2012-09-18 21:41:37 -07:00
|
|
|
};
|
2012-09-19 18:51:35 -07:00
|
|
|
let filtered = filter_tests(&opts, tests);
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2012-09-18 21:41:37 -07:00
|
|
|
let expected =
|
2015-03-25 17:06:52 -07:00
|
|
|
vec!("isize::test_pow".to_string(),
|
|
|
|
"isize::test_to_str".to_string(),
|
2014-05-25 03:17:19 -07:00
|
|
|
"sha1::test".to_string(),
|
|
|
|
"test::do_not_run_ignored_tests".to_string(),
|
|
|
|
"test::filter_for_ignored_option".to_string(),
|
|
|
|
"test::first_free_arg_should_be_a_filter".to_string(),
|
|
|
|
"test::ignored_tests_result_in_ignored".to_string(),
|
|
|
|
"test::parse_ignored_flag".to_string(),
|
|
|
|
"test::sort_tests".to_string());
|
2012-01-17 19:05:07 -08:00
|
|
|
|
2015-06-10 17:22:20 +01:00
|
|
|
for (a, b) in expected.iter().zip(filtered) {
|
2014-06-21 03:39:03 -07:00
|
|
|
assert!(*a == b.desc.name.to_string());
|
2012-09-18 21:41:37 -07:00
|
|
|
}
|
|
|
|
}
|
2013-07-11 17:05:23 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_metricmap_compare() {
|
|
|
|
let mut m1 = MetricMap::new();
|
|
|
|
let mut m2 = MetricMap::new();
|
|
|
|
m1.insert_metric("in-both-noise", 1000.0, 200.0);
|
|
|
|
m2.insert_metric("in-both-noise", 1100.0, 200.0);
|
|
|
|
|
|
|
|
m1.insert_metric("in-first-noise", 1000.0, 2.0);
|
|
|
|
m2.insert_metric("in-second-noise", 1000.0, 2.0);
|
|
|
|
|
|
|
|
m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
|
|
|
|
m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
|
|
|
|
|
|
|
|
m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
|
|
|
|
m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
|
|
|
|
|
|
|
|
m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
|
|
|
|
m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
|
|
|
|
|
|
|
|
m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
|
|
|
|
m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
|
|
|
|
}
|
2012-01-17 19:05:07 -08:00
|
|
|
}
|