BurntSushi / quickcheck

Automated property based testing for Rust (with shrinking).
The Unlicense
2.4k stars 149 forks source link

Gen from seed #277

Open jakoschiko opened 3 years ago

jakoschiko commented 3 years ago

Many crates implement quickcheck::Arbitrary and I was very happy that I could use these implementations in dicetest by implementing quickcheck::Gen. Unfortunately I can't do this anymore since quickcheck 1.0.

Would it be possible to provide a function that creates a Gen from a seed? Something like that:

impl Gen {
    pub fn from_seed(seed: u64, size: usize) -> Gen {
        Gen { rng: rand::rngs::SmallRng::seed_from_u64(seed), size: size }
    }
}
BurntSushi commented 3 years ago

That seems sensible to me. I think we should leave off the size parameter perhaps though. I regret that Gen's constructor has it instead of providing a set_size method. So we'll want that when this new constructor is added.

jakoschiko commented 3 years ago

Can I help by creating a PR?

BurntSushi commented 3 years ago

Sure! I am on parental leave though, so it could be a while. I rarely get the chance to open a laptop. :)

On Mon, Feb 8, 2021, 18:50 Jakob Schikowski notifications@github.com wrote:

Can I help by creating a PR?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/BurntSushi/quickcheck/issues/277#issuecomment-775542405, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADPPYSQUST7Q22MDRBGTZLS6B2DPANCNFSM4XJ6DBXQ .

jakoschiko commented 3 years ago

I'm in no hurry, I'll do it within the next days.

ggreif commented 3 years ago

May I suggest from_rng(rng) too? @jakoschiko I have a supplied random gen from the environment that I have to use. I tried

    Gen {
    rng: SmallRng::from_rng(rng).unwrap(),
    size: 42,
    }

but got

183 |     rng: SmallRng::from_rng(rng).unwrap(),
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private field

Maybe just making both fields pub would work? Of course that depends on how settled Gen is...

BurntSushi commented 3 years ago

No. Because that makes rand_core a public dependency.

jakoschiko commented 3 years ago

@ggreif rand_core was recently removed from the interface, see #265. I didn't want to discuss this in this issue, I just wanted a replacement. If elegance doesn't matter, you could generate an u64 using rng and pass it to Gen::from_seed. I think SmallRng::from_rng does something similar, but with more bytes.

ggreif commented 3 years ago

Great, a u64 seed is perfectly adequate for my purposes. I am happy without from_rng.