Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126module M = Git__.Strmap
(* Keys whose bucket the tests rely on, from git's [strhash] (FNV-1, 32 bits): [k0] and [k62] hash to 172 modulo 256, so they share a bucket in a table of 64 (bucket 44) and in one of 256; [k1] lands in bucket 45 of 64. *)let k0 = "k0"let k1 = "k1"let k62 = "k62"let keys m = List.map fst (M.to_list m)
let index_of k l = let rec go i = function | [] -> Alcotest.failf "%s not iterated" k | x :: _ when String.equal x k -> i | _ :: rest -> go (i + 1) rest in go 0 l
(* [k0] comes before [k62] in the iteration order of [m]. *)let k0_first m = let l = keys m in index_of k0 l < index_of k62 l
let test_strhash () = (* The FNV-1 32-bit reference vectors (hashmap.c's FNV32_BASE and FNV32_PRIME: hash = (hash * prime) ^ byte). *) Alcotest.(check int) "empty" 0x811c9dc5 (M.strhash ""); Alcotest.(check int) "a" 0x050c5d7e (M.strhash "a"); Alcotest.(check int) "foobar" 0x31f0b262 (M.strhash "foobar"); Alcotest.(check int) "k0 bucket" 172 (M.strhash k0 land 255); Alcotest.(check int) "k62 bucket" 172 (M.strhash k62 land 255); Alcotest.(check int) "k1 bucket" 45 (M.strhash k1 land 63)
let test_buckets_in_order () = (* hashmap_iter_next walks the table from bucket 0 up, whatever the order of insertion. *) let m = M.v () in M.replace m k1 1; M.replace m k0 0; Alcotest.(check (list (pair string int))) "bucket 44 then 45" [ (k0, 0); (k1, 1) ] (M.to_list m)
let test_new_entry_at_head () = (* hashmap_add links a new entry at the head of its bucket. *) let m = M.v () in M.replace m k0 0; M.replace m k62 62; Alcotest.(check (list string)) "newest first" [ k62; k0 ] (keys m)
let test_replace_in_place () = (* strmap_put on a bound key replaces the value and keeps the entry where it is. *) let m = M.v () in M.replace m k0 0; M.replace m k62 62; M.replace m k0 100; Alcotest.(check (list (pair string int))) "same order, new value" [ (k62, 62); (k0, 100) ] (M.to_list m); Alcotest.(check (option int)) "find" (Some 100) (M.find m k0)
let test_remove () = let m = M.v () in Alcotest.(check bool) "empty" true (M.is_empty m); M.replace m k0 0; M.replace m k62 62; M.remove m "absent"; Alcotest.(check (list string)) "absent key" [ k62; k0 ] (keys m); M.remove m k62; Alcotest.(check (list string)) "removed" [ k0 ] (keys m); Alcotest.(check bool) "mem removed" false (M.mem m k62); Alcotest.(check (option int)) "find removed" None (M.find m k62); Alcotest.(check bool) "not empty" false (M.is_empty m); M.remove m k0; Alcotest.(check bool) "empty again" true (M.is_empty m); Alcotest.(check (list string)) "no binding" [] (keys m)
let filler i = Fmt.str "f%d" i
let test_grow_reverses () = (* A table of 64 grows to 256 once it holds more than 64 * 80 / 100 = 51 entries; rehash pushes each old entry, bucket by bucket, at the head of its new bucket, which reverses the entries sharing one. *) let m = M.v () in M.replace m k0 0; M.replace m k62 62; for i = 1 to 49 do M.replace m (filler i) i done; Alcotest.(check bool) "51 entries: no rehash" false (k0_first m); M.replace m (filler 50) 50; Alcotest.(check bool) "52 entries: rehashed" true (k0_first m); Alcotest.(check int) "bindings" 52 (List.length (M.to_list m))
let test_shrink_reverses () = (* At 256 buckets the table shrinks back to 64 once fewer than (256 * 80 / 100) / 5 = 40 entries remain. *) let m = M.v () in M.replace m k0 0; M.replace m k62 62; for i = 1 to 50 do M.replace m (filler i) i done; for i = 1 to 12 do M.remove m (filler i) done; Alcotest.(check bool) "40 entries: no rehash" true (k0_first m); M.remove m (filler 13); Alcotest.(check bool) "39 entries: rehashed" false (k0_first m); Alcotest.(check int) "bindings" 39 (List.length (M.to_list m))
let suite = ( "strmap", [ Alcotest.test_case "strhash" `Quick test_strhash; Alcotest.test_case "buckets in order" `Quick test_buckets_in_order; Alcotest.test_case "new entry at head" `Quick test_new_entry_at_head; Alcotest.test_case "replace in place" `Quick test_replace_in_place; Alcotest.test_case "remove" `Quick test_remove; Alcotest.test_case "grow reverses" `Quick test_grow_reverses; Alcotest.test_case "shrink reverses" `Quick test_shrink_reverses; ] )