numbat_codec/
test_util.rs1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5pub 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
13pub 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
28pub 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
38pub 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
48pub 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
54pub 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
67pub 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
77pub 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
87pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
89where
90 V: TopEncode + TopDecode + PartialEq + Debug,
91{
92 check_top_encode_decode(element, expected_bytes);
93}
94
95pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
96where
97 V: TopEncode + TopDecode + PartialEq + Debug,
98{
99 let serialized_bytes = check_top_encode(&element);
101 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
102
103 let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
105 assert_eq!(deserialized, element);
106}
107
108pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
109where
110 V: NestedEncode + NestedDecode + PartialEq + Debug,
111{
112 let serialized_bytes = check_dep_encode(&element);
114 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
115
116 let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
118 assert_eq!(deserialized, element);
119}