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
|
pub mod data;
pub mod transient_0;
#[cfg(test)]
mod test {
use std::io::Cursor;
use super::data::{InnerStruct, OuterStruct, StructWithBool, StructWithInt32, StructWithInt64, StructWithList, StructWithOption, 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,
);
}
#[test]
fn struct_with_list() {
let value = StructWithList {
a: vec![10, 25],
b: vec![
InnerStruct { a: 42, b: 47 },
InnerStruct { a: 52, b: 57 },
InnerStruct { a: 62, b: 67 },
],
};
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_list,
super::data::transient_0::decode_struct_with_list,
);
}
#[test]
fn struct_with_option_none() {
let value = StructWithOption {
a: None,
b: None,
};
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_option,
super::data::transient_0::decode_struct_with_option,
);
}
#[test]
fn struct_with_option_some() {
let value = StructWithOption {
a: Some(10),
b: Some(InnerStruct { a: 42, b: 47 }),
};
assert_round_trip_encoding(
value,
super::data::transient_0::encode_struct_with_option,
super::data::transient_0::decode_struct_with_option,
);
}
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);
}
}
|