more tests

This commit is contained in:
Simon Cruanes 2026-04-09 00:58:25 +00:00
parent 749f973528
commit e42caf3b6a
3 changed files with 106 additions and 0 deletions

View file

@ -5,6 +5,7 @@ Containers_testlib.run_all ~descr:"containers"
T_array.get ();
T_bool.get ();
T_byte_buffer.get ();
T_byte_slice.get ();
T_canonical_sexp.get ();
T_char.get ();
T_either.get ();

View file

@ -152,3 +152,71 @@ let prop_consistent ops =
;;
q arb (fun ops -> prop_consistent ops)
;;
(* --- iter/fold_left/iteri off-by-one --- *)
t @@ fun () ->
(* empty buffer: iter should call f zero times *)
let b = create () in
let n = ref 0 in
iter (fun _ -> incr n) b;
!n = 0
;;
t @@ fun () ->
(* non-empty buffer: iter visits exactly [length b] chars *)
let b = create () in
append_string b "abc";
let chars = ref [] in
iter (fun c -> chars := c :: !chars) b;
List.rev !chars = [ 'a'; 'b'; 'c' ]
;;
t @@ fun () ->
(* fold_left on empty buffer returns accumulator unchanged *)
let b = create () in
fold_left (fun acc _ -> acc + 1) 0 b = 0
;;
t @@ fun () ->
(* fold_left counts exactly [length b] chars *)
let b = create () in
append_string b "hello";
fold_left (fun acc _ -> acc + 1) 0 b = 5
;;
t @@ fun () ->
(* iteri visits exactly [length b] indices *)
let b = create () in
append_string b "ab";
let pairs = ref [] in
iteri (fun i c -> pairs := (i, c) :: !pairs) b;
List.rev !pairs = [ 0, 'a'; 1, 'b' ]
;;
(* --- copy --- *)
t @@ fun () ->
let b = create () in
append_string b "hello";
let b2 = copy b in
contents b = contents b2
;;
t @@ fun () ->
(* copy is independent: mutating original doesn't affect copy *)
let b = create () in
append_string b "hello";
let b2 = copy b in
add_char b '!';
contents b2 = "hello"
;;
t @@ fun () ->
(* copy is independent: mutating copy doesn't affect original *)
let b = create () in
append_string b "hello";
let b2 = copy b in
add_char b2 '?';
contents b = "hello"

View file

@ -0,0 +1,37 @@
module T = (val Containers_testlib.make ~__FILE__ ())
include T
open CCByte_slice;;
(* --- of_string --- *)
t @@ fun () ->
let sl = of_string "hello" in
len sl = 5
;;
t @@ fun () ->
let sl = of_string "hello" in
contents sl = "hello"
;;
t @@ fun () ->
let sl = of_string "" in
len sl = 0
;;
(* --- clear --- *)
t @@ fun () ->
let sl = of_string "hello" in
clear sl;
len sl = 0
;;
t @@ fun () ->
(* after clear, get raises *)
let sl = of_string "hello" in
clear sl;
(try
let _ = get sl 0 in
false
with Invalid_argument _ -> true)