-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.Reduce.Tests.fs
More file actions
156 lines (120 loc) · 4.79 KB
/
TaskSeq.Reduce.Tests.fs
File metadata and controls
156 lines (120 loc) · 4.79 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
module TaskSeq.Tests.Reduce
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.reduce
// TaskSeq.reduceAsync
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.reduce (fun a _ -> a) null
assertNullArg
<| fun () -> TaskSeq.reduceAsync (fun a _ -> Task.fromResult a) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-reduce raises on empty`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.reduce (fun a b -> a + b)
|> Task.ignore
|> should throwAsyncExact typeof<System.ArgumentException>
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-reduceAsync raises on empty`` variant =
fun () ->
Gen.getEmptyVariant variant
|> TaskSeq.reduceAsync (fun a b -> task { return a + b })
|> Task.ignore
|> should throwAsyncExact typeof<System.ArgumentException>
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-reduce folds from first element`` variant = task {
// items are 1..10; sum = 55
let! sum =
Gen.getSeqImmutable variant
|> TaskSeq.reduce (fun acc item -> acc + item)
sum |> should equal 55
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-reduceAsync folds from first element`` variant = task {
let! sum =
Gen.getSeqImmutable variant
|> TaskSeq.reduceAsync (fun acc item -> task { return acc + item })
sum |> should equal 55
}
[<Fact>]
let ``TaskSeq-reduce returns single element without calling folder`` () = task {
let mutable called = false
let! result =
TaskSeq.singleton 42
|> TaskSeq.reduce (fun _ _ ->
called <- true
failwith "should not be called")
result |> should equal 42
called |> should equal false
}
[<Fact>]
let ``TaskSeq-reduceAsync returns single element without calling folder`` () = task {
let mutable called = false
let! result =
TaskSeq.singleton 42
|> TaskSeq.reduceAsync (fun _ _ -> task {
called <- true
return failwith "should not be called"
})
result |> should equal 42
called |> should equal false
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-reduce uses first element as initial accumulator`` variant = task {
// reduce must use element[0] as initial state; for 1..10 summing gives 55
// if it used 0 as initial, sum would also be 55 — but we verify the folder is called n-1 times
let mutable callCount = 0
let! sum =
Gen.getSeqImmutable variant
|> TaskSeq.reduce (fun acc item ->
callCount <- callCount + 1
acc + item)
sum |> should equal 55
callCount |> should equal 9 // 10 elements => 9 reduce calls
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-reduce can concatenate strings`` variant = task {
// items 1..10 as chars: ABCDEFGHIJ
let! letters =
Gen.getSeqImmutable variant
|> TaskSeq.map (fun i -> string (char (i + 64)))
|> TaskSeq.reduce (fun acc item -> acc + item)
letters |> should equal "ABCDEFGHIJ"
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-reduceAsync can concatenate strings`` variant = task {
let! letters =
Gen.getSeqImmutable variant
|> TaskSeq.map (fun i -> string (char (i + 64)))
|> TaskSeq.reduceAsync (fun acc item -> task { return acc + item })
letters |> should equal "ABCDEFGHIJ"
}
module SideEffects =
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-reduce folds correctly with side-effecting sequences`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! sum = ts |> TaskSeq.reduce (fun acc item -> acc + item)
sum |> should equal 55
// second enumeration produces next 10 elements: 11..20, sum = 155
let! sum2 = ts |> TaskSeq.reduce (fun acc item -> acc + item)
sum2 |> should equal 155
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-reduceAsync folds correctly with side-effecting sequences`` variant = task {
let ts = Gen.getSeqWithSideEffect variant
let! sum =
ts
|> TaskSeq.reduceAsync (fun acc item -> task { return acc + item })
sum |> should equal 55
let! sum2 =
ts
|> TaskSeq.reduceAsync (fun acc item -> task { return acc + item })
sum2 |> should equal 155
}