mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
test: add qcheck test for cbor
This commit is contained in:
parent
b1c7c64b87
commit
89702924d8
2 changed files with 80 additions and 2 deletions
|
|
@ -1,10 +1,23 @@
|
|||
(executable
|
||||
(name t_appendix_a)
|
||||
(modules t_appendix_a)
|
||||
(preprocess (action (run %{project_root}/src/core/cpp/cpp.exe %{input-file})))
|
||||
(preprocess
|
||||
(action
|
||||
(run %{project_root}/src/core/cpp/cpp.exe %{input-file})))
|
||||
(libraries yojson containers containers.cbor))
|
||||
|
||||
(rule
|
||||
(alias runtest)
|
||||
(deps t_appendix_a.exe appendix_a.json)
|
||||
(action
|
||||
(run ./t_appendix_a.exe ./appendix_a.json)))
|
||||
(run ./t_appendix_a.exe ./appendix_a.json)))
|
||||
|
||||
(executable
|
||||
(name t)
|
||||
(modules t)
|
||||
(libraries qcheck-core qcheck-core.runner containers containers.cbor))
|
||||
|
||||
(rule
|
||||
(alias runtest)
|
||||
(action
|
||||
(run ./t.exe --no-colors)))
|
||||
|
|
|
|||
65
src/cbor/tests/t.ml
Normal file
65
src/cbor/tests/t.ml
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
|
||||
module Q = QCheck
|
||||
module Cbor = Containers_cbor
|
||||
|
||||
let suite = ref []
|
||||
|
||||
[@@@ifge 4.08]
|
||||
|
||||
let gen_c : Cbor.t Q.Gen.t =
|
||||
let open Q.Gen in
|
||||
sized @@ fix @@ fun self size ->
|
||||
let recurse = self (size-1) in
|
||||
let base = [
|
||||
(1, return `Null);
|
||||
(1, return `Undefined);
|
||||
(3, let+ x = int in `Int x);
|
||||
(1, let+ b = bool in `Bool b);
|
||||
(1, let+ f = float in `Float f);
|
||||
(2, let+ s = string_size ~gen:printable (0--150) in `Text s);
|
||||
(2, let+ s = string_size ~gen:char (0--150) in `Bytes s);
|
||||
] in
|
||||
let rec_ = [
|
||||
(2, let+ l = list_size (0--5) recurse in `Array l);
|
||||
(2, let+ l = list_size (0--5) (pair (frequency base) recurse) in `Map l);
|
||||
]in
|
||||
frequency (if size>0 then base @ rec_ else base)
|
||||
|
||||
let rec shrink (c:Cbor.t) : Cbor.t Q.Iter.t =
|
||||
let open Q.Iter in
|
||||
match c with
|
||||
| `Null | `Undefined | `Bool false -> empty
|
||||
| `Bool true -> return (`Bool false)
|
||||
| `Simple i -> let+ i = Q.Shrink.int i in `Simple i
|
||||
| `Int i -> let+ i = Q.Shrink.int i in `Int i
|
||||
| `Tag(t,i) -> let+ i = shrink i in `Tag (t,i)
|
||||
| `Float _ -> empty
|
||||
| `Array l ->
|
||||
let+ l = Q.Shrink.list ~shrink l in `Array l
|
||||
| `Map l ->
|
||||
let shrink_pair (a,b) =
|
||||
(let+a = shrink a in a,b) <+> (let+b = shrink b in a,b)
|
||||
in
|
||||
let+ l = Q.Shrink.list ~shrink:shrink_pair l in `Map l
|
||||
| `Text s ->
|
||||
let+ s = Q.Shrink.string s in `Text s
|
||||
| `Bytes s ->
|
||||
let+ s = Q.Shrink.string s in `Bytes s
|
||||
|
||||
|
||||
let arb = Q.make ~shrink ~print:Cbor.to_string_diagnostic gen_c
|
||||
|
||||
let t1 =
|
||||
Q.Test.make ~count:100_000 ~name:"to_from_same" arb @@ fun c ->
|
||||
let s = Cbor.encode c in
|
||||
let c' = Cbor.decode_exn s in
|
||||
if not (c = c') then
|
||||
Q.Test.fail_reportf "@[<hv2>roundtrip failed:@ from %a@ to %a@]"
|
||||
Cbor.pp_diagnostic c Cbor.pp_diagnostic c';
|
||||
true
|
||||
|
||||
let () = suite := t1 :: !suite
|
||||
|
||||
[@@@endif]
|
||||
|
||||
let () = QCheck_base_runner.run_tests_main !suite
|
||||
Loading…
Add table
Reference in a new issue