2019-03-02 12:40:15 -06:00
|
|
|
//! Support code for rustc's built in unit-test and micro-benchmarking
|
|
|
|
//! framework.
|
|
|
|
//!
|
|
|
|
//! Almost all user code will only be interested in `Bencher` and
|
|
|
|
//! `black_box`. All other interactions (such as writing tests and
|
|
|
|
//! benchmarks themselves) should be done via the `#[test]` and
|
|
|
|
//! `#[bench]` attributes.
|
|
|
|
//!
|
|
|
|
//! See the [Testing Chapter](../book/ch11-00-testing.html) of the book for more details.
|
|
|
|
|
|
|
|
#![crate_name = "test"]
|
|
|
|
#![unstable(feature = "test", issue = "27812")]
|
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
|
|
|
|
test(attr(deny(warnings))))]
|
2019-03-04 11:04:08 -06:00
|
|
|
#![feature(asm)]
|
2019-03-03 10:39:57 -06:00
|
|
|
#![feature(staged_api)]
|
2019-03-03 08:50:52 -06:00
|
|
|
#![feature(test)]
|
2019-03-02 12:40:15 -06:00
|
|
|
|
|
|
|
extern crate libtest;
|
2019-03-04 11:17:41 -06:00
|
|
|
|
|
|
|
// FIXME: we should be more explicit about the exact APIs that we
|
|
|
|
// export to users.
|
2019-03-04 11:06:07 -06:00
|
|
|
pub use libtest::{
|
|
|
|
assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static,
|
|
|
|
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, ShouldPanic,
|
|
|
|
StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName, TestOpts,
|
2019-03-04 11:17:41 -06:00
|
|
|
TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk, stats::Summary
|
2019-03-04 11:06:07 -06:00
|
|
|
};
|
2019-03-04 11:04:08 -06:00
|
|
|
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 03:15:52 -05:00
|
|
|
pub use std::hint::black_box;
|
2019-03-19 19:22:19 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::Bencher;
|
|
|
|
use libtest::stats::Stats;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn sum_three_items(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
[1e20f64, 1.5f64, -1e20f64].sum();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn sum_many_f64(b: &mut Bencher) {
|
|
|
|
let nums = [-1e30f64, 1e60, 1e30, 1.0, -1e60];
|
|
|
|
let v = (0..500).map(|i| nums[i % 5]).collect::<Vec<_>>();
|
|
|
|
b.iter(|| {
|
|
|
|
v.sum();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
pub fn no_iter(_: &mut Bencher) {}
|
|
|
|
}
|