mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-03-13 00:36:17 -04:00
Compare commits
8 commits
330e4a0c9f
...
f4d0d55300
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4d0d55300 | ||
|
|
3b49ad2a4e | ||
|
|
1a11459991 | ||
|
|
0290aa9754 | ||
|
|
9df429005d | ||
|
|
fd1495324a | ||
|
|
765a8da876 | ||
|
|
18ffdd707b |
6 changed files with 40 additions and 3 deletions
|
|
@ -1153,12 +1153,14 @@ module Iter_ = struct
|
||||||
let bench_to_array n =
|
let bench_to_array n =
|
||||||
let iter () = Iter.to_array Iter.(1 -- n)
|
let iter () = Iter.to_array Iter.(1 -- n)
|
||||||
and gen () = Gen.to_array Gen.(1 -- n)
|
and gen () = Gen.to_array Gen.(1 -- n)
|
||||||
and oseq () = OSeq.to_array OSeq.(1 -- n) in
|
and oseq () = OSeq.to_array OSeq.(1 -- n)
|
||||||
|
and of_iter () = CCArray.of_iter Iter.(1 -- n) in
|
||||||
B.throughputN 3 ~repeat
|
B.throughputN 3 ~repeat
|
||||||
[
|
[
|
||||||
"iter.to_array", iter, ();
|
"iter.to_array", iter, ();
|
||||||
"gen.to_array", gen, ();
|
"gen.to_array", gen, ();
|
||||||
"oseq.to_array", oseq, ();
|
"oseq.to_array", oseq, ();
|
||||||
|
"ccarray.of_iter", of_iter, ();
|
||||||
]
|
]
|
||||||
|
|
||||||
let bench_cons n =
|
let bench_cons n =
|
||||||
|
|
|
||||||
|
|
@ -466,6 +466,12 @@ let to_seq a =
|
||||||
|
|
||||||
let to_iter a k = iter k a
|
let to_iter a k = iter k a
|
||||||
|
|
||||||
|
let of_iter (i : 'a iter) : 'a array =
|
||||||
|
let open CCVector in
|
||||||
|
let vec = create () in
|
||||||
|
i (push vec);
|
||||||
|
to_array vec
|
||||||
|
|
||||||
let to_gen a =
|
let to_gen a =
|
||||||
let k = ref 0 in
|
let k = ref 0 in
|
||||||
fun () ->
|
fun () ->
|
||||||
|
|
|
||||||
|
|
@ -240,6 +240,11 @@ val to_iter : 'a t -> 'a iter
|
||||||
in modification of the iterator.
|
in modification of the iterator.
|
||||||
@since 2.8 *)
|
@since 2.8 *)
|
||||||
|
|
||||||
|
val of_iter : 'a iter -> 'a t
|
||||||
|
(** [of_iter iter] builds a array from a given [iter].
|
||||||
|
In the result, elements appear in the same order as they did in the source [iter].
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val to_seq : 'a t -> 'a Seq.t
|
val to_seq : 'a t -> 'a Seq.t
|
||||||
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
|
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
|
||||||
The input array [a] is shared with the sequence and modification of it will result
|
The input array [a] is shared with the sequence and modification of it will result
|
||||||
|
|
|
||||||
|
|
@ -248,6 +248,11 @@ val to_iter : 'a t -> 'a iter
|
||||||
in modification of the iterator.
|
in modification of the iterator.
|
||||||
@since 2.8 *)
|
@since 2.8 *)
|
||||||
|
|
||||||
|
val of_iter : 'a iter -> 'a t
|
||||||
|
(** [of_iter iter] builds a array from a given [iter].
|
||||||
|
In the result, elements appear in the same order as they did in the source [iter].
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val to_seq : 'a t -> 'a Seq.t
|
val to_seq : 'a t -> 'a Seq.t
|
||||||
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
|
(** [to_seq a] returns a [Seq.t] of the elements of an array [a].
|
||||||
The input array [a] is shared with the sequence and modification of it will result
|
The input array [a] is shared with the sequence and modification of it will result
|
||||||
|
|
|
||||||
|
|
@ -309,3 +309,6 @@ q ~count:300 arr_arbitrary (fun a ->
|
||||||
Array.sort CCInt.compare a1;
|
Array.sort CCInt.compare a1;
|
||||||
sort_generic (module IA) ~cmp:CCInt.compare a2;
|
sort_generic (module IA) ~cmp:CCInt.compare a2;
|
||||||
a1 = a2)
|
a1 = a2)
|
||||||
|
;;
|
||||||
|
|
||||||
|
q Q.(array int) (fun a -> of_iter (to_iter a) = a)
|
||||||
|
|
|
||||||
|
|
@ -99,12 +99,28 @@ let rec shrink (c : Cbor.t) : Cbor.t Q.Iter.t =
|
||||||
let+ s = Q.Shrink.string s in
|
let+ s = Q.Shrink.string s in
|
||||||
`Bytes s
|
`Bytes s
|
||||||
|
|
||||||
let arb = Q.make ~shrink ~print:Cbor.to_string_diagnostic gen_c;;
|
let arb = Q.make ~shrink ~print:Cbor.to_string_diagnostic gen_c
|
||||||
|
|
||||||
|
let rec eq_c c c' =
|
||||||
|
match c, c' with
|
||||||
|
| `Null, `Null | `Undefined, `Undefined -> true
|
||||||
|
| `Simple i, `Simple i' -> Int.equal i i'
|
||||||
|
| `Bool b, `Bool b' -> Bool.equal b b'
|
||||||
|
| `Int i, `Int i' -> Int64.equal i i'
|
||||||
|
| `Float f, `Float f' -> Float.equal f f'
|
||||||
|
| `Bytes s, `Bytes s' -> String.equal s s'
|
||||||
|
| `Text t, `Text t' -> String.equal t t'
|
||||||
|
| `Array a, `Array a' -> CCList.equal eq_c a a'
|
||||||
|
| `Map m, `Map m' ->
|
||||||
|
CCList.equal (fun (t0, t1) (t0', t1') -> eq_c t0 t0' && eq_c t1 t1') m m'
|
||||||
|
| `Tag (i, t), `Tag (i', t') -> Int.equal i i' && eq_c t t'
|
||||||
|
| _ -> false
|
||||||
|
;;
|
||||||
|
|
||||||
q ~count:1_000 ~long_factor:10 arb @@ fun c ->
|
q ~count:1_000 ~long_factor:10 arb @@ fun c ->
|
||||||
let s = Cbor.encode c in
|
let s = Cbor.encode c in
|
||||||
let c' = Cbor.decode_exn s in
|
let c' = Cbor.decode_exn s in
|
||||||
if not (c = c') then
|
if not (eq_c c c') then
|
||||||
Q.Test.fail_reportf "@[<hv2>roundtrip failed:@ from %a@ to %a@]"
|
Q.Test.fail_reportf "@[<hv2>roundtrip failed:@ from %a@ to %a@]"
|
||||||
Cbor.pp_diagnostic c Cbor.pp_diagnostic c';
|
Cbor.pp_diagnostic c Cbor.pp_diagnostic c';
|
||||||
true
|
true
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue