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

Add ability to customize max default SizeRange via env var. #338

Merged
merged 3 commits into from
Jan 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions proptest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Unreleased

### New Features

- Setting `PROPTEST_MAX_DEFAULT_SIZE_RANGE` now customizes the default `SizeRange`
used by the default strategies for collections (like `Vec`). The default remains 100.

## 1.4.0

### Breaking Changes
Expand Down
8 changes: 5 additions & 3 deletions proptest/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use crate::tuple::TupleValueTree;
/// A value like `0..=std::usize::MAX` will still be accepted but will silently
/// truncate the maximum to `std::usize::MAX - 1`.
///
/// The `Default` is `0..100`.
/// The `Default` is `0..PROPTEST_MAX_DEFAULT_SIZE_RANGE`. The max can be set with
/// the `PROPTEST_MAX_DEFAULT_SIZE_RANGE` env var, which defaults to `100`.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct SizeRange(Range<usize>);

Expand All @@ -47,9 +48,10 @@ pub fn size_range(from: impl Into<SizeRange>) -> SizeRange {
}

impl Default for SizeRange {
/// Constructs a `SizeRange` equivalent to `size_range(0..100)`.
/// Constructs a `SizeRange` equivalent to `size_range(0..PROPTEST_MAX_DEFAULT_SIZE_RANGE)`.
/// The max can be set with the `PROPTEST_MAX_DEFAULT_SIZE_RANGE` env var, which defaults to `100`.
fn default() -> Self {
size_range(0..100)
size_range(0..Config::default().max_default_size_range)
}
}

Expand Down
19 changes: 19 additions & 0 deletions proptest/src/test_runner/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const MAX_FLAT_MAP_REGENS: &str = "PROPTEST_MAX_FLAT_MAP_REGENS";
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";
#[cfg(feature = "fork")]
const FORK: &str = "PROPTEST_FORK";
#[cfg(feature = "timeout")]
Expand Down Expand Up @@ -121,6 +123,12 @@ pub fn contextualize_config(mut result: Config) -> Config {
"u32",
MAX_SHRINK_ITERS,
),
MAX_DEFAULT_SIZE_RANGE => parse_or_warn(
&value,
&mut result.max_default_size_range,
"usize",
MAX_DEFAULT_SIZE_RANGE,
),
VERBOSE => {
parse_or_warn(&value, &mut result.verbose, "u32", VERBOSE)
}
Expand Down Expand Up @@ -165,6 +173,7 @@ fn default_default_config() -> Config {
#[cfg(feature = "std")]
max_shrink_time: 0,
max_shrink_iters: u32::MAX,
max_default_size_range: 100,
result_cache: noop_result_cache,
#[cfg(feature = "std")]
verbose: 0,
Expand Down Expand Up @@ -331,6 +340,16 @@ pub struct Config {
/// considered when the `std` feature is enabled, which it is by default.)
pub max_shrink_iters: u32,

/// The default maximum size to `proptest::collection::SizeRange`. The default
/// strategy for collections (like `Vec`) use collections in the range of
/// `0..max_default_size_range`.
///
/// The default is `100` which can be overridden by setting the
/// `PROPTEST_MAX_DEFAULT_SIZE_RANGE` environment variable. (The variable
/// is only considered when the `std` feature is enabled, which it is by
/// default.)
pub max_default_size_range: usize,

/// A function to create new result caches.
///
/// The default is to do no caching. The easiest way to enable caching is
Expand Down
Loading