Skip to content

Instantly share code, notes, and snippets.

@jindraivanek
Created May 16, 2023 12:06
Show Gist options
  • Save jindraivanek/b1605bc1863c44793b25ea7629d80686 to your computer and use it in GitHub Desktop.
Save jindraivanek/b1605bc1863c44793b25ea7629d80686 to your computer and use it in GitHub Desktop.
FsCheck example
module FsCheckExample
open System.IO
open NUnit.Framework
open FsCheck.NUnit
open FsCheck.PropOperators
open FsCheck.GenBuilder
open FsCheck
[<Property(Verbose = true, Replay = "(1593021609, 297184309)")>]
let ``FsCheck example`` (xs: int list) =
xs |> List.rev = xs
[<Property(Verbose = true)>]
let ``FsCheck example 2`` (xs: float list) =
xs |> List.rev |> List.rev = xs
[<Property(Verbose = true)>]
let ``FsCheck example 3`` (xs: float list) =
(xs |> List.forall (System.Double.IsRealNumber)) ==>
lazy (xs |> List.rev |> List.rev = xs)
let realFloatGen =
gen {
let! xs = Arb.generate<float> |> Gen.filter System.Double.IsRealNumber |> Gen.nonEmptyListOf
return xs
}
[<Test>]
let ``FsCheck example 4``() =
let property xs = xs |> List.rev |> List.rev = xs
property |> Prop.forAll (Arb.fromGen realFloatGen) |> Check.VerboseThrowOnFailure
type MyGenerators =
static member RealFloat() =
{new Arbitrary<float list>() with
override x.Generator = realFloatGen
override x.Shrinker t = base.Shrinker t }
[<Property(Verbose = true, Arbitrary = [|typeof<MyGenerators>|])>]
let ``FsCheck example 5`` (xs: float list) =
xs |> List.rev |> List.rev = xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment