-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.InsertAt.Tests.fs
More file actions
560 lines (444 loc) · 18.5 KB
/
TaskSeq.InsertAt.Tests.fs
File metadata and controls
560 lines (444 loc) · 18.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
module TaskSeq.Tests.InsertAt
open System
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.insertAt
// TaskSeq.insertManyAt
//
exception SideEffectPastEnd of string
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg <| fun () -> TaskSeq.insertAt 0 99 null
assertNullArg
<| fun () -> TaskSeq.insertManyAt 0 TaskSeq.empty null
[<Fact>]
let ``Null values argument is invalid for insertManyAt`` () =
assertNullArg
<| fun () -> TaskSeq.insertManyAt 0 null (TaskSeq.ofList [ 1 ])
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-insertAt(0) on empty input returns singleton`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.insertAt 0 42
|> verifySingleton 42
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-insertAt(1) on empty input should throw ArgumentException`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.insertAt 1 42
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
[<Fact>]
let ``TaskSeq-insertAt(-1) should throw ArgumentException on any input`` () =
fun () ->
TaskSeq.empty<int>
|> TaskSeq.insertAt -1 42
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
TaskSeq.init 10 id
|> TaskSeq.insertAt -1 42
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
[<Fact>]
let ``TaskSeq-insertAt(-1) should throw ArgumentException before awaiting`` () =
fun () ->
taskSeq {
do! longDelay ()
if false then
yield 0 // type inference
}
|> TaskSeq.insertAt -1 42
|> ignore // throws even without running the async. Bad coding, don't ignore a task!
// test without awaiting the async
|> should throw typeof<ArgumentException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-insertManyAt(0) on empty input returns singleton`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.insertManyAt 0 (TaskSeq.ofArray [| 42; 43; 44 |])
|> verifyDigitsAsString "jkl"
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-insertManyAt(1) on empty input should throw InvalidOperation`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.insertManyAt 1 (TaskSeq.ofArray [| 42; 43; 44 |])
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
[<Fact>]
let ``TaskSeq-insertManyAt(-1) should throw ArgumentException on any input`` () =
fun () ->
TaskSeq.empty<int>
|> TaskSeq.insertManyAt -1 (TaskSeq.ofArray [| 42; 43; 44 |])
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
TaskSeq.init 10 id
|> TaskSeq.insertManyAt -1 (TaskSeq.ofArray [| 42; 43; 44 |])
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
[<Fact>]
let ``TaskSeq-insertManyAt(-1) should throw ArgumentException before awaiting`` () =
fun () ->
taskSeq {
do! longDelay ()
if false then
yield 0 // type inference
}
|> TaskSeq.insertManyAt -1 (TaskSeq.ofArray [| 42; 43; 44 |])
|> ignore // throws even without running the async. Bad coding, don't ignore a task!
// test without awaiting the async
|> should throw typeof<ArgumentException>
[<Fact>]
let ``TaskSeq-insertManyAt() with empty sequenc as source`` () =
TaskSeq.empty<int>
|> TaskSeq.insertManyAt 0 TaskSeq.empty
|> verifyEmpty
[<Fact>]
let ``TaskSeq-insertManyAt() with empty sequence as source applies to non-empty sequence`` () =
TaskSeq.init 10 id
|> TaskSeq.insertManyAt 2 TaskSeq.empty
|> verify0To9
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertAt can insert after end of sequence`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 10 99
|> verifyDigitsAsString "ABCDEFGHIJ£"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertAt inserts item immediately after the indexed position`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 0 99
|> verifyDigitsAsString "£ABCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 1 99
|> verifyDigitsAsString "A£BCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 5 99
|> verifyDigitsAsString "ABCDE£FGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 10 99
|> verifyDigitsAsString "ABCDEFGHIJ£"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertAt can be repeated in a chain`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 0 99
|> TaskSeq.insertAt 0 99
|> TaskSeq.insertAt 0 99
|> TaskSeq.insertAt 0 99
|> TaskSeq.insertAt 0 99
|> verifyDigitsAsString "£££££ABCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 10 99
|> TaskSeq.insertAt 11 99
|> TaskSeq.insertAt 12 99
|> TaskSeq.insertAt 13 99
|> TaskSeq.insertAt 14 99
|> verifyDigitsAsString "ABCDEFGHIJ£££££"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertAt applies to a position in the new sequence`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 0 99
|> TaskSeq.insertAt 2 99
|> TaskSeq.insertAt 4 99
|> TaskSeq.insertAt 6 99
|> TaskSeq.insertAt 8 99
|> TaskSeq.insertAt 10 99
|> TaskSeq.insertAt 12 99
|> TaskSeq.insertAt 14 99
|> TaskSeq.insertAt 16 99
|> TaskSeq.insertAt 18 99
|> TaskSeq.insertAt 20 99
|> verifyDigitsAsString "£A£B£C£D£E£F£G£H£I£J£"
}
[<Fact>]
let ``TaskSeq-insertAt can be applied to an infinite task sequence`` () =
TaskSeq.initInfinite id
|> TaskSeq.insertAt 100 12345
|> TaskSeq.item 100
|> Task.map (should equal 12345)
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertAt throws when there are not enough elements`` variant =
fun () ->
TaskSeq.singleton 1
// insert after 1
|> TaskSeq.insertAt 2 99
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 11 99
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.insertAt 10_000_000 99
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertManyAt can insert after end of sequence`` variant = task {
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 10 (TaskSeq.ofArray [| 99; 100; 101 |])
|> verifyDigitsAsString "ABCDEFGHIJ£¤¥"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertManyAt inserts item immediately after the indexed position`` variant = task {
let values = TaskSeq.ofArray [| 99; 100; 101 |]
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 0 values
|> verifyDigitsAsString "£¤¥ABCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 1 values
|> verifyDigitsAsString "A£¤¥BCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 5 values
|> verifyDigitsAsString "ABCDE£¤¥FGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 10 values
|> verifyDigitsAsString "ABCDEFGHIJ£¤¥"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertManyAt can be repeated in a chain`` variant = task {
let values = TaskSeq.ofArray [| 99; 100; 101 |]
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 0 values
|> TaskSeq.insertManyAt 0 values
|> TaskSeq.insertManyAt 0 values
|> TaskSeq.insertManyAt 0 values
|> TaskSeq.insertManyAt 0 values
|> verifyDigitsAsString "£¤¥£¤¥£¤¥£¤¥£¤¥ABCDEFGHIJ"
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 10 values
|> TaskSeq.insertManyAt 11 values
|> TaskSeq.insertManyAt 12 values
|> TaskSeq.insertManyAt 13 values
|> TaskSeq.insertManyAt 14 values
|> verifyDigitsAsString "ABCDEFGHIJ£££££¤¥¤¥¤¥¤¥¤¥"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertManyAt applies to a position in the new sequence`` variant = task {
let values = TaskSeq.ofArray [| 99; 100; 101 |]
do!
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 0 values
|> TaskSeq.insertManyAt 4 values
|> TaskSeq.insertManyAt 8 values
|> TaskSeq.insertManyAt 12 values
|> TaskSeq.insertManyAt 16 values
|> TaskSeq.insertManyAt 20 values
|> TaskSeq.insertManyAt 24 values
|> TaskSeq.insertManyAt 28 values
|> TaskSeq.insertManyAt 32 values
|> TaskSeq.insertManyAt 36 values
|> TaskSeq.insertManyAt 40 values
|> verifyDigitsAsString "£¤¥A£¤¥B£¤¥C£¤¥D£¤¥E£¤¥F£¤¥G£¤¥H£¤¥I£¤¥J£¤¥"
}
[<Fact>]
let ``TaskSeq-insertManyAt (infinite) can be applied to an infinite task sequence`` () =
TaskSeq.initInfinite id
|> TaskSeq.insertManyAt 100 (TaskSeq.init 10 id)
|> TaskSeq.item 109
|> Task.map (should equal 9)
[<Fact>]
let ``TaskSeq-insertManyAt (infinite) with infinite task sequence as argument`` () =
TaskSeq.init 100 id
|> TaskSeq.insertManyAt 100 (TaskSeq.initInfinite id)
|> TaskSeq.item 1999
|> Task.map (should equal 1899) // the inserted infinite sequence started at 100, with value 0.
[<Fact>]
let ``TaskSeq-insertManyAt (infinite) with source and values both as infinite task sequence`` () = task {
// using two infinite task sequences
let ts =
TaskSeq.initInfinite id
|> TaskSeq.insertManyAt 1000 (TaskSeq.initInfinite id)
// the inserted infinite sequence started at 1000, with value 0.
do! ts |> TaskSeq.item 999 |> Task.map (should equal 999)
do! ts |> TaskSeq.item 1000 |> Task.map (should equal 0)
do! ts |> TaskSeq.item 2000 |> Task.map (should equal 1000)
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-insertManyAt throws when there are not enough elements`` variant =
let values = TaskSeq.ofArray [| 99; 100; 101 |]
fun () ->
TaskSeq.singleton 1
// insert after 1
|> TaskSeq.insertManyAt 2 values
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 11 values
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
fun () ->
Gen.getSeqImmutable variant
|> TaskSeq.insertManyAt 10_000_000 values
|> consumeTaskSeq
|> should throwAsyncExact typeof<ArgumentException>
module SideEffects =
// PoC test
[<Fact>]
let ``Seq-insertAt (poc-proof) will execute side effect before index`` () =
// NOTE: this test is for documentation purposes only, to show this behavior that is tested in this module
// this shows that Seq.insertAt executes more side effects than necessary.
let mutable x = 42
let items = seq {
x <- x + 1 // we are proving this gets executed with insertAt(0)
yield x
yield x * 2
}
items
|> Seq.insertAt 0 99
|> Seq.item 0 // put enumerator to inserted item
|> ignore
x |> should equal 43 // one time side effect executed. QED
[<Fact>]
let ``TaskSeq-insertAt(0) will execute side effects at start of sequence`` () =
// NOTE: while not strictly necessary, this mirrors behavior of Seq.insertAt
let mutable x = 42 // for this test, the potential mutation should not actually occur
let items = taskSeq {
x <- x + 1 // this is executed even with insertAt(0)
yield x
yield x * 2
}
items
|> TaskSeq.insertAt 0 99
|> TaskSeq.item 0 // consume only the first item
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // the mutable was updated
[<Fact>]
let ``TaskSeq-insertAt will execute last side effect when inserting past end`` () =
let mutable x = 42
let items = taskSeq {
yield x
yield x * 2
yield x * 4
x <- x + 1 // this is executed when inserting past last item
}
items
|> TaskSeq.insertAt 3 99
|> TaskSeq.item 3
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // as with 'seq', see first test in this block, we execute the side effect at index
[<Fact>]
let ``TaskSeq-insertAt will execute side effect just before index`` () =
let mutable x = 42
let items = taskSeq {
yield x
x <- x + 1 // this is executed, even though we insert after the first item
yield x * 2
yield x * 4
}
items
|> TaskSeq.insertAt 1 99
|> TaskSeq.item 1
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // as with 'seq', see first test in this block, we execute the side effect at index
[<Fact>]
let ``TaskSeq-insertAt exception at insertion index is thrown`` () =
fun () ->
taskSeq {
yield 1
yield! [ 2; 3 ]
do SideEffectPastEnd "at the end" |> raise // this is raised
yield 4
}
|> TaskSeq.insertAt 3 99
|> TaskSeq.item 3
|> Task.ignore
|> should throwAsyncExact typeof<SideEffectPastEnd>
[<Fact>]
let ``TaskSeq-insertAt prove that an exception from the taskSeq is thrown instead of exception from function`` () =
let items = taskSeq {
yield 42
yield! [ 1; 2 ]
do SideEffectPastEnd "at the end" |> raise // we SHOULD get here before ArgumentException is raised
}
fun () -> items |> TaskSeq.insertAt 4 99 |> consumeTaskSeq // this would raise ArgumentException normally, but not now
|> should throwAsyncExact typeof<SideEffectPastEnd>
[<Fact>]
let ``TaskSeq-insertManyAt(0) will execute side effects at start of sequence`` () =
// NOTE: while not strictly necessary, this mirrors behavior of Seq.insertManyAt
let mutable x = 42 // for this test, the potential mutation should not actually occur
let items = taskSeq {
x <- x + 1 // this is executed even with insertManyAt(0)
yield x
yield x * 2
}
items
|> TaskSeq.insertManyAt 0 (taskSeq { yield! [ 99; 100 ] })
|> TaskSeq.item 0 // consume only the first item
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // the mutable was updated
[<Fact>]
let ``TaskSeq-insertManyAt will execute last side effect when inserting past end`` () =
let mutable x = 42
let items = taskSeq {
yield x
yield x * 2
yield x * 4
x <- x + 1 // this is executed when inserting past last item
}
items
|> TaskSeq.insertManyAt 3 (taskSeq { yield! [ 99; 100 ] })
|> TaskSeq.item 3
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // as with 'seq', see first test in this block, we execute the side effect at index
[<Fact>]
let ``TaskSeq-insertManyAt will execute side effect just before index`` () =
let mutable x = 42
let items = taskSeq {
yield x
x <- x + 1 // this is executed, even though we insert after the first item
yield x * 2
yield x * 4
}
items
|> TaskSeq.insertManyAt 1 (taskSeq { yield! [ 99; 100 ] })
|> TaskSeq.item 1
|> Task.map (should equal 99)
|> Task.map (fun () -> x |> should equal 43) // as with 'seq', see first test in this block, we execute the side effect at index
[<Fact>]
let ``TaskSeq-insertManyAt exception at insertion index is thrown`` () =
fun () ->
taskSeq {
yield 1
yield! [ 2; 3 ]
do SideEffectPastEnd "at the end" |> raise // this is raised
yield 4
}
|> TaskSeq.insertManyAt 3 (taskSeq { yield! [ 99; 100 ] })
|> TaskSeq.item 3
|> Task.ignore
|> should throwAsyncExact typeof<SideEffectPastEnd>
[<Fact>]
let ``TaskSeq-insertManyAt prove that an exception from the taskSeq is thrown instead of exception from function`` () =
let items = taskSeq {
yield 42
yield! [ 1; 2 ]
do SideEffectPastEnd "at the end" |> raise // we SHOULD get here before ArgumentException is raised
}
fun () ->
items
|> TaskSeq.insertManyAt 4 (taskSeq { yield! [ 99; 100 ] })
|> consumeTaskSeq // this would raise ArgumentException normally, but not now
|> should throwAsyncExact typeof<SideEffectPastEnd>