testnumbat_codec/
test_util.rs

1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5/// Calls `top_encode_or_exit` and panics if an encoding error occurs.
6/// Do not use in smart contracts!
7pub fn top_encode_to_vec_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
8    let mut bytes = Vec::<u8>::new();
9    obj.top_encode_or_exit(&mut bytes, (), encode_panic_exit);
10    bytes
11}
12
13/// Calls `dep_encode_or_exit` and panics if an encoding error occurs.
14/// Do not use in smart contracts!
15pub fn dep_encode_to_vec_or_panic<T: NestedEncode>(obj: &T) -> Vec<u8> {
16    let mut bytes = Vec::<u8>::new();
17    obj.dep_encode_or_exit(&mut bytes, (), encode_panic_exit);
18    bytes
19}
20
21fn encode_panic_exit(_: (), en_err: EncodeError) -> ! {
22    panic!(
23        "encode panicked: {}",
24        core::str::from_utf8(en_err.message_bytes()).unwrap()
25    )
26}
27
28/// Calls both the fast exit and the regular top-encode,
29/// compares that the outputs are equal, then returns the result.
30/// To be used in serialization tests.
31pub fn check_top_encode<T: TopEncode>(obj: &T) -> Vec<u8> {
32    let fast_exit_bytes = top_encode_to_vec_or_panic(obj);
33    let result_bytes = top_encode_to_vec(obj).unwrap();
34    assert_eq!(fast_exit_bytes, result_bytes);
35    fast_exit_bytes
36}
37
38/// Calls both the fast exit and the regular dep-encode,
39/// compares that the outputs are equal, then returns the result.
40/// To be used in serialization tests.
41pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
42    let fast_exit_bytes = dep_encode_to_vec_or_panic(obj);
43    let result_bytes = dep_encode_to_vec(obj).unwrap();
44    assert_eq!(fast_exit_bytes, result_bytes);
45    fast_exit_bytes
46}
47
48/// Calls `top_decode_or_exit` and panics if an encoding error occurs.
49/// Do not use in smart contracts!
50pub fn top_decode_from_byte_slice_or_panic<T: TopDecode>(input: &[u8]) -> T {
51    T::top_decode_or_exit(input, (), decode_panic_exit)
52}
53
54/// Calls `dep_decode_or_exit` and panics if an encoding error occurs.
55/// Do not use in smart contracts!
56pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
57    dep_decode_from_byte_slice_or_exit(input, (), decode_panic_exit)
58}
59
60fn decode_panic_exit(_: (), de_err: DecodeError) -> ! {
61    panic!(
62        "decode panicked: {}",
63        core::str::from_utf8(de_err.message_bytes()).unwrap()
64    )
65}
66
67/// Calls both the fast exit and the regular top-decode,
68/// compares that the outputs are equal, then returns the result.
69/// To be used in serialization tests.
70pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
71    let fast_exit_obj = top_decode_from_byte_slice_or_panic(bytes);
72    let result_obj = T::top_decode(bytes).unwrap();
73    assert_eq!(fast_exit_obj, result_obj);
74    fast_exit_obj
75}
76
77/// Calls both the fast exit and the regular dep-decode,
78/// compares that the outputs are equal, then returns the result.
79/// To be used in serialization tests.
80pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
81    let fast_exit_obj = dep_decode_from_byte_slice_or_panic(bytes);
82    let result_obj = dep_decode_from_byte_slice::<T>(bytes).unwrap();
83    assert_eq!(fast_exit_obj, result_obj);
84    fast_exit_obj
85}
86
87/// backwards compatibility only, will remove in next major release
88#[deprecated]
89pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
90where
91    V: TopEncode + TopDecode + PartialEq + Debug,
92{
93    check_top_encode_decode(element, expected_bytes);
94}
95
96pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
97where
98    V: TopEncode + TopDecode + PartialEq + Debug,
99{
100    // serialize
101    let serialized_bytes = check_top_encode(&element);
102    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
103
104    // deserialize
105    let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
106    assert_eq!(deserialized, element);
107}
108
109pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
110where
111    V: NestedEncode + NestedDecode + PartialEq + Debug,
112{
113    // serialize
114    let serialized_bytes = check_dep_encode(&element);
115    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
116
117    // deserialize
118    let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
119    assert_eq!(deserialized, element);
120}