• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[macro_use]
2 extern crate serde_derive;
3 
4 #[cfg(feature = "std")]
5 mod std_tests {
6     use serde_cbor;
7 
8     use std::collections::BTreeMap;
9 
10     #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
11     struct TupleStruct(String, i32, u64);
12 
13     #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
14     struct UnitStruct;
15 
16     #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
17     struct Struct<'a> {
18         tuple_struct: TupleStruct,
19         tuple: (String, f32, f64),
20         map: BTreeMap<String, String>,
21         bytes: &'a [u8],
22         array: Vec<String>,
23         unit_array: Vec<UnitStruct>,
24     }
25 
26     use serde_cbor::value::Value;
27     use std::iter::FromIterator;
28 
29     #[test]
serde()30     fn serde() {
31         let tuple_struct = TupleStruct(format!("test"), -60, 3000);
32 
33         let tuple = (format!("hello"), -50.0040957, -12.094635556478);
34 
35         let map = BTreeMap::from_iter(
36             [
37                 (format!("key1"), format!("value1")),
38                 (format!("key2"), format!("value2")),
39                 (format!("key3"), format!("value3")),
40                 (format!("key4"), format!("value4")),
41             ]
42             .iter()
43             .cloned(),
44         );
45 
46         let bytes = b"test byte string";
47 
48         let array = vec![format!("one"), format!("two"), format!("three")];
49         let unit_array = vec![UnitStruct, UnitStruct, UnitStruct];
50 
51         let data = Struct {
52             tuple_struct,
53             tuple,
54             map,
55             bytes,
56             array,
57             unit_array,
58         };
59 
60         let value = serde_cbor::value::to_value(data.clone()).unwrap();
61         println!("{:?}", value);
62 
63         let data_ser = serde_cbor::to_vec(&value).unwrap();
64         let data_de_value: Value = serde_cbor::from_slice(&data_ser).unwrap();
65 
66         fn as_object(value: &Value) -> &BTreeMap<Value, Value> {
67             if let Value::Map(ref v) = value {
68                 return v;
69             }
70             panic!()
71         }
72 
73         for ((k1, v1), (k2, v2)) in as_object(&value)
74             .iter()
75             .zip(as_object(&data_de_value).iter())
76         {
77             assert_eq!(k1, k2);
78             assert_eq!(v1, v2);
79         }
80 
81         assert_eq!(value, data_de_value);
82     }
83 
84     #[derive(Debug, Deserialize, Serialize)]
85     struct SmallStruct {
86         spam: u32,
87         eggs: u32,
88     }
89 
90     #[test]
small_struct()91     fn small_struct() {
92         // Test whether the packed format works.
93         // Field names should not be serialized,
94         // instead field indizes are serialized.
95         let value = SmallStruct { spam: 17, eggs: 42 };
96         let data = serde_cbor::ser::to_vec_packed(&value).unwrap();
97         let reference = b"\xa2\x00\x11\x01\x18\x2a";
98         assert_eq!(data, reference);
99     }
100 }
101