• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use serde::Serialize;
2 use serde_cbor::ser::{Serializer, SliceWrite};
3 
4 #[test]
test_str()5 fn test_str() {
6     serialize_and_compare("foobar", b"ffoobar");
7 }
8 
9 #[test]
test_list()10 fn test_list() {
11     serialize_and_compare(&[1, 2, 3], b"\x83\x01\x02\x03");
12 }
13 
14 #[test]
test_float()15 fn test_float() {
16     serialize_and_compare(12.3f64, b"\xfb@(\x99\x99\x99\x99\x99\x9a");
17 }
18 
19 #[test]
test_integer()20 fn test_integer() {
21     // u8
22     serialize_and_compare(24, b"\x18\x18");
23     // i8
24     serialize_and_compare(-5, b"\x24");
25     // i16
26     serialize_and_compare(-300, b"\x39\x01\x2b");
27     // i32
28     serialize_and_compare(-23567997, b"\x3a\x01\x67\x9e\x7c");
29     // u64
30     serialize_and_compare(::core::u64::MAX, b"\x1b\xff\xff\xff\xff\xff\xff\xff\xff");
31 }
32 
serialize_and_compare<T: Serialize>(value: T, expected: &[u8])33 fn serialize_and_compare<T: Serialize>(value: T, expected: &[u8]) {
34     let mut slice = [0u8; 64];
35     let writer = SliceWrite::new(&mut slice);
36     let mut serializer = Serializer::new(writer);
37     value.serialize(&mut serializer).unwrap();
38     let writer = serializer.into_inner();
39     let end = writer.bytes_written();
40     let slice = writer.into_inner();
41     assert_eq!(&slice[..end], expected);
42 }
43 
44 #[cfg(feature = "std")]
45 mod std_tests {
46     use serde::Serializer;
47     use serde_cbor::ser;
48     use serde_cbor::{from_slice, to_vec};
49     use std::collections::BTreeMap;
50 
51     #[test]
test_string()52     fn test_string() {
53         let value = "foobar".to_owned();
54         assert_eq!(&to_vec(&value).unwrap()[..], b"ffoobar");
55     }
56 
57     #[test]
test_list()58     fn test_list() {
59         let value = vec![1, 2, 3];
60         assert_eq!(&to_vec(&value).unwrap()[..], b"\x83\x01\x02\x03");
61     }
62 
63     #[test]
test_object()64     fn test_object() {
65         let mut object = BTreeMap::new();
66         object.insert("a".to_owned(), "A".to_owned());
67         object.insert("b".to_owned(), "B".to_owned());
68         object.insert("c".to_owned(), "C".to_owned());
69         object.insert("d".to_owned(), "D".to_owned());
70         object.insert("e".to_owned(), "E".to_owned());
71         let vec = to_vec(&object).unwrap();
72         let test_object = from_slice(&vec[..]).unwrap();
73         assert_eq!(object, test_object);
74     }
75 
76     #[test]
test_object_list_keys()77     fn test_object_list_keys() {
78         let mut object = BTreeMap::new();
79         object.insert(vec![0i64], ());
80         object.insert(vec![100i64], ());
81         object.insert(vec![-1i64], ());
82         object.insert(vec![-2i64], ());
83         object.insert(vec![0i64, 0i64], ());
84         object.insert(vec![0i64, -1i64], ());
85         let vec = to_vec(&serde_cbor::value::to_value(object.clone()).unwrap()).unwrap();
86         assert_eq!(
87             vec![
88                 166, 129, 0, 246, 129, 24, 100, 246, 129, 32, 246, 129, 33, 246, 130, 0, 0, 246,
89                 130, 0, 32, 246
90             ],
91             vec
92         );
93         let test_object = from_slice(&vec[..]).unwrap();
94         assert_eq!(object, test_object);
95     }
96 
97     #[test]
test_object_object_keys()98     fn test_object_object_keys() {
99         use std::iter::FromIterator;
100         let mut object = BTreeMap::new();
101         let keys = vec![
102             vec!["a"],
103             vec!["b"],
104             vec!["c"],
105             vec!["d"],
106             vec!["aa"],
107             vec!["a", "aa"],
108         ]
109         .into_iter()
110         .map(|v| BTreeMap::from_iter(v.into_iter().map(|s| (s.to_owned(), ()))));
111 
112         for key in keys {
113             object.insert(key, ());
114         }
115         let vec = to_vec(&serde_cbor::value::to_value(object.clone()).unwrap()).unwrap();
116         assert_eq!(
117             vec![
118                 166, 161, 97, 97, 246, 246, 161, 97, 98, 246, 246, 161, 97, 99, 246, 246, 161, 97,
119                 100, 246, 246, 161, 98, 97, 97, 246, 246, 162, 97, 97, 246, 98, 97, 97, 246, 246
120             ],
121             vec
122         );
123         let test_object = from_slice(&vec[..]).unwrap();
124         assert_eq!(object, test_object);
125     }
126 
127     #[test]
test_float()128     fn test_float() {
129         let vec = to_vec(&12.3f64).unwrap();
130         assert_eq!(vec, b"\xfb@(\x99\x99\x99\x99\x99\x9a");
131     }
132 
133     #[test]
test_f32()134     fn test_f32() {
135         let vec = to_vec(&4000.5f32).unwrap();
136         assert_eq!(vec, b"\xfa\x45\x7a\x08\x00");
137     }
138 
139     #[test]
test_infinity()140     fn test_infinity() {
141         let vec = to_vec(&::std::f64::INFINITY).unwrap();
142         assert_eq!(vec, b"\xf9|\x00");
143     }
144 
145     #[test]
test_neg_infinity()146     fn test_neg_infinity() {
147         let vec = to_vec(&::std::f64::NEG_INFINITY).unwrap();
148         assert_eq!(vec, b"\xf9\xfc\x00");
149     }
150 
151     #[test]
test_nan()152     fn test_nan() {
153         let vec = to_vec(&::std::f32::NAN).unwrap();
154         assert_eq!(vec, b"\xf9\x7e\x00");
155     }
156 
157     #[test]
test_integer()158     fn test_integer() {
159         // u8
160         let vec = to_vec(&24).unwrap();
161         assert_eq!(vec, b"\x18\x18");
162         // i8
163         let vec = to_vec(&-5).unwrap();
164         assert_eq!(vec, b"\x24");
165         // i16
166         let vec = to_vec(&-300).unwrap();
167         assert_eq!(vec, b"\x39\x01\x2b");
168         // i32
169         let vec = to_vec(&-23567997).unwrap();
170         assert_eq!(vec, b"\x3a\x01\x67\x9e\x7c");
171         // u64
172         let vec = to_vec(&::std::u64::MAX).unwrap();
173         assert_eq!(vec, b"\x1b\xff\xff\xff\xff\xff\xff\xff\xff");
174     }
175 
176     #[test]
test_self_describing()177     fn test_self_describing() {
178         let mut vec = Vec::new();
179         {
180             let mut serializer = ser::Serializer::new(&mut vec);
181             serializer.self_describe().unwrap();
182             serializer.serialize_u64(9).unwrap();
183         }
184         assert_eq!(vec, b"\xd9\xd9\xf7\x09");
185     }
186 
187     #[test]
test_ip_addr()188     fn test_ip_addr() {
189         use std::net::Ipv4Addr;
190 
191         let addr = Ipv4Addr::new(8, 8, 8, 8);
192         let vec = to_vec(&addr).unwrap();
193         println!("{:?}", vec);
194         assert_eq!(vec.len(), 5);
195         let test_addr: Ipv4Addr = from_slice(&vec).unwrap();
196         assert_eq!(addr, test_addr);
197     }
198 
199     /// Test all of CBOR's fixed-length byte string types
200     #[test]
test_byte_string()201     fn test_byte_string() {
202         // Very short byte strings have 1-byte headers
203         let short = vec![0, 1, 2, 255];
204         let mut short_s = Vec::new();
205         serde_cbor::Serializer::new(&mut short_s)
206             .serialize_bytes(&short)
207             .unwrap();
208         assert_eq!(&short_s[..], [0x44, 0, 1, 2, 255]);
209 
210         // byte strings > 23 bytes have 2-byte headers
211         let medium = vec![
212             0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 255,
213         ];
214         let mut medium_s = Vec::new();
215         serde_cbor::Serializer::new(&mut medium_s)
216             .serialize_bytes(&medium)
217             .unwrap();
218         assert_eq!(
219             &medium_s[..],
220             [
221                 0x58, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
222                 21, 22, 255
223             ]
224         );
225 
226         // byte strings > 256 bytes have 3-byte headers
227         let long_vec = (0..256).map(|i| (i & 0xFF) as u8).collect::<Vec<_>>();
228         let mut long_s = Vec::new();
229         serde_cbor::Serializer::new(&mut long_s)
230             .serialize_bytes(&long_vec)
231             .unwrap();
232         assert_eq!(&long_s[0..3], [0x59, 1, 0]);
233         assert_eq!(&long_s[3..], &long_vec[..]);
234 
235         // byte strings > 2^16 bytes have 5-byte headers
236         let very_long_vec = (0..65536).map(|i| (i & 0xFF) as u8).collect::<Vec<_>>();
237         let mut very_long_s = Vec::new();
238         serde_cbor::Serializer::new(&mut very_long_s)
239             .serialize_bytes(&very_long_vec)
240             .unwrap();
241         assert_eq!(&very_long_s[0..5], [0x5a, 0, 1, 0, 0]);
242         assert_eq!(&very_long_s[5..], &very_long_vec[..]);
243 
244         // byte strings > 2^32 bytes have 9-byte headers, but they take too much RAM
245         // to test in Travis.
246     }
247 
248     #[test]
test_half()249     fn test_half() {
250         let vec = to_vec(&42.5f32).unwrap();
251         assert_eq!(vec, b"\xF9\x51\x50");
252         assert_eq!(from_slice::<f32>(&vec[..]).unwrap(), 42.5f32);
253     }
254 }
255