Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Created July 4, 2015 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swlaschin/3342176a3f657e1de484 to your computer and use it in GitHub Desktop.
Save swlaschin/3342176a3f657e1de484 to your computer and use it in GitHub Desktop.
Demonstration of using FsCheck to test "add" implementations
// =================================
// Demonstration of using FsCheck to
// test "add" implementations
//
// This is a more thorough version of the example
// in the talk at fsharpforfunandprofit.com/pbt
// =================================
System.IO.Directory.SetCurrentDirectory __SOURCE_DIRECTORY__
#r @"..\packages\FsCheck.2.0.1\lib\net45\FsCheck.dll"
open FsCheck
// bad implementation!
let add x y =
match (x,y) with
| (x,0) -> x // handle identity case
| (0,y) -> y // handle identity case
| _ -> 0 // all other cases return 0
let commutivity_property x y =
(add x y) = (add y x)
let associativity_property x y z =
add (add x y) z = add x (add y z)
let identity_property x =
(add x 0) = x && (add 0 x) = x
Check.Quick commutivity_property
// Ok, passed 100 tests.
Check.Quick associativity_property
// Falsifiable, after 3 tests (3 shrinks)
// 1
// 1
// -1
Check.Quick identity_property
// Ok, passed 100 tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment