cachet/test/test.ml

42 lines
1.5 KiB
OCaml
Raw Permalink Normal View History

2024-11-07 19:11:22 +00:00
let random ?g len =
2024-11-08 11:40:10 +00:00
let bstr = Bigarray.Array1.create Bigarray.char Bigarray.c_layout len in
2024-11-07 19:11:22 +00:00
for i = 0 to len - 1 do
let chr =
match g with
| Some g -> Char.unsafe_chr (Random.State.bits g land 0xff)
| None -> Char.unsafe_chr (Random.bits () land 0xff)
in
2024-11-08 11:40:10 +00:00
bstr.{i} <- chr
2024-11-07 19:11:22 +00:00
done;
bstr
let make ?cachesize ?pagesize ?g len =
let bstr = random ?g len in
let map () ~pos len =
if pos < 0 || len < 0 || pos > Bigarray.Array1.dim bstr then
Printf.ksprintf invalid_arg "map ~pos:%d %d" pos len;
let len' = Int.min (Bigarray.Array1.dim bstr - pos) len in
Bigarray.Array1.sub bstr pos len'
in
2024-11-08 11:40:10 +00:00
(Cachet.make ?cachesize ?pagesize ~map (), Cachet.Bstr.of_bigstring bstr)
2024-11-07 19:11:22 +00:00
let test01 =
Alcotest.test_case "test01" `Quick @@ fun () ->
let t, oracle = make ~cachesize:0x100 ~pagesize:0x100 0xe000 in
let a = Cachet.get_uint8 t 0xdead in
2024-11-08 11:40:10 +00:00
let b = Char.code (Cachet.Bstr.get oracle 0xdead) in
2024-11-07 19:11:22 +00:00
Alcotest.(check int) "0xdead" a b;
let a = Cachet.get_string t 0xdead ~len:10 in
2024-11-08 11:40:10 +00:00
let b = Cachet.Bstr.sub_string oracle ~off:0xdead ~len:10 in
2024-11-07 19:11:22 +00:00
Alcotest.(check string) "0xdead" a b;
let a = Cachet.get_string t 0xdea0 ~len:10 in
2024-11-08 11:40:10 +00:00
let b = Cachet.Bstr.sub_string oracle ~off:0xdea0 ~len:10 in
2024-11-07 19:11:22 +00:00
Alcotest.(check string) "0xdea0" a b;
let a = Cachet.get_seq t 0 in
2024-11-08 11:40:10 +00:00
let b = Cachet.Bstr.to_string oracle in
2024-11-07 19:11:22 +00:00
let a = List.of_seq a in
let a = String.concat "" a in
Alcotest.(check string) "all" a b
2024-11-08 11:40:10 +00:00
let () = Alcotest.run "cachet" [ ("simple", [ test01 ]) ]