Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 260 additions & 15 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ strip = true
debug = 0
strip = true

# Bigint libraries are slow without optimization, speed up testing
# Bigint libraries are slow without optimization, speed up testing. Note that this doesn't affect
# its dependencies, it should still be run in release mode where possible.
[profile.dev.package.test-float-parse]
opt-level = 3

Expand All @@ -92,4 +93,3 @@ codegen-units = 1
# If you want to use a crate with local modifications, you can set a path or git dependency here.
# For git dependencies, also add your source to ALLOWED_SOURCES in src/tools/tidy/src/extdeps.rs.
#[patch.crates-io]

19 changes: 19 additions & 0 deletions src/bootstrap/src/utils/proc_macro_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
/// See <https://github.com/rust-lang/rust/issues/134863>
pub static CRATES: &[&str] = &[
// tidy-alphabetical-start
"allocator-api2",
"anyhow",
"askama_derive",
"askama_parser",
"basic-toml",
"bitflags",
"block-buffer",
"bumpalo",
"cfg-if",
Expand All @@ -24,24 +27,32 @@ pub static CRATES: &[&str] = &[
"generic-array",
"hashbrown",
"heck",
"id-arena",
"ident_case",
"indexmap",
"intl-memoizer",
"intl_pluralrules",
"itoa",
"leb128fmt",
"libc",
"log",
"memchr",
"minimal-lexical",
"nom",
"pest",
"pest_generator",
"pest_meta",
"prettyplease",
"proc-macro2",
"quote",
"rustc-hash",
"ryu",
"self_cell",
"semver",
"serde",
"serde_core",
"serde_derive_internals",
"serde_json",
"sha2",
"smallvec",
"stable_deref_trait",
Expand All @@ -57,10 +68,18 @@ pub static CRATES: &[&str] = &[
"unic-langid-impl",
"unic-langid-macros",
"unicode-ident",
"unicode-xid",
"version_check",
"wasm-bindgen-macro-support",
"wasm-bindgen-shared",
"wasm-encoder",
"wasm-metadata",
"wasmparser",
"winnow",
"wit-bindgen-core",
"wit-bindgen-rust",
"wit-component",
"wit-parser",
"yoke",
"zerofrom",
"zerovec",
Expand Down
8 changes: 4 additions & 4 deletions src/tools/test-float-parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ edition = "2024"
publish = false

[dependencies]
indicatif = { version = "0.17.8", default-features = false }
indicatif = { version = "0.18.4", default-features = false }
num = "0.4.3"
rand = "0.9.0"
rand_chacha = "0.9.0"
rayon = "1"
rand = "0.10.0"
rand_chacha = "0.10.0"
rayon = "1.11.0"

[lib]
name = "test_float_parse"
Expand Down
9 changes: 9 additions & 0 deletions src/tools/test-float-parse/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::env;

fn main() {
// Forward the opt level so we can warn if the tests are going to be slow.
let opt = env::var("OPT_LEVEL").expect("OPT_LEVEL unset");
let profile = env::var("PROFILE").expect("PROFILE unset");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to tell Cargo that we read these environment variables, or is that assumed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've always assumed that these trigger a rerun, but I actually have no idea. Asked about this at rust-lang/cargo#16800.

println!("cargo::rustc-env=OPT_LEVEL={opt}");
println!("cargo::rustc-env=PROFILE={profile}");
}
4 changes: 2 additions & 2 deletions src/tools/test-float-parse/src/gen_/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::marker::PhantomData;
use std::ops::Range;
use std::sync::Mutex;

use rand::Rng;
use rand::RngExt;
use rand::distr::{Distribution, StandardUniform};
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng;
Expand Down Expand Up @@ -62,7 +62,7 @@ where
}

fn new() -> Self {
let rng = ChaCha8Rng::from_seed(SEED);
let rng = ChaCha8Rng::from_seed(SEED.0);

Self { iter: 0..Self::total_tests(), rng, marker: PhantomData }
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/test-float-parse/src/gen_/many_digits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::marker::PhantomData;
use std::ops::{Range, RangeInclusive};

use rand::distr::{Distribution, Uniform};
use rand::{Rng, SeedableRng};
use rand::{RngExt, SeedableRng};
use rand_chacha::ChaCha8Rng;

use crate::{Float, Generator, SEED};
Expand Down Expand Up @@ -39,7 +39,7 @@ impl<F: Float> Generator<F> for RandDigits<F> {
}

fn new() -> Self {
let rng = ChaCha8Rng::from_seed(SEED);
let rng = ChaCha8Rng::from_seed(SEED.0);
let range = Uniform::try_from(0..10).unwrap();

Self { rng, iter: 0..ITERATIONS, uniform: range, marker: PhantomData }
Expand Down
47 changes: 42 additions & 5 deletions src/tools/test-float-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use std::any::type_name;
use std::cmp::min;
use std::ops::RangeInclusive;
use std::process::ExitCode;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::{fmt, time};
use std::sync::{LazyLock, OnceLock};
use std::{env, fmt, time};

use rand::TryRng;
use rand::distr::{Distribution, StandardUniform};
use rand::rngs::SysRng;
use rayon::prelude::*;
use time::{Duration, Instant};
use traits::{Float, Generator, Int};
Expand Down Expand Up @@ -44,8 +46,41 @@ const MAX_BITS_FOR_EXHAUUSTIVE: u32 = 32;
/// `--skip-huge`.
const HUGE_TEST_CUTOFF: u64 = 5_000_000;

/// Seed for tests that use a deterministic RNG.
const SEED: [u8; 32] = *b"3.141592653589793238462643383279";
const SEED_ENV: &str = "TEST_FLOAT_PARSE_SEED";

/// Seed for tests that use a deterministic RNG, and its b64 representation for printing. Taken
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe save the dependency and just hex-dump? Should be fairly easy with something like {:#032x}{:032x} with u128...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, I did this.

/// from env if provided,
static SEED: LazyLock<([u8; 32], Box<str>)> = LazyLock::new(|| {
let seed = match env::var(SEED_ENV) {
Ok(s) => {
seed_from_str(&s).unwrap_or_else(|| panic!("{SEED_ENV} must be 32 bytes, hex encoded"))
}
Err(_) => {
let mut seed = [0u8; 32];
SysRng.try_fill_bytes(&mut seed).unwrap();
seed
}
};

let lo = u128::from_le_bytes(seed[..16].try_into().unwrap());
let hi = u128::from_le_bytes(seed[16..].try_into().unwrap());
let encoded = format!("{hi:032x}{lo:032x}").into_boxed_str();

(seed, encoded)
});

fn seed_from_str(s: &str) -> Option<[u8; 32]> {
let hi = s.get(..32)?;
let hi = u128::from_str_radix(hi, 16).ok()?;
let lo = s.get(32..)?;
let lo = u128::from_str_radix(lo, 16).ok()?;

let mut out = [0u8; 32];
out[..16].copy_from_slice(&lo.to_le_bytes());
out[16..].copy_from_slice(&hi.to_le_bytes());

Some(out)
}

/// Global configuration.
#[derive(Debug)]
Expand Down Expand Up @@ -78,8 +113,10 @@ pub fn run(cfg: Config, include: &[String], exclude: &[String]) -> ExitCode {
let threads = std::thread::available_parallelism().map(Into::into).unwrap_or(0) * 3 / 2;
rayon::ThreadPoolBuilder::new().num_threads(threads).build_global().unwrap();

println!("Starting test runner");
println!("Using {SEED_ENV}={}`", SEED.1);
let mut tests = register_tests(&cfg);
println!("registered");
println!("Tests registered");
let initial_tests: Vec<_> = tests.iter().map(|t| t.name.clone()).collect();

let unmatched: Vec<_> = include
Expand Down
16 changes: 14 additions & 2 deletions src/tools/test-float-parse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ enum ArgMode {
}

fn main() -> ExitCode {
if cfg!(debug_assertions) {
let opt = env!("OPT_LEVEL");
let profile = env!("PROFILE");

println!("Crate config:");
println!(" opt level: {opt}");
println!(" profile: {profile}");
println!(" debug assertions: {}", cfg!(debug_assertions));

if !matches!(opt, "2" | "3") || profile != "release" {
// Warn for `profile != release` because optimizations being on for this crate (always,
// via the workspace Cargo.toml) may not mean they are on for dependencies.
println!(
"WARNING: running in debug mode. Release mode is recommended to reduce test duration."
"WARNING: test-float-parse may be running unoptimized or with unoptimized \
dependencies. Release mode is recommended to reduce test duration."
);
std::thread::sleep(Duration::from_secs(2));
}
println!("Running with debug assertions={}", cfg!(debug_assertions));

let args: Vec<_> = std::env::args().skip(1).collect();
if args.iter().any(|arg| arg == "--help" || arg == "-h") {
Expand Down
12 changes: 11 additions & 1 deletion src/tools/test-float-parse/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;

use indicatif::{ProgressBar, ProgressStyle};

use crate::{Completed, Config, EarlyExit, FinishedAll, TestInfo};
use crate::{Completed, Config, EarlyExit, FinishedAll, SEED, SEED_ENV, TestInfo};

/// Templates for progress bars.
const PB_TEMPLATE: &str = "[{elapsed:3} {percent:3}%] {bar:20.cyan/blue} NAME \
Expand Down Expand Up @@ -135,6 +135,16 @@ pub fn finish_all(tests: &[TestInfo], total_elapsed: Duration, cfg: &Config) ->
);

if failed_generators > 0 || stopped_generators > 0 {
println!();
println!(
"ERROR: Some float parsing/printing tests failed.\n\
\n\
If you ever encounter this failure when not expected, PLEASE OPEN AN ISSUE!! The \
failure will likely go away on the next run, but may represent a real bug.\n\
\n\
To reproduce, rerun with the same seed: {}={}",
SEED_ENV, SEED.1
);
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
Expand Down
Loading