-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTaskSeq.MaxMin.Tests.fs
318 lines (253 loc) · 9.27 KB
/
TaskSeq.MaxMin.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
317
318
module TaskSeq.Tests.MaxMin
open System
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.max
// TaskSeq.min
// TaskSeq.maxBy
// TaskSeq.minBy
// TaskSeq.maxByAsync
// TaskSeq.minByAsync
//
type MinMax =
| Max = 0
| Min = 1
| MaxBy = 2
| MinBy = 3
| MaxByAsync = 4
| MinByAsync = 5
module MinMax =
let getFunction =
function
| MinMax.Max -> TaskSeq.max
| MinMax.Min -> TaskSeq.min
| MinMax.MaxBy -> TaskSeq.maxBy id
| MinMax.MinBy -> TaskSeq.minBy id
| MinMax.MaxByAsync -> TaskSeq.maxByAsync Task.fromResult
| MinMax.MinByAsync -> TaskSeq.minByAsync Task.fromResult
| _ -> failwith "impossible"
let getByFunction =
function
| MinMax.MaxBy -> TaskSeq.maxBy
| MinMax.MinBy -> TaskSeq.minBy
| MinMax.MaxByAsync -> fun by -> TaskSeq.maxByAsync (by >> Task.fromResult)
| MinMax.MinByAsync -> fun by -> TaskSeq.minByAsync (by >> Task.fromResult)
| _ -> failwith "impossible"
let getAll () =
[ MinMax.Max; MinMax.Min; MinMax.MaxBy; MinMax.MinBy; MinMax.MaxByAsync; MinMax.MinByAsync ]
|> List.map getFunction
let getAllMin () =
[ MinMax.Min; MinMax.MinBy; MinMax.MinByAsync ]
|> List.map getFunction
let getAllMax () =
[ MinMax.Max; MinMax.MaxBy; MinMax.MaxByAsync ]
|> List.map getFunction
let isMin =
function
| MinMax.Min
| MinMax.MinBy
| MinMax.MinByAsync -> true
| _ -> false
let isMax = isMin >> not
type AllMinMaxFunctions() as this =
inherit TheoryData<MinMax>()
do
this.Add MinMax.Max
this.Add MinMax.Min
this.Add MinMax.MaxBy
this.Add MinMax.MinBy
this.Add MinMax.MaxByAsync
this.Add MinMax.MinByAsync
type JustMin() as this =
inherit TheoryData<MinMax>()
do
this.Add MinMax.Min
this.Add MinMax.MinBy
this.Add MinMax.MinByAsync
type JustMax() as this =
inherit TheoryData<MinMax>()
do
this.Add MinMax.Max
this.Add MinMax.MaxBy
this.Add MinMax.MaxByAsync
type JustMinMaxBy() as this =
inherit TheoryData<MinMax>()
do
this.Add MinMax.MaxBy
this.Add MinMax.MinBy
this.Add MinMax.MaxByAsync
this.Add MinMax.MinByAsync
module EmptySeq =
[<Theory; ClassData(typeof<AllMinMaxFunctions>)>]
let ``Null source raises ArgumentNullException`` (minMaxType: MinMax) =
let minMax = MinMax.getFunction minMaxType
assertNullArg <| fun () -> minMax (null: TaskSeq<int>)
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``Empty sequence raises ArgumentException`` variant =
let test minMax =
let data = Gen.getEmptyVariant variant
fun () -> minMax data |> Task.ignore
|> should throwAsyncExact typeof<ArgumentException>
for minMax in MinMax.getAll () do
test minMax
module Functionality =
[<Fact>]
let ``TaskSeq-max should return maximum`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! max = TaskSeq.max ts
max |> should equal 'Z'
}
[<Fact>]
let ``TaskSeq-maxBy should return maximum of input, not the projection`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! max = TaskSeq.maxBy id ts
max |> should equal 'Z'
let ts = [ 1..10 ] |> TaskSeq.ofList
let! max = TaskSeq.maxBy (~-) ts
max |> should equal 1 // as negated, -1 is highest, should not return projection, but original
}
[<Fact>]
let ``TaskSeq-maxByAsync should return maximum of input, not the projection`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! max = TaskSeq.maxByAsync Task.fromResult ts
max |> should equal 'Z'
let ts = [ 1..10 ] |> TaskSeq.ofList
let! max = TaskSeq.maxByAsync (fun x -> Task.fromResult -x) ts
max |> should equal 1 // as negated, -1 is highest, should not return projection, but original
}
[<Fact>]
let ``TaskSeq-min should return minimum`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! min = TaskSeq.min ts
min |> should equal 'A'
}
[<Fact>]
let ``TaskSeq-minBy should return minimum of input, not the projection`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! min = TaskSeq.minBy id ts
min |> should equal 'A'
let ts = [ 1..10 ] |> TaskSeq.ofList
let! min = TaskSeq.minBy (~-) ts
min |> should equal 10 // as negated, -10 is lowest, should not return projection, but original
}
[<Fact>]
let ``TaskSeq-minByAsync should return minimum of input, not the projection`` () = task {
let ts = [ 'A' .. 'Z' ] |> TaskSeq.ofList
let! min = TaskSeq.minByAsync Task.fromResult ts
min |> should equal 'A'
let ts = [ 1..10 ] |> TaskSeq.ofList
let! min = TaskSeq.minByAsync (fun x -> Task.fromResult -x) ts
min |> should equal 10 // as negated, 1 is highest, should not return projection, but original
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-max, maxBy, maxByAsync returns maximum`` variant = task {
let ts = Gen.getSeqImmutable variant
for max in MinMax.getAllMax () do
let! max = max ts
max |> should equal 10
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-min, minBy, minByAsync returns minimum`` variant = task {
let ts = Gen.getSeqImmutable variant
for min in MinMax.getAllMin () do
let! min = min ts
min |> should equal 1
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-maxBy, maxByAsync returns maximum after projection`` variant = task {
let ts = Gen.getSeqImmutable variant
let! max = ts |> TaskSeq.maxBy (fun x -> -x)
max |> should equal 1 // because -1 maps to item '1'
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-minBy, minByAsync returns minimum after projection`` variant = task {
let ts = Gen.getSeqImmutable variant
let! min = ts |> TaskSeq.minBy (fun x -> -x)
min |> should equal 10 // because -10 maps to item 10
}
module SideEffects =
[<Fact>]
let ``TaskSeq-max, maxBy, maxByAsync prove we execute after-effects`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
yield i // 2
i <- i + 1
yield i // 3
yield i + 1 // 4
i <- i + 1 // we should get here
}
do! ts |> TaskSeq.max |> Task.map (should equal 4)
do! ts |> TaskSeq.maxBy (~-) |> Task.map (should equal 6) // next iteration & negation "-6" maps to "6"
do!
ts
|> TaskSeq.maxByAsync Task.fromResult
|> Task.map (should equal 12) // no negation
i |> should equal 12
}
[<Fact>]
let ``TaskSeq-min, minBy, minByAsync prove we execute after-effects test`` () = task {
let mutable i = 0
let ts = taskSeq {
i <- i + 1
i <- i + 1
yield i // 2
i <- i + 1
yield i // 3
yield i + 1 // 4
i <- i + 1 // we should get here
}
do! ts |> TaskSeq.min |> Task.map (should equal 2)
do! ts |> TaskSeq.minBy (~-) |> Task.map (should equal 8) // next iteration & negation
do!
ts
|> TaskSeq.minByAsync Task.fromResult
|> Task.map (should equal 10) // no negation
i |> should equal 12
}
[<Theory; ClassData(typeof<JustMax>)>]
let ``TaskSeq-max with sequence that changes length`` (minMax: MinMax) = task {
let max = MinMax.getFunction minMax
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
do! max ts |> Task.map (should equal 10)
do! max ts |> Task.map (should equal 20) // mutable state dangers!!
do! max ts |> Task.map (should equal 30) // id
}
[<Theory; ClassData(typeof<JustMin>)>]
let ``TaskSeq-min with sequence that changes length`` (minMax: MinMax) = task {
let min = MinMax.getFunction minMax
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
do! min ts |> Task.map (should equal 1)
do! min ts |> Task.map (should equal 1) // same min after changing state
do! min ts |> Task.map (should equal 1) // id
}
[<Theory; ClassData(typeof<JustMinMaxBy>)>]
let ``TaskSeq-minBy, maxBy with sequence that changes length`` (minMax: MinMax) =
let mutable i = 0
let ts = taskSeq {
i <- i + 10
yield! [ 1..i ]
}
let test minMaxFn v =
if MinMax.isMin minMax then
// this ensures the "min" version behaves like the "max" version
minMaxFn (~-) ts |> Task.map (should equal v)
else
minMaxFn id ts |> Task.map (should equal v)
task {
do! test (MinMax.getByFunction minMax) 10
do! test (MinMax.getByFunction minMax) 20
do! test (MinMax.getByFunction minMax) 30
}