numbat_codec/
test_util.rs1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
8 let mut bytes = Vec::<u8>::new();
9 let Ok(()) = obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler);
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 let Ok(()) = obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler);
18 bytes
19}
20
21pub fn check_top_encode<T: TopEncode>(obj: &T) -> Vec<u8> {
25 let fast_exit_bytes = top_encode_to_vec_u8_or_panic(obj);
26 let result_bytes = top_encode_to_vec_u8(obj).unwrap();
27 assert_eq!(fast_exit_bytes, result_bytes);
28 fast_exit_bytes
29}
30
31pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
35 let fast_exit_bytes = dep_encode_to_vec_or_panic(obj);
36 let result_bytes = dep_encode_to_vec(obj).unwrap();
37 assert_eq!(fast_exit_bytes, result_bytes);
38 fast_exit_bytes
39}
40
41pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
44 let Ok(result) = dep_decode_from_byte_slice(input, PanicErrorHandler);
45 result
46}
47
48pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
52 let Ok(fast_exit_obj) = T::top_decode_or_handle_err(bytes, PanicErrorHandler);
53 let result_obj = T::top_decode_or_handle_err(bytes, DefaultErrorHandler).unwrap();
54 assert_eq!(fast_exit_obj, result_obj);
55 fast_exit_obj
56}
57
58pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
62 let Ok(fast_exit_obj) = dep_decode_from_byte_slice(bytes, PanicErrorHandler);
63 let result_obj = dep_decode_from_byte_slice(bytes, DefaultErrorHandler).unwrap();
64 assert_eq!(fast_exit_obj, result_obj);
65 fast_exit_obj
66}
67
68#[deprecated]
70pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
71where
72 V: TopEncode + TopDecode + PartialEq + Debug,
73{
74 check_top_encode_decode(element, expected_bytes);
75}
76
77pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
78where
79 V: TopEncode + TopDecode + PartialEq + Debug,
80{
81 let serialized_bytes = check_top_encode(&element);
83 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
84
85 let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
87 assert_eq!(deserialized, element);
88}
89
90pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
91where
92 V: NestedEncode + NestedDecode + PartialEq + Debug,
93{
94 let serialized_bytes = check_dep_encode(&element);
96 assert_eq!(serialized_bytes.as_slice(), expected_bytes);
97
98 let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
100 assert_eq!(deserialized, element);
101}