2017-08-22 23:55:31 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2017-08-22 23:55:31 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
let rec permut = function
|
|
|
|
| [] -> [[]]
|
|
|
|
| x :: xs ->
|
|
|
|
let insert xs =
|
|
|
|
let rec loop acc left right =
|
|
|
|
match right with
|
|
|
|
| [] -> List.rev (x :: left) :: acc
|
|
|
|
| y :: ys ->
|
|
|
|
loop
|
|
|
|
((List.rev_append left (x :: right)) :: acc)
|
|
|
|
(y :: left) ys in
|
|
|
|
loop [] [] xs in
|
|
|
|
List.concat (List.map insert (permut xs))
|
|
|
|
|
|
|
|
let test_take_n _ =
|
|
|
|
ListLabels.iter (permut [1;2;3;4;5;6;7;8;9]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare 1 xs) [9]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
ListLabels.iter (permut [1;2;3;4;5;6;7;8;9]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare 3 xs) [7;8;9]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
let inv_compare x y = compare y x in
|
|
|
|
ListLabels.iter (permut [1;2;3;4;5;6;7;8;9]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare:inv_compare 3 xs) [3;2;1]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
(* less elements than the bound. *)
|
|
|
|
ListLabels.iter (permut [1;2;3;4;5;6;7;8;9]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare 12 xs) [1;2;3;4;5;6;7;8;9]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
(* with duplicates. *)
|
|
|
|
ListLabels.iter (permut [1;2;3;3;4;5;5;5;6]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare 3 xs) [5;5;6]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
ListLabels.iter (permut [1;2;3;3;4;5;5;5;6]) ~f:begin fun xs ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Assert.equal ~msg:__LOC__ (List.take_n ~compare 5 xs) [4;5;5;5;6]
|
2017-08-22 23:55:31 +04:00
|
|
|
end ;
|
|
|
|
return ()
|
|
|
|
|
|
|
|
let tests : (string * (string -> unit tzresult Lwt.t)) list = [
|
|
|
|
"take_n", test_take_n ;
|
|
|
|
]
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Test.run "utils." tests
|