From e42caf3b6a4c566200110e50da36a088fa1c8065 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 9 Apr 2026 00:58:25 +0000 Subject: [PATCH] more tests --- tests/core/t.ml | 1 + tests/core/t_byte_buffer.ml | 68 +++++++++++++++++++++++++++++++++++++ tests/core/t_byte_slice.ml | 37 ++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 tests/core/t_byte_slice.ml diff --git a/tests/core/t.ml b/tests/core/t.ml index 8508555a..ad4aa0df 100644 --- a/tests/core/t.ml +++ b/tests/core/t.ml @@ -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 (); diff --git a/tests/core/t_byte_buffer.ml b/tests/core/t_byte_buffer.ml index 7d9a0d3a..b55f6fc0 100644 --- a/tests/core/t_byte_buffer.ml +++ b/tests/core/t_byte_buffer.ml @@ -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" diff --git a/tests/core/t_byte_slice.ml b/tests/core/t_byte_slice.ml new file mode 100644 index 00000000..01ce67e7 --- /dev/null +++ b/tests/core/t_byte_slice.ml @@ -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)