|
@@ -690,14 +690,52 @@ mod test {
|
|
|
enum Color { Red, Blue }
|
|
|
let mut map: HashMap<Color, u16> = HashMap::new();
|
|
|
map.insert(Color::Red, 5);
|
|
|
- map.insert(Color::Blue, 7);
|
|
|
+ map.insert(Color::Blue, 256);
|
|
|
let buffer = super::to_vec(&map)?;
|
|
|
+ assert_eq!(12, buffer.len());
|
|
|
+ assert_eq!(vec![0x02, 0x00, 0x00, 0x00], &buffer[..4]);
|
|
|
+
|
|
|
+ // The entries could be output in an arbitrary order.
|
|
|
+ let mut expected_map: HashMap<[u8; 2], [u8; 2]> = HashMap::new();
|
|
|
+ expected_map.insert([0x00, 0x00], [0x05, 0x00]);
|
|
|
+ expected_map.insert([0x01, 0x00], [0x00, 0x01]);
|
|
|
+ assert_eq!(expected_map.get(&buffer[4..6]).unwrap(), &buffer[6..8]);
|
|
|
+ assert_eq!(expected_map.get(&buffer[8..10]).unwrap(), &buffer[10..12]);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_struct() -> Result<()> {
|
|
|
+ #[derive(Serialize)]
|
|
|
+ struct Bag {
|
|
|
+ name: &'static str,
|
|
|
+ weight: u16,
|
|
|
+ value: i8
|
|
|
+ }
|
|
|
+ let value = Bag { name: "box", weight: 10, value: -1 };
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
let expected = vec![
|
|
|
- 0x02, 0x00, 0x00, 0x00, // Number of entries.
|
|
|
- 0x00, 0x00, // First key.
|
|
|
- 0x05, 0x00, // First value.
|
|
|
- 0x01, 0x00, // Second key.
|
|
|
- 0x07, 0x00, // Second value.
|
|
|
+ 0x03, 0x00, 0x00, 0x00, 'b' as u8, 'o' as u8, 'x' as u8, // name
|
|
|
+ 0x0A, 0x00, // weight
|
|
|
+ 0xFF // value
|
|
|
+ ];
|
|
|
+ assert_eq!(expected, buffer);
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_struct_variant() -> Result<()> {
|
|
|
+ #[derive(Serialize)]
|
|
|
+ enum Shape {
|
|
|
+ Rectangle { upper_left_corner: (u16, u16), width: u16, height: u16 },
|
|
|
+ Circle { center: (u16, u16), radius: u16 },
|
|
|
+ }
|
|
|
+ let value = Shape::Circle { center: (0x1D42, 0x9FE0), radius: 0xA100 };
|
|
|
+ let buffer = super::to_vec(&value)?;
|
|
|
+ let expected = vec![
|
|
|
+ 0x01, 0x00, // The variant index.
|
|
|
+ 0x42, 0x1D, 0xE0, 0x9F, // The center.
|
|
|
+ 0x00, 0xA1, // The radius.
|
|
|
];
|
|
|
assert_eq!(expected, buffer);
|
|
|
Ok(())
|