File: test_lwt_seq.ml

package info (click to toggle)
lwt 5.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: ml: 22,030; ansic: 7,167; makefile: 92; python: 62
file content (379 lines) | stat: -rw-r--r-- 9,771 bytes parent folder | download | duplicates (3)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
   details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)



open Lwt.Syntax

open Test

let l =  [1; 2; 3; 4; 5]
let a = Lwt_seq.of_list l
let rec pause n =
   if n <= 0 then
      Lwt.return_unit
   else
      let* () = Lwt.pause () in
      pause (n - 1)
let pause n = pause (n mod 5)
let b =
   Lwt_seq.unfold_lwt
     (function
      | [] -> let+ () = pause 2 in None
      | x::xs -> let+ () = pause (x+2) in Some (x, xs))
     l

let suite_base = suite "lwt_seq" [
  test "fold_left" begin fun () ->
    let n = ref 1 in
    Lwt_seq.fold_left (fun acc x ->
      let r = x = !n && acc in
      incr n; r) true a
  end;
  test "fold_left_s" begin fun () ->
    let n = ref 1 in
    Lwt_seq.fold_left_s (fun acc x ->
      let r = x = !n && acc in
      incr n; Lwt.return r) true a
  end;

  test "map" begin fun () ->
    let v = Lwt_seq.map (fun x -> (x * 2)) a in
    let+ l' = Lwt_seq.to_list v in
    l' = [2; 4; 6; 8; 10]
  end;
  test "map_s" begin fun () ->
    let v = Lwt_seq.map_s (fun x -> Lwt.return (x * 2)) a in
    let+ l' = Lwt_seq.to_list v in
    l' = [2; 4; 6; 8; 10]
  end;

  test "filter" begin fun () ->
    let v = Lwt_seq.filter (fun x -> (x mod 2 = 0)) a in
    let+ l' = Lwt_seq.to_list v in
    l' = [2; 4]
  end;
  test "filter_s" begin fun () ->
    let v = Lwt_seq.filter_s (fun x -> Lwt.return (x mod 2 = 0)) a in
    let+ l' = Lwt_seq.to_list v in
    l' = [2; 4]
  end;

  test "iter_n(1)" begin fun () ->
    let max_concurrency = 1 in
    let running = ref 0 in
    let sum = ref 0 in
    let f x =
       incr running;
       assert (!running <= max_concurrency);
       let* () = pause x in
       sum := !sum + x;
       decr running;
       Lwt.return_unit
    in
    let* () = Lwt_seq.iter_n ~max_concurrency f a in
    assert (!sum = List.fold_left (+) 0 l);
    sum := 0;
    let* () = Lwt_seq.iter_n ~max_concurrency f b in
    assert (!sum = List.fold_left (+) 0 l);
    Lwt.return_true
  end;
  test "iter_n(2)" begin fun () ->
    let max_concurrency = 2 in
    let running = ref 0 in
    let sum = ref 0 in
    let f x =
       incr running;
       assert (!running <= max_concurrency);
       let* () = pause x in
       sum := !sum + x;
       decr running;
       Lwt.return_unit
    in
    let* () = Lwt_seq.iter_n ~max_concurrency f a in
    assert (!sum = List.fold_left (+) 0 l);
    sum := 0;
    let* () = Lwt_seq.iter_n ~max_concurrency f b in
    assert (!sum = List.fold_left (+) 0 l);
    Lwt.return_true
  end;
  test "iter_n(100)" begin fun () ->
    let max_concurrency = 100 in
    let running = ref 0 in
    let sum = ref 0 in
    let f x =
       incr running;
       assert (!running <= max_concurrency);
       let* () = pause x in
       sum := !sum + x;
       decr running;
       Lwt.return_unit
    in
    let* () = Lwt_seq.iter_n ~max_concurrency f a in
    assert (!sum = List.fold_left (+) 0 l);
    sum := 0;
    let* () = Lwt_seq.iter_n ~max_concurrency f b in
    assert (!sum = List.fold_left (+) 0 l);
    Lwt.return_true
  end;

  test "filter_map" begin fun () ->
    let v = Lwt_seq.filter_map (fun x ->
      if x mod 2 = 0 then Some (x * 2) else None) a
    in
    let+ l' = Lwt_seq.to_list v in
    l' = [4; 8]
  end;
  test "filter_map_s" begin fun () ->
    let v = Lwt_seq.filter_map_s (fun x ->
      Lwt.return (if x mod 2 = 0 then Some (x * 2) else None)) a
    in
    let+ l' = Lwt_seq.to_list v in
    l' = [4; 8]
  end;

  test "unfold" begin fun () ->
    let range first last =
      let step i = if i > last then None else Some (i, succ i) in
      Lwt_seq.unfold step first
    in
    let* a = Lwt_seq.to_list (range 1 3) in
    let+ b = Lwt_seq.to_list (range 1 0) in
      ([1;2;3] = a) &&
      ([] = b)
  end;

  test "unfold_lwt" begin fun () ->
    let range first last =
      let step i =
         if i > last then Lwt.return_none else Lwt.return_some (i, succ i)
      in
      Lwt_seq.unfold_lwt step first
    in
    let* a = Lwt_seq.to_list (range 1 3) in
    let+ b = Lwt_seq.to_list (range 1 0) in
      ([1;2;3] = a) &&
      ([] = b)
  end;


  test "fold-into-exception-from-of-seq" begin fun () ->
    let fail = fun () -> failwith "XXX" in
    let seq = fun () -> Seq.Cons (1, (fun () -> Seq.Cons (2, fail))) in
    let a = Lwt_seq.of_seq seq in
    let+ n =
      Lwt.catch
        (fun () -> Lwt_seq.fold_left (+) 0 a)
        (function
          | Failure x when x = "XXX" -> Lwt.return (-1)
          | exc -> raise exc)
    in
    n = (-1)
  end;

  test "fold-into-immediate-exception-from-of-seq" begin fun () ->
    let fail = fun () -> failwith "XXX" in
    let seq = fail in
    let a = Lwt_seq.of_seq seq in
    let+ n =
      Lwt.catch
        (fun () -> Lwt_seq.fold_left (+) 0 a)
        (function
          | Failure x when x = "XXX" -> Lwt.return (-1)
          | exc -> raise exc)
    in
    n = (-1)
  end;

  test "fold-into-exception-from-of-seq-lwt" begin fun () ->
    let fail = fun () -> failwith "XXX" in
    let seq: int Lwt.t Seq.t = fun () ->
      Seq.Cons (Lwt.return 1,
        fun () ->
          Seq.Cons (Lwt.return 2, fail)) in
    let a = Lwt_seq.of_seq_lwt seq in
    let+ n =
      Lwt.catch
        (fun () -> Lwt_seq.fold_left (+) 0 a)
        (function
          | Failure x when x = "XXX" -> Lwt.return (-1)
          | exc -> raise exc)
    in
    n = (-1)
  end;

  test "fold-into-immediate-exception-from-of-seq-lwt" begin fun () ->
    let fail = fun () -> failwith "XXX" in
    let seq: int Lwt.t Seq.t = fail in
    let a = Lwt_seq.of_seq_lwt seq in
    let+ n =
      Lwt.catch
        (fun () -> Lwt_seq.fold_left (+) 0 a)
        (function
          | Failure x when x = "XXX" -> Lwt.return (-1)
          | exc -> raise exc)
    in
    n = (-1)
  end;
]

let fs = [(+); (-); (fun x _ -> x); min; max]
let ls = [
   [];
   l;
   l@l@l;
   List.rev l;
   [0;0;0];
   [max_int;0;min_int];
   [max_int;max_int];
]
let cs = [0;1;max_int;min_int;44;5]
let with_flc test =
   Lwt_list.for_all_s
     (fun f ->
        Lwt_list.for_all_s
          (fun l ->
             Lwt_list.for_all_s
               (fun c -> test f l c)
               cs)
          ls)
     fs
let equals l1 seq2 =
   let* l2 = Lwt_seq.to_list seq2 in
   Lwt.return (l1 = l2)
let commutes lf sf l =
   equals (lf l) (sf (Lwt_seq.of_list l))


let suite_fuzzing = suite "lwt_seq(pseudo-fuzzing)" [

  test "map" begin fun () ->
     with_flc (fun f l c ->
        let lf = List.map (fun x -> f x c) in
        let sf = Lwt_seq.map (fun x -> f x c) in
        commutes lf sf l
     )
  end;

  test "map_s" begin fun () ->
     with_flc (fun f l c ->
        let lf = List.map (fun x -> f x c) in
        let sf = Lwt_seq.map_s (fun x -> Lwt.return (f x c)) in
        commutes lf sf l
     )
  end;

  test "iter" begin fun () ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r] in
        let sf s =
           let r = ref c in
           fun () ->
             let* () = Lwt_seq.iter (fun x -> r := f !r x) s in
             Lwt.return (Lwt_seq.Cons (!r, Lwt_seq.empty)) in
        commutes lf sf l
     )
  end;

  test "iter_s" begin fun () ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r] in
        let sf s =
           let r = ref c in
           fun () ->
             let* () = Lwt_seq.iter_s (fun x -> r := f !r x; Lwt.return_unit) s in
             Lwt.return (Lwt_seq.Cons (!r, Lwt_seq.empty)) in
        commutes lf sf l
     )
  end;

  (* the [f]s commute sufficiently for parallel execution *)
  test "iter_p" begin fun () ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r]
        in
        let sf s =
           Lwt_seq.return_lwt @@
           let r = ref c in
           let+ () = Lwt_seq.iter_p (fun x -> r := f !r x; Lwt.return_unit) s in
           !r
        in
        commutes lf sf l
     )
  end;

  test "iter_p (pause)" begin fun () ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r]
        in
        let sf s =
           Lwt_seq.return_lwt @@
           let r = ref c in
           let+ () =
              Lwt_seq.iter_p
                (fun x ->
                   let* () = pause x in
                   r := f !r x;
                   pause x)
                s
           in
           !r
        in
        commutes lf sf l
     )
  end;

  test "iter_n" begin fun () ->
     l |> Lwt_list.for_all_s @@ fun max_concurrency ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r] in
        let sf s =
           Lwt_seq.return_lwt @@
           let r = ref c in
           let+ () = Lwt_seq.iter_n ~max_concurrency (fun x -> r := f !r x; Lwt.return_unit) s in
           !r
        in
        commutes lf sf l
     )
  end;

  test "iter_n (pause)" begin fun () ->
     l |> Lwt_list.for_all_s @@ fun max_concurrency ->
     with_flc (fun f l c ->
        let lf l =
           let r = ref c in
           List.iter (fun x -> r := f !r x) l;
           [!r] in
        let sf s =
           Lwt_seq.return_lwt @@
           let r = ref c in
           let+ () =
              Lwt_seq.iter_n ~max_concurrency
                 (fun x ->
                    let* () = pause x in
                    r := f !r x;
                    pause x)
                 s
           in
           !r
        in
        commutes lf sf l
     )
  end;

]