t_ds_cdb.ml 858 B

123456789101112131415161718192021222324252627282930313233
  1. open Seppo_lib
  2. let find_1 (fn : string) (k : string) : string option =
  3. let cdb = fn |> Ds_cdb.open_cdb_in in
  4. let ret =
  5. match k |> Bytes.of_string |> Ds_cdb.find_first cdb with
  6. | Some b -> Some (b |> Bytes.to_string)
  7. | None -> None
  8. in
  9. cdb |> Ds_cdb.close_cdb_in;
  10. ret
  11. let test_find_1 () =
  12. "s" |> find_1 "mini.cdb" |> Option.get |> Assrt.equals_string __LOC__ "ß";
  13. assert (None = ("zzz" |> find_1 "mini.cdb"));
  14. assert true
  15. let test_make () =
  16. let fn = "tmp/t.cdb" in
  17. (try Unix.unlink fn with Unix.Unix_error (_, _, _) -> ());
  18. let cdb = Ds_cdb.open_out fn in
  19. Ds_cdb.add cdb ("k" |> Bytes.of_string) ("v" |> Bytes.of_string);
  20. Ds_cdb.close_cdb_out cdb;
  21. find_1 fn "k" |> Option.get |> Assrt.equals_string __LOC__ "v";
  22. assert true
  23. let () =
  24. Unix.chdir "../../../test/";
  25. test_find_1 ();
  26. test_make ();
  27. assert true