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
274 changes: 261 additions & 13 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]

9 changes: 5 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,12 @@ edition = "2024"
publish = false

[dependencies]
indicatif = { version = "0.17.8", default-features = false }
base64 = "0.22.1"
indicatif = { version = "0.18.3", 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
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?

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
34 changes: 29 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,15 @@ 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 base64::engine::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
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 +48,26 @@ 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
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...

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

/// Global configuration.
#[derive(Debug)]
Expand Down Expand Up @@ -78,8 +100,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