Random testing in F# with FsCheck
One of the emerging trends in software development is TDD or Test-Driven Development, a methodology based on writing tests first and then coding in order to pass the tests.
Besides unit testing libraries inherited by the .Net Framework, F# can now count on FsCheck, a random testing framework cloned from Haskell’s QuickCheck.
A random testing library generates a set of test cases and attempts to falsify the properties defined by the developer.
This approach is not exhaustive but if all the tests of large enough test suite are passed we can safely assume that the code is correct.
Let’s see how to get started using FsCheck to validate the RSA implementation written some time ago.
The first step is to download the latest release (currently 0.3) of FsCheck from the Download page.
You can either get the binaries or the source code, that also contains an example console application.
Assuming that you downloaded the binaries, you have then to open/create an F# application and add a Reference (Project – Add Reference) to the FsCheck.dll library file:

Project referencing FsCheck library
In order to use the methods provided by FsCheck we have to include its namescope into our code by adding a open FsCheck clause at the beginning of our program.
For the sake of example, let’s fix the two distinct random prime numbers p and q and the public exponent e.
We have then to define our first property, that I’m going to call prop_rsa, which basically asserts that decrypting a message that was previously encrypted we get back the original message.
In order to run a property prop_myproperty we just have to run quickCheck myproperty, so in this case we’ll run quickCheck prop_rsa.
#light open System open FsCheck // RSA sample data let p = 61 let q = 53 let e = 17 let n = p * q let d = private_exponent e p q let prop_rsa message = let encrypted = encrypt message e n decrypt encrypted d n = message quickCheck prop_rsa Console.ReadKey() |> ignore
If everything goes well, we should see a console window like the following one, with the number of tests passed (by default, FsCheck generates 100 test cases).
If a test fails, FsCheck will stop the execution and show which test failed. If you want to see all the generated test cases, you can run verboseCheck instead of quickCheck.
The next step should be writing a custom generator in order to generate not only the message but also the prime numbers and the public exponent, but I think we can cover that in a future post.
You can download the complete solution here.
claudio on December 28th 2008 in Functional programming

Rick Minerich's Development Wonderland : F# for Testing and Analysis at Code Camp 11 New England responded on 26 Mar 2009 at 9:22 pm #
[...] More Claudio Cherubino’s Random testing in F# with FsCheck Matthew Podwysocki’s FP Unit Testing Part 2 – QuickCheck and FsCheck Kurt Schelfthout’s [...]
Rick Minerich's Development Wonderland : F# for Testing and Analysis at Code Camp 11 New England responded on 27 Mar 2009 at 11:38 pm #
[...] More about FsCheck Claudio Cherubino’s Random testing in F# with FsCheck Matthew Podwysocki’s FP Unit Testing Part 2 – QuickCheck and FsCheck Kurt Schelfthout’s [...]