Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

458 fix wasm #519

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 0 additions & 2 deletions book/src/proptest/wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ features = ["std"]
A few APIs are unavailable on `wasm` targets (beyond those which are removed by
deselecting certain default features):

- Numeric strategies for `i128` and `u128`.

- The `Arbitrary` implementation for `std::env::VarError`.
9 changes: 2 additions & 7 deletions proptest/src/arbitrary/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@
use crate::bool;
use crate::char;
use crate::num::{
f32, f64, i16, i32, i64, i8, isize, u16, u32, u64, u8, usize,
f32, f64, i128, i16, i32, i64, i8, isize, u128, u16, u32, u64, u8, usize,
};
#[cfg(not(target_arch = "wasm32"))]
use crate::num::{i128, u128};

arbitrary!(bool, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);

#[cfg(not(target_arch = "wasm32"))]
arbitrary!(i128, u128);
arbitrary!(bool, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, i128, u128);

// Note that for floating point types we limit the space since a lot of code
// isn't prepared for (and is not intended to be) things like NaN and infinity.
Expand Down
2 changes: 0 additions & 2 deletions proptest/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,12 @@ signed_integer_bin_search!(i8);
signed_integer_bin_search!(i16);
signed_integer_bin_search!(i32);
signed_integer_bin_search!(i64);
#[cfg(not(target_arch = "wasm32"))]
signed_integer_bin_search!(i128);
signed_integer_bin_search!(isize);
unsigned_integer_bin_search!(u8);
unsigned_integer_bin_search!(u16);
unsigned_integer_bin_search!(u32);
unsigned_integer_bin_search!(u64);
#[cfg(not(target_arch = "wasm32"))]
unsigned_integer_bin_search!(u128);
unsigned_integer_bin_search!(usize);

Expand Down
63 changes: 23 additions & 40 deletions proptest/src/test_runner/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,34 @@
use crate::std_facade::Box;
use core::u32;

#[cfg(feature = "std")]
use std::env;
#[cfg(feature = "std")]
use std::ffi::OsString;
#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "std")]
use std::str::FromStr;

use crate::test_runner::result_cache::{noop_result_cache, ResultCache};
use crate::test_runner::rng::RngAlgorithm;
use crate::test_runner::FailurePersistence;
#[cfg(feature = "std")]
use crate::test_runner::FileFailurePersistence;

#[cfg(feature = "std")]
const CASES: &str = "PROPTEST_CASES";
#[cfg(feature = "std")]
const MAX_LOCAL_REJECTS: &str = "PROPTEST_MAX_LOCAL_REJECTS";
#[cfg(feature = "std")]
const MAX_GLOBAL_REJECTS: &str = "PROPTEST_MAX_GLOBAL_REJECTS";
#[cfg(feature = "std")]
const MAX_FLAT_MAP_REGENS: &str = "PROPTEST_MAX_FLAT_MAP_REGENS";
#[cfg(feature = "std")]
const MAX_SHRINK_TIME: &str = "PROPTEST_MAX_SHRINK_TIME";
#[cfg(feature = "std")]
const MAX_SHRINK_ITERS: &str = "PROPTEST_MAX_SHRINK_ITERS";
#[cfg(feature = "std")]
const MAX_DEFAULT_SIZE_RANGE: &str = "PROPTEST_MAX_DEFAULT_SIZE_RANGE";
Copy link
Contributor

Choose a reason for hiding this comment

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

did this one get deleted on accident? i believe it's still in use in parse_or_warn

Choose a reason for hiding this comment

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

Idk - it's been a very long time - but CI seems to pass?

Copy link
Contributor

Choose a reason for hiding this comment

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

odd

Variable `MAX_DEFAULT_SIZE_RANGE` should have snake_case name, e.g. `max_default_size_range`

so arbitrary tokens in pattern match positions seem to be treated as new variable bindings and default to (), that's why this still compiles even though there's a warning.

i don't think i have push access to this branch though so @matthew-russo you'll need to update please

Copy link

Choose a reason for hiding this comment

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

Yeah this is fixed in #518

Choose a reason for hiding this comment

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

so arbitrary tokens in pattern match positions seem to be treated as new variable bindings and default to (), that's why this still compiles even though there's a warning.

Hm but that doesn't explain why parse_or_warn accepted it if it's () and used in an argument that expects &str.

#[cfg(feature = "fork")]
const FORK: &str = "PROPTEST_FORK";
#[cfg(feature = "timeout")]
const TIMEOUT: &str = "PROPTEST_TIMEOUT";
#[cfg(feature = "std")]
const VERBOSE: &str = "PROPTEST_VERBOSE";
#[cfg(feature = "std")]
const RNG_ALGORITHM: &str = "PROPTEST_RNG_ALGORITHM";
#[cfg(feature = "std")]
const DISABLE_FAILURE_PERSISTENCE: &str =
"PROPTEST_DISABLE_FAILURE_PERSISTENCE";

