quickcheck icon indicating copy to clipboard operation
quickcheck copied to clipboard

api: add Gen::set_size and Gen::from_seed

Open jakoschiko opened this issue 4 years ago • 4 comments

The seed still can't be set by QuickCheck users, but the new Gen constructor is useful for other crates that use QuickCheck only for its Arbitrary trait.

Closes #277

jakoschiko avatar Feb 09 '21 01:02 jakoschiko

@BurntSushi

Some questions:

  • I called the new constructor Gen::from_seed. Is that okay? Or maybe Gen::from_seed_u64?
  • You regret the current constructor Gen::new because it takes a size parameter. Should I add a Default implementation for Gen with random seed and default size?
  • Are you sure that you want a Gen::set_size? Arbitrary::arbitrary takes a &mut Gen and this function makes it very easy to change not only the own local size, but also the size of the Arbitrary::arbitrary caller. Maybe we should provide a Gen::with_size that returns a copy.

jakoschiko avatar Feb 09 '21 01:02 jakoschiko

Just my thoughts here, but Gen::with_size seems reasonable here. Maybe even one that takes a closure? I.e., to generate arbitrary vec's with a size parameter of 10: gen.with_size(10, Vec::arbitrary)

It would be implemented something like

impl Gen {
  pub fn with_size<T>(&mut self, size: usize, f: impl FnOnce(&mut Gen) -> T) -> T {
    let old_size = self.size;
    self.size = size;
    let res = f(self);
    self.size = old_size;
    res
  }
}

audunska avatar Mar 26 '21 15:03 audunska

What's the status of this PR? It would be super useful to have a seedable Gen.

aakoshh avatar Jan 18 '23 09:01 aakoshh

^ I am also interested in this.

JarredAllen avatar Feb 21 '23 23:02 JarredAllen