How to create a fixed list generator for FsCheck

At first I tried to create a generator in which the first 5 elements were fixed (and in any test using the Prop.forAllfirst five will always be executed), but I could not do this.

Now I'm trying to simplify this by having one generator for random data within the range and one generator for non-random data, i.e. fixed sequence. It is similar to Gen.constant, except that instead of a single value, it is a sequence of values.

I have this (simplified reproducible example, works with NUnit and xUnit):

[<Property(Verbose = true, MaxTest=5)>]
static member MultiplyIdentityCornerCases () =
    Gen.elements [0L; -1L; 1L; Int64.MinValue; Int64.MaxValue]
    |> Arb.fromGen 
    |> Prop.forAll <| fun x -> x = x * 1L

Exit (I don’t know where it comes from null):

0:
<null>
9223372036854775807L
1:
<null>
-9223372036854775807L
2:
<null>
-9223372036854775807L
3:
<null>
1L
4:
<null>
-9223372036854775807L
Ok, passed 5 tests.

, , , , . , NUnit ( ) testdata, , FsCheck ( , , ).

, FsCheck , , , , , . FsCheck, testdata.

+4
1

, , :

open System
open FsCheck
open FsCheck.Xunit

[<Property>]
let MultiplyIdentityCornerCases () =
    Gen.oneof [
        Gen.elements [Int64.MinValue; -1L; 0L; 1L; Int64.MaxValue]
        Arb.generate ]
    |> Arb.fromGen
    |> Prop.forAll <| fun x -> x = x * 1L

Gen.oneof, .

Gen.elements , , . 0L 20% , , Gen.oneof Gen.elements.

, "" 50% * 20% = 10% .

100 , 10 0L, 10 Int64.MinValue .. .


, - :

open System
open Xunit
open FsCheck
open FsCheck.Xunit
open Swensen.Unquote

[<Theory>]
[<InlineData(Int64.MinValue)>]
[<InlineData(-1L)>]
[<InlineData( 0L)>]
[<InlineData( 1L)>]
[<InlineData(Int64.MaxValue)>]
let MultiplyIdentityCornerCases x = x =! x * 1L

[<Property>]
let MultiplyIdentityCornerCasesProperty x =
    MultiplyIdentityCornerCases x

xUnit.net [<Theory>] , . , .

, MultiplyIdentityCornerCasesProperty, [<Property>], .

+7

Source: https://habr.com/ru/post/1663351/


All Articles