|
1 |
| -//! Support code for rustc's built in unit-test and micro-benchmarking |
2 |
| -//! framework. |
3 |
| -//! |
4 |
| -//! Almost all user code will only be interested in `Bencher` and |
5 |
| -//! `black_box`. All other interactions (such as writing tests and |
6 |
| -//! benchmarks themselves) should be done via the `#[test]` and |
7 |
| -//! `#[bench]` attributes. |
8 |
| -//! |
9 |
| -//! See the [Testing Chapter](../book/ch11-00-testing.html) of the book for more details. |
10 |
| -
|
11 |
| -// Currently, not much of this is meant for users. It is intended to |
12 |
| -// support the simplest interface possible for representing and |
13 |
| -// running tests while providing a base that other test frameworks may |
14 |
| -// build off of. |
15 |
| - |
16 |
| -// N.B., this is also specified in this crate's Cargo.toml, but libsyntax contains logic specific to |
17 |
| -// this crate, which relies on this attribute (rather than the value of `--crate-name` passed by |
18 |
| -// cargo) to detect this crate. |
19 |
| - |
20 |
| -#![deny(rust_2018_idioms)] |
21 |
| -#![crate_name = "libtest"] |
22 |
| -#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))] |
23 |
| -#![unstable(feature = "test", issue = "27812")] |
24 |
| -#![feature(asm)] |
25 |
| -#![feature(fnbox)] |
| 1 | +//! Rust's built-in unit-test and micro-benchmarking framework. |
26 | 2 | #![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc, rustc_private))]
|
27 |
| -#![feature(nll)] |
| 3 | +#![feature(fnbox)] |
28 | 4 | #![feature(set_stdio)]
|
29 | 5 | #![feature(panic_unwind)]
|
30 |
| -#![feature(staged_api)] |
31 | 6 | #![feature(termination_trait_lib)]
|
32 | 7 | #![feature(test)]
|
| 8 | +#![deny(rust_2018_idioms)] |
33 | 9 |
|
34 | 10 | use getopts;
|
| 11 | + |
| 12 | +extern crate test; |
| 13 | + |
35 | 14 | #[cfg(any(unix, target_os = "cloudabi"))]
|
36 | 15 | extern crate libc;
|
37 |
| -use rustc_term as term; |
38 | 16 |
|
39 | 17 | // FIXME(#54291): rustc and/or LLVM don't yet support building with panic-unwind
|
40 | 18 | // on aarch64-pc-windows-msvc, so we don't link libtest against
|
@@ -75,15 +53,6 @@ use std::time::{Duration, Instant};
|
75 | 53 | const TEST_WARN_TIMEOUT_S: u64 = 60;
|
76 | 54 | const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in quiet mode
|
77 | 55 |
|
78 |
| -// to be used by rustc to compile tests in libtest |
79 |
| -pub mod test { |
80 |
| - pub use crate::{ |
81 |
| - assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static, |
82 |
| - Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, ShouldPanic, |
83 |
| - StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName, TestOpts, |
84 |
| - TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk, |
85 |
| - }; |
86 |
| -} |
87 | 56 |
|
88 | 57 | mod formatters;
|
89 | 58 | pub mod stats;
|
@@ -1585,24 +1554,6 @@ impl MetricMap {
|
1585 | 1554 |
|
1586 | 1555 | // Benchmarking
|
1587 | 1556 |
|
1588 |
| -/// A function that is opaque to the optimizer, to allow benchmarks to |
1589 |
| -/// pretend to use outputs to assist in avoiding dead-code |
1590 |
| -/// elimination. |
1591 |
| -/// |
1592 |
| -/// This function is a no-op, and does not even read from `dummy`. |
1593 |
| -#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] |
1594 |
| -pub fn black_box<T>(dummy: T) -> T { |
1595 |
| - // we need to "use" the argument in some way LLVM can't |
1596 |
| - // introspect. |
1597 |
| - unsafe { asm!("" : : "r"(&dummy)) } |
1598 |
| - dummy |
1599 |
| -} |
1600 |
| -#[cfg(any(target_arch = "asmjs", target_arch = "wasm32"))] |
1601 |
| -#[inline(never)] |
1602 |
| -pub fn black_box<T>(dummy: T) -> T { |
1603 |
| - dummy |
1604 |
| -} |
1605 |
| - |
1606 | 1557 | impl Bencher {
|
1607 | 1558 | /// Callback for benchmark functions to run in their body.
|
1608 | 1559 | pub fn iter<T, F>(&mut self, mut inner: F)
|
@@ -1636,7 +1587,7 @@ where
|
1636 | 1587 | {
|
1637 | 1588 | let start = Instant::now();
|
1638 | 1589 | for _ in 0..k {
|
1639 |
| - black_box(inner()); |
| 1590 | + test::black_box(inner()); |
1640 | 1591 | }
|
1641 | 1592 | return ns_from_dur(start.elapsed());
|
1642 | 1593 | }
|
@@ -1792,11 +1743,12 @@ pub mod bench {
|
1792 | 1743 | #[cfg(test)]
|
1793 | 1744 | mod tests {
|
1794 | 1745 | use crate::bench;
|
1795 |
| - use crate::test::{ |
| 1746 | + use crate::{ |
1796 | 1747 | filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
|
1797 | 1748 | ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed, TrFailedMsg,
|
1798 | 1749 | TrIgnored, TrOk,
|
1799 | 1750 | };
|
| 1751 | + use crate::test::black_box; |
1800 | 1752 | use crate::Bencher;
|
1801 | 1753 | use crate::Concurrent;
|
1802 | 1754 | use std::sync::mpsc::channel;
|
|
0 commit comments