-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.Item.Tests.fs
316 lines (249 loc) · 10.3 KB
/
TaskSeq.Item.Tests.fs
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.Item
open System
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.item
// TaskSeq.tryItem
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg <| fun () -> TaskSeq.item 42 null
assertNullArg <| fun () -> TaskSeq.tryItem 42 null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-item throws on empty sequences`` variant = task {
fun () -> Gen.getEmptyVariant variant |> TaskSeq.item 0 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-item throws on empty sequence - high index`` variant = task {
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.item 50000
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryItem returns None on empty sequences`` variant = task {
let! nothing = Gen.getEmptyVariant variant |> TaskSeq.tryItem 0
nothing |> should be None'
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-tryItem returns None on empty sequence - high index`` variant = task {
let! nothing = Gen.getEmptyVariant variant |> TaskSeq.tryItem 50000
nothing |> should be None'
}
module Singleton =
[<Fact>]
let ``TaskSeq-item gets the first item in a singleton sequence`` () = task {
let! head = taskSeq { yield 10 } |> TaskSeq.item 0 // zero-based!
head |> should equal 10
}
[<Fact>]
let ``TaskSeq-tryItem gets the first item in a singleton sequence`` () = task {
let! head = taskSeq { yield 10 } |> TaskSeq.tryItem 0 // zero-based!
head |> should be Some'
head |> should equal (Some 10)
}
[<Fact>]
let ``TaskSeq-item throws when accessing 2nd item in singleton sequence`` () = task {
fun () -> taskSeq { yield 10 } |> TaskSeq.item 1 |> Task.ignore // zero-based!
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-tryItem returns None when accessing 2nd item in singleton sequence`` () = task {
let! nothing = taskSeq { yield 10 } |> TaskSeq.tryItem 1 // zero-based!
nothing |> should be None'
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-item throws when not found`` variant = task {
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.item 10
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryItem returns None when not found`` variant = task {
let! nothing = Gen.getSeqImmutable variant |> TaskSeq.tryItem 10 // zero-based index
nothing |> should be None'
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-item can get the first and last item in a longer sequence`` variant = task {
let! head = Gen.getSeqImmutable variant |> TaskSeq.item 0
let! tail = Gen.getSeqImmutable variant |> TaskSeq.item 9
head |> should equal 1
tail |> should equal 10
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-tryItem can get the first and last item in a longer sequence`` variant = task {
let! head = Gen.getSeqImmutable variant |> TaskSeq.tryItem 0 // zero-based!
let! tail = Gen.getSeqImmutable variant |> TaskSeq.tryItem 9
head |> should equal (Some 1)
tail |> should equal (Some 10)
}
module SideEffect =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-item prove it searches the whole sequence`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
fun () -> ts |> TaskSeq.item 10 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
let! head = TaskSeq.head ts
head |> should equal 11 // all side effects have executed
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-tryItem prove it searches the whole sequence`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! item = ts |> TaskSeq.tryItem 10
item |> should be None'
let! head = TaskSeq.head ts
head |> should equal 11 // all side effects have executed
}
[<Fact>]
let ``TaskSeq-item prove we don't iterate further than the found item`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1
yield 2
i <- i + 1 // we never get here
}
// zero-based index
do! ts |> TaskSeq.item 1 |> Task.map (should equal 2)
i |> should equal 2 // last side effect is not executed
}
[<Fact>]
let ``TaskSeq-tryItem prove we don't iterate further than the found item`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1
yield 2
i <- i + 1 // we never get here
}
// zero-based index
do! ts |> TaskSeq.tryItem 1 |> Task.map (should equal (Some 2))
i |> should equal 2 // last side effect is not executed
}
[<Fact>]
let ``TaskSeq-item prove we iterate beyond the end when not found`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1
yield 2
i <- i + 1 // we never get here
}
// zero-based
fun () -> ts |> TaskSeq.item 2 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
i |> should equal 3 // last side effect MUST be executed
}
[<Fact>]
let ``TaskSeq-tryItem prove we iterate beyond the end when not found`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
yield 1
i <- i + 1
yield 2
i <- i + 1 // we never get here
}
do! ts |> TaskSeq.tryItem 2 |> Task.map (should be None')
i |> should equal 3 // last side effect MUST be executed
}
module Performance =
[<Theory; InlineData 10_000; InlineData 100_000; InlineData 1_000_000>]
let ``TaskSeq-tryItem in a very long sequence -- yield variant`` total = task {
let! head =
taskSeq {
for i in [ 0..total ] do
yield i
}
|> TaskSeq.tryItem total // zero-based!
head |> should equal (Some total)
}
[<Theory; InlineData 10_000; InlineData 100_000; InlineData 1_000_000>]
let ``[compare] Seq-tryItem in a very long sequence -- yield using F# Seq`` total = task {
// this test is just for smoke-test perf comparison with TaskSeq above
let head =
seq {
for i in [ 0..total ] do
yield i
}
|> Seq.tryItem total // zero-based!
head |> should equal (Some total)
}
[<Theory; InlineData 10_000; InlineData 100_000; InlineData 1_000_000>]
let ``TaskSeq-tryItem in a very long sequence -- array variant`` total = task {
let! head = taskSeq { yield! [| 0..total |] } |> TaskSeq.tryItem total // zero-based!
head |> should equal (Some total)
}
[<Theory; InlineData 10_000; InlineData 100_000; InlineData 1_000_000>]
let ``[compare] Seq-tryItem in a very long sequence -- array using F# Seq`` total = task {
// this test is just for smoke-test perf comparison with TaskSeq above
let head = seq { yield! [| 0..total |] } |> Seq.tryItem total // zero-based!
head |> should equal (Some total)
}
module Other =
[<Fact>]
let ``TaskSeq-item accepts Int-MaxValue`` () = task {
let make50 () = Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 50
fun () -> make50 () |> TaskSeq.item Int32.MaxValue |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
TaskSeq.empty<string>
|> TaskSeq.item Int32.MaxValue
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-tryItem accepts Int-MaxValue`` () = task {
let! nope =
Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 50
|> TaskSeq.tryItem Int32.MaxValue
nope |> should be None'
let! nope = TaskSeq.empty<string> |> TaskSeq.tryItem Int32.MaxValue
nope |> should be None'
}
[<Fact>]
let ``TaskSeq-item always throws with negative values`` () = task {
let make50 () = Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 50
fun () -> make50 () |> TaskSeq.item -1 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () -> make50 () |> TaskSeq.item -10000 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () -> make50 () |> TaskSeq.item Int32.MinValue |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () -> TaskSeq.empty<string> |> TaskSeq.item -1 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () -> TaskSeq.empty<string> |> TaskSeq.item -10000 |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
TaskSeq.empty<string>
|> TaskSeq.item Int32.MinValue
|> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
}
[<Fact>]
let ``TaskSeq-tryItem throws with negative values`` () = task {
let make50 () = Gen.sideEffectTaskSeqMicro 50L<µs> 1000L<µs> 50
let! nothing = make50 () |> TaskSeq.tryItem -1
nothing |> should be None'
let! nothing = make50 () |> TaskSeq.tryItem -10000
nothing |> should be None'
let! nothing = make50 () |> TaskSeq.tryItem Int32.MinValue
nothing |> should be None'
let! nothing = TaskSeq.empty<string> |> TaskSeq.tryItem -1
nothing |> should be None'
let! nothing = TaskSeq.empty<string> |> TaskSeq.tryItem -10000
nothing |> should be None'
let! nothing = TaskSeq.empty<string> |> TaskSeq.tryItem Int32.MinValue
nothing |> should be None'
}