mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
fix to blit_from_bounded, several more qtests
This commit is contained in:
parent
b57ca9d06a
commit
420f7c6bcc
1 changed files with 57 additions and 8 deletions
|
|
@ -202,26 +202,75 @@ struct
|
||||||
let copy b =
|
let copy b =
|
||||||
{ b with buf=Array.copy b.buf; }
|
{ b with buf=Array.copy b.buf; }
|
||||||
|
|
||||||
(*$T
|
(*$Q
|
||||||
let s = Bytes.of_string "hello world" in \
|
Q.printable_string (fun s -> \
|
||||||
let s_len = Bytes.length s in \
|
let s_len = Bytes.length s in \
|
||||||
let b = ByteBuffer.create s_len in \
|
let b = ByteBuffer.create s_len in \
|
||||||
ByteBuffer.blit_from b s 0 s_len; \
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
let b' = ByteBuffer.copy b in \
|
let b' = ByteBuffer.copy b in \
|
||||||
try ByteBuffer.iteri b (fun i c -> if ByteBuffer.get_front b' i <> c then raise Exit); true with Exit -> false
|
try ByteBuffer.iteri b (fun i c -> if ByteBuffer.get_front b' i <> c then raise Exit); true with Exit -> false)
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let capacity b =
|
let capacity b =
|
||||||
let len = Array.length b.buf in
|
let len = Array.length b.buf in
|
||||||
match len with 0 -> 0 | l -> l - 1
|
match len with 0 -> 0 | l -> l - 1
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
Q.printable_string (fun s -> \
|
||||||
|
let s_len = Bytes.length s in \
|
||||||
|
let b = ByteBuffer.create s_len in \
|
||||||
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
|
ByteBuffer.capacity b >= Bytes.length s)
|
||||||
|
*)
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
(Q.pair Q.small_int Q.printable_string) (fun (i, s) -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let s_len = Bytes.length s in \
|
||||||
|
let b = ByteBuffer.create ~bounded:true i in \
|
||||||
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
|
ByteBuffer.capacity b <= i)
|
||||||
|
*)
|
||||||
|
|
||||||
let max_capacity b = if b.bounded then Some b.size else None
|
let max_capacity b = if b.bounded then Some b.size else None
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
Q.small_int (fun i -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let b = ByteBuffer.create i in \
|
||||||
|
ByteBuffer.max_capacity b = None)
|
||||||
|
*)
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
Q.small_int (fun i -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let b = ByteBuffer.create ~bounded:true i in \
|
||||||
|
ByteBuffer.max_capacity b = Some i)
|
||||||
|
*)
|
||||||
|
|
||||||
let length b =
|
let length b =
|
||||||
if b.stop >= b.start
|
if b.stop >= b.start
|
||||||
then b.stop - b.start
|
then b.stop - b.start
|
||||||
else (Array.length b.buf - b.start) + b.stop
|
else (Array.length b.buf - b.start) + b.stop
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
(Q.pair Q.small_int Q.printable_string) (fun (i, s) -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let s_len = Bytes.length s in \
|
||||||
|
let b = ByteBuffer.create i in \
|
||||||
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
|
ByteBuffer.length b = s_len)
|
||||||
|
*)
|
||||||
|
|
||||||
|
(*$Q
|
||||||
|
(Q.pair Q.small_int Q.printable_string) (fun (i, s) -> \
|
||||||
|
let i = abs i in \
|
||||||
|
let s_len = Bytes.length s in \
|
||||||
|
let b = ByteBuffer.create ~bounded:true i in \
|
||||||
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
|
ByteBuffer.length b <= i)
|
||||||
|
*)
|
||||||
|
|
||||||
(* resize [b] so that inner capacity is [cap] *)
|
(* resize [b] so that inner capacity is [cap] *)
|
||||||
let resize b cap elem =
|
let resize b cap elem =
|
||||||
assert (cap >= Array.length b.buf);
|
assert (cap >= Array.length b.buf);
|
||||||
|
|
@ -249,7 +298,7 @@ struct
|
||||||
let desired = Array.length b.buf + len + 24 in
|
let desired = Array.length b.buf + len + 24 in
|
||||||
min (b.size+1) desired in
|
min (b.size+1) desired in
|
||||||
resize b new_size from_buf.(0);
|
resize b new_size from_buf.(0);
|
||||||
let good = capacity b - length b >= len in
|
let good = capacity b = b.size || capacity b - length b >= len in
|
||||||
if not good then begin
|
if not good then begin
|
||||||
print_endline ("capacity " ^ string_of_int (capacity b) ^ " and length " ^
|
print_endline ("capacity " ^ string_of_int (capacity b) ^ " and length " ^
|
||||||
string_of_int (length b) ^ " difference is less than " ^
|
string_of_int (length b) ^ " difference is less than " ^
|
||||||
|
|
@ -484,12 +533,12 @@ struct
|
||||||
for i = 0 to b.stop - 1 do f i b.buf.(i) done;
|
for i = 0 to b.stop - 1 do f i b.buf.(i) done;
|
||||||
)
|
)
|
||||||
|
|
||||||
(*$T
|
(*$Q
|
||||||
let s = Bytes.of_string "hello world" in \
|
Q.printable_string (fun s -> \
|
||||||
let s_len = Bytes.length s in \
|
let s_len = Bytes.length s in \
|
||||||
let b = ByteBuffer.create s_len in \
|
let b = ByteBuffer.create s_len in \
|
||||||
ByteBuffer.blit_from b s 0 s_len; \
|
ByteBuffer.blit_from b s 0 s_len; \
|
||||||
try ByteBuffer.iteri b (fun i c -> if ByteBuffer.get_front b i <> c then raise Exit); true with Exit -> false
|
try ByteBuffer.iteri b (fun i c -> if ByteBuffer.get_front b i <> c then raise Exit); true with Exit -> false)
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let get b i =
|
let get b i =
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue