| 1 |
open! Core |
| 2 |
open! Import |
| 3 |
module Blob = Bin_prot.Blob |
| 4 |
module Opaque = Blob.Opaque |
| 5 |
module Type_class = Bin_prot.Type_class |
| 6 |
|
| 7 |
let buf = lazy (Bin_prot.Common.create_buf 10_000) |
| 8 |
|
| 9 |
let bin_dump_to_string bin_a a = |
| 10 |
let buf = Bin_prot.Utils.bin_dump bin_a.Type_class.writer a in |
| 11 |
let len = Bin_prot.Common.buf_len buf in |
| 12 |
let str = Bytes.create len in |
| 13 |
Bin_prot.Common.blit_buf_string buf str ~len; |
| 14 |
Bytes.to_string str |
| 15 |
;; |
| 16 |
|
| 17 |
let bin_read_from_string bin_reader str = |
| 18 |
let buf = Lazy.force buf in |
| 19 |
let len = String.length str in |
| 20 |
Bin_prot.Common.blit_string_buf str buf ~len; |
| 21 |
let pos_ref = ref 0 in |
| 22 |
let a = bin_reader.Type_class.read buf ~pos_ref in |
| 23 |
assert (Int.( = ) !pos_ref len); |
| 24 |
a |
| 25 |
;; |
| 26 |
|
| 27 |
let print_bin_dump tag bin_a a = |
| 28 |
printf "%s : %s\n" tag (bin_dump_to_string bin_a a |> String.escaped) |
| 29 |
;; |
| 30 |
|
| 31 |
let run_stability_test (bin_a : 'a Type_class.t) equal a : unit = |
| 32 |
print_bin_dump "bin dump" bin_a a; |
| 33 |
print_bin_dump "bin dump (blob)" (Blob.bin_t bin_a) a; |
| 34 |
let buf = Lazy.force buf in |
| 35 |
let dump_and_read bin_t equal t = |
| 36 |
let str = bin_dump_to_string bin_t t in |
| 37 |
assert (equal t (bin_read_from_string bin_t.reader str)); |
| 38 |
let (_ : Blob.Ignored.t) = bin_read_from_string Blob.Ignored.bin_reader_t str in |
| 39 |
str |
| 40 |
in |
| 41 |
let bin_dump_blob = dump_and_read (Blob.bin_t bin_a) equal a in |
| 42 |
let bin_dump_opaque_big_string = |
| 43 |
dump_and_read |
| 44 |
Opaque.Bigstring.bin_t |
| 45 |
Poly.equal |
| 46 |
(Opaque.Bigstring.to_opaque a bin_a.writer) |
| 47 |
in |
| 48 |
let bin_dump_opaque_string = |
| 49 |
dump_and_read |
| 50 |
Opaque.String.bin_t |
| 51 |
Poly.equal |
| 52 |
(Opaque.String.to_opaque ~buf a bin_a.writer) |
| 53 |
in |
| 54 |
printf |
| 55 |
!"bin dump as opaque big string and string are the same? %{Bool}\n" |
| 56 |
(String.( = ) bin_dump_blob bin_dump_opaque_big_string |
| 57 |
&& String.( = ) bin_dump_blob bin_dump_opaque_string) |
| 58 |
;; |
| 59 |
|
| 60 |
let%test_module _ = |
| 61 |
(module struct |
| 62 |
open Blob |
| 63 |
open Poly |
| 64 |
|
| 65 |
module Mystery = struct |
| 66 |
type t = |
| 67 |
{ name : string |
| 68 |
; age : int |
| 69 |
; favorite_colors : string list |
| 70 |
} |
| 71 |
[@@deriving bin_io] |
| 72 |
|
| 73 |
let value = { name = "Drew"; age = 25; favorite_colors = [ "Blue"; "Yellow" ] } |
| 74 |
end |
| 75 |
|
| 76 |
module T = struct |
| 77 |
type 'a t = |
| 78 |
{ header : string |
| 79 |
; mystery : 'a |
| 80 |
; footer : string |
| 81 |
} |
| 82 |
[@@deriving bin_io] |
| 83 |
|
| 84 |
let value mystery = { header = "header"; mystery; footer = "footer" } |
| 85 |
end |
| 86 |
|
| 87 |
|
| 88 |
module Known = struct |
| 89 |
type nonrec t = Mystery.t t T.t [@@deriving bin_io] |
| 90 |
|
| 91 |
let value = T.value Mystery.value |
| 92 |
end |
| 93 |
|
| 94 |
module Unknown = struct |
| 95 |
type t = Opaque.Bigstring.t T.t [@@deriving bin_io] |
| 96 |
|
| 97 |
let value = T.value (Opaque.Bigstring.to_opaque Mystery.value Mystery.bin_writer_t) |
| 98 |
end |
| 99 |
|
| 100 |
let convert bin_writer bin_reader value = |
| 101 |
let buffer = Bin_prot.Utils.bin_dump bin_writer value in |
| 102 |
bin_reader.Bin_prot.Type_class.read buffer ~pos_ref:(ref 0) |
| 103 |
;; |
| 104 |
|
| 105 |
let roundtrip { Bin_prot.Type_class.reader; writer; shape = _ } value = |
| 106 |
assert (convert writer reader value = value) |
| 107 |
;; |
| 108 |
|
| 109 |
module Dropped = struct |
| 110 |
type t = Ignored.t T.t [@@deriving bin_read] |
| 111 |
|
| 112 |
let bin_size_t = T.bin_size_t Ignored.bin_size_t |
| 113 |
end |
| 114 |
|
| 115 |
let bigstring_to_string bigstring = |
| 116 |
let len = Bigarray.Array1.dim bigstring in |
| 117 |
String.init len ~f:(fun i -> bigstring.{i}) |
| 118 |
;; |
| 119 |
|
| 120 |
let%test_unit "roundtrip known" = roundtrip Known.bin_t Known.value |
| 121 |
let%test_unit "roundtrip unknown" = roundtrip Unknown.bin_t Unknown.value |
| 122 |
|
| 123 |
let%test_unit "opaque and wrapped serialize the same way" = |
| 124 |
let known_buffer = Bin_prot.Utils.bin_dump Known.bin_writer_t Known.value in |
| 125 |
let unknown_buffer = Bin_prot.Utils.bin_dump Unknown.bin_writer_t Unknown.value in |
| 126 |
let known_s = bigstring_to_string known_buffer in |
| 127 |
let unknown_s = bigstring_to_string unknown_buffer in |
| 128 |
if known_s <> unknown_s then failwith (Printf.sprintf "%s <> %s" known_s unknown_s) |
| 129 |
;; |
| 130 |
|
| 131 |
let%test_unit "serialized wrapped deserializes to the expected opaque" = |
| 132 |
let unknown_from_known = |
| 133 |
convert Known.bin_writer_t Unknown.bin_reader_t Known.value |
| 134 |
in |
| 135 |
assert (Unknown.value = unknown_from_known) |
| 136 |
;; |
| 137 |
|
| 138 |
let%test_unit "serialized opaque deserializes to the expected wrapped" = |
| 139 |
let known_from_unknown = |
| 140 |
convert Unknown.bin_writer_t Known.bin_reader_t Unknown.value |
| 141 |
in |
| 142 |
assert (Known.value = known_from_unknown) |
| 143 |
;; |
| 144 |
|
| 145 |
let%test_unit "Dropped" = |
| 146 |
let buffer = Bin_prot.Utils.bin_dump Known.bin_writer_t Known.value in |
| 147 |
let value = Dropped.bin_reader_t.Bin_prot.Type_class.read buffer ~pos_ref:(ref 0) in |
| 148 |
let ignored = value.mystery in |
| 149 |
|
| 150 |
|
| 151 |
assert ({ Known.value with mystery = ignored } = value); |
| 152 |
|
| 153 |
assert (Dropped.bin_size_t value = Known.bin_size_t Known.value) |
| 154 |
;; |
| 155 |
end) |
| 156 |
;; |