]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | #![allow(rustc::internal)] |
2 | ||
9fa01778 XL |
3 | extern crate serialize as rustc_serialize; |
4 | ||
46de9a89 FG |
5 | use rustc_serialize::opaque::{Decoder, Encoder}; |
6 | use rustc_serialize::{Decodable, Encodable}; | |
9fa01778 XL |
7 | use std::fmt::Debug; |
8 | ||
9 | #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] | |
10 | struct Struct { | |
11 | a: (), | |
12 | b: u8, | |
13 | c: u16, | |
14 | d: u32, | |
15 | e: u64, | |
16 | f: usize, | |
17 | ||
18 | g: i8, | |
19 | h: i16, | |
20 | i: i32, | |
21 | j: i64, | |
22 | k: isize, | |
23 | ||
24 | l: char, | |
25 | m: String, | |
26 | n: f32, | |
27 | o: f64, | |
28 | p: bool, | |
29 | q: Option<u32>, | |
30 | } | |
31 | ||
9fa01778 XL |
32 | fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) { |
33 | let mut encoder = Encoder::new(Vec::new()); | |
34 | ||
35 | for value in &values { | |
36 | Encodable::encode(&value, &mut encoder).unwrap(); | |
37 | } | |
38 | ||
39 | let data = encoder.into_inner(); | |
40 | let mut decoder = Decoder::new(&data[..], 0); | |
41 | ||
42 | for value in values { | |
43 | let decoded = Decodable::decode(&mut decoder).unwrap(); | |
44 | assert_eq!(value, decoded); | |
45 | } | |
46 | } | |
47 | ||
48 | #[test] | |
49 | fn test_unit() { | |
50 | check_round_trip(vec![(), (), (), ()]); | |
51 | } | |
52 | ||
53 | #[test] | |
54 | fn test_u8() { | |
55 | let mut vec = vec![]; | |
56 | for i in ::std::u8::MIN..::std::u8::MAX { | |
57 | vec.push(i); | |
58 | } | |
59 | check_round_trip(vec); | |
60 | } | |
61 | ||
62 | #[test] | |
63 | fn test_u16() { | |
64 | for i in ::std::u16::MIN..::std::u16::MAX { | |
65 | check_round_trip(vec![1, 2, 3, i, i, i]); | |
66 | } | |
67 | } | |
68 | ||
69 | #[test] | |
70 | fn test_u32() { | |
71 | check_round_trip(vec![1, 2, 3, ::std::u32::MIN, 0, 1, ::std::u32::MAX, 2, 1]); | |
72 | } | |
73 | ||
74 | #[test] | |
75 | fn test_u64() { | |
76 | check_round_trip(vec![1, 2, 3, ::std::u64::MIN, 0, 1, ::std::u64::MAX, 2, 1]); | |
77 | } | |
78 | ||
79 | #[test] | |
80 | fn test_usize() { | |
81 | check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]); | |
82 | } | |
83 | ||
84 | #[test] | |
85 | fn test_i8() { | |
86 | let mut vec = vec![]; | |
87 | for i in ::std::i8::MIN..::std::i8::MAX { | |
88 | vec.push(i); | |
89 | } | |
90 | check_round_trip(vec); | |
91 | } | |
92 | ||
93 | #[test] | |
94 | fn test_i16() { | |
95 | for i in ::std::i16::MIN..::std::i16::MAX { | |
96 | check_round_trip(vec![-1, 2, -3, i, i, i, 2]); | |
97 | } | |
98 | } | |
99 | ||
100 | #[test] | |
101 | fn test_i32() { | |
102 | check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]); | |
103 | } | |
104 | ||
105 | #[test] | |
106 | fn test_i64() { | |
107 | check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]); | |
108 | } | |
109 | ||
110 | #[test] | |
111 | fn test_isize() { | |
112 | check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]); | |
113 | } | |
114 | ||
115 | #[test] | |
116 | fn test_bool() { | |
117 | check_round_trip(vec![false, true, true, false, false]); | |
118 | } | |
119 | ||
120 | #[test] | |
121 | fn test_f32() { | |
122 | let mut vec = vec![]; | |
123 | for i in -100..100 { | |
124 | vec.push((i as f32) / 3.0); | |
125 | } | |
126 | check_round_trip(vec); | |
127 | } | |
128 | ||
129 | #[test] | |
130 | fn test_f64() { | |
131 | let mut vec = vec![]; | |
132 | for i in -100..100 { | |
133 | vec.push((i as f64) / 3.0); | |
134 | } | |
135 | check_round_trip(vec); | |
136 | } | |
137 | ||
138 | #[test] | |
139 | fn test_char() { | |
140 | let vec = vec!['a', 'b', 'c', 'd', 'A', 'X', ' ', '#', 'Ö', 'Ä', 'µ', '€']; | |
141 | check_round_trip(vec); | |
142 | } | |
143 | ||
144 | #[test] | |
145 | fn test_string() { | |
46de9a89 FG |
146 | let vec = 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(), | |
154 | ]; | |
9fa01778 XL |
155 | |
156 | check_round_trip(vec); | |
157 | } | |
158 | ||
159 | #[test] | |
160 | fn test_option() { | |
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)]); | |
166 | ||
167 | let none_i8: Option<i8> = None; | |
168 | check_round_trip(vec![none_i8]); | |
169 | ||
170 | let none_i16: Option<i16> = None; | |
171 | check_round_trip(vec![none_i16]); | |
172 | ||
173 | let none_i32: Option<i32> = None; | |
174 | check_round_trip(vec![none_i32]); | |
175 | ||
176 | let none_i64: Option<i64> = None; | |
177 | check_round_trip(vec![none_i64]); | |
178 | ||
179 | let none_isize: Option<isize> = None; | |
180 | check_round_trip(vec![none_isize]); | |
181 | } | |
182 | ||
183 | #[test] | |
184 | fn test_struct() { | |
185 | check_round_trip(vec![Struct { | |
46de9a89 FG |
186 | a: (), |
187 | b: 10, | |
188 | c: 11, | |
189 | d: 12, | |
190 | e: 13, | |
191 | f: 14, | |
192 | ||
193 | g: 15, | |
194 | h: 16, | |
195 | i: 17, | |
196 | j: 18, | |
197 | k: 19, | |
198 | ||
199 | l: 'x', | |
200 | m: "abc".to_string(), | |
201 | n: 20.5, | |
202 | o: 21.5, | |
203 | p: false, | |
204 | q: None, | |
205 | }]); | |
9fa01778 XL |
206 | |
207 | check_round_trip(vec![Struct { | |
46de9a89 FG |
208 | a: (), |
209 | b: 101, | |
210 | c: 111, | |
211 | d: 121, | |
212 | e: 131, | |
213 | f: 141, | |
214 | ||
215 | g: -15, | |
216 | h: -16, | |
217 | i: -17, | |
218 | j: -18, | |
219 | k: -19, | |
220 | ||
221 | l: 'y', | |
222 | m: "def".to_string(), | |
223 | n: -20.5, | |
224 | o: -21.5, | |
225 | p: true, | |
226 | q: Some(1234567), | |
227 | }]); | |
9fa01778 XL |
228 | } |
229 | ||
230 | #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] | |
231 | enum Enum { | |
232 | Variant1, | |
233 | Variant2(usize, f32), | |
46de9a89 | 234 | Variant3 { a: i32, b: char, c: bool }, |
9fa01778 XL |
235 | } |
236 | ||
237 | #[test] | |
238 | fn test_enum() { | |
46de9a89 FG |
239 | check_round_trip(vec![ |
240 | Enum::Variant1, | |
241 | Enum::Variant2(1, 2.5), | |
242 | Enum::Variant3 { a: 3, b: 'b', c: false }, | |
243 | Enum::Variant3 { a: -4, b: 'f', c: true }, | |
244 | ]); | |
9fa01778 XL |
245 | } |
246 | ||
247 | #[test] | |
248 | fn test_sequence() { | |
249 | let mut vec = vec![]; | |
250 | for i in -100i64..100i64 { | |
251 | vec.push(i * 100000); | |
252 | } | |
253 | ||
254 | check_round_trip(vec![vec]); | |
255 | } | |
256 | ||
257 | #[test] | |
258 | fn test_hash_map() { | |
259 | use std::collections::HashMap; | |
260 | let mut map = HashMap::new(); | |
261 | for i in -100i64..100i64 { | |
262 | map.insert(i * 100000, i * 10000); | |
263 | } | |
264 | ||
265 | check_round_trip(vec![map]); | |
266 | } | |
267 | ||
268 | #[test] | |
269 | fn test_tuples() { | |
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())]); | |
275 | } |