-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.Fold.Tests.fs
More file actions
270 lines (211 loc) · 7.85 KB
/
TaskSeq.Fold.Tests.fs
File metadata and controls
270 lines (211 loc) · 7.85 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
module TaskSeq.Tests.Fold
open System.Text
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.fold
// TaskSeq.foldAsync
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.fold (fun _ _ -> 42) 0 null
assertNullArg
<| fun () -> TaskSeq.foldAsync (fun _ _ -> Task.fromResult 42) 0 null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-fold takes state when empty`` variant = task {
let! empty =
Gen.getEmptyVariant variant
|> TaskSeq.fold (fun _ item -> char (item + 64)) '_'
empty |> should equal '_'
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-foldAsync takes state when empty`` variant = task {
let! alphabet =
Gen.getEmptyVariant variant
|> TaskSeq.foldAsync (fun _ item -> task { return char (item + 64) }) '_'
alphabet |> should equal '_'
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-fold does not call folder function when empty`` variant = task {
let mutable called = false
let! _ =
Gen.getEmptyVariant variant
|> TaskSeq.fold
(fun state _ ->
called <- true
state)
0
called |> should be False
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-foldAsync does not call folder function when empty`` variant = task {
let mutable called = false
let! _ =
Gen.getEmptyVariant variant
|> TaskSeq.foldAsync
(fun state _ -> task {
called <- true
return state
})
0
called |> should be False
}
module Functionality =
[<Fact>]
let ``TaskSeq-fold calls folder exactly N times for N elements`` () = task {
let mutable callCount = 0
let! _ =
TaskSeq.ofList [ 1; 2; 3; 4; 5 ]
|> TaskSeq.fold
(fun acc item ->
callCount <- callCount + 1
acc + item)
0
callCount |> should equal 5
}
[<Fact>]
let ``TaskSeq-foldAsync calls folder exactly N times for N elements`` () = task {
let mutable callCount = 0
let! _ =
TaskSeq.ofList [ 1; 2; 3; 4; 5 ]
|> TaskSeq.foldAsync
(fun acc item -> task {
callCount <- callCount + 1
return acc + item
})
0
callCount |> should equal 5
}
[<Fact>]
let ``TaskSeq-fold over singleton calls folder once`` () = task {
let mutable callCount = 0
let! result =
TaskSeq.singleton 42
|> TaskSeq.fold
(fun acc item ->
callCount <- callCount + 1
acc + item)
0
result |> should equal 42
callCount |> should equal 1
}
[<Fact>]
let ``TaskSeq-fold over two elements calls folder twice`` () = task {
let mutable callCount = 0
let! result =
taskSeq {
yield 10
yield 20
}
|> TaskSeq.fold
(fun acc item ->
callCount <- callCount + 1
acc + item)
0
result |> should equal 30
callCount |> should equal 2
}
[<Fact>]
let ``TaskSeq-fold is left-associative: applies folder left-to-right`` () = task {
// For non-commutative ops like string concat, order matters.
// fold f s [a;b;c] = f (f (f s a) b) c
let! result =
TaskSeq.ofList [ "b"; "c"; "d" ]
|> TaskSeq.fold (fun acc item -> acc + item) "a"
result |> should equal "abcd"
}
[<Fact>]
let ``TaskSeq-foldAsync is left-associative: applies folder left-to-right`` () = task {
let! result =
TaskSeq.ofList [ "b"; "c"; "d" ]
|> TaskSeq.foldAsync (fun acc item -> task { return acc + item }) "a"
result |> should equal "abcd"
}
[<Fact>]
let ``TaskSeq-fold with null initial state works for reference types`` () = task {
let! result =
TaskSeq.ofList [ "hello"; " "; "world" ]
|> TaskSeq.fold
(fun acc item ->
match acc with
| null -> item
| _ -> acc + item)
null
result |> should equal "hello world"
}
[<Fact>]
let ``TaskSeq-foldAsync and fold return the same result for pure functions`` () = task {
let input = [ 1..10 ]
let! syncResult =
TaskSeq.ofList input
|> TaskSeq.fold (fun acc item -> acc + item) 0
let! asyncResult =
TaskSeq.ofList input
|> TaskSeq.foldAsync (fun acc item -> task { return acc + item }) 0
syncResult |> should equal asyncResult
}
[<Fact>]
let ``TaskSeq-fold accumulates a list in correct order`` () = task {
let! result =
TaskSeq.ofList [ 1; 2; 3; 4; 5 ]
|> TaskSeq.fold (fun acc item -> acc @ [ item ]) []
result |> should equal [ 1; 2; 3; 4; 5 ]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-fold sum over immutable variants`` variant = task {
// items are 1..10; sum = 55
let! result =
Gen.getSeqImmutable variant
|> TaskSeq.fold (fun acc item -> acc + item) 0
result |> should equal 55
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-foldAsync sum over immutable variants`` variant = task {
let! result =
Gen.getSeqImmutable variant
|> TaskSeq.foldAsync (fun acc item -> task { return acc + item }) 0
result |> should equal 55
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-fold folds with every item`` variant = task {
let! letters =
(StringBuilder(), Gen.getSeqImmutable variant)
||> TaskSeq.fold (fun state item -> state.Append(char item + '@'))
letters.ToString() |> should equal "ABCDEFGHIJ"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-foldAsync folds with every item`` variant = task {
let! letters =
(StringBuilder(), Gen.getSeqImmutable variant)
||> TaskSeq.foldAsync (fun state item -> task { return state.Append(char item + '@') })
letters.ToString() |> should equal "ABCDEFGHIJ"
}
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-fold folds with every item, next fold has different state`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! letters =
(StringBuilder(), ts)
||> TaskSeq.fold (fun state item -> state.Append(char item + '@'))
string letters |> should equal "ABCDEFGHIJ"
let! moreLetters =
(letters, ts)
||> TaskSeq.fold (fun state item -> state.Append(char item + '@'))
string moreLetters |> should equal "ABCDEFGHIJKLMNOPQRST"
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-foldAsync folds with every item, next fold has different state`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! letters =
(StringBuilder(), ts)
||> TaskSeq.foldAsync (fun state item -> task { return state.Append(char item + '@') })
string letters |> should equal "ABCDEFGHIJ"
let! moreLetters =
(letters, ts)
||> TaskSeq.foldAsync (fun state item -> task { return state.Append(char item + '@') })
string moreLetters |> should equal "ABCDEFGHIJKLMNOPQRST"
}