1 #![allow(rustc::internal)]
3 extern crate serialize as rustc_serialize;
5 use rustc_serialize::opaque::{Decoder, Encoder};
6 use rustc_serialize::{Decodable, Encodable};
9 #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
32 fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
33 let mut encoder = Encoder::new(Vec::new());
35 for value in &values {
36 Encodable::encode(&value, &mut encoder).unwrap();
39 let data = encoder.into_inner();
40 let mut decoder = Decoder::new(&data[..], 0);
43 let decoded = Decodable::decode(&mut decoder).unwrap();
44 assert_eq!(value, decoded);
50 check_round_trip(vec![(), (), (), ()]);
56 for i in ::std::u8::MIN..::std::u8::MAX {
59 check_round_trip(vec);
64 for i in ::std::u16::MIN..::std::u16::MAX {
65 check_round_trip(vec![1, 2, 3, i, i, i]);
71 check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]);
76 check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]);
81 check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]);
87 for i in ::std::i8::MIN..::std::i8::MAX {
90 check_round_trip(vec);
95 for i in ::std::i16::MIN..::std::i16::MAX {
96 check_round_trip(vec![-1, 2, -3, i, i, i, 2]);
102 check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]);
107 check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]);
112 check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]);
117 check_round_trip(vec![false, true, true, false, false]);
122 let mut vec = vec![];
124 vec.push((i as f32) / 3.0);
126 check_round_trip(vec);
131 let mut vec = vec![];
133 vec.push((i as f64) / 3.0);
135 check_round_trip(vec);
140 let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€'];
141 check_round_trip(vec);
147 "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(),
156 check_round_trip(vec);
161 check_round_trip(vec![Some(-1i8)]);
162 check_round_trip(vec![Some(-2i16)]);
163 check_round_trip(vec![Some(-3i32)]);
164 check_round_trip(vec![Some(-4i64)]);
165 check_round_trip(vec![Some(-5isize)]);
167 let none_i8: Option<i8> = None;
168 check_round_trip(vec![none_i8]);
170 let none_i16: Option<i16> = None;
171 check_round_trip(vec![none_i16]);
173 let none_i32: Option<i32> = None;
174 check_round_trip(vec![none_i32]);
176 let none_i64: Option<i64> = None;
177 check_round_trip(vec![none_i64]);
179 let none_isize: Option<isize> = None;
180 check_round_trip(vec![none_isize]);
185 check_round_trip(vec![Struct {
200 m: "abc".to_string(),
207 check_round_trip(vec![Struct {
222 m: "def".to_string(),
230 #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
233 Variant2(usize, f32),
234 Variant3 { a: i32, b: char, c: bool },
239 check_round_trip(vec![
241 Enum::Variant2(1, 2.5),
242 Enum::Variant3 { a: 3, b: 'b', c: false },
243 Enum::Variant3 { a: -4, b: 'f', c: true },
249 let mut vec = vec![];
250 for i in -100i64..100i64 {
251 vec.push(i * 100000);
254 check_round_trip(vec![vec]);
259 use std::collections::HashMap;
260 let mut map = HashMap::new();
261 for i in -100i64..100i64 {
262 map.insert(i * 100000, i * 10000);
265 check_round_trip(vec![map]);
270 check_round_trip(vec![('x', (), false, 0.5f32)]);
271 check_round_trip(vec![(9i8, 10u16, 1.5f64)]);
272 check_round_trip(vec![(-12i16, 11u8, 12usize)]);
273 check_round_trip(vec![(1234567isize, 100000000000000u64, 99999999999999i64)]);
274 check_round_trip(vec![(String::new(), "some string".to_string())]);