/// Override the config fields from environment variables, if any are set.
/// Without the `std` feature this function returns config unchanged.
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
pub fn contextualize_config(mut result: Config) -> Config {
use std::env;
use std::ffi::OsString;
use std::fmt;
use std::str::FromStr;

const CASES: &str = "PROPTEST_CASES";
const MAX_LOCAL_REJECTS: &str = "PROPTEST_MAX_LOCAL_REJECTS";
const MAX_GLOBAL_REJECTS: &str = "PROPTEST_MAX_GLOBAL_REJECTS";
const MAX_FLAT_MAP_REGENS: &str = "PROPTEST_MAX_FLAT_MAP_REGENS";
const MAX_SHRINK_TIME: &str = "PROPTEST_MAX_SHRINK_TIME";
const MAX_SHRINK_ITERS: &str = "PROPTEST_MAX_SHRINK_ITERS";
#[cfg(feature = "fork")]
const FORK: &str = "PROPTEST_FORK";
#[cfg(feature = "timeout")]
const TIMEOUT: &str = "PROPTEST_TIMEOUT";
const VERBOSE: &str = "PROPTEST_VERBOSE";
const RNG_ALGORITHM: &str = "PROPTEST_RNG_ALGORITHM";
const DISABLE_FAILURE_PERSISTENCE: &str =
"PROPTEST_DISABLE_FAILURE_PERSISTENCE";

fn parse_or_warn<T: FromStr + fmt::Display>(
src: &OsString,
dst: &mut T,
Expand Down Expand Up @@ -150,7 +133,7 @@ pub fn contextualize_config(mut result: Config) -> Config {
}

/// Without the `std` feature this function returns config unchanged.
#[cfg(not(feature = "std"))]
#[cfg(not(all(feature = "std", not(target_arch = "wasm32"))))]
pub fn contextualize_config(result: Config) -> Config {
result
}
Expand Down Expand Up @@ -186,7 +169,7 @@ fn default_default_config() -> Config {
lazy_static! {
static ref DEFAULT_CONFIG: Config = {
let mut default_config = default_default_config();
default_config.failure_persistence = Some(Box::new(FileFailurePersistence::default()));
default_config.failure_persistence = Some(Box::new(crate::test_runner::FileFailurePersistence::default()));
contextualize_config(default_config)
};
}
Expand Down
29 changes: 11 additions & 18 deletions proptest/src/test_runner/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ where
F: Fn(V) -> TestCaseResult,
R: Iterator<Item = TestCaseResult>,
{
use std::time;

#[cfg(feature = "timeout")]
let timeout = runner.config.timeout();

if let Some(result) = replay_from_fork.next() {
Expand All @@ -250,7 +249,8 @@ where
return result.clone().map(|_| TestCaseOk::CacheHitSuccess);
}

let time_start = time::Instant::now();
#[cfg(feature = "timeout")]
let time_start = std::time::Instant::now();

let mut result = unwrap_or!(
panic::catch_unwind(AssertUnwindSafe(|| test(case))),
Expand All @@ -263,6 +263,7 @@ where
// If there is a timeout and we exceeded it, fail the test here so we get
// consistent behaviour. (The parent process cannot precisely time the test
// cases itself.)
#[cfg(feature = "timeout")]
if timeout > 0 && result.is_ok() {
let elapsed = time_start.elapsed();
let elapsed_millis = elapsed.as_secs() as u32 * 1000
Expand Down Expand Up @@ -762,35 +763,27 @@ impl TestRunner {
fork_output: &mut ForkOutput,
is_from_persisted_seed: bool,
) -> Option<Reason> {
#[cfg(feature = "std")]
use std::time;

let mut last_failure = None;
let mut iterations = 0;
#[cfg(feature = "std")]
let start_time = time::Instant::now();
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
let start_time = std::time::Instant::now();

verbose_message!(self, TRACE, "Starting shrinking");

if case.simplify() {
loop {
#[cfg(feature = "std")]
let timed_out = if self.config.max_shrink_time > 0 {
let mut timed_out: Option<u64> = None;
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
if self.config.max_shrink_time > 0 {
let elapsed = start_time.elapsed();
let elapsed_ms = elapsed
.as_secs()
.saturating_mul(1000)
.saturating_add(elapsed.subsec_millis().into());
if elapsed_ms > self.config.max_shrink_time as u64 {
Some(elapsed_ms)
} else {
None
timed_out = Some(elapsed_ms);
}
} else {
None
};
#[cfg(not(feature = "std"))]
let timed_out: Option<u64> = None;
}

let bail = if iterations >= self.config.max_shrink_iters() {
#[cfg(feature = "std")]
Expand Down
Loading