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
|
pub mod data;
pub mod transient_0;
#[cfg(test)]
mod test {
use std::io::Cursor;
use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64, StructWithString};
#[test]
fn struct_with_bool() {
let value = StructWithBool { a: true, b: false };
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_bool,
super::data::transient_0::decode_struct_with_bool,
);
}
#[test]
fn struct_with_int_32() {
let value = StructWithInt32 { a: 10, b: 25 };
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_int_32,
super::data::transient_0::decode_struct_with_int_32,
);
}
#[test]
fn struct_with_int_64() {
let value = StructWithInt64 { a: 10, b: 25 };
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_int_64,
super::data::transient_0::decode_struct_with_int_64,
);
}
#[test]
fn struct_with_string() {
let value = StructWithString { a: "abc".to_string(), b: "def".to_string() };
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_string,
super::data::transient_0::decode_struct_with_string,
);
}
#[test]
fn nested_struct() {
let value = OuterStruct { inner: InnerStruct { a: 10, b: 25 }, c: 42 };
assert_round_trip_encoding(
value,
super::data::transient_0::encode_outer_struct,
super::data::transient_0::decode_outer_struct,
);
}
fn assert_round_trip_encoding<T: std::cmp::PartialEq + std::fmt::Debug>(
value: T,
encode: impl Fn(&T, &mut Cursor<Vec<u8>>) -> std::io::Result<()>,
decode: impl Fn(&mut Cursor<Vec<u8>>) -> std::io::Result<T>,
) {
let mut bytes = Cursor::new(Vec::new());
encode(&value, &mut bytes).unwrap();
bytes.set_position(0);
let decoded_value = decode(&mut bytes).unwrap();
assert_eq!(value, decoded_value);
}
}
|