Skip to content

Commit

Permalink
Lwt_seq: test iter(s) via pseudo-fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-proust committed Mar 4, 2021
1 parent e6f56b5 commit 5a26b5b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion test/core/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Test.run "core"
Test_lwt_condition.suite;
Test_lwt_pool.suite;
Test_lwt_sequence.suite;
Test_lwt_seq.suite;
Test_lwt_seq.suite_base;
Test_lwt_seq.suite_fuzzing;
])
73 changes: 72 additions & 1 deletion test/core/test_lwt_seq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open Test

let l = [1; 2; 3; 4; 5]

let suite = suite "lwt_seq" [
let suite_base = suite "lwt_seq" [
test "list" begin fun () ->
let a = Lwt_seq.of_list l in
let n = ref 1 in
Expand Down Expand Up @@ -92,3 +92,74 @@ let suite = suite "lwt_seq" [
n = 0
end;
]

let fs = [(+); (-); (fun x _ -> x); (fun _ x -> x); min; max]
let ls = [
[];
[0;1;2;3;4;5];
[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 x !r) l;
[!r] in
let sf s =
let r = ref c in
fun () ->
let* () = Lwt_seq.iter (fun x -> r := f x !r) 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 x !r) l;
[!r] in
let sf s =
let r = ref c in
fun () ->
let* () = Lwt_seq.iter_s (fun x -> r := f x !r; Lwt.return_unit) s in
Lwt.return (Lwt_seq.Cons (!r, Lwt_seq.empty)) in
commutes lf sf l
)
end;
]

0 comments on commit 5a26b5b

Please sign in to comment.