-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeq.CompareWith.Tests.fs
More file actions
243 lines (200 loc) · 6.13 KB
/
TaskSeq.CompareWith.Tests.fs
File metadata and controls
243 lines (200 loc) · 6.13 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
module TaskSeq.Tests.CompareWith
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.compareWith
// TaskSeq.compareWithAsync
//
let inline sign x =
if x < 0 then -1
elif x > 0 then 1
else 0
module EmptySeq =
[<Fact>]
let ``Null source1 is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.compareWith compare null TaskSeq.empty<int>
assertNullArg
<| fun () -> TaskSeq.compareWithAsync (fun a b -> Task.fromResult (compare a b)) null TaskSeq.empty<int>
[<Fact>]
let ``Null source2 is invalid`` () =
assertNullArg
<| fun () -> TaskSeq.compareWith compare TaskSeq.empty<int> null
assertNullArg
<| fun () -> TaskSeq.compareWithAsync (fun a b -> Task.fromResult (compare a b)) TaskSeq.empty<int> null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-compareWith of two empty sequences is 0`` variant = task {
let empty = Gen.getEmptyVariant variant
let! result = TaskSeq.compareWith compare empty TaskSeq.empty<int>
result |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-compareWithAsync of two empty sequences is 0`` variant = task {
let empty = Gen.getEmptyVariant variant
let! result = TaskSeq.compareWithAsync (fun a b -> Task.fromResult (compare a b)) empty TaskSeq.empty<int>
result |> should equal 0
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-compareWith: empty source1 is less than non-empty source2`` variant = task {
let empty = Gen.getEmptyVariant variant
let! result = TaskSeq.compareWith compare empty (TaskSeq.singleton 1)
result |> should equal -1
}
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-compareWith: non-empty source1 is greater than empty source2`` variant = task {
let empty = Gen.getEmptyVariant variant
let! result = TaskSeq.compareWith compare (TaskSeq.singleton 1) empty
result |> should equal 1
}
module Immutable =
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-compareWith: equal sequences return 0`` variant = task {
let src1 = Gen.getSeqImmutable variant
let src2 = Gen.getSeqImmutable variant
let! result = TaskSeq.compareWith compare src1 src2
result |> should equal 0
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-compareWithAsync: equal sequences return 0`` variant = task {
let src1 = Gen.getSeqImmutable variant
let src2 = Gen.getSeqImmutable variant
let! result = TaskSeq.compareWithAsync (fun a b -> Task.fromResult (compare a b)) src1 src2
result |> should equal 0
}
[<Fact>]
let ``TaskSeq-compareWith: first element differs`` () = task {
let src1 = taskSeq {
1
2
3
}
let src2 = taskSeq {
2
2
3
}
let! result = TaskSeq.compareWith compare src1 src2
sign result |> should equal -1
}
[<Fact>]
let ``TaskSeq-compareWith: last element differs`` () = task {
let src1 = taskSeq {
1
2
4
}
let src2 = taskSeq {
1
2
3
}
let! result = TaskSeq.compareWith compare src1 src2
sign result |> should equal 1
}
[<Fact>]
let ``TaskSeq-compareWith: source1 shorter returns negative`` () = task {
let src1 = taskSeq {
1
2
}
let src2 = taskSeq {
1
2
3
}
let! result = TaskSeq.compareWith compare src1 src2
result |> should equal -1
}
[<Fact>]
let ``TaskSeq-compareWith: source2 shorter returns positive`` () = task {
let src1 = taskSeq {
1
2
3
}
let src2 = taskSeq {
1
2
}
let! result = TaskSeq.compareWith compare src1 src2
result |> should equal 1
}
[<Fact>]
let ``TaskSeq-compareWith: uses custom comparer result sign`` () = task {
// comparer returns a large number, not just -1/0/1
let bigCompare (a: int) (b: int) = (a - b) * 100
let src1 = taskSeq {
1
2
3
}
let src2 = taskSeq {
1
2
5
}
let! result = TaskSeq.compareWith bigCompare src1 src2
// 3 compared to 5 gives (3-5)*100 = -200, which is negative
result |> should be (lessThan 0)
}
[<Fact>]
let ``TaskSeq-compareWith: stops at first non-zero comparison`` () = task {
let mutable callCount = 0
let countingCompare a b =
callCount <- callCount + 1
compare a b
let src1 = taskSeq {
1
99
99
99
}
let src2 = taskSeq {
2
99
99
99
}
let! result = TaskSeq.compareWith countingCompare src1 src2
sign result |> should equal -1
// Should stop after first comparison
callCount |> should equal 1
}
[<Fact>]
let ``TaskSeq-compareWithAsync: async comparer works`` () = task {
let src1 = taskSeq {
1
2
3
}
let src2 = taskSeq {
1
2
4
}
let! result =
TaskSeq.compareWithAsync
(fun a b -> task {
// simulate async work with a yield point
return compare a b
})
src1
src2
sign result |> should equal -1
}
[<Fact>]
let ``TaskSeq-compareWithAsync: async comparer works correctly`` () = task {
let src1 = taskSeq {
10
20
30
}
let src2 = taskSeq {
10
20
30
}
let! result = TaskSeq.compareWithAsync (fun a b -> Task.fromResult (compare a b)) src1 src2
result |> should equal 0
}