Skip to content

Commit

Permalink
fixup! xapi-stdext-std: add Listext.List.find_minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
psafont committed Jan 17, 2024
1 parent 4b4ccb2 commit d33f0b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/xapi-stdext-std/listext.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ module List : sig

val find_minimum : ('a -> 'a -> int) -> 'a list -> 'a option
(** [find_minimum cmp l] returns the lowest element in [l] according to
the sort order of [cmp], or [None] if the list is empty. *)
the sort order of [cmp], or [None] if the list is empty. When two ore
more elements match the lowest value, the left-most is returned. *)

(** {1 Using indices to manipulate lists} *)

Expand Down
67 changes: 46 additions & 21 deletions lib/xapi-stdext-std/listext_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let test_list tested_f (name, case, expected) =
let check () = Alcotest.(check @@ list int) name expected (tested_f case) in
(name, `Quick, check)

let test_option tested_f (name, case, expected) =
let check () = Alcotest.(check @@ option int) name expected (tested_f case) in
let test_option typ tested_f (name, case, expected) =
let check () = Alcotest.(check @@ option typ) name expected (tested_f case) in
(name, `Quick, check)

let test_chopped_list tested_f (name, case, expected) =
Expand Down Expand Up @@ -180,28 +180,52 @@ let test_sub =
let tests = List.map test specs in
("sub", tests)

let test_find_minimum =
let test_find_minimum (name, pp, typ, specs) =
let test ((cmp, cmp_name), input, expected) =
let name = Printf.sprintf "%s of [%s]" cmp_name (pp input) in
test_option typ (Listext.find_minimum cmp) (name, input, expected)
in
let tests = List.map test specs in
(Printf.sprintf "find_minimum (%s)" name, tests)

let test_find_minimum_int =
let ascending = (Int.compare, "ascending") in
let descending = ((fun a b -> Int.compare b a), "descending") in
let specs =
[
(ascending, [], None)
; (ascending, [1; 2; 3; 4; 5], Some 1)
; (ascending, [2; 3; 1; 5; 4], Some 1)
; (descending, [], None)
; (descending, [1; 2; 3; 4; 5], Some 5)
; (descending, [2; 3; 1; 5; 4], Some 5)
]
let specs_int =
( "int"
, (fun a -> String.concat "; " (List.map string_of_int a))
, Alcotest.int
, [
(ascending, [], None)
; (ascending, [1; 2; 3; 4; 5], Some 1)
; (ascending, [2; 3; 1; 5; 4], Some 1)
; (descending, [], None)
; (descending, [1; 2; 3; 4; 5], Some 5)
; (descending, [2; 3; 1; 5; 4], Some 5)
]
)
in
let test ((cmp, cmp_name), input, expected) =
let name =
Printf.sprintf "find_minimum %s of [%s]" cmp_name
(String.concat "; " (List.map string_of_int input))
in
test_option (Listext.find_minimum cmp) (name, input, expected)
test_find_minimum specs_int

let test_find_minimum_tuple =
let ascending = ((fun (a, _) (b, _) -> Int.compare a b), "ascending") in
let descending = ((fun (a, _) (b, _) -> Int.compare b a), "descending") in
let specs_tuple =
( "tuple"
, (fun a ->
String.concat "; "
(List.map (fun (a, b) -> "(" ^ string_of_int a ^ ", " ^ b ^ ")") a)
)
, Alcotest.(pair int string)
, [
(ascending, [(1, "fst"); (1, "snd")], Some (1, "fst"))
; (descending, [(1, "fst"); (1, "snd")], Some (1, "fst"))
; (ascending, [(1, "fst"); (1, "snd"); (2, "nil")], Some (1, "fst"))
; (descending, [(1, "nil"); (2, "fst"); (2, "snd")], Some (2, "fst"))
]
)
in
let tests = List.map test specs in
("find_minimum", tests)
test_find_minimum specs_tuple

let () =
Alcotest.run "Listext"
Expand All @@ -211,5 +235,6 @@ let () =
; test_drop
; test_chop
; test_sub
; test_find_minimum
; test_find_minimum_int
; test_find_minimum_tuple
]

0 comments on commit d33f0b7

Please sign in to comment.