-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.Iteri2Mapi2.Tests.fs
More file actions
316 lines (245 loc) · 10.8 KB
/
TaskSeq.Iteri2Mapi2.Tests.fs
File metadata and controls
316 lines (245 loc) · 10.8 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
module TaskSeq.Tests.Iteri2Mapi2
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.iteri2
// TaskSeq.iteri2Async
// TaskSeq.mapi2
// TaskSeq.mapi2Async
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid for iteri2`` () =
assertNullArg
<| fun () -> TaskSeq.iteri2 (fun _ _ _ -> ()) null (TaskSeq.empty<int>)
assertNullArg
<| fun () -> TaskSeq.iteri2 (fun _ _ _ -> ()) (TaskSeq.empty<int>) null
[<Fact>]
let ``Null source is invalid for iteri2Async`` () =
assertNullArg
<| fun () -> TaskSeq.iteri2Async (fun _ _ _ -> Task.fromResult ()) null (TaskSeq.empty<int>)
assertNullArg
<| fun () -> TaskSeq.iteri2Async (fun _ _ _ -> Task.fromResult ()) (TaskSeq.empty<int>) null
[<Fact>]
let ``Null source is invalid for mapi2`` () =
assertNullArg
<| fun () -> TaskSeq.mapi2 (fun _ x _ -> x) null (TaskSeq.empty<int>)
assertNullArg
<| fun () -> TaskSeq.mapi2 (fun _ x _ -> x) (TaskSeq.empty<int>) null
[<Fact>]
let ``Null source is invalid for mapi2Async`` () =
assertNullArg
<| fun () -> TaskSeq.mapi2Async (fun _ x _ -> Task.fromResult x) null (TaskSeq.empty<int>)
assertNullArg
<| fun () -> TaskSeq.mapi2Async (fun _ x _ -> Task.fromResult x) (TaskSeq.empty<int>) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-iteri2 does nothing when first source is empty`` variant = task {
let tq = Gen.getEmptyVariant variant
let mutable count = 0
do! TaskSeq.iteri2 (fun _ _ _ -> count <- count + 1) tq (TaskSeq.ofSeq [ 1; 2; 3 ])
count |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-iteri2 does nothing when second source is empty`` variant = task {
let tq = Gen.getEmptyVariant variant
let mutable count = 0
do! TaskSeq.iteri2 (fun _ _ _ -> count <- count + 1) (TaskSeq.ofSeq [ 1; 2; 3 ]) tq
count |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapi2 returns empty when first source is empty`` variant =
TaskSeq.mapi2 (fun i x y -> i + x + y) (Gen.getEmptyVariant variant) (TaskSeq.ofSeq [ 1..10 ])
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapi2 returns empty when second source is empty`` variant =
TaskSeq.mapi2 (fun i x y -> i + x + y) (TaskSeq.ofSeq [ 1..10 ]) (Gen.getEmptyVariant variant)
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapi2Async returns empty when first source is empty`` variant =
TaskSeq.mapi2Async (fun i x y -> task { return i + x + y }) (Gen.getEmptyVariant variant) (TaskSeq.ofSeq [ 1..10 ])
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-mapi2Async returns empty when second source is empty`` variant =
TaskSeq.mapi2Async (fun i x y -> task { return i + x + y }) (TaskSeq.ofSeq [ 1..10 ]) (Gen.getEmptyVariant variant)
|> verifyEmpty
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-iteri2 visits all elements of equal-length sequences`` variant = task {
let tq = Gen.getSeqImmutable variant
let mutable sum = 0
do! TaskSeq.iteri2 (fun _ x y -> sum <- sum + x + y) tq (TaskSeq.ofSeq [ 1..10 ])
sum |> should equal 110 // (1..10) + (1..10) = 55 + 55
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-iteri2 passes correct zero-based indices`` variant = task {
let tq = Gen.getSeqImmutable variant
let mutable indexSum = 0
do! TaskSeq.iteri2 (fun i _ _ -> indexSum <- indexSum + i) tq (TaskSeq.ofSeq [ 1..10 ])
indexSum |> should equal 45 // 0+1+2+...+9
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-iteri2Async visits all elements of equal-length sequences`` variant = task {
let tq = Gen.getSeqImmutable variant
let mutable sum = 0
do! TaskSeq.iteri2Async (fun _ x y -> task { sum <- sum + x + y }) tq (TaskSeq.ofSeq [ 1..10 ])
sum |> should equal 110
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-iteri2Async passes correct zero-based indices`` variant = task {
let tq = Gen.getSeqImmutable variant
let mutable indexSum = 0
do! TaskSeq.iteri2Async (fun i _ _ -> task { indexSum <- indexSum + i }) tq (TaskSeq.ofSeq [ 1..10 ])
indexSum |> should equal 45
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapi2 maps in correct order with correct indices`` variant = task {
let tq = Gen.getSeqImmutable variant
let results = ResizeArray()
do!
TaskSeq.mapi2 (fun i x y -> (i, x, y)) tq (TaskSeq.ofSeq [ 10..19 ])
|> TaskSeq.iter (fun t -> results.Add t)
let indices, xs, ys = results |> Seq.toList |> List.unzip3
indices |> should equal [ 0..9 ]
xs |> should equal [ 1..10 ]
ys |> should equal [ 10..19 ]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapi2Async maps in correct order with correct indices`` variant = task {
let tq = Gen.getSeqImmutable variant
let results = ResizeArray()
do!
TaskSeq.mapi2Async (fun i x y -> task { return (i, x, y) }) tq (TaskSeq.ofSeq [ 10..19 ])
|> TaskSeq.iter (fun t -> results.Add t)
let indices, xs, ys = results |> Seq.toList |> List.unzip3
indices |> should equal [ 0..9 ]
xs |> should equal [ 1..10 ]
ys |> should equal [ 10..19 ]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-mapi2 using heterogeneous element types`` variant = task {
let tq = Gen.getSeqImmutable variant
let result =
TaskSeq.mapi2
(fun i x (s: string) -> sprintf "%d:%d:%s" i x s)
tq
(TaskSeq.ofSeq [ "a"; "b"; "c"; "d"; "e"; "f"; "g"; "h"; "i"; "j" ])
let! lst = result |> TaskSeq.toListAsync
lst
|> should equal [ "0:1:a"; "1:2:b"; "2:3:c"; "3:4:d"; "4:5:e"; "5:6:f"; "6:7:g"; "7:8:h"; "8:9:i"; "9:10:j" ]
}
module Truncation =
[<Fact>]
let ``TaskSeq-iteri2 stops at the shorter first sequence`` () = task {
let mutable count = 0
do! TaskSeq.iteri2 (fun _ _ _ -> count <- count + 1) (TaskSeq.ofSeq [ 1..3 ]) (TaskSeq.ofSeq [ 1..10 ])
count |> should equal 3
}
[<Fact>]
let ``TaskSeq-iteri2 stops at the shorter second sequence`` () = task {
let mutable count = 0
do! TaskSeq.iteri2 (fun _ _ _ -> count <- count + 1) (TaskSeq.ofSeq [ 1..10 ]) (TaskSeq.ofSeq [ 1..4 ])
count |> should equal 4
}
[<Fact>]
let ``TaskSeq-iteri2Async stops at the shorter first sequence`` () = task {
let mutable count = 0
do! TaskSeq.iteri2Async (fun _ _ _ -> task { count <- count + 1 }) (TaskSeq.ofSeq [ 1..3 ]) (TaskSeq.ofSeq [ 1..10 ])
count |> should equal 3
}
[<Fact>]
let ``TaskSeq-iteri2Async stops at the shorter second sequence`` () = task {
let mutable count = 0
do! TaskSeq.iteri2Async (fun _ _ _ -> task { count <- count + 1 }) (TaskSeq.ofSeq [ 1..10 ]) (TaskSeq.ofSeq [ 1..4 ])
count |> should equal 4
}
[<Fact>]
let ``TaskSeq-mapi2 stops at the shorter first sequence`` () = task {
let result = TaskSeq.mapi2 (fun i x y -> i + x + y) (TaskSeq.ofSeq [ 1..3 ]) (TaskSeq.ofSeq [ 1..10 ])
let! lst = result |> TaskSeq.toListAsync
lst |> should equal [ 2; 5; 8 ] // (0+1+1), (1+2+2), (2+3+3)
}
[<Fact>]
let ``TaskSeq-mapi2 stops at the shorter second sequence`` () = task {
let result = TaskSeq.mapi2 (fun i x y -> i + x + y) (TaskSeq.ofSeq [ 1..10 ]) (TaskSeq.ofSeq [ 1..4 ])
let! lst = result |> TaskSeq.toListAsync
lst |> should equal [ 2; 5; 8; 11 ] // (0+1+1), (1+2+2), (2+3+3), (3+4+4)
}
[<Fact>]
let ``TaskSeq-mapi2Async stops at the shorter first sequence`` () = task {
let result = TaskSeq.mapi2Async (fun i x y -> task { return i + x + y }) (TaskSeq.ofSeq [ 1..3 ]) (TaskSeq.ofSeq [ 1..10 ])
let! lst = result |> TaskSeq.toListAsync
lst |> should equal [ 2; 5; 8 ]
}
[<Fact>]
let ``TaskSeq-mapi2Async stops at the shorter second sequence`` () = task {
let result = TaskSeq.mapi2Async (fun i x y -> task { return i + x + y }) (TaskSeq.ofSeq [ 1..10 ]) (TaskSeq.ofSeq [ 1..4 ])
let! lst = result |> TaskSeq.toListAsync
lst |> should equal [ 2; 5; 8; 11 ]
}
[<Fact>]
let ``TaskSeq-iteri2 index is always zero-based and matches iteration count even with truncation`` () = task {
let indices = ResizeArray()
do! TaskSeq.iteri2 (fun i _ _ -> indices.Add i) (TaskSeq.ofSeq [ 1..5 ]) (TaskSeq.ofSeq [ 10..12 ])
indices |> Seq.toList |> should equal [ 0; 1; 2 ]
}
[<Fact>]
let ``TaskSeq-mapi2 index is always zero-based and matches iteration count even with truncation`` () = task {
let result = TaskSeq.mapi2 (fun i _ _ -> i) (TaskSeq.ofSeq [ 1..5 ]) (TaskSeq.ofSeq [ 10..12 ])
let! lst = result |> TaskSeq.toListAsync
lst |> should equal [ 0; 1; 2 ]
}
module SideEffects =
[<Fact>]
let ``TaskSeq-mapi2 is lazy and does not evaluate until iterated`` () =
let mutable sideEffect = 0
let ts1 = taskSeq {
sideEffect <- sideEffect + 1
yield 1
sideEffect <- sideEffect + 1
yield 2
}
let ts2 = TaskSeq.ofSeq [ 10; 20 ]
// building the mapped sequence should not evaluate anything
let _ = TaskSeq.mapi2 (fun i x y -> (i, x, y)) ts1 ts2
sideEffect |> should equal 0
[<Fact>]
let ``TaskSeq-iteri2 evaluates side effects in both sequences`` () = task {
let mutable s1 = 0
let mutable s2 = 0
let ts1 = taskSeq {
s1 <- s1 + 1
yield 1
s1 <- s1 + 1
yield 2
}
let ts2 = taskSeq {
s2 <- s2 + 1
yield 10
s2 <- s2 + 1
yield 20
}
do! TaskSeq.iteri2 (fun _ _ _ -> ()) ts1 ts2
s1 |> should equal 2
s2 |> should equal 2
}
[<Fact>]
let ``TaskSeq-iteri2Async evaluates side effects in both sequences`` () = task {
let mutable s1 = 0
let mutable s2 = 0
let ts1 = taskSeq {
s1 <- s1 + 1
yield 1
s1 <- s1 + 1
yield 2
}
let ts2 = taskSeq {
s2 <- s2 + 1
yield 10
s2 <- s2 + 1
yield 20
}
do! TaskSeq.iteri2Async (fun _ _ _ -> task { () }) ts1 ts2
s1 |> should equal 2
s2 |> should equal 2
}