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