-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTaskSeq.Unfold.Tests.fs
More file actions
271 lines (215 loc) · 8.45 KB
/
TaskSeq.Unfold.Tests.fs
File metadata and controls
271 lines (215 loc) · 8.45 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
module TaskSeq.Tests.Unfold
open Xunit
open FsUnit.Xunit
open FSharp.Control
//
// TaskSeq.unfold
// TaskSeq.unfoldAsync
//
module EmptySeq =
[<Fact>]
let ``TaskSeq-unfold generator returning None immediately yields empty sequence`` () = task {
let! result = TaskSeq.unfold (fun _ -> None) 0 |> TaskSeq.toArrayAsync
result |> should be Empty
}
[<Fact>]
let ``TaskSeq-unfoldAsync generator returning None immediately yields empty sequence`` () = task {
let! result =
TaskSeq.unfoldAsync (fun _ -> task { return None }) 0
|> TaskSeq.toArrayAsync
result |> should be Empty
}
module Functionality =
[<Fact>]
let ``TaskSeq-unfold generates a finite sequence`` () = task {
// unfold 0..9
let! result =
TaskSeq.unfold (fun n -> if n < 10 then Some(n, n + 1) else None) 0
|> TaskSeq.toArrayAsync
result |> should equal [| 0..9 |]
}
[<Fact>]
let ``TaskSeq-unfoldAsync generates a finite sequence`` () = task {
let! result =
TaskSeq.unfoldAsync (fun n -> task { return if n < 10 then Some(n, n + 1) else None }) 0
|> TaskSeq.toArrayAsync
result |> should equal [| 0..9 |]
}
[<Fact>]
let ``TaskSeq-unfold generates a singleton sequence`` () = task {
let! result =
TaskSeq.unfold (fun s -> if s = 0 then Some(42, 1) else None) 0
|> TaskSeq.toArrayAsync
result |> should equal [| 42 |]
}
[<Fact>]
let ``TaskSeq-unfoldAsync generates a singleton sequence`` () = task {
let! result =
TaskSeq.unfoldAsync (fun s -> task { return if s = 0 then Some(42, 1) else None }) 0
|> TaskSeq.toArrayAsync
result |> should equal [| 42 |]
}
[<Fact>]
let ``TaskSeq-unfold uses state correctly to thread accumulator`` () = task {
// Fibonacci: state = (a, b), yield a, new state = (b, a+b)
let! fibs =
TaskSeq.unfold (fun (a, b) -> if a > 100 then None else Some(a, (b, a + b))) (1, 1)
|> TaskSeq.toArrayAsync
fibs
|> should equal [| 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89 |]
}
[<Fact>]
let ``TaskSeq-unfoldAsync uses state correctly to thread accumulator`` () = task {
let! fibs =
TaskSeq.unfoldAsync (fun (a, b) -> task { return if a > 100 then None else Some(a, (b, a + b)) }) (1, 1)
|> TaskSeq.toArrayAsync
fibs
|> should equal [| 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89 |]
}
[<Fact>]
let ``TaskSeq-unfold can be truncated to limit infinite-like sequences`` () = task {
// counters counting from 1 upward, take first 100
let! result =
TaskSeq.unfold (fun n -> Some(n, n + 1)) 1
|> TaskSeq.take 100
|> TaskSeq.toArrayAsync
result |> should equal [| 1..100 |]
result |> Array.length |> should equal 100
}
[<Fact>]
let ``TaskSeq-unfoldAsync can be truncated to limit infinite-like sequences`` () = task {
let! result =
TaskSeq.unfoldAsync (fun n -> task { return Some(n, n + 1) }) 1
|> TaskSeq.take 100
|> TaskSeq.toArrayAsync
result |> should equal [| 1..100 |]
result |> Array.length |> should equal 100
}
[<Fact>]
let ``TaskSeq-unfold generates string sequences from state`` () = task {
// build "A", "B", ..., "Z"
let! letters =
TaskSeq.unfold (fun c -> if c > int 'Z' then None else Some(string (char c), c + 1)) (int 'A')
|> TaskSeq.toArrayAsync
letters
|> should equal [| for c in 'A' .. 'Z' -> string c |]
}
[<Fact>]
let ``TaskSeq-unfold calls generator exactly once per element plus one final None call`` () = task {
let mutable callCount = 0
let! result =
TaskSeq.unfold
(fun n ->
callCount <- callCount + 1
if n < 5 then Some(n, n + 1) else None)
0
|> TaskSeq.toArrayAsync
result |> should equal [| 0..4 |]
callCount |> should equal 6 // 5 Some + 1 None
}
[<Fact>]
let ``TaskSeq-unfold re-iterating restarts from initial state`` () = task {
let ts = TaskSeq.unfold (fun n -> if n < 5 then Some(n, n + 1) else None) 0
let! first = ts |> TaskSeq.toArrayAsync
let! second = ts |> TaskSeq.toArrayAsync
first |> should equal second
first |> should equal [| 0..4 |]
}
[<Fact>]
let ``TaskSeq-unfoldAsync re-iterating restarts from initial state`` () = task {
let ts = TaskSeq.unfoldAsync (fun n -> task { return if n < 5 then Some(n, n + 1) else None }) 0
let! first = ts |> TaskSeq.toArrayAsync
let! second = ts |> TaskSeq.toArrayAsync
first |> should equal second
first |> should equal [| 0..4 |]
}
module SideEffects =
[<Fact>]
let ``TaskSeq-unfold generator side-effects accumulate across re-iterations`` () = task {
// The generator closes over mutable external state. Each re-iteration starts fresh from
// the initial seed (0), but the external counter keeps climbing — demonstrating that
// the IAsyncEnumerable itself is stateless but the captured state is shared.
let mutable totalCalls = 0
let ts =
TaskSeq.unfold
(fun n ->
totalCalls <- totalCalls + 1
if n < 3 then Some(n, n + 1) else None)
0
let! first = ts |> TaskSeq.toArrayAsync
first |> should equal [| 0; 1; 2 |]
totalCalls |> should equal 4 // 3 Some + 1 None
let! second = ts |> TaskSeq.toArrayAsync
second |> should equal [| 0; 1; 2 |]
totalCalls |> should equal 8 // called 4 more times for the second iteration
}
[<Fact>]
let ``TaskSeq-unfoldAsync generator side-effects accumulate across re-iterations`` () = task {
let mutable totalCalls = 0
let ts =
TaskSeq.unfoldAsync
(fun n -> task {
totalCalls <- totalCalls + 1
return if n < 3 then Some(n, n + 1) else None
})
0
let! first = ts |> TaskSeq.toArrayAsync
first |> should equal [| 0; 1; 2 |]
totalCalls |> should equal 4
let! second = ts |> TaskSeq.toArrayAsync
second |> should equal [| 0; 1; 2 |]
totalCalls |> should equal 8
}
[<Fact>]
let ``TaskSeq-unfold with take stops generator calls at the limit`` () = task {
let mutable callCount = 0
// Infinite generator: always returns Some
let ts =
TaskSeq.unfold
(fun n ->
callCount <- callCount + 1
Some(n, n + 1))
0
let! result = ts |> TaskSeq.take 5 |> TaskSeq.toArrayAsync
result |> should equal [| 0; 1; 2; 3; 4 |]
// take 5 pulls exactly 5 elements; with an always-Some generator no
// extra sentinel call is needed, so callCount should be exactly 5.
callCount |> should equal 5
}
[<Fact>]
let ``TaskSeq-unfoldAsync with take stops generator calls at the limit`` () = task {
let mutable callCount = 0
let ts =
TaskSeq.unfoldAsync
(fun n -> task {
callCount <- callCount + 1
return Some(n, n + 1)
})
0
let! result = ts |> TaskSeq.take 5 |> TaskSeq.toArrayAsync
result |> should equal [| 0; 1; 2; 3; 4 |]
callCount |> should equal 5
}
[<Fact>]
let ``TaskSeq-unfold propagates exception thrown inside the generator`` () =
let ts =
TaskSeq.unfold
(fun n ->
if n = 3 then
failwith "generator-boom"
Some(n, n + 1))
0
fun () -> ts |> consumeTaskSeq
|> should throwAsyncExact typeof<System.Exception>
[<Fact>]
let ``TaskSeq-unfoldAsync propagates exception thrown inside the async generator`` () =
let ts =
TaskSeq.unfoldAsync
(fun n -> task {
if n = 3 then
failwith "async-generator-boom"
return Some(n, n + 1)
})
0
fun () -> ts |> consumeTaskSeq
|> should throwAsyncExact typeof<System.Exception>