Skip to main content

numbat_codec/
test_util.rs

1use crate::*;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5/// Calls top encode and panics if an encoding error occurs.
6/// Do not use in smart contracts!
7pub 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
13/// Calls nested encode 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    let Ok(()) = obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler);
18    bytes
19}
20
21/// Calls both the fast exit and the regular top-encode,
22/// compares that the outputs are equal, then returns the result.
23/// To be used in serialization tests.
24pub 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
31/// Calls both the fast exit and the regular dep-encode,
32/// compares that the outputs are equal, then returns the result.
33/// To be used in serialization tests.
34pub 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
41/// Calls nested decode and panics if an encoding error occurs.
42/// Do not use in smart contracts!
43pub 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
48/// Calls both the fast exit and the regular top-decode,
49/// compares that the outputs are equal, then returns the result.
50/// To be used in serialization tests.
51pub 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
58/// Calls both the fast exit and the regular dep-decode,
59/// compares that the outputs are equal, then returns the result.
60/// To be used in serialization tests.
61pub 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/// backwards compatibility only, will remove in next major release
69#[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    // serialize
82    let serialized_bytes = check_top_encode(&element);
83    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
84
85    // deserialize
86    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    // serialize
95    let serialized_bytes = check_dep_encode(&element);
96    assert_eq!(serialized_bytes.as_slice(), expected_bytes);
97
98    // deserialize
99    let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
100    assert_eq!(deserialized, element);
101}