multiversx_sc_codec/
test_util.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::*;
use alloc::vec::Vec;
use core::fmt::Debug;
use unwrap_infallible::UnwrapInfallible;

/// Calls top encode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn top_encode_to_vec_u8_or_panic<T: TopEncode>(obj: &T) -> Vec<u8> {
    let mut bytes = Vec::<u8>::new();
    obj.top_encode_or_handle_err(&mut bytes, PanicErrorHandler)
        .unwrap_infallible();
    bytes
}

/// Calls nested encode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn dep_encode_to_vec_or_panic<T: NestedEncode>(obj: &T) -> Vec<u8> {
    let mut bytes = Vec::<u8>::new();
    obj.dep_encode_or_handle_err(&mut bytes, PanicErrorHandler)
        .unwrap_infallible();
    bytes
}

/// Calls both the fast exit and the regular top-encode,
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_top_encode<T: TopEncode>(obj: &T) -> Vec<u8> {
    let fast_exit_bytes = top_encode_to_vec_u8_or_panic(obj);
    let result_bytes = top_encode_to_vec_u8(obj).unwrap();
    assert_eq!(fast_exit_bytes, result_bytes);
    fast_exit_bytes
}

/// Calls both the fast exit and the regular dep-encode,
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_dep_encode<T: NestedEncode>(obj: &T) -> Vec<u8> {
    let fast_exit_bytes = dep_encode_to_vec_or_panic(obj);
    let result_bytes = dep_encode_to_vec(obj).unwrap();
    assert_eq!(fast_exit_bytes, result_bytes);
    fast_exit_bytes
}

/// Calls nested decode and panics if an encoding error occurs.
/// Do not use in smart contracts!
pub fn dep_decode_from_byte_slice_or_panic<T: NestedDecode>(input: &[u8]) -> T {
    dep_decode_from_byte_slice(input, PanicErrorHandler).unwrap_infallible()
}

/// Calls both the fast exit and the regular top-decode,
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_top_decode<T: TopDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
    let fast_exit_obj = T::top_decode_or_handle_err(bytes, PanicErrorHandler).unwrap_infallible();
    let result_obj = T::top_decode_or_handle_err(bytes, DefaultErrorHandler).unwrap();
    assert_eq!(fast_exit_obj, result_obj);
    fast_exit_obj
}

/// Calls both the fast exit and the regular dep-decode,
/// compares that the outputs are equal, then returns the result.
/// To be used in serialization tests.
pub fn check_dep_decode<T: NestedDecode + PartialEq + Debug>(bytes: &[u8]) -> T {
    let fast_exit_obj = dep_decode_from_byte_slice(bytes, PanicErrorHandler).unwrap_infallible();
    let result_obj = dep_decode_from_byte_slice(bytes, DefaultErrorHandler).unwrap();
    assert_eq!(fast_exit_obj, result_obj);
    fast_exit_obj
}

/// backwards compatibility only, will remove in next major release
#[deprecated]
pub fn ser_deser_ok<V>(element: V, expected_bytes: &[u8])
where
    V: TopEncode + TopDecode + PartialEq + Debug,
{
    check_top_encode_decode(element, expected_bytes);
}

pub fn check_top_encode_decode<V>(element: V, expected_bytes: &[u8])
where
    V: TopEncode + TopDecode + PartialEq + Debug,
{
    // serialize
    let serialized_bytes = check_top_encode(&element);
    assert_eq!(serialized_bytes.as_slice(), expected_bytes);

    // deserialize
    let deserialized: V = check_top_decode::<V>(&serialized_bytes[..]);
    assert_eq!(deserialized, element);
}

pub fn check_dep_encode_decode<V>(element: V, expected_bytes: &[u8])
where
    V: NestedEncode + NestedDecode + PartialEq + Debug,
{
    // serialize
    let serialized_bytes = check_dep_encode(&element);
    assert_eq!(serialized_bytes.as_slice(), expected_bytes);

    // deserialize
    let deserialized: V = check_dep_decode::<V>(&serialized_bytes[..]);
    assert_eq!(deserialized, element);
}