1 #![allow(rustc::internal)]
3 use rustc_macros::{Decodable, Encodable};
4 use rustc_serialize::opaque::{Decoder, Encoder};
5 use rustc_serialize::{Decodable, Encodable};
8 #[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
31 fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + PartialEq + Debug>(
34 let mut encoder = Encoder::new(Vec::new());
36 for value in &values {
37 Encodable::encode(value, &mut encoder).unwrap();
40 let data = encoder.into_inner();
41 let mut decoder = Decoder::new(&data[..], 0);
44 let decoded = Decodable::decode(&mut decoder);
45 assert_eq!(value, decoded);
51 check_round_trip(vec![(), (), (), ()]);
57 for i in u8::MIN..u8::MAX {
60 check_round_trip(vec);
65 for i in u16::MIN..u16::MAX {
66 check_round_trip(vec![1, 2, 3, i, i, i]);
72 check_round_trip(vec![1, 2, 3, u32::MIN, 0, 1, u32::MAX, 2, 1]);
77 check_round_trip(vec![1, 2, 3, u64::MIN, 0, 1, u64::MAX, 2, 1]);
82 check_round_trip(vec![1, 2, 3, usize::MIN, 0, 1, usize::MAX, 2, 1]);
88 for i in i8::MIN..i8::MAX {
91 check_round_trip(vec);
96 for i in i16::MIN..i16::MAX {
97 check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
103 check_round_trip(vec![-1, 2, -3, i32::MIN, 0, 1, i32::MAX, 2, 1]);
108 check_round_trip(vec![-1, 2, -3, i64::MIN, 0, 1, i64::MAX, 2, 1]);
113 check_round_trip(vec![-1, 2, -3, isize::MIN, 0, 1, isize::MAX, 2, 1]);
118 check_round_trip(vec![false, true, true, false, false]);
123 let mut vec = vec![];
125 vec.push((i as f32) / 3.0);
127 check_round_trip(vec);
132 let mut vec = vec![];
134 vec.push((i as f64) / 3.0);
136 check_round_trip(vec);
141 let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€'];
142 check_round_trip(vec);
148 "abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
149 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
150 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
151 "abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
152 "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
153 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
154 "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string(),
157 check_round_trip(vec);
162 check_round_trip(vec![Some(-1i8)]);
163 check_round_trip(vec![Some(-2i16)]);
164 check_round_trip(vec![Some(-3i32)]);
165 check_round_trip(vec![Some(-4i64)]);
166 check_round_trip(vec![Some(-5isize)]);
168 let none_i8: Option<i8> = None;
169 check_round_trip(vec![none_i8]);
171 let none_i16: Option<i16> = None;
172 check_round_trip(vec![none_i16]);
174 let none_i32: Option<i32> = None;
175 check_round_trip(vec![none_i32]);
177 let none_i64: Option<i64> = None;
178 check_round_trip(vec![none_i64]);
180 let none_isize: Option<isize> = None;
181 check_round_trip(vec![none_isize]);
186 check_round_trip(vec![Struct {
201 m: "abc".to_string(),
208 check_round_trip(vec![Struct {
223 m: "def".to_string(),
231 #[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
234 Variant2(usize, f32),
235 Variant3 { a: i32, b: char, c: bool },
240 check_round_trip(vec![
242 Enum::Variant2(1, 2.5),
243 Enum::Variant3 { a: 3, b: 'b', c: false },
244 Enum::Variant3 { a: -4, b: 'f', c: true },
250 let mut vec = vec![];
251 for i in -100i64..100i64 {
252 vec.push(i * 100000);
255 check_round_trip(vec![vec]);
260 use std::collections::HashMap;
261 let mut map = HashMap::new();
262 for i in -100i64..100i64 {
263 map.insert(i * 100000, i * 10000);
266 check_round_trip(vec![map]);
271 check_round_trip(vec![('x', (), false, 0.5f32)]);
272 check_round_trip(vec![(9i8, 10u16, 1.5f64)]);
273 check_round_trip(vec![(-12i16, 11u8, 12usize)]);
274 check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
275 check_round_trip(vec![(String::new(), "some string".to_string())]);