09 Randomized Testing and QCheck

The story begins with this

It was a dark and stormy night...

A researcher was using a dial up modem to connect to his university computer cluster during an electrical storm. Random noise was fed over the telephone line, and everything crashed. He came up with the idea of doing this intentionally to test programs.

This has been done for X-windows system, windows NT, macOS, etc... Results have been getting worse on GUIs, but better on the command line. This testing, also called "fuzzing," has become a standard practice for security testing.

OCaml also has support for randomized testing. Let's go back to our leap year testing. QCheck is a library for doing quick, randomized checks.

And let's bring back our things from earlier.

Now let's generate some tests!

Here we generate a bunch of random years, then we check if they are either multiples of 4, or leap years. We can't generate all leap years or all not leap years, because that's the thing we're tyring the check!

We can then convert these to OUnit tests and throw them in our test suite!