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