Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation - role of Gen.t vs. arbitrary #281

Open
lindig opened this issue Jul 13, 2023 · 3 comments
Open

Documentation - role of Gen.t vs. arbitrary #281

lindig opened this issue Jul 13, 2023 · 3 comments

Comments

@lindig
Copy link

lindig commented Jul 13, 2023

I am picking up QCheck and I struggle with how to write my tests; I believe the documentation would benefit from describing the relation of generators and arbitrariness better because they have a similar structure. For example: I need a small list of key-value pairs. Should I use the Gen.t or arbitrary combinators to define this? Some arbitrary functions take Gen.t values as arguments - which is surprising and creates the confusion.

@jmid
Copy link
Collaborator

jmid commented Jul 13, 2023

Acknowledged! 🙂

In a past role teaching this stuff I prepared slides to explain the Gen.t vs arbitrary difference.
See https://janmidtgaard.dk/quickcheck/slides/lec03.pdf from p.48 of the pdf (labeled 21/48 in the bottom right corner).
The slides summarize my own view: Gen.t is purely for generation and nothing else, while arbitrary is a generator packaged up with a corresponding printer, shrinker, etc.

With that in mind, e.g., a string arbitrary combinator needing a 'size generator' we really only need "something that can generate an int size", i.e., an int Gen.t.

Whereas my old slides go top-down, the relevant Cornell book chapter instead explains this bottom-up, starting from "pure generation" and explains arbitrary as their enhancement IIRC.

Finally I should mention that QCheck2 with integrated shrinking, doesn't have the same distinction because generators and shrinkers are merged into one common (lazy tree) structure.

For example: I need a small list of key-value pairs. Should I use the Gen.t or arbitrary combinators to define this?

Tests built with Test.make accept an arbitrary in order to be able to print (and shrink) any counterexamples.
As such I expect you would want something like the following (using char keys and bool values):

list_of_size Gen.small_nat (pair char bool) : (char * bool) list QCheck.arbitrary =

Notice how this uses Gen.t for size generation, for the reason explained above.

I hope this helps! As always, PRs to improve the documentation are very welcome! 🙏😃

@lindig
Copy link
Author

lindig commented Jul 14, 2023

The documentation on the slides is helpful; maybe include a link or the slides in this repository. I believe this kind of high-level explanation would at the top of the MLI file would help. Something that is not obvious: is QCheck2 an older or more recent version? The current release is 0.21.x so I fail to see the connection and which interface should new code target?

@jmid
Copy link
Collaborator

jmid commented Jul 14, 2023

The documentation on the slides is helpful; maybe include a link or the slides in this repository. I believe this kind of high-level explanation would at the top of the MLI file would help.

That's a good point and a constructive suggestion, thanks!

Something that is not obvious: is QCheck2 an older or more recent version? The current release is 0.21.x so I fail to see the connection and which interface should new code target?

Indeed. QCheck2 offers integrated shrinking (see #106 #109 and #116 for details).
It is a newer development offering "shrinking for free", meaning an end user does not need to write a boilerplate shrinker for 'custom generators' (think: 2 for the price of 1).

QCheck2 isn't as battle tested as the older QCheck(1).
I started an effort to strengthen both implementations by adding tests in #153 and collecting a suite of shrinker benchmarks in #177 about 2 years back.
For this reason I chose to go with QCheck(1) for https://github.com/ocaml-multicore/multicoretests 1.5 years ago.
One day:tm: (famous last words) I want to switch it to QCheck2.
I believe @vch9 has already switched to using QCheck2 to test Tezos so it may indeed be ready for production.

The QCheck(1) vs. QCheck2 distinction could certainly also be made clearer in the documentation...

Finally I'm realizing that https://c-cube.github.io/qcheck/0.21/qcheck-core/QCheck2/index.html is perhaps not as easily reachable from https://c-cube.github.io/qcheck/ as one could hope for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants