quicktheories / QuickTheories

Property based testing for Java 8
Apache License 2.0
505 stars 51 forks source link

Generate unique values #45

Closed diveshpremdeep closed 6 years ago

diveshpremdeep commented 6 years ago

Is there any way I can configure a Gen<T> to provide unique values (i.e. do not provide a value that has already been generated)?

Simplest case, is there any way I can configure a Gen<String> to always provide me unique strings?

hcoles commented 6 years ago

No and yes.

There is no way to avoid a Gen generating duplicate values, but this doesn't actually matter as quicktheories will only assess each value combination (at most) once.

So for the simple case of a Gen they are always effectively unique for each run. If you have a theory that consumes two Gens, then you may get duplicate values but the combination will always be unique.

diveshpremdeep commented 6 years ago

Thanks for your reply.

I don't understand the this doesn't actually matter as quicktheories will only assess each value combination (at most) once bit though.

hcoles commented 6 years ago

For example

public class RandomTest implements WithQuickTheories {

    @Test
    public void doesNotRepeat() {
      qt()
      .forAll(arbitrary().pick(1,2,3))
      .checkAssert( i -> System.out.println(i));
    }

    @Test
    public void doesNotRepeatCombination() {
      qt()
      .forAll(arbitrary().pick(1,2,3), arbitrary().pick("a", "b", "c"))
      .checkAssert( (i,s) -> System.out.println(i + " : " + s));
    }    
}

Gives the output

1
3
2

1 : a
3 : c
1 : c
3 : a
3 : b
2 : a
1 : b
2 : b
2 : c

Only three output values are possible for each Gen. QuickTheories discards all duplicate combinations.

diveshpremdeep commented 6 years ago

Cheers, I understand now.