Struct diem_proptest_helpers::GrowingSubset
source · pub struct GrowingSubset<Ix, T> { /* private fields */ }
Expand description
A set of elements, each with an associated key, that grows over time.
This is called GrowingSubset
because the universal set the subset grows from is provided
upfront. At any time, the items included form the current subset.
GrowingSubset
integrates with proptest
through the pick_item
and pick_value
methods.
Examples
use diem_proptest_helpers::GrowingSubset;
let items = vec![(1, "a"), (3, "c"), (2, "b"), (2, "d")];
let mut subset: GrowingSubset<_, _> = items.into_iter().collect();
assert_eq!(subset.total_len(), 4);
assert_eq!(subset.len(), 0);
assert_eq!(subset.current(), &[]);
subset.advance_to(&2);
assert_eq!(subset.len(), 1);
assert_eq!(subset.current(), &[(1, "a")]);
subset.advance_to(&4);
assert_eq!(subset.len(), 4);
assert_eq!(subset.current(), &[(1, "a"), (2, "b"), (2, "d"), (3, "c")]);
subset.advance_to(&5);
assert_eq!(subset.len(), 4);
assert_eq!(subset.current(), &[(1, "a"), (2, "b"), (2, "d"), (3, "c")]);
Implementations§
source§impl<Ix, T> GrowingSubset<Ix, T>where
Ix: Ord,
impl<Ix, T> GrowingSubset<Ix, T>where Ix: Ord,
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the current subset.
See total_len
for the length of the universal set.
sourcepub fn total_len(&self) -> usize
pub fn total_len(&self) -> usize
Returns the total number of elements in the universal set.
This remains fixed once the GrowingSubset
has been created.
sourcepub fn current(&self) -> &[(Ix, T)]
pub fn current(&self) -> &[(Ix, T)]
Returns a slice containing the items in the current subset.
sourcepub fn pick_item(&self, index: &Index) -> &(Ix, T)
pub fn pick_item(&self, index: &Index) -> &(Ix, T)
Chooses an (index, value) pair from the current subset using the provided
Index
instance as the source of randomness.
sourcepub fn pick_value(&self, index: &Index) -> &T
pub fn pick_value(&self, index: &Index) -> &T
Chooses a value from the current subset using the provided
Index
instance as the source of randomness.
sourcepub fn advance_to(&mut self, to_idx: &Ix)
pub fn advance_to(&mut self, to_idx: &Ix)
Advances the valid subset to the provided index. After the end of this, the current subset
will contain all elements where the index is less than to_idx
.
If duplicate indexes exist, advance_to
will cause all of the corresponding items to be
included.
It is expected that advance_to
will be called with larger indexes over time.
Trait Implementations§
source§impl<Ix: Clone, T: Clone> Clone for GrowingSubset<Ix, T>
impl<Ix: Clone, T: Clone> Clone for GrowingSubset<Ix, T>
source§fn clone(&self) -> GrowingSubset<Ix, T>
fn clone(&self) -> GrowingSubset<Ix, T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<Ix, T> FromIterator<(Ix, T)> for GrowingSubset<Ix, T>where
Ix: Ord,
impl<Ix, T> FromIterator<(Ix, T)> for GrowingSubset<Ix, T>where Ix: Ord,
Constructs a GrowingSubset
from an iterator of (index, value) pairs.
The input does not need to be pre-sorted.