View raw

1 (** The bounded FIFO cache: hits, misses, eviction order, and no-op re-adds. *) 2 3 let test_find_and_add () = 4 let cache = Ogit.Cache.create ~capacity:4 in 5 Alcotest.(check (option int)) "miss" None (Ogit.Cache.find cache "a"); 6 Ogit.Cache.add cache "a" 1; 7 Alcotest.(check (option int)) "hit" (Some 1) (Ogit.Cache.find cache "a") 8 9 let test_eviction () = 10 let cache = Ogit.Cache.create ~capacity:2 in 11 Ogit.Cache.add cache "a" 1; 12 Ogit.Cache.add cache "b" 2; 13 Ogit.Cache.add cache "c" 3; 14 Alcotest.(check (option int)) "oldest evicted" None (Ogit.Cache.find cache "a"); 15 Alcotest.(check (option int)) "second kept" (Some 2) (Ogit.Cache.find cache "b"); 16 Alcotest.(check (option int)) "newest kept" (Some 3) (Ogit.Cache.find cache "c") 17 18 let test_readd_is_noop () = 19 let cache = Ogit.Cache.create ~capacity:2 in 20 Ogit.Cache.add cache "a" 1; 21 Ogit.Cache.add cache "a" 9; 22 Alcotest.(check (option int)) 23 "value unchanged" (Some 1) (Ogit.Cache.find cache "a") 24 25 let suite = 26 ( "cache", 27 [ 28 Alcotest.test_case "find and add" `Quick test_find_and_add; 29 Alcotest.test_case "eviction" `Quick test_eviction; 30 Alcotest.test_case "re-add is a no-op" `Quick test_readd_is_noop; 31 ] ) 32