diff --git a/tests/FSharpx.Collections.Tests/SeqTests.fs b/tests/FSharpx.Collections.Tests/SeqTests.fs index 66cdc9cd..45ce65be 100644 --- a/tests/FSharpx.Collections.Tests/SeqTests.fs +++ b/tests/FSharpx.Collections.Tests/SeqTests.fs @@ -156,7 +156,9 @@ module SeqTests = |> Expect.sequenceEqual "tail" (List.toSeq [ 2; 3; 4 ]) } - ptest "I should not be able to get the tail of a empty sequence" { Expect.throwsT<_> "empty tail" (fun () -> Seq.tail [] |> ignore) } + test "I should not be able to get the tail of an empty sequence" { + Expect.throwsT "empty tail" (fun () -> Seq.tail [] |> Seq.toList |> ignore) + } test "I should be able to get the tail of a empty sequence without a fail" { Seq.tailNoFail [] |> Expect.sequenceEqual "tailNoFail" Seq.empty @@ -246,6 +248,77 @@ module SeqTests = testPropertyWithConfig config10k "I should interperse always 2n-1 elements" intersperse + test "span should split sequence at predicate boundary" { + let before, after = Seq.span (fun x -> x < 5.0) data + Expect.sequenceEqual "before" [ 1.; 2.; 3.; 4. ] (before |> Seq.toList) + Expect.sequenceEqual "after" [ 5.; 6.; 7.; 8.; 9.; 10. ] (after |> Seq.toList) + } + + test "span on empty sequence yields two empty sequences" { + let before, after = Seq.span (fun (x: float) -> x < 5.0) Seq.empty + Expect.sequenceEqual "before" [] (before |> Seq.toList) + Expect.sequenceEqual "after" [] (after |> Seq.toList) + } + + test "span where predicate is always true returns whole sequence and empty" { + let before, after = Seq.span (fun x -> x < 100.0) data + Expect.sequenceEqual "before" data (before |> Seq.toList) + Expect.sequenceEqual "after" [] (after |> Seq.toList) + } + + test "groupNeighboursBy groups consecutive equal elements" { + let input = [ 1; 1; 2; 2; 2; 1; 3 ] + let groups = Seq.groupNeighboursBy id input |> Seq.toList + let keys = groups |> List.map fst + let values = groups |> List.map(snd >> Seq.toList) + Expect.equal "keys" [ 1; 2; 1; 3 ] keys + Expect.equal "groups" [ [ 1; 1 ]; [ 2; 2; 2 ]; [ 1 ]; [ 3 ] ] values + } + + test "groupNeighboursBy on empty sequence returns empty" { + let groups = Seq.groupNeighboursBy id (Seq.empty) |> Seq.toList + Expect.equal "empty groups" [] groups + } + + test "groupNeighboursBy with projection" { + let input = [ "a1"; "a2"; "b1"; "b2"; "a3" ] + + let groups = Seq.groupNeighboursBy (fun (s: string) -> s.[0]) input |> Seq.toList + + let keys = groups |> List.map fst + let values = groups |> List.map(snd >> Seq.toList) + Expect.equal "keys" [ 'a'; 'b'; 'a' ] keys + Expect.equal "groups" [ [ "a1"; "a2" ]; [ "b1"; "b2" ]; [ "a3" ] ] values + } + + test "choice1s extracts only Choice1Of2 values" { + let input: Choice list = + [ Choice1Of2 1; Choice2Of2 "a"; Choice1Of2 2; Choice2Of2 "b" ] + + let result = Seq.choice1s input |> Seq.toList + Expect.equal "choice1s" [ 1; 2 ] result + } + + test "choice2s extracts Choice2Of2 values from sequence" { + let input: Choice list = + [ Choice1Of2 1; Choice2Of2 "a"; Choice1Of2 2; Choice2Of2 "b" ] + + let result = Seq.choice2s input |> Seq.toList + Expect.equal "choice2s" [ "a"; "b" ] result + } + + test "partitionChoices with empty input returns empty sequences" { + let input: Choice list = [] + + let lefts, rights = Seq.partitionChoices input + Expect.equal "lefts empty" [] (lefts |> Seq.toList) + Expect.equal "rights empty" [] (rights |> Seq.toList) + } + + test "equalsWith returns false for sequences of different lengths" { + Expect.isFalse "equalsWith" (Seq.equalsWith (=) [ 1; 2 ] [ 1; 2; 3 ]) + } + test "cons prepends an element to a seq" { Seq.cons 0 [ 1; 2; 3 ] |> Expect.sequenceEqual "cons" (List.toSeq [ 0; 1; 2; 3 ])