Last active
August 29, 2015 14:05
-
-
Save yukitos/4db499ede35c8ad82863 to your computer and use it in GitHub Desktop.
FsCheck function to generate Japanese characters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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>() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- Checking Target --- | |
Target.JapaneseChar-Falsifiable, after 1 test (0 shrinks) (StdGen (1898206864,295898127)): | |
'\004' | |
--- Checking Target --- | |
Target.JapaneseChar-Ok, passed 100 tests. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Variation: