|
| 1 | +module TaskSeq.Tests.SplitInto |
| 2 | + |
| 3 | +open Xunit |
| 4 | +open FsUnit.Xunit |
| 5 | + |
| 6 | +open FSharp.Control |
| 7 | + |
| 8 | +// |
| 9 | +// TaskSeq.splitInto |
| 10 | +// |
| 11 | + |
| 12 | +module EmptySeq = |
| 13 | + [<Fact>] |
| 14 | + let ``TaskSeq-splitInto with null source raises`` () = assertNullArg <| fun () -> TaskSeq.splitInto 1 null |
| 15 | + |
| 16 | + [<Fact>] |
| 17 | + let ``TaskSeq-splitInto with zero raises ArgumentException before awaiting`` () = |
| 18 | + fun () -> TaskSeq.empty<int> |> TaskSeq.splitInto 0 |> ignore // throws eagerly, before enumeration |
| 19 | + |> should throw typeof<System.ArgumentException> |
| 20 | + |
| 21 | + [<Fact>] |
| 22 | + let ``TaskSeq-splitInto with negative raises ArgumentException before awaiting`` () = |
| 23 | + fun () -> TaskSeq.empty<int> |> TaskSeq.splitInto -1 |> ignore |
| 24 | + |> should throw typeof<System.ArgumentException> |
| 25 | + |
| 26 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 27 | + let ``TaskSeq-splitInto on empty sequence yields empty`` variant = |
| 28 | + Gen.getEmptyVariant variant |
| 29 | + |> TaskSeq.splitInto 1 |
| 30 | + |> verifyEmpty |
| 31 | + |
| 32 | + [<Theory; ClassData(typeof<TestEmptyVariants>)>] |
| 33 | + let ``TaskSeq-splitInto(99) on empty sequence yields empty`` variant = |
| 34 | + Gen.getEmptyVariant variant |
| 35 | + |> TaskSeq.splitInto 99 |
| 36 | + |> verifyEmpty |
| 37 | + |
| 38 | +module Immutable = |
| 39 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 40 | + let ``TaskSeq-splitInto preserves all elements in order`` variant = task { |
| 41 | + do! |
| 42 | + Gen.getSeqImmutable variant |
| 43 | + |> TaskSeq.splitInto 3 |
| 44 | + |> TaskSeq.collect TaskSeq.ofArray |
| 45 | + |> verify1To10 |
| 46 | + } |
| 47 | + |
| 48 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 49 | + let ``TaskSeq-splitInto(2) splits 10-element sequence into 2 chunks of 5`` variant = task { |
| 50 | + let! chunks = |
| 51 | + Gen.getSeqImmutable variant |
| 52 | + |> TaskSeq.splitInto 2 |
| 53 | + |> TaskSeq.toArrayAsync |
| 54 | + |
| 55 | + chunks |> should equal [| [| 1..5 |]; [| 6..10 |] |] |
| 56 | + } |
| 57 | + |
| 58 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 59 | + let ``TaskSeq-splitInto(5) splits 10-element sequence into 5 chunks of 2`` variant = task { |
| 60 | + let! chunks = |
| 61 | + Gen.getSeqImmutable variant |
| 62 | + |> TaskSeq.splitInto 5 |
| 63 | + |> TaskSeq.toArrayAsync |
| 64 | + |
| 65 | + chunks |
| 66 | + |> should equal [| [| 1; 2 |]; [| 3; 4 |]; [| 5; 6 |]; [| 7; 8 |]; [| 9; 10 |] |] |
| 67 | + } |
| 68 | + |
| 69 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 70 | + let ``TaskSeq-splitInto(10) splits 10-element sequence into 10 singleton chunks`` variant = task { |
| 71 | + let! chunks = |
| 72 | + Gen.getSeqImmutable variant |
| 73 | + |> TaskSeq.splitInto 10 |
| 74 | + |> TaskSeq.toArrayAsync |
| 75 | + |
| 76 | + chunks |> Array.length |> should equal 10 |
| 77 | + |
| 78 | + chunks |
| 79 | + |> Array.iteri (fun i chunk -> chunk |> should equal [| i + 1 |]) |
| 80 | + } |
| 81 | + |
| 82 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 83 | + let ``TaskSeq-splitInto(1) returns the whole sequence as one chunk`` variant = task { |
| 84 | + let! chunks = |
| 85 | + Gen.getSeqImmutable variant |
| 86 | + |> TaskSeq.splitInto 1 |
| 87 | + |> TaskSeq.toArrayAsync |
| 88 | + |
| 89 | + chunks |> should equal [| [| 1..10 |] |] |
| 90 | + } |
| 91 | + |
| 92 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 93 | + let ``TaskSeq-splitInto(3) distributes remainder across first chunks`` variant = task { |
| 94 | + // 10 elements into 3 chunks: 10 / 3 = 3 remainder 1 → [4; 3; 3] |
| 95 | + let! chunks = |
| 96 | + Gen.getSeqImmutable variant |
| 97 | + |> TaskSeq.splitInto 3 |
| 98 | + |> TaskSeq.toArrayAsync |
| 99 | + |
| 100 | + chunks |> Array.length |> should equal 3 |
| 101 | + chunks.[0] |> should equal [| 1; 2; 3; 4 |] |
| 102 | + chunks.[1] |> should equal [| 5; 6; 7 |] |
| 103 | + chunks.[2] |> should equal [| 8; 9; 10 |] |
| 104 | + } |
| 105 | + |
| 106 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 107 | + let ``TaskSeq-splitInto(4) distributes remainder across first chunks`` variant = task { |
| 108 | + // 10 elements into 4 chunks: 10 / 4 = 2 remainder 2 → [3; 3; 2; 2] |
| 109 | + let! chunks = |
| 110 | + Gen.getSeqImmutable variant |
| 111 | + |> TaskSeq.splitInto 4 |
| 112 | + |> TaskSeq.toArrayAsync |
| 113 | + |
| 114 | + chunks |> Array.length |> should equal 4 |
| 115 | + chunks.[0] |> should equal [| 1; 2; 3 |] |
| 116 | + chunks.[1] |> should equal [| 4; 5; 6 |] |
| 117 | + chunks.[2] |> should equal [| 7; 8 |] |
| 118 | + chunks.[3] |> should equal [| 9; 10 |] |
| 119 | + } |
| 120 | + |
| 121 | + [<Theory; ClassData(typeof<TestImmTaskSeq>)>] |
| 122 | + let ``TaskSeq-splitInto count greater than length returns one element per chunk`` variant = task { |
| 123 | + // 10 elements into 20 chunks → 10 singleton chunks |
| 124 | + let! chunks = |
| 125 | + Gen.getSeqImmutable variant |
| 126 | + |> TaskSeq.splitInto 20 |
| 127 | + |> TaskSeq.toArrayAsync |
| 128 | + |
| 129 | + chunks |> Array.length |> should equal 10 |
| 130 | + |
| 131 | + chunks |
| 132 | + |> Array.iteri (fun i chunk -> chunk |> should equal [| i + 1 |]) |
| 133 | + } |
| 134 | + |
| 135 | +module SideEffects = |
| 136 | + [<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>] |
| 137 | + let ``TaskSeq-splitInto preserves all side-effectful elements in order`` variant = task { |
| 138 | + do! |
| 139 | + Gen.getSeqWithSideEffect variant |
| 140 | + |> TaskSeq.splitInto 3 |
| 141 | + |> TaskSeq.collect TaskSeq.ofArray |
| 142 | + |> verify1To10 |
| 143 | + } |
0 commit comments