From 9643ee94a99cb44b42ef951ba5aa6f8fbac4c302 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 7 Oct 2020 12:03:37 -0400 Subject: [PATCH] test: add some basic tests to CCMutHeap --- src/data/CCMutHeap.ml | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/data/CCMutHeap.ml b/src/data/CCMutHeap.ml index 2c260713..10d6b110 100644 --- a/src/data/CCMutHeap.ml +++ b/src/data/CCMutHeap.ml @@ -144,3 +144,78 @@ module Make(Elt : RANKED) = struct x end [@@inline] + +(*$inject + type elt = { + x: string; + mutable rank: int; + mutable idx: int; + } + module Elt = struct + type t = elt + let idx x = x.idx + let set_idx x i = x.idx <- i + let lt a b = + if a.rank = b.rank then a.x < b.x else a.rank < b.rank + end + module H = CCMutHeap.Make(Elt) +*) + +(*$R + let h = H.create() in + let x1 = {x="a"; rank=10; idx= -1} in + let x2 = {x="b"; rank=10; idx= -1} in + let x3 = {x="c"; rank=10; idx= -1} in + H.insert h x1; + assert (H.in_heap x1); + assert (not (H.in_heap x2)); + assert (not (H.in_heap x3)); + H.insert h x2; + H.insert h x3; + + assert (Elt.lt x1 x2); + assert (Elt.lt x2 x3); + + let x = H.remove_min h in + assert (x == x1); + + let x = H.remove_min h in + assert (x == x2); + + let x = H.remove_min h in + assert (x == x3); + + assert (try ignore (H.remove_min h); false with Not_found -> true); + + *) + +(*$R + let h = H.create() in + let x1 = {x="a"; rank=10; idx= -1} in + let x2 = {x="b"; rank=10; idx= -1} in + let x3 = {x="c"; rank=10; idx= -1} in + H.insert h x1; + H.insert h x2; + H.insert h x3; + + x3.rank <- 2; + H.decrease h x3; + + assert (Elt.lt x3 x1); + assert (Elt.lt x3 x2); + + let x = H.remove_min h in + assert (x == x3); + + x1.rank <- 20; + H.increase h x1; + + let x = H.remove_min h in + assert (x == x2); + + let x = H.remove_min h in + assert (x == x1); + + assert (try ignore (H.remove_min h); false with Not_found -> true); + + *)