t_mcdb2.ml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. open Alcotest
  2. open Mcdb__Mcdb2
  3. let set_up () =
  4. Unix.chdir "../../../mcdb/"
  5. let tc_strstrmap () =
  6. let module StrTuple = struct
  7. type t = string * string
  8. let compare (x0,y0) (x1,y1) =
  9. match String.compare x0 x1 with
  10. 0 -> String.compare y0 y1
  11. | c -> c
  12. end in
  13. let module StrMap = Map.Make(StrTuple) in
  14. let m = StrMap.(empty |> add ("a","A") "aa") in
  15. StrMap.find_opt ("a","A") m
  16. |> Option.value ~default:"b"
  17. |> check string __LOC__ "aa"
  18. let tc_read4b () =
  19. let open Bigarray in
  20. let fd = Unix.openfile "testdata/mini.cdb" [ Unix.O_RDONLY ] 0 in
  21. let mf = Unix.map_file fd int8_unsigned c_layout false [| -1 |] in
  22. let arr = array1_of_genarray mf in
  23. let _len = Array1.dim arr in
  24. 0 |> read4b arr |> check Alcotest.int __LOC__ 2080;
  25. 256 * 8 + 0 |> read4b arr |> check Alcotest.int __LOC__ 1;
  26. 256 * 8 + 4 |> read4b arr |> check Alcotest.int __LOC__ 2;
  27. fd |> Unix.close
  28. let tc_blit () =
  29. let open Bigarray in
  30. let fd = Unix.openfile "testdata/mini.cdb" [ Unix.O_RDONLY ] 0 in
  31. let mf = Unix.map_file fd int8_unsigned c_layout false [| -1 |] in
  32. let arr = array1_of_genarray mf in
  33. 256 * 8 + 0 |> read4b arr |> check Alcotest.int __LOC__ 1;
  34. 256 * 8 + 4 |> read4b arr |> check Alcotest.int __LOC__ 2;
  35. let b0 = blit arr (256 * 8 + 2*4) (1) in
  36. let b1 = blit arr (256 * 8 + 2*4 + 1) (2) in
  37. fd |> Unix.close;
  38. b0 |> String.of_bytes |> check string __LOC__ "a";
  39. b1 |> String.of_bytes |> check string __LOC__ "Ä"
  40. let tc_record () =
  41. let open Bigarray in
  42. let fd = Unix.openfile "testdata/mini.cdb" [ Unix.O_RDONLY ] 0 in
  43. let mf = Unix.map_file fd int8_unsigned c_layout false [| -1 |] in
  44. let arr = array1_of_genarray mf in
  45. let (k,v) = 256*8 |> read_record arr in
  46. fd |> Unix.close;
  47. k |> String.of_bytes |> check string __LOC__ "a";
  48. v |> String.of_bytes |> check string __LOC__ "Ä"
  49. let tc_fold_left () =
  50. (match "testdata/mini.cdb"
  51. |> fold_left
  52. (fun init (_,(k,v)) -> (k |> Bytes.to_string, v |> Bytes.to_string) :: init)
  53. []
  54. |> List.rev with
  55. | [("a","Ä");
  56. ("b","B");
  57. ("s","ß");] -> ()
  58. | _ -> Alcotest.fail __LOC__);
  59. match "testdata/mini.cdb"
  60. |> fold_left
  61. (fun init (_,(k,v)) -> (k |> Bytes.to_string, v |> Bytes.to_string) :: init)
  62. []
  63. |> List.rev with
  64. | [("a","Ä");
  65. ("b","B");
  66. ("s","ß");] -> ()
  67. | _ -> Alcotest.fail __LOC__
  68. let tc_fold_left_key () =
  69. let k = "b" |> Bytes.of_string in
  70. (match "testdata/mini.cdb"
  71. |> fold_left_key k
  72. (fun init v -> (v |> Bytes.to_string) :: init)
  73. []
  74. |> List.rev with
  75. | ["B"] -> ()
  76. | _ -> Alcotest.fail __LOC__);
  77. let k = "b" |> Bytes.of_string in
  78. "testdata/mini-dup.cdb"
  79. |> fold_left_key k
  80. (fun init v -> (v |> Bytes.to_string) :: init)
  81. []
  82. |> List.rev
  83. |> List.length
  84. |> check int __LOC__ 20
  85. let tc_find_first_key () =
  86. let k = "b" |> Bytes.of_string in
  87. (match "testdata/mini.cdb"
  88. |> find_first_key k
  89. with
  90. | Some b -> b |> Bytes.to_string |> check string __LOC__ "B"
  91. | _ -> Alcotest.fail __LOC__)
  92. let tc_array () =
  93. (* an array with 256 entries *)
  94. (* each entry: hash + position *)
  95. ()
  96. let tc_scan_data () =
  97. (* let l = Mcdb__Mcdb2.scan_data "testdata/mini-dup.cdb" in *)
  98. (* an array with 256 entries *)
  99. (* each entry: hash + position *)
  100. (* l |> List.length |> check int __LOC__ 0 *)
  101. let i = IntMap.(empty |> add 1 [1000]) in
  102. i |> IntMap.cardinal |> check int __LOC__ 1;
  103. (match i |> IntMap.find_opt 1 with
  104. | Some [v] -> v |> check int __LOC__ 1000
  105. | _ -> fail __LOC__ );
  106. let i = i |> IntMap.add 1 (match i |> IntMap.find_opt 1 with
  107. | Some l -> 1001 :: l
  108. | None -> []) in
  109. i |> IntMap.cardinal |> check int __LOC__ 1;
  110. (match i |> IntMap.find_opt 1 with
  111. | Some [a; b] ->
  112. a |> check int __LOC__ 1001;
  113. b |> check int __LOC__ 1000
  114. | _ -> fail __LOC__ );
  115. let a = Array.make 256 IntMap.empty in
  116. (1234,12) |> kv_add' a;
  117. (* (1234,12) |> kv_add' a; *)
  118. (1234,13) |> kv_add' a;
  119. (1234,14) |> kv_add' a;
  120. (1235,15) |> kv_add' a;
  121. let hm = a.(1235 mod 256) in
  122. hm |> IntMap.cardinal |> check int __LOC__ 1;
  123. (match hm |> IntMap.find_opt 1235 with
  124. | Some s -> s |> IntSet.find_opt 15 |> Option.value ~default:3 |> check int __LOC__ 15
  125. | _ -> fail __LOC__);
  126. let hm = a.(1234 mod 256) in
  127. hm |> IntMap.cardinal |> check int __LOC__ 1;
  128. (match hm |> IntMap.find_opt 1234 with
  129. | Some s ->
  130. s |> IntSet.cardinal |> check int __LOC__ 3;
  131. s |> IntSet.find_opt 14 |> Option.value ~default:3 |> check int __LOC__ 14;
  132. s |> IntSet.find_opt 13 |> Option.value ~default:3 |> check int __LOC__ 13;
  133. s |> IntSet.find_opt 12 |> Option.value ~default:3 |> check int __LOC__ 12
  134. | _ -> fail __LOC__);
  135. ()
  136. let tc_sortedset () =
  137. let _s = IntSet.(empty |> add 1 |> add 2 |> add 2) in
  138. _s |> IntSet.cardinal |> check int __LOC__ 2;
  139. ()
  140. let () =
  141. run
  142. "Mcdb2" [
  143. __FILE__ , [
  144. "set_up" , `Quick, set_up;
  145. "tc_strstrmap", `Quick, tc_strstrmap;
  146. "tc_read4b", `Quick, tc_read4b;
  147. "tc_blit", `Quick, tc_blit;
  148. "tc_record", `Quick, tc_record;
  149. "tc_fold_left", `Quick, tc_fold_left;
  150. "tc_fold_left_key", `Quick, tc_fold_left_key;
  151. "tc_find_first_key", `Quick, tc_find_first_key;
  152. "tc_array", `Quick, tc_array;
  153. "tc_scan_data", `Quick, tc_scan_data;
  154. "tc_sortedset", `Quick, tc_sortedset;
  155. ]
  156. ]