diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index d6be177..f9064ac 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -2,14 +2,39 @@ trigger: - master jobs: -- template: ci/azure-job-test-all.yml +- template: ci/azure-job-test-nightly.yml parameters: vmImage: vs2017-win2016 - name: x86_64_msvc + name: x86_64_msvc_nightly target: x86_64-pc-windows-msvc -- template: ci/azure-job-test-all.yml + toolchain: nightly-x86_64-msvc +- template: ci/azure-job-test-stable.yml + parameters: + vmImage: vs2017-win2016 + name: x86_64_msvc_beta + target: x86_64-pc-windows-msvc + toolchain: beta-x86_64-msvc +- template: ci/azure-job-test-stable.yml + parameters: + vmImage: vs2017-win2016 + name: x86_64_msvc_stable + target: x86_64-pc-windows-msvc + toolchain: stable-x86_64-msvc +- template: ci/azure-job-test-nightly.yml parameters: vmImage: vs2017-win2016 toolchain: nightly-x86_64-gnu - name: x86_64_mingw + name: x86_64_mingw_nightly + target: x86_64-pc-windows-gnu +- template: ci/azure-job-test-stable.yml + parameters: + vmImage: vs2017-win2016 + toolchain: beta-x86_64-gnu + name: x86_64_mingw_beta + target: x86_64-pc-windows-gnu +- template: ci/azure-job-test-stable.yml + parameters: + vmImage: vs2017-win2016 + toolchain: stable-x86_64-gnu + name: x86_64_mingw_stable target: x86_64-pc-windows-gnu diff --git a/.travis.yml b/.travis.yml index 88542af..6c0609a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,9 @@ matrix: - name: "Tools" install: true script: sh ci/tools.sh - - # cross targets: + - name: "Integration" + install: true + script: sh ci/integration.sh - name: "aarch64-linux-android" env: TARGET=aarch64-linux-android CROSS=1 - name: "aarch64-unknown-linux-gnu" @@ -39,19 +40,37 @@ matrix: packages: - gcc-multilib - - name: "i686-apple-darwin" + - name: "i686-apple-darwin (nightly)" env: TARGET=i686-apple-darwin os: osx osx_image: xcode10 + - name: "i686-apple-darwin (beta)" + env: TARGET=i686-apple-darwin + os: osx + rust: beta + osx_image: xcode10 + - name: "i686-apple-darwin (stable)" + env: TARGET=i686-apple-darwin + os: osx + rust: stable + osx_image: xcode10 - name: "i686-linux-android" env: TARGET=i686-linux-android CROSS=1 - name: "i686-pc-windows-gnu" env: TARGET=i686-pc-windows-gnu CROSS=1 - name: "i686-unknown-freebsd" env: TARGET=i686-unknown-freebsd NORUN=1 CROSS=1 - - name: "i686-unknown-linux-gnu" + - name: "i686-unknown-linux-gnu (nightly)" + env: TARGET=i686-unknown-linux-gnu CROSS=1 + addons: *gcc_multilib + - name: "i686-unknown-linux-gnu (beta)" env: TARGET=i686-unknown-linux-gnu CROSS=1 addons: *gcc_multilib + rust: beta + - name: "i686-unknown-linux-gnu (stable)" + env: TARGET=i686-unknown-linux-gnu CROSS=1 + addons: *gcc_multilib + rust: stable - name: "i686-unknown-linux-musl" env: TARGET=i686-unknown-linux-musl CROSS=1 - name: "mips-unknown-linux-gnu" @@ -72,25 +91,49 @@ matrix: env: TARGET=s390x-unknown-linux-gnu CROSS=1 NORUN=1 - name: "sparc64-unknown-linux-gnu" env: TARGET=sparc64-unknown-linux-gnu CROSS=1 NORUN=1 - - name: "x86_64-apple-darwin" + - name: "x86_64-apple-darwin (nightly)" env: TARGET=x86_64-apple-darwin os: osx osx_image: xcode10 install: true + - name: "x86_64-apple-darwin (beta)" + env: TARGET=x86_64-apple-darwin + os: osx + osx_image: xcode10 + install: true + rust: beta + - name: "x86_64-apple-darwin (stable)" + env: TARGET=x86_64-apple-darwin + os: osx + osx_image: xcode10 + install: true + rust: stable - name: "x86_64-linux-android" env: TARGET=x86_64-linux-android CROSS=1 - name: "x86_64-sun-solaris" env: TARGET=x86_64-sun-solaris NORUN=1 CROSS=1 - name: "x86_64-unknown-freebsd" env: TARGET=x86_64-unknown-freebsd NORUN=1 CROSS=1 - - name: "x86_64-unknown-linux-gnu" + - name: "x86_64-unknown-linux-gnu (nightly)" + env: TARGET=x86_64-unknown-linux-gnu + install: true + - name: "x86_64-unknown-linux-gnu (beta)" env: TARGET=x86_64-unknown-linux-gnu install: true + rust: beta + - name: "x86_64-unknown-linux-gnu (stable)" + env: TARGET=x86_64-unknown-linux-gnu + install: true + rust: stable - name: "x86_64-unknown-linux-musl" env: TARGET=x86_64-unknown-linux-musl CROSS=1 - name: "x86_64-unknown-netbsd" env: TARGET=x86_64-unknown-netbsd NORUN=1 CROSS=1 + allow_failures: + # FIXME: the compiletest-rs crate needs to be updated to libtest 0.0.2 + - name: "Integration" + install: - travis_retry rustup target add $TARGET - | diff --git a/Cargo.toml b/Cargo.toml index 811738f..1b545f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,20 @@ -[workspace] -members = [ "libtest" ] \ No newline at end of file +[package] +authors = ["The Rust Project Developers"] +name = "libtest" +version = "0.0.2" +edition = "2018" +description = "Rust's built in unit-test and micro-benchmarking framework" +license = "MIT/Apache-2.0" +documentation = "https://docs.rs/libtest" +homepage = "https://github.com/rust-lang/libtest" +repository = "https://github.com/rust-lang/libtest" +readme = "README.md" + +[dependencies] +getopts = "0.2" +termcolor = "1.0" +libc = "0.2" + +[features] +default = [] +unstable = [] \ No newline at end of file diff --git a/README.md b/README.md index 709c31f..6736361 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,18 @@ libtest - Rust's built-in unit-testing and benchmarking framework See [The Rust Programming Language chapter on Testing](https://doc.rust-lang.org/book/ch11-00-testing.html). +## Cargo features + +* `unstable` (disabled by default): enables nightly features. Currently, this enables: + * `feature(set_stdio)`: better output reporting + * `feature(panic_unwind)`: explicitly links against the `panic_unwind` crate + on platforms that support it, but avoid that on platforms that do not. This + allows using `libtest` on platforms like `aarch64-pc-windows-msvc` which do + not yet support `panic_unwind`. + * `feature(termination_trait_lib)`: exposes the `assert_test_result` API + * `feature(test)`: uses `test::black_box` in benchmarks. On stable Rust, this is + worked around with volatile loads and stores which aren't as good. + ## Platform support * "build" shows whether the library compiles diff --git a/ci/azure-job-test-nightly.yml b/ci/azure-job-test-nightly.yml new file mode 100644 index 0000000..0b9fc2c --- /dev/null +++ b/ci/azure-job-test-nightly.yml @@ -0,0 +1,17 @@ +parameters: + toolchain: 'nightly' + vmImage: 'ubuntu-16.04' + name: '' + +jobs: +- job: ${{ parameters.name }} + pool: + vmImage: ${{ parameters.vmImage }} + steps: + - template: azure-install-rust.yml + parameters: + toolchain: ${{ parameters.toolchain }} + - script: cargo test -vv --all --target ${{ parameters.target }} + - script: cargo test -vv --all --release --target ${{ parameters.target }} + - script: cargo test -vv --all --target ${{ parameters.target }} --features=unstable + - script: cargo test -vv --all --release --target ${{ parameters.target }} --features=unstable diff --git a/ci/azure-job-test-all.yml b/ci/azure-job-test-stable.yml similarity index 100% rename from ci/azure-job-test-all.yml rename to ci/azure-job-test-stable.yml diff --git a/ci/integration.sh b/ci/integration.sh new file mode 100644 index 0000000..70ac3fe --- /dev/null +++ b/ci/integration.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +set -ex + +# compile-test +rm -r compiletest-rs || true +git clone https://github.com/laumann/compiletest-rs +( + cd compiletest-rs + sed -i '' 's@libtest = "0.0.2"@libtest = { path = "..", features = [ "unstable" ] }@g' Cargo.toml + echo "[workspace]" >> Cargo.toml + cargo build + cargo build --features=unstable +) diff --git a/ci/run.sh b/ci/run.sh index d6120da..1d87b44 100644 --- a/ci/run.sh +++ b/ci/run.sh @@ -16,3 +16,8 @@ fi "${CARGO}" "${CMD}" -vv --all --target="${TARGET}" "${CARGO}" "${CMD}" -vv --all --target="${TARGET}" --release + +if [ "${TRAVIS_RUST_VERSION}" = "nightly" ]; then + "${CARGO}" "${CMD}" -vv --all --target="${TARGET}" --features=unstable + "${CARGO}" "${CMD}" -vv --all --target="${TARGET}" --features=unstable --release +fi diff --git a/libtest/Cargo.toml b/libtest/Cargo.toml deleted file mode 100644 index c1ce6b4..0000000 --- a/libtest/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -authors = ["The Rust Project Developers"] -name = "libtest" -version = "0.0.1" -edition = "2018" -description = "Rust's built in unit-test and micro-benchmarking framework" -license = "MIT/Apache-2.0" -documentation = "https://docs.rs/libterm" -homepage = "https://github.com/rust-lang/libtest" -repository = "https://github.com/rust-lang/libtest" -readme = "README.md" - -[lib] -name = "libtest" -path = "lib.rs" -crate-type = ["dylib", "rlib"] - -[dependencies] -getopts = "0.2" -term = "0.5" \ No newline at end of file diff --git a/libtest/README.md b/libtest/README.md deleted file mode 120000 index 32d46ee..0000000 --- a/libtest/README.md +++ /dev/null @@ -1 +0,0 @@ -../README.md \ No newline at end of file diff --git a/libtest/formatters/json.rs b/src/formatters/json.rs similarity index 100% rename from libtest/formatters/json.rs rename to src/formatters/json.rs diff --git a/libtest/formatters/mod.rs b/src/formatters/mod.rs similarity index 100% rename from libtest/formatters/mod.rs rename to src/formatters/mod.rs diff --git a/libtest/formatters/pretty.rs b/src/formatters/pretty.rs similarity index 91% rename from libtest/formatters/pretty.rs rename to src/formatters/pretty.rs index 8552dfe..f31716c 100644 --- a/libtest/formatters/pretty.rs +++ b/src/formatters/pretty.rs @@ -1,4 +1,5 @@ use super::*; +use termcolor::Color; pub(crate) struct PrettyFormatter { out: OutputLocation, @@ -31,29 +32,29 @@ impl PrettyFormatter { } pub fn write_ok(&mut self) -> io::Result<()> { - self.write_short_result("ok", term::color::GREEN) + self.write_short_result("ok", Color::Green) } pub fn write_failed(&mut self) -> io::Result<()> { - self.write_short_result("FAILED", term::color::RED) + self.write_short_result("FAILED", Color::Red) } pub fn write_ignored(&mut self) -> io::Result<()> { - self.write_short_result("ignored", term::color::YELLOW) + self.write_short_result("ignored", Color::Yellow) } pub fn write_allowed_fail(&mut self) -> io::Result<()> { - self.write_short_result("FAILED (allowed)", term::color::YELLOW) + self.write_short_result("FAILED (allowed)", Color::Yellow) } pub fn write_bench(&mut self) -> io::Result<()> { - self.write_pretty("bench", term::color::CYAN) + self.write_pretty("bench", Color::Cyan) } pub fn write_short_result( &mut self, result: &str, - color: term::color::Color, + color: Color, ) -> io::Result<()> { self.write_pretty(result, color)?; self.write_plain("\n") @@ -62,12 +63,15 @@ impl PrettyFormatter { pub fn write_pretty( &mut self, word: &str, - color: term::color::Color, + color: Color, ) -> io::Result<()> { + use termcolor::WriteColor; match self.out { OutputLocation::Pretty(ref mut term) => { if self.use_color { - term.fg(color)?; + term.set_color( + termcolor::ColorSpec::new().set_fg(Some(color)), + )?; } term.write_all(word.as_bytes())?; if self.use_color { @@ -223,9 +227,9 @@ impl OutputFormatter for PrettyFormatter { if success { // There's no parallelism at this point so it's safe to use color - self.write_pretty("ok", term::color::GREEN)?; + self.write_pretty("ok", Color::Green)?; } else { - self.write_pretty("FAILED", term::color::RED)?; + self.write_pretty("FAILED", Color::Red)?; } let s = if state.allowed_fail > 0 { diff --git a/libtest/formatters/terse.rs b/src/formatters/terse.rs similarity index 92% rename from libtest/formatters/terse.rs rename to src/formatters/terse.rs index a7b25be..f57a71f 100644 --- a/libtest/formatters/terse.rs +++ b/src/formatters/terse.rs @@ -1,4 +1,5 @@ use super::*; +use termcolor::Color; pub(crate) struct TerseFormatter { out: OutputLocation, @@ -29,29 +30,29 @@ impl TerseFormatter { } pub fn write_ok(&mut self) -> io::Result<()> { - self.write_short_result(".", term::color::GREEN) + self.write_short_result(".", Color::Green) } pub fn write_failed(&mut self) -> io::Result<()> { - self.write_short_result("F", term::color::RED) + self.write_short_result("F", Color::Red) } pub fn write_ignored(&mut self) -> io::Result<()> { - self.write_short_result("i", term::color::YELLOW) + self.write_short_result("i", Color::Yellow) } pub fn write_allowed_fail(&mut self) -> io::Result<()> { - self.write_short_result("a", term::color::YELLOW) + self.write_short_result("a", Color::Yellow) } pub fn write_bench(&mut self) -> io::Result<()> { - self.write_pretty("bench", term::color::CYAN) + self.write_pretty("bench", Color::Cyan) } pub fn write_short_result( &mut self, result: &str, - color: term::color::Color, + color: Color, ) -> io::Result<()> { self.write_pretty(result, color)?; if self.test_count % QUIET_MODE_MAX_COLUMN == QUIET_MODE_MAX_COLUMN - 1 @@ -74,12 +75,15 @@ impl TerseFormatter { pub fn write_pretty( &mut self, word: &str, - color: term::color::Color, + color: Color, ) -> io::Result<()> { + use termcolor::WriteColor; match self.out { OutputLocation::Pretty(ref mut term) => { if self.use_color { - term.fg(color)?; + term.set_color( + termcolor::ColorSpec::new().set_fg(Some(color)), + )?; } term.write_all(word.as_bytes())?; if self.use_color { @@ -233,9 +237,9 @@ impl OutputFormatter for TerseFormatter { if success { // There's no parallelism at this point so it's safe to use color - self.write_pretty("ok", term::color::GREEN)?; + self.write_pretty("ok", Color::Green)?; } else { - self.write_pretty("FAILED", term::color::RED)?; + self.write_pretty("FAILED", Color::Red)?; } let s = if state.allowed_fail > 0 { diff --git a/libtest/lib.rs b/src/lib.rs similarity index 95% rename from libtest/lib.rs rename to src/lib.rs index f8f5284..4da6733 100644 --- a/libtest/lib.rs +++ b/src/lib.rs @@ -1,10 +1,8 @@ //! Rust's built-in unit-test and micro-benchmarking framework. -#![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc, rustc_private))] -#![feature(fnbox)] -#![feature(set_stdio)] -#![feature(panic_unwind)] -#![feature(termination_trait_lib)] -#![feature(test)] +#![cfg_attr( + feature = "unstable", + feature(set_stdio, panic_unwind, termination_trait_lib, test) +)] #![deny(rust_2018_idioms)] #![allow( clippy::pub_enum_variant_names, @@ -13,10 +11,6 @@ clippy::cast_precision_loss )] -use getopts; - -extern crate test; - #[cfg(any(unix, target_os = "cloudabi"))] extern crate libc; @@ -26,13 +20,12 @@ extern crate libc; // libtest won't be fully functional on this platform. // // See also: https://github.com/rust-lang/rust/issues/54190#issuecomment-422904437 -#[cfg(not(all(windows, target_arch = "aarch64")))] +#[cfg(all(features = "unstable", not(all(windows, target_arch = "aarch64"))))] extern crate panic_unwind; use std::{ any::Any, borrow::Cow, - boxed::FnBox, cmp, collections::BTreeMap, env, fmt, @@ -40,7 +33,7 @@ use std::{ io::{self, prelude::*}, panic::{catch_unwind, AssertUnwindSafe}, path::PathBuf, - process::{self, Termination}, + process, sync::{ mpsc::{channel, Sender}, Arc, Mutex, @@ -49,12 +42,42 @@ use std::{ time::{Duration, Instant}, }; +#[cfg(feature = "unstable")] +use std::process::Termination; +pub use termcolor::ColorChoice; + const TEST_WARN_TIMEOUT_S: u64 = 60; const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in quiet mode mod formatters; pub mod stats; +fn set_print( + sink: Option>, +) -> Option> { + #[cfg(feature = "unstable")] + { + io::set_print(sink) + } + #[cfg(not(feature = "unstable"))] + { + sink + } +} + +fn set_panic( + sink: Option>, +) -> Option> { + #[cfg(feature = "unstable")] + { + io::set_panic(sink) + } + #[cfg(not(feature = "unstable"))] + { + sink + } +} + use crate::formatters::{ JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter, }; @@ -142,7 +165,7 @@ pub trait TDynBenchFn: Send { pub enum TestFn { StaticTestFn(fn()), StaticBenchFn(fn(&mut Bencher)), - DynTestFn(Box), + DynTestFn(Box), DynBenchFn(Box), } @@ -303,6 +326,7 @@ pub fn test_main_static(tests: &[&TestDescAndFn]) { /// Invoked when unit tests terminate. Should panic if the unit /// Tests is considered a failure. By default, invokes `report()` /// and checks for a `0` result. +#[cfg(feature = "unstable")] pub fn assert_test_result(result: T) { let code = result.report(); if code != 0 { @@ -315,13 +339,6 @@ pub fn assert_test_result(result: T) { } } -#[derive(Copy, Clone, Debug)] -pub enum ColorConfig { - AutoColor, - AlwaysColor, - NeverColor, -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum OutputFormat { Pretty, @@ -347,7 +364,7 @@ pub struct TestOpts { pub bench_benchmarks: bool, pub logfile: Option, pub nocapture: bool, - pub color: ColorConfig, + pub color: ColorChoice, pub format: OutputFormat, pub test_threads: Option, pub skip: Vec, @@ -367,7 +384,7 @@ impl TestOpts { bench_benchmarks: false, logfile: None, nocapture: false, - color: ColorConfig::AutoColor, + color: ColorChoice::Auto, format: OutputFormat::Pretty, test_threads: None, skip: vec![], @@ -582,7 +599,7 @@ pub fn parse_opts(args: &[String]) -> Option { Ok(0) => { return Some(Err( "argument for --test-threads must not be 0".to_string() - )) + )); } Ok(n) => Some(n), Err(e) => { @@ -597,9 +614,9 @@ pub fn parse_opts(args: &[String]) -> Option { }; let color = match matches.opt_str("color").as_ref().map(|s| &**s) { - Some("auto") | None => ColorConfig::AutoColor, - Some("always") => ColorConfig::AlwaysColor, - Some("never") => ColorConfig::NeverColor, + Some("auto") | None => ColorChoice::Auto, + Some("always") => ColorChoice::Always, + Some("never") => ColorChoice::Never, Some(v) => { return Some(Err(format!( @@ -671,8 +688,9 @@ pub enum TestResult { unsafe impl Send for TestResult {} enum OutputLocation { - Pretty(Box), - Raw(T), + Pretty(termcolor::StandardStream), + #[allow(dead_code)] + Raw(T), // used in tests } impl Write for OutputLocation { @@ -823,10 +841,8 @@ pub fn list_tests_console( } } - let mut output = match term::stdout() { - None => OutputLocation::Raw(io::stdout()), - Some(t) => OutputLocation::Pretty(t), - }; + let mut output: OutputLocation> = + OutputLocation::Pretty(termcolor::StandardStream::stdout(opts.color)); let quiet = opts.format == OutputFormat::Terse; let mut st = ConsoleTestState::new(opts)?; @@ -935,11 +951,8 @@ pub fn run_tests_console( } } - let output = match term::stdout() { - None => OutputLocation::Raw(io::stdout()), - Some(t) => OutputLocation::Pretty(t), - }; - + let output: OutputLocation> = + OutputLocation::Pretty(termcolor::StandardStream::stdout(opts.color)); let max_name_len = tests .iter() .max_by_key(|t| len_if_padded(*t)) @@ -1023,9 +1036,9 @@ fn should_sort_failures_before_printing_them() { fn use_color(opts: &TestOpts) -> bool { match opts.color { - ColorConfig::AutoColor => !opts.nocapture && stdout_isatty(), - ColorConfig::AlwaysColor => true, - ColorConfig::NeverColor => false, + ColorChoice::Auto => !opts.nocapture && stdout_isatty(), + ColorChoice::Always | ColorChoice::AlwaysAnsi => true, + ColorChoice::Never => false, } } @@ -1460,6 +1473,7 @@ pub fn convert_benchmarks_to_tests( .collect() } +#[allow(clippy::redundant_closure)] pub fn run_test( opts: &TestOpts, force_ignore: bool, @@ -1471,7 +1485,7 @@ pub fn run_test( desc: TestDesc, monitor_ch: Sender, nocapture: bool, - testfn: Box, + mut testfn: Box, concurrency: Concurrent, ) { // Buffer for capturing standard I/O @@ -1484,16 +1498,16 @@ pub fn run_test( None } else { Some(( - io::set_print(Some(Box::new(Sink(data2.clone())))), - io::set_panic(Some(Box::new(Sink(data2)))), + crate::set_print(Some(Box::new(Sink(data2.clone())))), + crate::set_panic(Some(Box::new(Sink(data2)))), )) }; - let result = catch_unwind(AssertUnwindSafe(testfn)); + let result = catch_unwind(AssertUnwindSafe(move || testfn())); if let Some((printio, panicio)) = oldio { - io::set_print(printio); - io::set_panic(panicio); + crate::set_print(printio); + crate::set_panic(panicio); }; let test_result = calc_result(&desc, result); @@ -1546,8 +1560,8 @@ pub fn run_test( |harness| (benchfn)(harness), ); } - TestFn::DynTestFn(f) => { - let cb = move || __rust_begin_short_backtrace(f); + TestFn::DynTestFn(mut f) => { + let cb = move || __rust_begin_short_backtrace(|| f()); run_test_inner( desc, monitor_ch, @@ -1568,7 +1582,7 @@ pub fn run_test( /// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. #[inline(never)] -fn __rust_begin_short_backtrace(f: F) { +fn __rust_begin_short_backtrace(mut f: F) { f() } @@ -1667,13 +1681,30 @@ fn ns_from_dur(dur: Duration) -> u64 { dur.as_secs() * 1_000_000_000 + u64::from(dur.subsec_nanos()) } +#[inline(never)] +#[allow(clippy::needless_pass_by_value)] +fn black_box(x: T) -> T { + #[cfg(all(feature = "unstable", not(stage0)))] + { + std::hint::black_box(x) + } + #[cfg(any(not(feature = "unstable"), stage0))] + { + unsafe { + let v = std::ptr::read_volatile(&x as *const T); + std::mem::forget(x); + v + } + } +} + fn ns_iter_inner(inner: &mut F, k: u64) -> u64 where F: FnMut() -> T, { let start = Instant::now(); for _ in 0..k { - test::black_box(inner()); + black_box(inner()); } ns_from_dur(start.elapsed()) } @@ -1748,14 +1779,14 @@ where pub mod bench { use super::{ - BenchMode, BenchSamples, Bencher, MonitorMsg, Sender, Sink, TestDesc, - TestResult, + stats, BenchMode, BenchSamples, Bencher, MonitorMsg, Sender, Sink, + TestDesc, TestResult, + }; + use std::{ + cmp, + panic::{catch_unwind, AssertUnwindSafe}, + sync::{Arc, Mutex}, }; - use crate::stats; - use std::cmp; - use std::io; - use std::panic::{catch_unwind, AssertUnwindSafe}; - use std::sync::{Arc, Mutex}; pub fn benchmark( desc: TestDesc, @@ -1778,16 +1809,16 @@ pub mod bench { None } else { Some(( - io::set_print(Some(Box::new(Sink(data2.clone())))), - io::set_panic(Some(Box::new(Sink(data2)))), + crate::set_print(Some(Box::new(Sink(data2.clone())))), + crate::set_panic(Some(Box::new(Sink(data2)))), )) }; let result = catch_unwind(AssertUnwindSafe(|| bs.bench(f))); if let Some((printio, panicio)) = oldio { - io::set_print(printio); - io::set_panic(panicio); + crate::set_print(printio); + crate::set_panic(panicio); }; let test_result = match result { diff --git a/libtest/stats.rs b/src/stats.rs similarity index 98% rename from libtest/stats.rs rename to src/stats.rs index 45e0938..9b912e7 100644 --- a/libtest/stats.rs +++ b/src/stats.rs @@ -895,30 +895,30 @@ mod tests { fn test_sum_f64_between_ints_that_sum_to_0() { assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(), 1.2); } -} - -#[cfg(test)] -mod bench { - extern crate test; - use self::test::Bencher; - use crate::stats::Stats; - #[bench] - pub fn sum_three_items(b: &mut Bencher) { + #[test] + pub fn sum_three_items() { + let mut b = crate::Bencher { + mode: crate::BenchMode::Auto, + summary: None, + bytes: 1, + }; b.iter(|| { [1e20f64, 1.5f64, -1e20f64].sum(); }) } - #[bench] - pub fn sum_many_f64(b: &mut Bencher) { + + #[test] + pub fn sum_many_f64() { + let mut b = crate::Bencher { + mode: crate::BenchMode::Auto, + summary: None, + bytes: 1, + }; let nums = [-1e30f64, 1e60, 1e30, 1.0, -1e60]; let v = (0..500).map(|i| nums[i % 5]).collect::>(); - b.iter(|| { v.sum(); }) } - - #[bench] - pub fn no_iter(_: &mut Bencher) {} }