-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.DistinctUntilChanged.Tests.fs
More file actions
323 lines (259 loc) · 10.4 KB
/
TaskSeq.DistinctUntilChanged.Tests.fs
File metadata and controls
323 lines (259 loc) · 10.4 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
module TaskSeq.Tests.DistinctUntilChanged
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.distinctUntilChanged / distinctUntilChangedWith / distinctUntilChangedWithAsync
//
module EmptySeq =
[<Fact>]
let ``TaskSeq-distinctUntilChanged with null source raises`` () = assertNullArg <| fun () -> TaskSeq.distinctUntilChanged null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-distinctUntilChanged has no effect`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith with null source raises`` () =
assertNullArg
<| fun () -> TaskSeq.distinctUntilChangedWith (fun _ _ -> false) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-distinctUntilChangedWith has no effect on empty`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.distinctUntilChangedWith (fun _ _ -> false)
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync with null source raises`` () =
assertNullArg
<| fun () -> TaskSeq.distinctUntilChangedWithAsync (fun _ _ -> task { return false }) null
[<Theory; ClassData(typeof<TestEmptyVariants>)>]
let ``TaskSeq-distinctUntilChangedWithAsync has no effect on empty`` variant = task {
do!
Gen.getEmptyVariant variant
|> TaskSeq.distinctUntilChangedWithAsync (fun _ _ -> task { return false })
|> TaskSeq.toListAsync
|> Task.map (List.isEmpty >> should be True)
}
module Functionality =
[<Fact>]
let ``TaskSeq-distinctUntilChanged should return no consecutive duplicates`` () = task {
let ts =
[ 'A'; 'A'; 'B'; 'Z'; 'C'; 'C'; 'Z'; 'C'; 'D'; 'D'; 'D'; 'Z' ]
|> TaskSeq.ofList
let! xs = ts |> TaskSeq.distinctUntilChanged |> TaskSeq.toListAsync
xs
|> List.map string
|> String.concat ""
|> should equal "ABZCZCDZ"
}
[<Fact>]
let ``TaskSeq-distinctUntilChanged with single element returns singleton`` () = task {
let! xs =
taskSeq { yield 42 }
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 42 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChanged with all identical elements returns one element`` () = task {
let! xs =
taskSeq { yield! [ 7; 7; 7; 7; 7 ] }
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 7 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChanged with all distinct elements returns all`` () = task {
let! xs =
taskSeq { yield! [ 1; 2; 3; 4; 5 ] }
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 1; 2; 3; 4; 5 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChanged with alternating pairs`` () = task {
// [A;A;B;B;A;A] -> [A;B;A]
let! xs =
taskSeq { yield! [ 'A'; 'A'; 'B'; 'B'; 'A'; 'A' ] }
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 'A'; 'B'; 'A' ]
}
[<Theory; ClassData(typeof<TestImmTaskSeq>)>]
let ``TaskSeq-distinctUntilChanged on immutable all-unique seq preserves all elements`` variant = task {
// getSeqImmutable yields 1..10, all unique, so all are returned
let! xs =
Gen.getSeqImmutable variant
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 1..10 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith with structural equality comparer behaves like distinctUntilChanged`` () = task {
let ts =
[ 'A'; 'A'; 'B'; 'Z'; 'C'; 'C'; 'Z'; 'C'; 'D'; 'D'; 'D'; 'Z' ]
|> TaskSeq.ofList
let! xs =
ts
|> TaskSeq.distinctUntilChangedWith (=)
|> TaskSeq.toListAsync
xs
|> List.map string
|> String.concat ""
|> should equal "ABZCZCDZ"
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith with always-true comparer returns only first element`` () = task {
let! xs =
taskSeq { yield! [ 1; 2; 3; 4; 5 ] }
|> TaskSeq.distinctUntilChangedWith (fun _ _ -> true)
|> TaskSeq.toListAsync
xs |> should equal [ 1 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith with always-false comparer returns all elements`` () = task {
let! xs =
taskSeq { yield! [ 1; 1; 2; 2; 3 ] }
|> TaskSeq.distinctUntilChangedWith (fun _ _ -> false)
|> TaskSeq.toListAsync
xs |> should equal [ 1; 1; 2; 2; 3 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith can use custom projection for equality`` () = task {
// Treat values as equal if their absolute difference is <= 1
let closeEnough a b = abs (a - b) <= 1
let! xs =
taskSeq { yield! [ 10; 11; 9; 20; 21; 5 ] }
|> TaskSeq.distinctUntilChangedWith closeEnough
|> TaskSeq.toListAsync
// 10≈11 skip; 11≈9 skip (|11-9|=2? no, |11-9|=2>1, so keep 9); 9 vs 20 keep; 20≈21 skip; 21 vs 5 keep
// Wait: |10-11|=1 skip 11; |10-9|=1 skip 9; 10 vs 20 keep 20; |20-21|=1 skip 21; 20 vs 5 keep 5
xs |> should equal [ 10; 20; 5 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith with single element returns singleton`` () = task {
let! xs =
taskSeq { yield 99 }
|> TaskSeq.distinctUntilChangedWith (fun _ _ -> true)
|> TaskSeq.toListAsync
xs |> should equal [ 99 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith case-insensitive string comparison`` () = task {
let! xs =
taskSeq { yield! [ "Hello"; "hello"; "HELLO"; "World"; "world" ] }
|> TaskSeq.distinctUntilChangedWith (fun a b -> System.String.Compare(a, b, System.StringComparison.OrdinalIgnoreCase) = 0)
|> TaskSeq.toListAsync
xs |> should equal [ "Hello"; "World" ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync with structural equality behaves like distinctUntilChanged`` () = task {
let ts = [ 1; 1; 2; 3; 3; 4 ] |> TaskSeq.ofList
let! xs =
ts
|> TaskSeq.distinctUntilChangedWithAsync (fun a b -> task { return a = b })
|> TaskSeq.toListAsync
xs |> should equal [ 1; 2; 3; 4 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync with always-true async comparer returns only first element`` () = task {
let! xs =
taskSeq { yield! [ 10; 20; 30 ] }
|> TaskSeq.distinctUntilChangedWithAsync (fun _ _ -> task { return true })
|> TaskSeq.toListAsync
xs |> should equal [ 10 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync with always-false async comparer returns all elements`` () = task {
let! xs =
taskSeq { yield! [ 5; 5; 5 ] }
|> TaskSeq.distinctUntilChangedWithAsync (fun _ _ -> task { return false })
|> TaskSeq.toListAsync
xs |> should equal [ 5; 5; 5 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync can perform async work in comparer`` () = task {
let mutable comparerCallCount = 0
let asyncComparer a b = task {
comparerCallCount <- comparerCallCount + 1
return a = b
}
let! xs =
taskSeq { yield! [ 1; 1; 2; 2; 3 ] }
|> TaskSeq.distinctUntilChangedWithAsync asyncComparer
|> TaskSeq.toListAsync
xs |> should equal [ 1; 2; 3 ]
// comparer called for each pair of consecutive elements (4 pairs for 5 elements)
comparerCallCount |> should equal 4
}
module SideEffects =
[<Fact>]
let ``TaskSeq-distinctUntilChanged consumes every element exactly once`` () = task {
let mutable count = 0
let ts = taskSeq {
for i in 1..6 do
count <- count + 1
yield i % 3 // yields 1,2,0,1,2,0 — no consecutive duplicates
}
let! xs = ts |> TaskSeq.distinctUntilChanged |> TaskSeq.toListAsync
count |> should equal 6
xs |> should equal [ 1; 2; 0; 1; 2; 0 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChanged skips duplicates without extra evaluation`` () = task {
let mutable count = 0
let ts = taskSeq {
for i in [ 1; 1; 2; 2; 3 ] do
count <- count + 1
yield i
}
let! xs = ts |> TaskSeq.distinctUntilChanged |> TaskSeq.toListAsync
// All 5 source elements must still be consumed
count |> should equal 5
xs |> should equal [ 1; 2; 3 ]
}
[<Theory; ClassData(typeof<TestSideEffectTaskSeq>)>]
let ``TaskSeq-distinctUntilChanged on side-effect seq preserves all unique elements`` variant = task {
// getSeqWithSideEffect yields 1..10 (all unique on first iteration)
let! xs =
Gen.getSeqWithSideEffect variant
|> TaskSeq.distinctUntilChanged
|> TaskSeq.toListAsync
xs |> should equal [ 1..10 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWith consumes every element exactly once`` () = task {
let mutable count = 0
let ts = taskSeq {
for i in 1..5 do
count <- count + 1
yield i
}
let! xs =
ts
|> TaskSeq.distinctUntilChangedWith (fun a b -> a = b)
|> TaskSeq.toListAsync
count |> should equal 5
xs |> should equal [ 1; 2; 3; 4; 5 ]
}
[<Fact>]
let ``TaskSeq-distinctUntilChangedWithAsync consumes every element exactly once`` () = task {
let mutable count = 0
let ts = taskSeq {
for i in 1..5 do
count <- count + 1
yield i
}
let! xs =
ts
|> TaskSeq.distinctUntilChangedWithAsync (fun a b -> task { return a = b })
|> TaskSeq.toListAsync
count |> should equal 5
xs |> should equal [ 1; 2; 3; 4; 5 ]
}