proptest: `sample()` and `select()` functions are missing

My use case is a graph, where all nodes are randomly created, and then I want to select a subset for each node to connect to when I create the actual graph.

However, there doesn’t seem to be either a sample() or select() api with which to do this.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 52

Commits related to this issue

Most upvoted comments

Done initial work on subsequence - todo: tests and more CollectionStrategy impls. Work in #8 .

@AltSysrq That shrinking logic seems like a better idea with a better notion of chaos (locality / delta).

@vitiral bounds refers to (min/max) - an amount is not a Range<usize> - but what a parameter is named isn’t that important. What is important is giving a clear documentation about the behaviors of the method. I think fail-fast behavior in response to what is clearly a programmer-error is appropriate - the standard library does use panics for that in certain places - it is even more appropriate for a testing lib.

To be clear, subsequence effectively would first randomize amount in [bounds.start, bounds.end] and then use sample with that.

Now I know what you want =)

Doing this as a trait is probably reasonable.

Is the following valid?

([1, 2, 3, 4], [2, 1]) <-- note that the ordering has changed.

What are the semantics of (un)shrinking? A proposal:

  • The ValueTree should be generated with an initial sub-Vec.
  • For simplify() the ValueTree should remove one tail element from the last-returned sub-Vec if it hasn’t reached the minimum length. This removed element is added to another Vec stored in the ValueTree
  • For complicate(), the last removed element should be re-added.

The type is thus:

struct SubVecValueTree<T: Clone> {
    minimum: usize,
    current: Vec<T>,
    removed: Vec<T>,
}

A version on this is to alternate between removing the first / last element. It depends on if you want to bias towards the first element being the simplest, or if you want the middle element to be the simplest (in which case you alternate).

I think .select is unnecessary since we already have Union, so you can do:

Union::new_weighted(my_vec.into_iter().map(|elt| (1, Just(elt))))

Tho for performance a more specialized solution may be right.