1 #[macro_use]
2 extern crate serde_derive;
3 
4 #[cfg(feature = "std")]
5 mod std_tests {
6     use std::u8;
7 
8     use serde_cbor::de::from_mut_slice;
9     use serde_cbor::ser::{to_vec, to_vec_packed};
10     use serde_cbor::{from_reader, from_slice};
11 
to_binary(s: &'static str) -> Vec<u8>12     fn to_binary(s: &'static str) -> Vec<u8> {
13         assert!(s.len() % 2 == 0);
14         let mut b = Vec::with_capacity(s.len() / 2);
15         for i in 0..s.len() / 2 {
16             b.push(u8::from_str_radix(&s[i * 2..(i + 1) * 2], 16).unwrap());
17         }
18         b
19     }
20 
21     macro_rules! testcase {
22         ($name:ident, f64, $expr:expr, $s:expr) => {
23             #[test]
24             fn $name() {
25                 let expr: f64 = $expr;
26                 let mut serialized = to_binary($s);
27                 assert_eq!(to_vec(&expr).unwrap(), serialized);
28                 let parsed: f64 = from_slice(&serialized[..]).unwrap();
29                 if !expr.is_nan() {
30                     assert_eq!(expr, parsed);
31                 } else {
32                     assert!(parsed.is_nan())
33                 }
34 
35                 let parsed: f64 = from_reader(&mut &serialized[..]).unwrap();
36                 if !expr.is_nan() {
37                     assert_eq!(expr, parsed);
38                 } else {
39                     assert!(parsed.is_nan())
40                 }
41 
42                 let parsed: f64 = from_mut_slice(&mut serialized[..]).unwrap();
43                 if !expr.is_nan() {
44                     assert_eq!(expr, parsed);
45                 } else {
46                     assert!(parsed.is_nan())
47                 }
48             }
49         };
50         ($name:ident, $ty:ty, $expr:expr, $s:expr) => {
51             #[test]
52             fn $name() {
53                 let expr: $ty = $expr;
54                 let mut serialized = to_binary($s);
55                 assert_eq!(
56                     to_vec(&expr).expect("ser1 works"),
57                     serialized,
58                     "serialization differs"
59                 );
60                 let parsed: $ty = from_slice(&serialized[..]).expect("de1 works");
61                 assert_eq!(parsed, expr, "parsed result differs");
62                 let packed = &to_vec_packed(&expr).expect("serializing packed")[..];
63                 let parsed_from_packed: $ty = from_slice(packed).expect("parsing packed");
64                 assert_eq!(parsed_from_packed, expr, "packed roundtrip fail");
65 
66                 let parsed: $ty = from_reader(&mut &serialized[..]).unwrap();
67                 assert_eq!(parsed, expr, "parsed result differs");
68                 let mut packed = to_vec_packed(&expr).expect("serializing packed");
69                 let parsed_from_packed: $ty =
70                     from_reader(&mut &packed[..]).expect("parsing packed");
71                 assert_eq!(parsed_from_packed, expr, "packed roundtrip fail");
72 
73                 let parsed: $ty = from_mut_slice(&mut serialized[..]).unwrap();
74                 assert_eq!(parsed, expr, "parsed result differs");
75                 let parsed_from_packed: $ty =
76                     from_mut_slice(&mut packed[..]).expect("parsing packed");
77                 assert_eq!(parsed_from_packed, expr, "packed roundtrip fail");
78             }
79         };
80     }
81 
82     testcase!(test_bool_false, bool, false, "f4");
83     testcase!(test_bool_true, bool, true, "f5");
84     testcase!(test_isize_neg_256, isize, -256, "38ff");
85     testcase!(test_isize_neg_257, isize, -257, "390100");
86     testcase!(test_isize_255, isize, 255, "18ff");
87     testcase!(test_i8_5, i8, 5, "05");
88     testcase!(test_i8_23, i8, 23, "17");
89     testcase!(test_i8_24, i8, 24, "1818");
90     testcase!(test_i8_neg_128, i8, -128, "387f");
91     testcase!(test_u32_98745874, u32, 98745874, "1a05e2be12");
92     testcase!(test_f32_1234_point_5, f32, 1234.5, "fa449a5000");
93     testcase!(test_f64_12345_point_6, f64, 12345.6, "fb40c81ccccccccccd");
94     testcase!(test_f64_nan, f64, ::std::f64::NAN, "f97e00");
95     testcase!(test_f64_infinity, f64, ::std::f64::INFINITY, "f97c00");
96     testcase!(test_f64_neg_infinity, f64, -::std::f64::INFINITY, "f9fc00");
97     testcase!(test_char_null, char, '\x00', "6100");
98     testcase!(test_char_broken_heart, char, '��', "64f09f9294");
99     testcase!(
100         test_str_pangram_de,
101         String,
102         "aâø↓é".to_owned(),
103         "6a61c3a2c3b8e28693c3a9"
104     );
105     testcase!(test_unit, (), (), "f6");
106 
107     #[derive(Debug, PartialEq, Deserialize, Serialize)]
108     struct UnitStruct;
109     testcase!(test_unit_struct, UnitStruct, UnitStruct, "f6");
110 
111     #[derive(Debug, PartialEq, Deserialize, Serialize)]
112     struct NewtypeStruct(bool);
113     testcase!(
114         test_newtype_struct,
115         NewtypeStruct,
116         NewtypeStruct(true),
117         "f5"
118     );
119 
120     testcase!(test_option_none, Option<u8>, None, "f6");
121     testcase!(test_option_some, Option<u8>, Some(42), "182a");
122 
123     #[derive(Debug, PartialEq, Deserialize, Serialize)]
124     struct Person {
125         name: String,
126         year_of_birth: u16,
127         profession: Option<String>,
128     }
129 
130     testcase!(test_person_struct,
131     Person,
132     Person {
133         name: "Grace Hopper".to_string(),
134         year_of_birth: 1906,
135         profession: Some("computer scientist".to_string()),
136     },
137     "a3646e616d656c477261636520486f707065726d796561725f6f665f62697274681907726a70726f66657373696f6e72636f6d707574657220736369656e74697374");
138 
139     #[derive(Debug, PartialEq, Deserialize, Serialize)]
140     struct OptionalPerson {
141         name: String,
142         #[serde(skip_serializing_if = "Option::is_none")]
143         year_of_birth: Option<u16>,
144         profession: Option<String>,
145     }
146 
147     testcase!(test_optional_person_struct,
148     OptionalPerson,
149     OptionalPerson {
150         name: "Grace Hopper".to_string(),
151         year_of_birth: None,
152         profession: Some("computer scientist".to_string()),
153     },
154     "a2646e616d656c477261636520486f707065726a70726f66657373696f6e72636f6d707574657220736369656e74697374");
155 
156     #[derive(Debug, PartialEq, Deserialize, Serialize)]
157     enum Color {
158         Red,
159         Blue,
160         Yellow,
161         Other(u64),
162         Alpha(u64, u8),
163     }
164 
165     testcase!(test_color_enum, Color, Color::Blue, "64426c7565");
166     testcase!(
167         test_color_enum_transparent,
168         Color,
169         Color::Other(42),
170         "a1654f74686572182a"
171     );
172     testcase!(
173         test_color_enum_with_alpha,
174         Color,
175         Color::Alpha(234567, 60),
176         "a165416c706861821a00039447183c"
177     );
178     testcase!(test_i128_a, i128, -1i128, "20");
179     testcase!(
180         test_i128_b,
181         i128,
182         -18446744073709551616i128,
183         "3BFFFFFFFFFFFFFFFF"
184     );
185     testcase!(test_u128, u128, 17, "11");
186 }
187