Generative testing seems interesting, but I needed to generate random UUIDs as part of the testing. java.util.UUID/newRandom doesn't play nice with test.check shrinking.
The java code looks like:
public static UUID randomUUID()
{
long lsb = r.nextLong();
long msb = r.nextLong();
lsb &= 0x3FFFFFFFFFFFFFFFL;
lsb |= 0x8000000000000000L; // set top two bits to variant 2
msb &= 0xFFFFFFFFFFFF0FFFL;
msb |= 0x4000; // Version 4;
return new UUID( msb, lsb );
}
Which is trickier to translate to Clojure than it would seem.
How do I write a random UUID function in Clojure that can be successfully shrunk?
A fn that takes two longs and generates a proper type 4 UUID is:
You can use a regex to check the result, (need to convert it to a string first).
Then you can test it as following:
And the tests look like:
Just for fun, I removed one of the valid characters (9) for the second field, and this is what a failing test looks like, so you can see how shrinking from :fail to :smallest can help.
Which shows you how much noise shrinking can remove from your test case.