diff options
| author | Trevor Gross <tmgross@umich.edu> | 2025-02-05 21:18:33 +0000 |
|---|---|---|
| committer | Trevor Gross <t.gross35@gmail.com> | 2025-02-05 16:30:11 -0600 |
| commit | e01ce5d53a929019a8c9655c7dee4c74a911d465 (patch) | |
| tree | 51e086d54b6c69a7da86dc385341bd30d8c4b65f | |
| parent | 9458abd20439c75584bd1b33a1ef7b6891ab5e8a (diff) | |
| download | rust-e01ce5d53a929019a8c9655c7dee4c74a911d465.tar.gz rust-e01ce5d53a929019a8c9655c7dee4c74a911d465.zip | |
Commonize the signature for all instances of `get_test_cases`
In order to make these more interchangeable in more places, always return `(impl Iterator, u64)`. This will facilitate using other generators for extensive tests.
6 files changed, 45 insertions, 33 deletions
diff --git a/library/compiler-builtins/libm/crates/libm-test/benches/random.rs b/library/compiler-builtins/libm/crates/libm-test/benches/random.rs index 56d288c332e..66486a56a2a 100644 --- a/library/compiler-builtins/libm/crates/libm-test/benches/random.rs +++ b/library/compiler-builtins/libm/crates/libm-test/benches/random.rs @@ -54,7 +54,7 @@ where let ctx = CheckCtx::new(Op::IDENTIFIER, CheckBasis::Musl, GeneratorKind::Random); let benchvec: Vec<_> = - random::get_test_cases::<Op::RustArgs>(&ctx).take(BENCH_ITER_ITEMS).collect(); + random::get_test_cases::<Op::RustArgs>(&ctx).0.take(BENCH_ITER_ITEMS).collect(); // Perform a sanity check that we are benchmarking the same thing // Don't test against musl if it is not available diff --git a/library/compiler-builtins/libm/crates/libm-test/examples/plot_domains.rs b/library/compiler-builtins/libm/crates/libm-test/examples/plot_domains.rs index fb7b854df85..441889c6941 100644 --- a/library/compiler-builtins/libm/crates/libm-test/examples/plot_domains.rs +++ b/library/compiler-builtins/libm/crates/libm-test/examples/plot_domains.rs @@ -58,7 +58,13 @@ where let mut ctx = CheckCtx::new(Op::IDENTIFIER, CheckBasis::Mpfr, GeneratorKind::QuickSpaced); plot_one_generator(out_dir, &ctx, "logspace", config, spaced::get_test_cases::<Op>(&ctx).0); ctx.gen_kind = GeneratorKind::EdgeCases; - plot_one_generator(out_dir, &ctx, "edge_cases", config, edge_cases::get_test_cases::<Op>(&ctx)); + plot_one_generator( + out_dir, + &ctx, + "edge_cases", + config, + edge_cases::get_test_cases::<Op>(&ctx).0, + ); } /// Plot the output of a single generator. diff --git a/library/compiler-builtins/libm/crates/libm-test/src/gen/edge_cases.rs b/library/compiler-builtins/libm/crates/libm-test/src/gen/edge_cases.rs index d4014bdb360..8de954ae332 100644 --- a/library/compiler-builtins/libm/crates/libm-test/src/gen/edge_cases.rs +++ b/library/compiler-builtins/libm/crates/libm-test/src/gen/edge_cases.rs @@ -9,7 +9,7 @@ use crate::{CheckCtx, FloatExt, MathOp, test_log}; /// Generate a sequence of edge cases, e.g. numbers near zeroes and infiniteis. pub trait EdgeCaseInput<Op> { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> + Send; + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self> + Send, u64); } /// Create a list of values around interesting points (infinities, zeroes, NaNs). @@ -140,10 +140,10 @@ macro_rules! impl_edge_case_input { where Op: MathOp<RustArgs = Self, FTy = $fty>, { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0); let iter0 = iter0.map(|v| (v,)); - KnownSize::new(iter0, steps0) + (iter0, steps0) } } @@ -151,13 +151,13 @@ macro_rules! impl_edge_case_input { where Op: MathOp<RustArgs = Self, FTy = $fty>, { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0); let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1); let iter = iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second))); let count = steps0.checked_mul(steps1).unwrap(); - KnownSize::new(iter, count) + (iter, count) } } @@ -165,7 +165,7 @@ macro_rules! impl_edge_case_input { where Op: MathOp<RustArgs = Self, FTy = $fty>, { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0); let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1); let (iter2, steps2) = float_edge_cases::<Op>(ctx, 2); @@ -177,7 +177,7 @@ macro_rules! impl_edge_case_input { }); let count = steps0.checked_mul(steps1).unwrap().checked_mul(steps2).unwrap(); - KnownSize::new(iter, count) + (iter, count) } } @@ -185,7 +185,7 @@ macro_rules! impl_edge_case_input { where Op: MathOp<RustArgs = Self, FTy = $fty>, { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let (iter0, steps0) = int_edge_cases(ctx, 0); let (iter1, steps1) = float_edge_cases::<Op>(ctx, 1); @@ -193,7 +193,7 @@ macro_rules! impl_edge_case_input { iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second))); let count = steps0.checked_mul(steps1).unwrap(); - KnownSize::new(iter, count) + (iter, count) } } @@ -201,7 +201,7 @@ macro_rules! impl_edge_case_input { where Op: MathOp<RustArgs = Self, FTy = $fty>, { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let (iter0, steps0) = float_edge_cases::<Op>(ctx, 0); let (iter1, steps1) = int_edge_cases(ctx, 1); @@ -209,7 +209,7 @@ macro_rules! impl_edge_case_input { iter0.flat_map(move |first| iter1.clone().map(move |second| (first, second))); let count = steps0.checked_mul(steps1).unwrap(); - KnownSize::new(iter, count) + (iter, count) } } }; @@ -224,10 +224,13 @@ impl_edge_case_input!(f128); pub fn get_test_cases<Op>( ctx: &CheckCtx, -) -> impl ExactSizeIterator<Item = Op::RustArgs> + use<'_, Op> +) -> (impl Iterator<Item = Op::RustArgs> + Send + use<'_, Op>, u64) where Op: MathOp, Op::RustArgs: EdgeCaseInput<Op>, { - Op::RustArgs::get_cases(ctx) + let (iter, count) = Op::RustArgs::get_cases(ctx); + + // Wrap in `KnownSize` so we get an assertion if the cuunt is wrong. + (KnownSize::new(iter, count), count) } diff --git a/library/compiler-builtins/libm/crates/libm-test/src/gen/random.rs b/library/compiler-builtins/libm/crates/libm-test/src/gen/random.rs index 56c39981a44..5b127f38d09 100644 --- a/library/compiler-builtins/libm/crates/libm-test/src/gen/random.rs +++ b/library/compiler-builtins/libm/crates/libm-test/src/gen/random.rs @@ -26,8 +26,8 @@ pub(crate) static SEED: LazyLock<[u8; 32]> = LazyLock::new(|| { }); /// Generate a sequence of random values of this type. -pub trait RandomInput { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self>; +pub trait RandomInput: Sized { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self> + Send, u64); } /// Generate a sequence of deterministically random floats. @@ -51,25 +51,25 @@ fn random_ints(count: u64, range: RangeInclusive<i32>) -> impl Iterator<Item = i macro_rules! impl_random_input { ($fty:ty) => { impl RandomInput for ($fty,) { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let count = iteration_count(ctx, 0); let iter = random_floats(count).map(|f: $fty| (f,)); - KnownSize::new(iter, count) + (iter, count) } } impl RandomInput for ($fty, $fty) { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let count0 = iteration_count(ctx, 0); let count1 = iteration_count(ctx, 1); let iter = random_floats(count0) .flat_map(move |f1: $fty| random_floats(count1).map(move |f2: $fty| (f1, f2))); - KnownSize::new(iter, count0 * count1) + (iter, count0 * count1) } } impl RandomInput for ($fty, $fty, $fty) { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let count0 = iteration_count(ctx, 0); let count1 = iteration_count(ctx, 1); let count2 = iteration_count(ctx, 2); @@ -78,30 +78,30 @@ macro_rules! impl_random_input { random_floats(count2).map(move |f3: $fty| (f1, f2, f3)) }) }); - KnownSize::new(iter, count0 * count1 * count2) + (iter, count0 * count1 * count2) } } impl RandomInput for (i32, $fty) { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let count0 = iteration_count(ctx, 0); let count1 = iteration_count(ctx, 1); let range0 = int_range(ctx, 0); let iter = random_ints(count0, range0) .flat_map(move |f1: i32| random_floats(count1).map(move |f2: $fty| (f1, f2))); - KnownSize::new(iter, count0 * count1) + (iter, count0 * count1) } } impl RandomInput for ($fty, i32) { - fn get_cases(ctx: &CheckCtx) -> impl ExactSizeIterator<Item = Self> { + fn get_cases(ctx: &CheckCtx) -> (impl Iterator<Item = Self>, u64) { let count0 = iteration_count(ctx, 0); let count1 = iteration_count(ctx, 1); let range1 = int_range(ctx, 1); let iter = random_floats(count0).flat_map(move |f1: $fty| { random_ints(count1, range1.clone()).map(move |f2: i32| (f1, f2)) }); - KnownSize::new(iter, count0 * count1) + (iter, count0 * count1) } } }; @@ -117,6 +117,9 @@ impl_random_input!(f128); /// Create a test case iterator. pub fn get_test_cases<RustArgs: RandomInput>( ctx: &CheckCtx, -) -> impl Iterator<Item = RustArgs> + use<'_, RustArgs> { - RustArgs::get_cases(ctx) +) -> (impl Iterator<Item = RustArgs> + Send + use<'_, RustArgs>, u64) { + let (iter, count) = RustArgs::get_cases(ctx); + + // Wrap in `KnownSize` so we get an assertion if the cuunt is wrong. + (KnownSize::new(iter, count), count) } diff --git a/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs b/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs index 0b0a9f09721..c8beaffc3af 100644 --- a/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs +++ b/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs @@ -39,7 +39,7 @@ macro_rules! musl_tests { fn [< musl_random_ $fn_name >]() { type Op = libm_test::op::$fn_name::Routine; let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::Random); - let cases = random::get_test_cases::<<Op as MathOp>::RustArgs>(&ctx); + let cases = random::get_test_cases::<<Op as MathOp>::RustArgs>(&ctx).0; musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name); } @@ -48,7 +48,7 @@ macro_rules! musl_tests { fn [< musl_edge_case_ $fn_name >]() { type Op = libm_test::op::$fn_name::Routine; let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::EdgeCases); - let cases = edge_cases::get_test_cases::<Op>(&ctx); + let cases = edge_cases::get_test_cases::<Op>(&ctx).0; musl_runner::<Op>(&ctx, cases, musl_math_sys::$fn_name); } diff --git a/library/compiler-builtins/libm/crates/libm-test/tests/multiprecision.rs b/library/compiler-builtins/libm/crates/libm-test/tests/multiprecision.rs index 761ca1f851e..0d5c5e60c20 100644 --- a/library/compiler-builtins/libm/crates/libm-test/tests/multiprecision.rs +++ b/library/compiler-builtins/libm/crates/libm-test/tests/multiprecision.rs @@ -29,7 +29,7 @@ macro_rules! mp_tests { fn [< mp_random_ $fn_name >]() { type Op = libm_test::op::$fn_name::Routine; let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::Random); - let cases = random::get_test_cases::<<Op as MathOp>::RustArgs>(&ctx); + let cases = random::get_test_cases::<<Op as MathOp>::RustArgs>(&ctx).0; mp_runner::<Op>(&ctx, cases); } @@ -38,7 +38,7 @@ macro_rules! mp_tests { fn [< mp_edge_case_ $fn_name >]() { type Op = libm_test::op::$fn_name::Routine; let ctx = CheckCtx::new(Op::IDENTIFIER, BASIS, GeneratorKind::EdgeCases); - let cases = edge_cases::get_test_cases::<Op>(&ctx); + let cases = edge_cases::get_test_cases::<Op>(&ctx).0; mp_runner::<Op>(&ctx, cases); } |
