Skip to content

Instantly share code, notes, and snippets.

@yukitos
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yukitos/4db499ede35c8ad82863 to your computer and use it in GitHub Desktop.
Save yukitos/4db499ede35c8ad82863 to your computer and use it in GitHub Desktop.
FsCheck function to generate Japanese characters.
namespace FsCheck.Ext
module Test =
open System
open FsCheck
open FsCheck.Arb
type JapaneseChar = char
let japaneseChar (s: string) : JapaneseChar =
char s
type MyGenerators =
static member JapaneseChar() =
{ new Arbitrary<JapaneseChar>() with
override x.Generator = Gen.oneof [
Gen.choose(0x3000, 0x303f) |> Gen.map (Char.ConvertFromUtf32 >> japaneseChar)
Gen.choose(0x3040, 0x309f) |> Gen.map (Char.ConvertFromUtf32 >> japaneseChar)
]
}
let sample n = Gen.sample 1000 n
type Target() =
static member JapaneseChar (value:JapaneseChar) =
(generate<JapaneseChar> |> sample 10 |> List.forall (fun _ -> true)
, shrink<JapaneseChar> value |> Seq.forall (fun shrunkv -> int shrunkv <= abs (int value)))
// Here I registered my own Arbitrary, but it doesn't work correctly at the first time
Arb.register<MyGenerators>() |> ignore
Check.QuickAll<Target>()
// Register again *after running QuickAll*.
// This time QuickAll works fine.
Arb.register<MyGenerators>() |> ignore
Check.QuickAll<Target>()
--- Checking Target ---
Target.JapaneseChar-Falsifiable, after 1 test (0 shrinks) (StdGen (1898206864,295898127)):
'\004'
--- Checking Target ---
Target.JapaneseChar-Ok, passed 100 tests.
@yukitos
Copy link
Author

yukitos commented Aug 14, 2014

Variation:

    Check.QuickAll<Target>() // Doesn't work (but it's OK because I don't register my own Arbitrary yet)
    Arb.register<MyGenerators>() |> ignore
    Check.QuickAll<Target>() // Works fine
    Arb.register<MyGenerators>() |> ignore
    Arb.register<MyGenerators>() |> ignore
    Check.QuickAll<Target>() // Desn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment