swh:1:snp:61dcfc0dd5674a0e65803e88039c122d9532074e
Tip revision: 53ffb245809ddb335916e9a6df2637f58e1451a7 authored by Raphaƫl Proust on 20 September 2022, 15:22:34 UTC
Specialise `case` tag in With_JSON_discriminant to enforce invariant
Specialise `case` tag in With_JSON_discriminant to enforce invariant
Tip revision: 53ffb24
fixed_array.ml
(*****************************************************************************)
(* *)
(* Open Source License *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com> *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(*****************************************************************************)
let range n = Array.init n Fun.id
let test_roundtrip len () =
let open Data_encoding in
let encoding = Fixed.array len int31 in
let data = range len in
assert (Array.length data = len) ;
let blob = Data_encoding.Binary.to_string_exn encoding data in
let data_out = Data_encoding.Binary.of_string_exn encoding blob in
assert (Array.length data = Array.length data_out) ;
Array.iter2 (fun a b -> assert (Int.equal a b)) data data_out
let test_invalid_argument () =
(match Data_encoding.Fixed.array 0 Data_encoding.int31 with
| _ -> assert false
| exception Invalid_argument _ -> ()) ;
(match Data_encoding.Fixed.array (-1) Data_encoding.int31 with
| _ -> assert false
| exception Invalid_argument _ -> ()) ;
(match Data_encoding.(Fixed.array 1 (Variable.list int31)) with
| _ -> assert false
| exception Invalid_argument _ -> ()) ;
(match Data_encoding.Fixed.array 1 Data_encoding.unit with
| _ -> assert false
| exception Invalid_argument _ -> ()) ;
(match
Data_encoding.(Fixed.array 1 (obj1 (opt "zeroable" (Variable.list int31))))
with
| _ -> assert false
| exception Invalid_argument _ -> ()) ;
()
let test_array_too_long len () =
let open Data_encoding in
let encoding = Fixed.array len int31 in
let encodinglong = Fixed.array (len + 1) int31 in
let encodingshort = Fixed.array (len - 1) int31 in
let data = range len in
assert (Array.length data = len) ;
(match Data_encoding.Binary.to_string_exn encodinglong data with
| _ -> assert false
| exception Data_encoding.Binary.Write_error Array_invalid_length -> ()) ;
(match Data_encoding.Binary.to_string_exn encodingshort data with
| _ -> assert false
| exception Data_encoding.Binary.Write_error Array_invalid_length -> ()) ;
let blob = Data_encoding.Binary.to_string_exn encoding data in
(match Data_encoding.Binary.of_string_exn encodinglong blob with
| _ -> assert false
| exception Data_encoding.Binary.Read_error Not_enough_data -> ()) ;
(match Data_encoding.Binary.of_string_exn encodingshort blob with
| _ -> assert false
| exception Data_encoding.Binary.Read_error Extra_bytes -> ()) ;
()
let tests =
[
("roundtrip1", `Quick, test_roundtrip 1);
("roundtrip2", `Quick, test_roundtrip 2);
("roundtrip19", `Quick, test_roundtrip 19);
("roundtrip1232", `Quick, test_roundtrip 1232);
("invalid_arg", `Quick, test_invalid_argument);
("too-long 10", `Quick, test_array_too_long 10);
("too-long 120", `Quick, test_array_too_long 120);
]