-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.ChunkBy.Tests.fs
More file actions
135 lines (115 loc) · 4.26 KB
/
TaskSeq.ChunkBy.Tests.fs
File metadata and controls
135 lines (115 loc) · 4.26 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
module TaskSeq.Tests.ChunkBy
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.chunkBy
// TaskSeq.chunkByAsync
//
module EmptySeq =
[<Fact>]
let ``Null source is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.chunkBy id (null: TaskSeq<int>)
assertNullArg
<| fun () -> TaskSeq.chunkByAsync (fun x -> Task.fromResult x) (null: TaskSeq<int>)
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-chunkBy on empty gives empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.chunkBy id
|> verifyEmpty
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-chunkByAsync on empty gives empty`` variant =
Gen.getEmptyVariant variant
|> TaskSeq.chunkByAsync (fun x -> Task.fromResult x)
|> verifyEmpty
module Functionality =
[<Fact>]
let ``TaskSeq-chunkBy groups consecutive equal elements`` () = task {
let ts = taskSeq { yield! [ 1; 1; 2; 2; 2; 3 ] }
let! result = TaskSeq.chunkBy id ts |> TaskSeq.toArrayAsync
result |> should haveLength 3
result[0] |> should equal (1, [| 1; 1 |])
result[1] |> should equal (2, [| 2; 2; 2 |])
result[2] |> should equal (3, [| 3 |])
}
[<Fact>]
let ``TaskSeq-chunkBy with all same key yields one chunk`` () = task {
let ts = taskSeq { yield! [ 5; 5; 5; 5 ] }
let! result = TaskSeq.chunkBy id ts |> TaskSeq.toArrayAsync
result |> should haveLength 1
result[0] |> should equal (5, [| 5; 5; 5; 5 |])
}
[<Fact>]
let ``TaskSeq-chunkBy with all different keys yields singleton chunks`` () = task {
let ts = taskSeq { yield! [ 1..5 ] }
let! result = TaskSeq.chunkBy id ts |> TaskSeq.toArrayAsync
result |> should haveLength 5
result
|> Array.iteri (fun i (k, arr) ->
k |> should equal (i + 1)
arr |> should equal [| i + 1 |])
}
[<Fact>]
let ``TaskSeq-chunkBy with singleton source yields one chunk`` () = task {
let ts = TaskSeq.singleton 42
let! result = TaskSeq.chunkBy id ts |> TaskSeq.toArrayAsync
result |> should haveLength 1
result[0] |> should equal (42, [| 42 |])
}
[<Fact>]
let ``TaskSeq-chunkBy uses projection key, not element`` () = task {
let ts = taskSeq {
yield "a1"
yield "a2"
yield "b1"
yield "b2"
yield "a3"
}
let! result =
TaskSeq.chunkBy (fun (s: string) -> s[0]) ts
|> TaskSeq.toArrayAsync
result |> should haveLength 3
let k0, arr0 = result[0]
k0 |> should equal 'a'
arr0 |> should equal [| "a1"; "a2" |]
let k1, arr1 = result[1]
k1 |> should equal 'b'
arr1 |> should equal [| "b1"; "b2" |]
let k2, arr2 = result[2]
k2 |> should equal 'a'
arr2 |> should equal [| "a3" |]
}
[<Fact>]
let ``TaskSeq-chunkBy does not merge non-consecutive equal keys`` () = task {
// Key alternates: 1, 2, 1, 2 — should produce 4 chunks not 2
let ts = taskSeq { yield! [ 1; 2; 1; 2 ] }
let! result = TaskSeq.chunkBy id ts |> TaskSeq.toArrayAsync
result |> should haveLength 4
}
[<Fact>]
let ``TaskSeq-chunkByAsync groups consecutive by async key`` () = task {
let ts = taskSeq { yield! [ 1; 1; 2; 3; 3 ] }
let! result =
TaskSeq.chunkByAsync (fun x -> Task.fromResult (x % 2 = 0)) ts
|> TaskSeq.toArrayAsync
// odd, even, odd -> 3 chunks
result |> should haveLength 3
let k0, arr0 = result[0]
k0 |> should equal false
arr0 |> should equal [| 1; 1 |]
let k1, arr1 = result[1]
k1 |> should equal true
arr1 |> should equal [| 2 |]
let k2, arr2 = result[2]
k2 |> should equal false
arr2 |> should equal [| 3; 3 |]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-chunkBy all elements same key as variants`` variant = task {
let ts = Gen.getSeqImmutable variant
let! result = TaskSeq.chunkBy (fun _ -> 0) ts |> TaskSeq.toArrayAsync
result |> should haveLength 1
let _, arr = result[0]
arr |> should haveLength 10
}