1 #![allow(rustc::internal)]
3 extern crate serialize as rustc_serialize;
5 use rustc_serialize::{Encodable, Decodable};
6 use rustc_serialize::opaque::{Encoder, Decoder};
9 #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
33 fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
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).unwrap();
45 assert_eq!(value, decoded);
51 check_round_trip(vec![(), (), (), ()]);
57 for i in ::std::u8::MIN..::std::u8::MAX {
60 check_round_trip(vec);
65 for i in ::std::u16::MIN..::std::u16::MAX {
66 check_round_trip(vec![1, 2, 3, i, i, i]);
72 check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]);
77 check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]);
82 check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]);
88 for i in ::std::i8::MIN..::std::i8::MAX {
91 check_round_trip(vec);
96 for i in ::std::i16::MIN..::std::i16::MAX {
97 check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
103 check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]);
108 check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]);
113 check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::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);
147 let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
148 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
149 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
150 "abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
151 "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
152 "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
153 "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()];
155 check_round_trip(vec);
160 check_round_trip(vec![Some(-1i8)]);
161 check_round_trip(vec![Some(-2i16)]);
162 check_round_trip(vec![Some(-3i32)]);
163 check_round_trip(vec![Some(-4i64)]);
164 check_round_trip(vec![Some(-5isize)]);
166 let none_i8: Option<i8> = None;
167 check_round_trip(vec![none_i8]);
169 let none_i16: Option<i16> = None;
170 check_round_trip(vec![none_i16]);
172 let none_i32: Option<i32> = None;
173 check_round_trip(vec![none_i32]);
175 let none_i64: Option<i64> = None;
176 check_round_trip(vec![none_i64]);
178 let none_isize: Option<isize> = None;
179 check_round_trip(vec![none_isize]);
184 check_round_trip(vec![Struct {
199 m: "abc".to_string(),
206 check_round_trip(vec![Struct {
221 m: "def".to_string(),
229 #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
232 Variant2(usize, f32),
242 check_round_trip(vec![Enum::Variant1,
243 Enum::Variant2(1, 2.5),
258 let mut vec = vec![];
259 for i in -100i64..100i64 {
260 vec.push(i * 100000);
263 check_round_trip(vec![vec]);
268 use std::collections::HashMap;
269 let mut map = HashMap::new();
270 for i in -100i64..100i64 {
271 map.insert(i * 100000, i * 10000);
274 check_round_trip(vec![map]);
279 check_round_trip(vec![('x', (), false, 0.5f32)]);
280 check_round_trip(vec![(9i8, 10u16, 1.5f64)]);
281 check_round_trip(vec![(-12i16, 11u8, 12usize)]);
282 check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
283 check_round_trip(vec![(String::new(), "some string".to_string())]);