Skip to content

Commit f2f72dc

Browse files
committed
xapi-stdext-threads, test: use stable testing interface
Alcotest changed how match_raises to actually be reasonable instead of failing when the mathing function returns true (note how is_oob expects a string with two spaces) This also highlights why matching strings in exception is a bad idea. Instead raise a non-stringy exception and use polymorphic compare. Signed-off-by: Pau Ruiz Safont <pau.ruizsafont@cloud.com>
1 parent 5c387c4 commit f2f72dc

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/ipq.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type 'a t = {default: 'a event; mutable size: int; mutable data: 'a event array}
1919

2020
exception EmptyHeap
2121

22+
exception OutOfBounds of int
23+
2224
let create n default =
2325
if n <= 0 then
2426
invalid_arg "create"
@@ -61,7 +63,7 @@ let maximum h =
6163
let remove h s =
6264
if h.size <= 0 then raise EmptyHeap ;
6365
if s < 0 || s >= h.size then
64-
invalid_arg (Printf.sprintf "%s: index %d out of bounds" __FUNCTION__ s) ;
66+
raise (OutOfBounds s) ;
6567
let n = h.size - 1 in
6668
let d = h.data in
6769
let x = d.(n) in

ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/ipq.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type 'a t
1818

1919
exception EmptyHeap
2020

21+
exception OutOfBounds of int
22+
2123
val create : int -> 'a -> 'a t
2224
(** [create n default] creates an empty Imperative priority queue.
2325
The queue initially is initialized to store [n] elements.

ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/ipq_test.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ module Ipq = Xapi_stdext_threads_scheduler.Ipq
1818
let test_out_of_index () =
1919
let q = Ipq.create 10 0 in
2020
Ipq.add q {Ipq.ev= 123; Ipq.time= Mtime_clock.elapsed ()} ;
21-
let is_oob = function
22-
| Invalid_argument s when String.ends_with ~suffix:" out of bounds" s ->
23-
true
24-
| _ ->
25-
false
26-
in
2721
let oob_check n =
28-
(Alcotest.match_raises "out of bound" is_oob @@ fun () -> Ipq.remove q n) ;
22+
let oob = Ipq.OutOfBounds n in
23+
(Alcotest.check_raises "out of bound" oob @@ fun () -> Ipq.remove q n) ;
2924
Alcotest.(check bool) "same value" false (Ipq.is_empty q)
3025
in
3126
oob_check 10 ;

0 commit comments

Comments
 (0)