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