mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
add StaticBenchAsTestFn and DynBenchAsTestFn to convert benches to tests
Before this commit, both static and dynamic benches were converted to a DynTestFn, with a boxed closure that ran the benchmarks exactly once. While this worked, it conflicted with -Z panic-abort-tests as the flag does not support dynamic tests. With this change, a StaticBenchFn is converted to a StaticBenchAsTestFn, avoiding any dynamic test creation. DynBenchFn is also converted to DynBenchAsTestFn for completeness.
This commit is contained in:
parent
fa451fe6db
commit
2f7fd2e5f7
@ -199,7 +199,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
|
||||
let TestDescAndFn { desc, testfn } = test;
|
||||
|
||||
let fntype = match testfn {
|
||||
StaticTestFn(..) | DynTestFn(..) => {
|
||||
StaticTestFn(..) | DynTestFn(..) | StaticBenchAsTestFn(..) | DynBenchAsTestFn(..) => {
|
||||
st.tests += 1;
|
||||
"test"
|
||||
}
|
||||
|
@ -240,16 +240,6 @@ impl FilteredTests {
|
||||
self.tests.push((TestId(self.next_id), test));
|
||||
self.next_id += 1;
|
||||
}
|
||||
fn add_bench_as_test(
|
||||
&mut self,
|
||||
desc: TestDesc,
|
||||
benchfn: impl Fn(&mut Bencher) -> Result<(), String> + Send + 'static,
|
||||
) {
|
||||
let testfn = DynTestFn(Box::new(move || {
|
||||
bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
|
||||
}));
|
||||
self.add_test(desc, testfn);
|
||||
}
|
||||
fn total_len(&self) -> usize {
|
||||
self.tests.len() + self.benches.len()
|
||||
}
|
||||
@ -307,14 +297,14 @@ where
|
||||
if opts.bench_benchmarks {
|
||||
filtered.add_bench(desc, DynBenchFn(benchfn));
|
||||
} else {
|
||||
filtered.add_bench_as_test(desc, benchfn);
|
||||
filtered.add_test(desc, DynBenchAsTestFn(benchfn));
|
||||
}
|
||||
}
|
||||
StaticBenchFn(benchfn) => {
|
||||
if opts.bench_benchmarks {
|
||||
filtered.add_bench(desc, StaticBenchFn(benchfn));
|
||||
} else {
|
||||
filtered.add_bench_as_test(desc, benchfn);
|
||||
filtered.add_test(desc, StaticBenchAsTestFn(benchfn));
|
||||
}
|
||||
}
|
||||
testfn => {
|
||||
@ -525,12 +515,8 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd
|
||||
.into_iter()
|
||||
.map(|x| {
|
||||
let testfn = match x.testfn {
|
||||
DynBenchFn(benchfn) => DynTestFn(Box::new(move || {
|
||||
bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
|
||||
})),
|
||||
StaticBenchFn(benchfn) => DynTestFn(Box::new(move || {
|
||||
bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
|
||||
})),
|
||||
DynBenchFn(benchfn) => DynBenchAsTestFn(benchfn),
|
||||
StaticBenchFn(benchfn) => StaticBenchAsTestFn(benchfn),
|
||||
f => f,
|
||||
};
|
||||
TestDescAndFn { desc: x.desc, testfn }
|
||||
|
@ -85,8 +85,10 @@ impl fmt::Display for TestName {
|
||||
pub enum TestFn {
|
||||
StaticTestFn(fn() -> Result<(), String>),
|
||||
StaticBenchFn(fn(&mut Bencher) -> Result<(), String>),
|
||||
StaticBenchAsTestFn(fn(&mut Bencher) -> Result<(), String>),
|
||||
DynTestFn(Box<dyn FnOnce() -> Result<(), String> + Send>),
|
||||
DynBenchFn(Box<dyn Fn(&mut Bencher) -> Result<(), String> + Send>),
|
||||
DynBenchAsTestFn(Box<dyn Fn(&mut Bencher) -> Result<(), String> + Send>),
|
||||
}
|
||||
|
||||
impl TestFn {
|
||||
@ -94,8 +96,10 @@ impl TestFn {
|
||||
match *self {
|
||||
StaticTestFn(..) => PadNone,
|
||||
StaticBenchFn(..) => PadOnRight,
|
||||
StaticBenchAsTestFn(..) => PadNone,
|
||||
DynTestFn(..) => PadNone,
|
||||
DynBenchFn(..) => PadOnRight,
|
||||
DynBenchAsTestFn(..) => PadNone,
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,8 +107,10 @@ impl TestFn {
|
||||
match self {
|
||||
StaticTestFn(f) => Runnable::Test(RunnableTest::Static(f)),
|
||||
StaticBenchFn(f) => Runnable::Bench(RunnableBench::Static(f)),
|
||||
StaticBenchAsTestFn(f) => Runnable::Test(RunnableTest::StaticBenchAsTest(f)),
|
||||
DynTestFn(f) => Runnable::Test(RunnableTest::Dynamic(f)),
|
||||
DynBenchFn(f) => Runnable::Bench(RunnableBench::Dynamic(f)),
|
||||
DynBenchAsTestFn(f) => Runnable::Test(RunnableTest::DynamicBenchAsTest(f)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -114,8 +120,10 @@ impl fmt::Debug for TestFn {
|
||||
f.write_str(match *self {
|
||||
StaticTestFn(..) => "StaticTestFn(..)",
|
||||
StaticBenchFn(..) => "StaticBenchFn(..)",
|
||||
StaticBenchAsTestFn(..) => "StaticBenchAsTestFn(..)",
|
||||
DynTestFn(..) => "DynTestFn(..)",
|
||||
DynBenchFn(..) => "DynBenchFn(..)",
|
||||
DynBenchAsTestFn(..) => "DynBenchAsTestFn(..)",
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -128,6 +136,8 @@ pub(crate) enum Runnable {
|
||||
pub(crate) enum RunnableTest {
|
||||
Static(fn() -> Result<(), String>),
|
||||
Dynamic(Box<dyn FnOnce() -> Result<(), String> + Send>),
|
||||
StaticBenchAsTest(fn(&mut Bencher) -> Result<(), String>),
|
||||
DynamicBenchAsTest(Box<dyn Fn(&mut Bencher) -> Result<(), String> + Send>),
|
||||
}
|
||||
|
||||
impl RunnableTest {
|
||||
@ -135,13 +145,21 @@ impl RunnableTest {
|
||||
match self {
|
||||
RunnableTest::Static(f) => __rust_begin_short_backtrace(f),
|
||||
RunnableTest::Dynamic(f) => __rust_begin_short_backtrace(f),
|
||||
RunnableTest::StaticBenchAsTest(f) => {
|
||||
crate::bench::run_once(|b| __rust_begin_short_backtrace(|| f(b)))
|
||||
}
|
||||
RunnableTest::DynamicBenchAsTest(f) => {
|
||||
crate::bench::run_once(|b| __rust_begin_short_backtrace(|| f(b)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_dynamic(&self) -> bool {
|
||||
match self {
|
||||
RunnableTest::Static(_) => false,
|
||||
RunnableTest::StaticBenchAsTest(_) => false,
|
||||
RunnableTest::Dynamic(_) => true,
|
||||
RunnableTest::DynamicBenchAsTest(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user