Skip to content

Commit

Permalink
Add ability to customize max default SizeRange via env var. (#338)
Browse files Browse the repository at this point in the history
The implementation directly accesses Config::default() from
SizeRange::default().

I don't love the implementation for two reasons
- It clones the full config on every call to SizeRange::default()
- It is hard to test.

However, it does achieve the goal. I am open to better ideas!

Co-authored-by: Tomas Zemanovic <tzemanovic@gmail.com>
  • Loading branch information
nipunn1313 and tzemanovic authored Jan 13, 2024
1 parent ffce269 commit 5f87b3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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

0 comments on commit 5f87b3d

Please sign in to comment.