This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,53 @@
open Test
let test01 =
let descr = {text|cstring|text} in
Test.test ~title:"cstring" ~descr @@ fun () ->
let buf = Bstr.create 0x7ff in
let test str =
let pos = ref 0 in
let len = String.length str in
Bin.encode_bstr Bin.cstring str buf pos;
check (!pos == len + 1);
check (Bstr.get_uint8 buf len == 0);
check (Bstr.sub_string buf ~off:0 ~len = str);
pos := 0;
let str' = Bin.decode_bstr Bin.cstring buf pos in
check (!pos == len + 1);
check (str = str')
in
test "foo"; test "bar"
let test02 =
let descr = {text|varint|text} in
Test.test ~title:"varint" ~descr @@ fun () ->
let buf = Bstr.create 0x7ff in
let test value expected =
let pos = ref 0 in
Bin.encode_bstr Bin.varint value buf pos;
let len = String.length expected in
check (!pos == len);
check (Bstr.sub_string buf ~off:0 ~len = expected);
pos := 0;
let value' = Bin.decode_bstr Bin.varint buf pos in
check (!pos == len);
check (value == value')
in
test 0 "\000";
test 127 "\127";
test 128 "\128\001";
test 16384 "\128\128\001";
test 88080384 "\128\128\128\042"
let ( / ) = Filename.concat
let () =
let tests = [ test01; test02 ] in
let ({ Test.directory } as runner) = Test.runner (Sys.getcwd () / "_tests") in
let run idx test =
Format.printf "test%03d: %!" (succ idx);
Test.run runner test;
Format.printf "ok\n%!"
in
Format.printf "Run tests into %s\n%!" directory;
List.iteri run tests

View file

@ -0,0 +1,22 @@
(library
(name test)
(modules test)
(libraries unix))
(test
(name t)
(modules t)
(package bstr)
(libraries bstr test))
(test
(name b)
(modules b)
(package bin)
(libraries bin bstr test))
(test
(name s)
(modules s)
(package slice)
(libraries slice.bstr test))

View file

@ -0,0 +1,83 @@
open Test
module S = Slice_bstr
let test01 =
let descr = {text|String.Sub misc. base functions|text} in
Test.test ~title:"misc" ~descr @@ fun () ->
let eq sbstr str = String.equal (S.to_string sbstr) str |> check in
let err v =
match Lazy.force v with
| exception Invalid_argument _ -> check true
| _ -> check false
in
eq S.empty "";
eq (S.of_string "abc") "abc";
eq (S.string "abc" ~off:0 ~len:1) "a";
eq (S.string "abc" ~off:1 ~len:1) "b";
eq (S.string "abc" ~off:1 ~len:2) "bc";
eq (S.string "abc" ~off:2 ~len:1) "c";
eq (S.string "abc" ~off:2 ~len:0) "";
let v = S.string "abc" ~off:2 ~len:1 in
lazy (S.get v 3) |> err;
lazy (S.get v 2) |> err;
lazy (S.get v 1) |> err;
check (S.get v 0 == 'c')
let test02 =
let descr = {text|overlap|text} in
Test.test ~title:"overlap" ~descr @@ fun () ->
let test value expected =
match (value, expected) with
| None, None -> check true
| Some (len, a, b), Some (len', x, y) ->
Format.eprintf "len:%d, a:%d, b:%d\n%!" len a b;
check (a == x && b == y && len == len')
| _ -> check false
in
let t = S.string (String.make 10 '\000') in
let ab = S.sub t ~off:5 ~len:5 in
let cd = S.sub t ~off:0 ~len:5 in
test (S.overlap ab cd) None;
let ab = S.sub t ~off:0 ~len:5 in
let cd = S.sub t ~off:5 ~len:5 in
test (S.overlap ab cd) None;
let ab = S.sub t ~off:0 ~len:6 in
let cd = S.sub t ~off:5 ~len:5 in
test (S.overlap ab cd) (Some (1, 5, 0));
let ab = S.sub t ~off:5 ~len:5 in
let cd = S.sub t ~off:0 ~len:6 in
test (S.overlap ab cd) (Some (1, 0, 5));
let ab = S.sub t ~off:0 ~len:8 in
let cd = S.sub t ~off:2 ~len:8 in
test (S.overlap ab cd) (Some (6, 2, 0));
let ab = S.sub t ~off:0 ~len:10 in
let cd = S.sub t ~off:2 ~len:8 in
test (S.overlap ab cd) (Some (8, 2, 0));
let ab = S.sub t ~off:0 ~len:10 in
let cd = S.sub t ~off:2 ~len:6 in
test (S.overlap ab cd) (Some (6, 2, 0));
let ab = S.sub t ~off:0 ~len:8 in
let cd = S.sub t ~off:0 ~len:10 in
test (S.overlap ab cd) (Some (8, 0, 0));
let ab = S.sub t ~off:2 ~len:6 in
let cd = S.sub t ~off:0 ~len:10 in
test (S.overlap ab cd) (Some (6, 0, 2));
let ab = S.sub t ~off:2 ~len:8 in
let cd = S.sub t ~off:0 ~len:10 in
test (S.overlap ab cd) (Some (8, 0, 2));
let ab = S.sub t ~off:2 ~len:8 in
let cd = S.sub t ~off:0 ~len:8 in
test (S.overlap ab cd) (Some (6, 0, 2))
let ( / ) = Filename.concat
let () =
let tests = [ test01; test02 ] in
let ({ Test.directory } as runner) = Test.runner (Sys.getcwd () / "_tests") in
let run idx test =
Format.printf "test%03d: %!" (succ idx);
Test.run runner test;
Format.printf "ok\n%!"
in
Format.printf "Run tests into %s\n%!" directory;
List.iteri run tests

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,75 @@
let strf fmt = Format.asprintf fmt
let ( / ) = Filename.concat
let check test =
let bt = Printexc.get_callstack max_int in
try
assert test;
print_string ".";
flush stdout
with exn ->
print_string "x";
flush stdout;
Printexc.raise_with_backtrace exn bt
type t = { title: string; descr: string; fn: unit -> unit }
let test ~title ~descr fn = { title; descr; fn }
type runner = { directory: string }
let rec mkdir_p path perm =
if path <> "" then begin
try Unix.mkdir path perm with
| Unix.Unix_error (EEXIST, _, _) when Sys.is_directory path -> ()
| Unix.Unix_error (ENOENT, _, _) ->
mkdir_p (Filename.dirname path) perm;
Unix.mkdir path perm
end
let mkdir ({ directory } as runner) = mkdir_p directory 0o755; runner
type ('a, 'b) str = ('a -> 'b, Format.formatter, unit, string) format4
let runner ?(g = Random.State.make_self_init ())
?(fmt : ('a, 'b) str = "run-%s") root =
let random_string len =
let res = Bytes.create len in
for i = 0 to len - 1 do
let chr =
match Random.State.int g (26 + 26 + 10) with
| n when n < 26 -> Char.chr (Char.code 'a' + n)
| n when n < 26 + 26 -> Char.chr (Char.code 'A' + n - 26)
| n -> Char.chr (Char.code '0' + n - 26 - 26)
in
Bytes.set res i chr
done;
Bytes.unsafe_to_string res
in
let rec go retry =
if retry >= 10 then failwith "Impossible to create a test directory";
let directory = root / strf fmt (random_string 4) in
if Sys.file_exists directory then go (succ retry) else mkdir { directory }
in
go 0
let run { directory= dir } { title; fn; _ } =
let old_stderr = Unix.dup Unix.stderr in
let new_stderr = open_out_bin (dir / strf "%s.stderr" title) in
Unix.dup2 (Unix.descr_of_out_channel new_stderr) Unix.stderr;
let finally () =
flush stderr;
Unix.dup2 old_stderr Unix.stderr;
Unix.close old_stderr;
close_out new_stderr
in
Format.eprintf "*** %s ***\n%!" title;
try Fun.protect ~finally fn
with exn ->
let ic = open_in_bin (dir / strf "%s.stderr" title) in
let ln = in_channel_length ic in
let rs = Bytes.create ln in
really_input ic rs 0 ln;
Format.printf "Terminated with: %S\n%!" (Printexc.to_string exn);
Format.printf "%s\n%!" (Bytes.unsafe_to_string rs);
exit 1

View file

@ -0,0 +1,8 @@
type t
type runner = private { directory: string }
type ('a, 'b) str = ('a -> 'b, Format.formatter, unit, string) format4
val check : bool -> unit
val test : title:string -> descr:string -> (unit -> unit) -> t
val runner : ?g:Random.State.t -> ?fmt:(string, string) str -> string -> runner
val run : runner -> t -> unit