mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
fix(map_by_2): Fixed bug in implementation and added some unit tests
This commit is contained in:
parent
e72026b616
commit
6fdbc2c88f
2 changed files with 19 additions and 1 deletions
|
|
@ -114,7 +114,7 @@ let map_by_2 f seq k =
|
||||||
let f y =
|
let f y =
|
||||||
match !r with
|
match !r with
|
||||||
| None -> r := Some y
|
| None -> r := Some y
|
||||||
| Some x -> k (f x y)
|
| Some x -> r := None; k (f x y)
|
||||||
in
|
in
|
||||||
seq f;
|
seq f;
|
||||||
match !r with
|
match !r with
|
||||||
|
|
|
||||||
|
|
@ -347,3 +347,21 @@ let () =
|
||||||
let () =
|
let () =
|
||||||
let errcode = QCheck_base_runner.run_tests ~colors:false !qchecks in
|
let errcode = QCheck_base_runner.run_tests ~colors:false !qchecks in
|
||||||
if errcode <> 0 then exit errcode
|
if errcode <> 0 then exit errcode
|
||||||
|
|
||||||
|
(* map_by_2 tests *)
|
||||||
|
let test = OUnit.assert_equal ~printer:Q.Print.(list int)
|
||||||
|
let () = test [] (map_by_2 (fun a _ -> a) (of_list []) |> to_list)
|
||||||
|
(* Test empty iterator *)
|
||||||
|
let () = test [1] (map_by_2 (fun _ b -> b) (of_list [1]) |> to_list)
|
||||||
|
let () = test [3] (map_by_2 (fun _ b -> b) (1--3) |> drop 1 |> to_list)
|
||||||
|
let () = test [9] (map_by_2 (fun _ b -> b) (1--9) |> drop 4 |> to_list)
|
||||||
|
(* Odd number of elements should leave the last element in the iterator.
|
||||||
|
For an increasing integer range [1,2k] (fun _ b -> b) returns only
|
||||||
|
even numbers so this is sufficient to test that this element is left
|
||||||
|
in the iterator. *)
|
||||||
|
let () = test [1] (map_by_2 (fun a _ -> a) (1--2) |> to_list)
|
||||||
|
let () = test [2] (map_by_2 (fun _ b -> b) (1--2) |> to_list)
|
||||||
|
(* Test two elements *)
|
||||||
|
let () = test [1;3;5;7;9] (map_by_2 (fun a _ -> a) (1--10) |> to_list)
|
||||||
|
let () = test [2;4;6;8;10] (map_by_2 (fun _ b -> b) (1--10) |> to_list)
|
||||||
|
(* Test more than two elements *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue