Skip to content

Property based testing revealed inconsistency in chunkBySize #501

@forki

Description

@forki

I wrote the following test:

type Result<'a> = 
| Success of 'a
| Error of string

let run f = 
    try
        Success(f())
    with
    | exn -> Error exn.Message

let chunkBySize<'a when 'a : equality> (xs : 'a []) size =
    let s = run (fun () -> xs |> Seq.chunkBySize size |> Seq.map Seq.toArray |> Seq.toArray)
    let l = run (fun () -> xs |> List.ofArray |> List.chunkBySize size |> Seq.map Seq.toArray |> Seq.toArray)
    let a = run (fun () ->  xs |> Array.chunkBySize size |> Seq.map Seq.toArray |> Seq.toArray)
    s = a && l = a


[<Test>]
let ``chunkBySize is consistent`` () =
    Check.QuickThrowOnFailure chunkBySize<int>
    Check.QuickThrowOnFailure chunkBySize<string>
    Check.QuickThrowOnFailure chunkBySize<float>

which returned:

Test 'FSharp.Core.PropertyTests.CollectionModulesConsistency.chunkBySize is consistent' failed: System.Exception : Falsifiable, after 10 tests (16 shrinks) (StdGen (1102101551,296021308)):
[|0; 0; 0; 0; |]
3
    at <StartupCode$FsCheck>.$Runner.get_throwingRunner@349-1.Invoke(String message)
    at <StartupCode$FsCheck>.$Runner.get_throwingRunner@339.FsCheck-IRunner-OnFinished(String , TestResult )
    at FsCheck.Runner.check[a](Config config, a p)
    at FsCheck.Check.QuickThrowOnFailure[Testable](Testable property)
    D:\code\visualfsharp\src\fsharp\FSharp.Core.PropertyTests\CollectionModulesConsistency.fs(102,0): at FSharp.Core.PropertyTests.CollectionModulesConsistency.chunkBySize is consistent()

So it seems this one revealed inconsistent behaviour between List, Seq and Array (which still might be OK)

so I investigated the case and created this test:

[<Test>]
let ``chunkBySize of zeros`` () =
    let a = Seq.chunkBySize 3 [|0;0;0;0|]
    let b = List.chunkBySize 3 [0;0;0;0]
    let c = Array.chunkBySize 3 [|0;0;0;0|] 

    Assert.AreEqual(a = b && a = c) // false after converting to Array

which prints:

seq [[|0; 0; 0|]; [|0|]]
[[0; 0; 0]; [0]]
[|[|0; 0; 0|]; null|]

so where is this null coming from.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions