-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric_tuple.rs
133 lines (110 loc) · 3.82 KB
/
numeric_tuple.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
mod unittest;
use std::mem::size_of;
use byteserde::prelude::*;
use byteserde_derive::{ByteDeserializeSlice, ByteSerializeHeap, ByteSerializeStack, ByteSerializedLenOf, ByteSerializedSizeOf};
use log::info;
use unittest::setup;
#[rustfmt::skip]
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice,
ByteSerializedSizeOf, ByteSerializedLenOf, Default, Debug, PartialEq)]
struct Bytes(#[byteserde(replace(i8::MIN))] i8, u8);
#[test]
fn test_bytes() {
bytes()
}
fn bytes() {
setup::log::configure();
let inp_bytes = Bytes(-1, 1);
// stack
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_bytes).unwrap();
info!("ser_stack: {ser_stack:#x}");
assert_eq!(i8::MIN as u8, ser_stack.as_slice()[0]);
assert_eq!(0x01, ser_stack.as_slice()[1]);
// heap
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_bytes).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
// deserialize
let out_bytes: Bytes = from_serializer_stack(&ser_stack).unwrap();
info!("inp_bytes: {inp_bytes:?}");
info!("out_bytes: {out_bytes:?}");
assert_eq!(out_bytes, Bytes(i8::MIN, inp_bytes.1,));
}
#[rustfmt::skip]
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice, ByteSerializedSizeOf, ByteSerializedLenOf, Default, Debug, PartialEq)]
#[byteserde(endian = "le")]
struct Numerics(
#[byteserde(endian = "ne")] // ne test local attribute
u16,
#[byteserde(endian = "le")] u16, // le test local attribute
#[byteserde(endian = "be")] u16, // be test local attribute
u16, // le test global attribute
i16,
u16,
i32,
u32,
i64,
u64,
u8, // this shall cause alignment padding used in struct size_of test
i128,
u128,
f32,
f64,
);
#[test]
fn test_numerics() {
numerics()
}
fn numerics() {
setup::log::configure();
let inp_num = Numerics(0x00FF_u16, 0x00FF_u16, 0x00FF_u16, 0x00FF_u16, -16, 16, -32, 32, -64, 64, 8_u8, -128, 128, -1.32, 1.64);
// stack
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_num).unwrap();
info!("ser_stack: {ser_stack:#x}");
let field_ne_local_macro = &ser_stack.as_slice()[0..2];
assert_eq!(field_ne_local_macro, 0x00FF_u16.to_ne_bytes());
let field_le_local_macro = &ser_stack.as_slice()[2..4];
assert_eq!(field_le_local_macro, 0x00FF_u16.to_le_bytes());
let field_be_local_macro = &ser_stack.as_slice()[4..6];
assert_eq!(field_be_local_macro, 0x00FF_u16.to_be_bytes());
let field_be_global_macro = &ser_stack.as_slice()[6..8];
assert_eq!(field_be_global_macro, 0x00FF_u16.to_le_bytes());
// heap
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_num).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
// deserialize
let out_num: Numerics = from_serializer_stack(&ser_stack).unwrap();
info!("inp_num: {inp_num:?}");
info!("out_num: {out_num:?}");
assert_eq!(inp_num, out_num);
}
#[test]
fn test_size_and_len() {
size_len();
}
fn size_len() {
setup::log::configure();
let ln_of = Bytes::default().byte_len();
let sz_of = Bytes::byte_size();
let sz_of_aligned = size_of::<Bytes>();
info!("ln_of: {ln_of}");
info!("sz_of: {sz_of}");
info!("sz_of_aligned: {sz_of_aligned}");
assert_eq!(ln_of, sz_of);
assert_eq!(sz_of, size_of::<Bytes>());
let ln_of = Numerics::default().byte_len();
let sz_of = Numerics::byte_size();
let sz_of_aligned = size_of::<Numerics>();
info!("ln_of: {ln_of}");
info!("sz_of: {sz_of}");
info!("sz_of_aligned: {sz_of_aligned}");
assert_eq!(ln_of, sz_of);
assert!(sz_of < sz_of_aligned);
assert_eq!(sz_of, 81);
}
fn main() {
bytes();
numerics();
size_len()
}