[lru] strengthen tests with key-value pair order

Summary: Tests for the LRU cache now expect a fixed order of key-value pairs in the cache where previously only the set of key-value pairs was considered.

Reviewed By: jvillard

Differential Revision: D25301133

fbshipit-source-id: 0d8077950
master
Nikos Gorogiannis 4 years ago committed by Facebook GitHub Bot
parent 3800a050f1
commit 3ceac69079

@ -60,6 +60,8 @@ module Make (Key : Hashtbl.HashedType) = struct
let remove {list} n = Doubly_linked.remove list n let remove {list} n = Doubly_linked.remove list n
let clear {list} = Doubly_linked.clear list let clear {list} = Doubly_linked.clear list
let to_list {list} = Doubly_linked.to_list list
end end
type 'a t = {map: ('a * key Doubly_linked.Elt.t) Hash.t; lru: LRU.t} type 'a t = {map: ('a * key Doubly_linked.Elt.t) Hash.t; lru: LRU.t}
@ -114,5 +116,6 @@ module Make (Key : Hashtbl.HashedType) = struct
F.pp_print_string f "}" F.pp_print_string f "}"
let bindings {map} = Seq.fold_left (fun acc (k, (v, _node)) -> (k, v) :: acc) [] (Hash.to_seq map) let bindings {map; lru} =
LRU.to_list lru |> List.map ~f:(fun key -> (key, Hash.find map key |> fst))
end end

@ -32,6 +32,7 @@ module type S = sig
-> unit -> unit
val bindings : 'a t -> (key * 'a) list val bindings : 'a t -> (key * 'a) list
(** visible for testing mainly; makes linear number of hashtable lookups *)
end end
module Make (Key : Caml.Hashtbl.HashedType) : S with type key = Key.t module Make (Key : Caml.Hashtbl.HashedType) : S with type key = Key.t

@ -17,6 +17,14 @@ let inputs =
LRUHash.replace map 0 10 ; LRUHash.replace map 0 10 ;
map ) map )
, [(0, 10)] ) , [(0, 10)] )
; ( "LRU order"
, (fun () ->
let map = LRUHash.create ~initial_size:5 ~max_size:3 in
LRUHash.replace map 0 10 ;
LRUHash.replace map 1 10 ;
LRUHash.replace map 2 10 ;
map )
, [(2, 10); (1, 10); (0, 10)] )
; ( "LRU1" ; ( "LRU1"
, (fun () -> , (fun () ->
let map = LRUHash.create ~initial_size:5 ~max_size:3 in let map = LRUHash.create ~initial_size:5 ~max_size:3 in
@ -27,7 +35,7 @@ let inputs =
LRUHash.replace map 3 10 ; LRUHash.replace map 3 10 ;
LRUHash.replace map 4 10 ; LRUHash.replace map 4 10 ;
map ) map )
, [(1, 10); (3, 10); (4, 10)] ) , [(4, 10); (3, 10); (1, 10)] )
; ( "LRU2" ; ( "LRU2"
, (fun () -> , (fun () ->
let map = LRUHash.create ~initial_size:5 ~max_size:3 in let map = LRUHash.create ~initial_size:5 ~max_size:3 in
@ -37,7 +45,17 @@ let inputs =
LRUHash.replace map 0 20 ; LRUHash.replace map 0 20 ;
LRUHash.replace map 3 10 ; LRUHash.replace map 3 10 ;
map ) map )
, [(0, 20); (2, 10); (3, 10)] ) , [(3, 10); (0, 20); (2, 10)] )
; ( "remove"
, (fun () ->
let map = LRUHash.create ~initial_size:5 ~max_size:3 in
LRUHash.replace map 0 10 ;
LRUHash.replace map 1 10 ;
LRUHash.replace map 2 10 ;
LRUHash.remove map 1 ;
LRUHash.replace map 3 10 ;
map )
, [(3, 10); (2, 10); (0, 10)] )
; ( "clear" ; ( "clear"
, (fun () -> , (fun () ->
let map = LRUHash.create ~initial_size:5 ~max_size:3 in let map = LRUHash.create ~initial_size:5 ~max_size:3 in
@ -50,11 +68,6 @@ let inputs =
let tests = let tests =
let compare (k1, v1) (k2, v2) =
let c = k1 - k2 in
if c <> 0 then c else v1 - v2
in
"LRUHashtble" "LRUHashtble"
>::: List.map inputs ~f:(fun (name, input, expected) -> >::: List.map inputs ~f:(fun (name, input, expected) ->
name name >:: fun _ -> assert_equal (input () |> LRUHash.bindings) expected )
>:: fun _ -> assert_equal (input () |> LRUHash.bindings |> List.sort ~compare) expected )

Loading…
Cancel
Save