Skip to content
This repository was archived by the owner on Aug 12, 2021. It is now read-only.

Commit e8e843c

Browse files
committed
Import black_box from the test crate
1 parent 93c898d commit e8e843c

File tree

1 file changed

+9
-57
lines changed

1 file changed

+9
-57
lines changed

libtest/lib.rs

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
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.
262
#![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc, rustc_private))]
27-
#![feature(nll)]
3+
#![feature(fnbox)]
284
#![feature(set_stdio)]
295
#![feature(panic_unwind)]
30-
#![feature(staged_api)]
316
#![feature(termination_trait_lib)]
327
#![feature(test)]
8+
#![deny(rust_2018_idioms)]
339

3410
use getopts;
11+
12+
extern crate test;
13+
3514
#[cfg(any(unix, target_os = "cloudabi"))]
3615
extern crate libc;
37-
use rustc_term as term;
3816

3917
// FIXME(#54291): rustc and/or LLVM don't yet support building with panic-unwind
4018
// on aarch64-pc-windows-msvc, so we don't link libtest against
@@ -75,15 +53,6 @@ use std::time::{Duration, Instant};
7553
const TEST_WARN_TIMEOUT_S: u64 = 60;
7654
const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in quiet mode
7755

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-
}
8756

8857
mod formatters;
8958
pub mod stats;
@@ -1585,24 +1554,6 @@ impl MetricMap {
15851554

15861555
// Benchmarking
15871556

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-
16061557
impl Bencher {
16071558
/// Callback for benchmark functions to run in their body.
16081559
pub fn iter<T, F>(&mut self, mut inner: F)
@@ -1636,7 +1587,7 @@ where
16361587
{
16371588
let start = Instant::now();
16381589
for _ in 0..k {
1639-
black_box(inner());
1590+
test::black_box(inner());
16401591
}
16411592
return ns_from_dur(start.elapsed());
16421593
}
@@ -1792,11 +1743,12 @@ pub mod bench {
17921743
#[cfg(test)]
17931744
mod tests {
17941745
use crate::bench;
1795-
use crate::test::{
1746+
use crate::{
17961747
filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
17971748
ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed, TrFailedMsg,
17981749
TrIgnored, TrOk,
17991750
};
1751+
use crate::test::black_box;
18001752
use crate::Bencher;
18011753
use crate::Concurrent;
18021754
use std::sync::mpsc::channel;

0 commit comments

Comments
 (0)