From 6e968ffe9dc2b9a2b38acbe10c6d897dfdacc5ca Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 28 Nov 2023 20:39:31 -0500 Subject: [PATCH] add an example extracted from a bench --- src/examples/dune | 12 ++++++ src/examples/orgchart.proto | 19 +++++++++ src/examples/orgchart_ml.ml | 81 +++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 src/examples/orgchart.proto create mode 100644 src/examples/orgchart_ml.ml diff --git a/src/examples/dune b/src/examples/dune index f4919db5..8b77368e 100644 --- a/src/examples/dune +++ b/src/examples/dune @@ -65,3 +65,15 @@ (modules file_server) ; just check that it compiles (package pbrt_services) (libraries pbrt pbrt_yojson pbrt_services)) + +(rule + (targets orgchart.ml orgchart.mli) + (deps orgchart.proto) + (action + (run ocaml-protoc --pp --binary --ml_out ./ %{deps}))) + +(test + (name orgchart_ml) + (modules orgchart orgchart_ml) + (package pbrt) + (libraries pbrt)) diff --git a/src/examples/orgchart.proto b/src/examples/orgchart.proto new file mode 100644 index 00000000..13a07d46 --- /dev/null +++ b/src/examples/orgchart.proto @@ -0,0 +1,19 @@ + +syntax = "proto3"; + +message Person { + string name = 1; + sint64 age = 2; +} + +message Store { + string address = 1; + repeated Person employees = 2; + repeated Person clients = 3; +} + +message Company { + string name = 1; + repeated Store stores = 2; + repeated Company subsidiaries = 3; +} diff --git a/src/examples/orgchart_ml.ml b/src/examples/orgchart_ml.ml new file mode 100644 index 00000000..33d9cbd5 --- /dev/null +++ b/src/examples/orgchart_ml.ml @@ -0,0 +1,81 @@ +open Orgchart + +let spf = Printf.sprintf + +(* company, with [n] stores and [2^depth] subsidiaries *) +let rec mk_company ~n ~depth : company = + { + name = "bigcorp"; + subsidiaries = + (if depth = 0 then + [] + else ( + let c = mk_company ~n ~depth:(depth - 1) in + [ c; c ] + )); + stores = + List.init n (fun i -> + { + address = spf "%d foobar street" i; + clients = + List.init 2 (fun j -> + { + name = spf "client_%d_%d" i j; + age = Int64.of_int ((j mod 30) + 15); + }); + employees = + List.init 2 (fun j -> + { + name = spf "employee_%d_%d" i j; + age = Int64.of_int ((j mod 30) + 18); + }); + }); + } + +let test ~n ~depth () : unit = + let c = mk_company ~n ~depth in + let enc = Pbrt.Encoder.create () in + encode_pb_company c enc; + let str = Pbrt.Encoder.to_string enc in + let c2 = + let dec = Pbrt.Decoder.of_string str in + decode_pb_company dec + in + + if c <> c2 then ( + Format.eprintf "c=%a@." pp_company c; + Format.eprintf "dec(enc(c))=%a@." pp_company c2; + failwith @@ spf "failed for n=%d, depth=%d" n depth + ) +(* else Printf.eprintf "ok for n=%d, depth=%d\n%!" n depth *) + +let () = + List.iter + (fun (n, depth) -> test ~n ~depth ()) + [ + 1, 1; + 1, 1; + 1, 4; + 1, 6; + 1, 10; + 2, 1; + 2, 4; + 2, 6; + 2, 10; + 5, 1; + 5, 4; + 5, 6; + 10, 1; + 10, 2; + 10, 3; + 10, 4; + 20, 1; + 50, 1; + 20, 3; + 20, 4; + 50, 1; + 50, 3; + 50, 4; + 100, 1; + 100, 3; + ]