1 #![allow(bad_style)]
2
3 #[cfg(feature = "serde")]
4 use serde_test::{assert_tokens, Token};
5 use std::iter::FromIterator;
6 use tinyvec::*;
7
8 #[test]
test_a_vec()9 fn test_a_vec() {
10 let mut expected: ArrayVec<[i32; 4]> = Default::default();
11 expected.push(1);
12 expected.push(2);
13 expected.push(3);
14
15 let actual = array_vec!(1, 2, 3);
16
17 assert_eq!(expected, actual);
18
19 assert_eq!(array_vec![0u8; 4], array_vec!(0u8, 0u8, 0u8, 0u8));
20 assert_eq!(array_vec![0u8; 4], array_vec!([u8; 4] => 0, 0, 0, 0));
21 assert_eq!(array_vec![0; 4], array_vec!(0, 0, 0, 0));
22 assert_eq!(array_vec![0; 4], array_vec!([u8; 4] => 0, 0, 0, 0));
23
24 let expected2 = array_vec![1.1; 3];
25 let actual2 = array_vec!([f32; 3] => 1.1, 1.1, 1.1);
26 assert_eq!(expected2, actual2);
27 }
28
29 #[test]
ArrayVec_push_pop()30 fn ArrayVec_push_pop() {
31 let mut av: ArrayVec<[i32; 4]> = Default::default();
32 assert_eq!(av.len(), 0);
33 assert_eq!(av.pop(), None);
34
35 av.push(10_i32);
36 assert_eq!(av.len(), 1);
37 assert_eq!(av[0], 10);
38 assert_eq!(av.pop(), Some(10));
39 assert_eq!(av.len(), 0);
40 assert_eq!(av.pop(), None);
41
42 av.push(10);
43 av.push(11);
44 av.push(12);
45 av.push(13);
46 assert_eq!(av[0], 10);
47 assert_eq!(av[1], 11);
48 assert_eq!(av[2], 12);
49 assert_eq!(av[3], 13);
50 assert_eq!(av.len(), 4);
51 assert_eq!(av.pop(), Some(13));
52 assert_eq!(av.len(), 3);
53 assert_eq!(av.pop(), Some(12));
54 assert_eq!(av.len(), 2);
55 assert_eq!(av.pop(), Some(11));
56 assert_eq!(av.len(), 1);
57 assert_eq!(av.pop(), Some(10));
58 assert_eq!(av.len(), 0);
59 assert_eq!(av.pop(), None);
60 }
61
62 #[test]
63 #[should_panic]
ArrayVec_push_overflow()64 fn ArrayVec_push_overflow() {
65 let mut av: ArrayVec<[i32; 0]> = Default::default();
66 av.push(7);
67 }
68
69 #[test]
ArrayVec_formatting()70 fn ArrayVec_formatting() {
71 // check that we get the comma placement correct
72
73 let mut av: ArrayVec<[i32; 4]> = Default::default();
74 assert_eq!(format!("{:?}", av), "[]");
75 av.push(10);
76 assert_eq!(format!("{:?}", av), "[10]");
77 av.push(11);
78 assert_eq!(format!("{:?}", av), "[10, 11]");
79 av.push(12);
80 assert_eq!(format!("{:?}", av), "[10, 11, 12]");
81
82 // below here just asserts that the impls exist.
83
84 //
85 let av: ArrayVec<[i32; 4]> = Default::default();
86 assert_eq!(format!("{:b}", av), "[]");
87 assert_eq!(format!("{:o}", av), "[]");
88 assert_eq!(format!("{:x}", av), "[]");
89 assert_eq!(format!("{:X}", av), "[]");
90 assert_eq!(format!("{}", av), "[]");
91 //
92 let av: ArrayVec<[f32; 4]> = Default::default();
93 assert_eq!(format!("{:e}", av), "[]");
94 assert_eq!(format!("{:E}", av), "[]");
95 //
96 let av: ArrayVec<[&'static str; 4]> = Default::default();
97 assert_eq!(format!("{:p}", av), "[]");
98 }
99
100 #[test]
ArrayVec_iteration()101 fn ArrayVec_iteration() {
102 let av = array_vec!([i32; 4] => 10, 11, 12, 13);
103
104 let mut i = av.into_iter();
105 assert_eq!(i.next(), Some(10));
106 assert_eq!(i.next(), Some(11));
107 assert_eq!(i.next(), Some(12));
108 assert_eq!(i.next(), Some(13));
109 assert_eq!(i.next(), None);
110
111 let av = array_vec!([i32; 4] => 10, 11, 12, 13);
112
113 let mut av2: ArrayVec<[i32; 4]> = av.clone().into_iter().collect();
114 assert_eq!(av, av2);
115
116 // IntoIterator for &mut ArrayVec
117 for x in &mut av2 {
118 *x = -*x;
119 }
120
121 // IntoIterator for &ArrayVec
122 assert!(av.iter().zip(&av2).all(|(&a, &b)| a == -b));
123 }
124
125 #[test]
ArrayVec_append()126 fn ArrayVec_append() {
127 let mut av = array_vec!([i32; 8] => 1, 2, 3);
128 let mut av2 = array_vec!([i32; 8] => 4, 5, 6);
129 //
130 av.append(&mut av2);
131 assert_eq!(av.as_slice(), &[1_i32, 2, 3, 4, 5, 6]);
132 assert_eq!(av2.as_slice(), &[]);
133 }
134
135 #[test]
ArrayVec_remove()136 fn ArrayVec_remove() {
137 let mut av: ArrayVec<[i32; 10]> = Default::default();
138 av.push(1);
139 av.push(2);
140 av.push(3);
141 assert_eq!(av.remove(1), 2);
142 assert_eq!(&av[..], &[1, 3][..]);
143 }
144
145 #[test]
146 #[should_panic]
ArrayVec_remove_invalid()147 fn ArrayVec_remove_invalid() {
148 let mut av: ArrayVec<[i32; 1]> = Default::default();
149 av.push(1);
150 av.remove(1);
151 }
152
153 #[test]
ArrayVec_swap_remove()154 fn ArrayVec_swap_remove() {
155 let mut av: ArrayVec<[i32; 10]> = Default::default();
156 av.push(1);
157 av.push(2);
158 av.push(3);
159 av.push(4);
160 assert_eq!(av.swap_remove(3), 4);
161 assert_eq!(&av[..], &[1, 2, 3][..]);
162 assert_eq!(av.swap_remove(0), 1);
163 assert_eq!(&av[..], &[3, 2][..]);
164 assert_eq!(av.swap_remove(0), 3);
165 assert_eq!(&av[..], &[2][..]);
166 assert_eq!(av.swap_remove(0), 2);
167 assert_eq!(&av[..], &[][..]);
168 }
169
170 #[test]
ArrayVec_drain()171 fn ArrayVec_drain() {
172 let mut av: ArrayVec<[i32; 10]> = Default::default();
173 av.push(1);
174 av.push(2);
175 av.push(3);
176
177 assert_eq!(Vec::from_iter(av.clone().drain(..)), vec![1, 2, 3]);
178
179 assert_eq!(Vec::from_iter(av.clone().drain(..2)), vec![1, 2]);
180 assert_eq!(Vec::from_iter(av.clone().drain(..3)), vec![1, 2, 3]);
181
182 assert_eq!(Vec::from_iter(av.clone().drain(..=1)), vec![1, 2]);
183 assert_eq!(Vec::from_iter(av.clone().drain(..=2)), vec![1, 2, 3]);
184
185 assert_eq!(Vec::from_iter(av.clone().drain(0..)), vec![1, 2, 3]);
186 assert_eq!(Vec::from_iter(av.clone().drain(1..)), vec![2, 3]);
187
188 assert_eq!(Vec::from_iter(av.clone().drain(0..2)), vec![1, 2]);
189 assert_eq!(Vec::from_iter(av.clone().drain(0..3)), vec![1, 2, 3]);
190 assert_eq!(Vec::from_iter(av.clone().drain(1..2)), vec![2]);
191 assert_eq!(Vec::from_iter(av.clone().drain(1..3)), vec![2, 3]);
192
193 assert_eq!(Vec::from_iter(av.clone().drain(0..=1)), vec![1, 2]);
194 assert_eq!(Vec::from_iter(av.clone().drain(0..=2)), vec![1, 2, 3]);
195 assert_eq!(Vec::from_iter(av.clone().drain(1..=1)), vec![2]);
196 assert_eq!(Vec::from_iter(av.clone().drain(1..=2)), vec![2, 3]);
197 }
198
199 #[test]
ArrayVec_splice()200 fn ArrayVec_splice() {
201 let mut av: ArrayVec<[i32; 10]> = Default::default();
202 av.push(1);
203 av.push(2);
204 av.push(3);
205
206 // splice returns the same things as drain
207 assert_eq!(Vec::from_iter(av.clone().splice(.., None)), vec![1, 2, 3]);
208
209 assert_eq!(Vec::from_iter(av.clone().splice(..2, None)), vec![1, 2]);
210 assert_eq!(Vec::from_iter(av.clone().splice(..3, None)), vec![1, 2, 3]);
211
212 assert_eq!(Vec::from_iter(av.clone().splice(..=1, None)), vec![1, 2]);
213 assert_eq!(Vec::from_iter(av.clone().splice(..=2, None)), vec![1, 2, 3]);
214
215 assert_eq!(Vec::from_iter(av.clone().splice(0.., None)), vec![1, 2, 3]);
216 assert_eq!(Vec::from_iter(av.clone().splice(1.., None)), vec![2, 3]);
217
218 assert_eq!(Vec::from_iter(av.clone().splice(0..2, None)), vec![1, 2]);
219 assert_eq!(Vec::from_iter(av.clone().splice(0..3, None)), vec![1, 2, 3]);
220 assert_eq!(Vec::from_iter(av.clone().splice(1..2, None)), vec![2]);
221 assert_eq!(Vec::from_iter(av.clone().splice(1..3, None)), vec![2, 3]);
222
223 assert_eq!(Vec::from_iter(av.clone().splice(0..=1, None)), vec![1, 2]);
224 assert_eq!(Vec::from_iter(av.clone().splice(0..=2, None)), vec![1, 2, 3]);
225 assert_eq!(Vec::from_iter(av.clone().splice(1..=1, None)), vec![2]);
226 assert_eq!(Vec::from_iter(av.clone().splice(1..=2, None)), vec![2, 3]);
227
228 // splice removes the same things as drain
229 let mut av2 = av.clone();
230 av2.splice(.., None);
231 assert_eq!(av2, array_vec![]);
232
233 let mut av2 = av.clone();
234 av2.splice(..2, None);
235 assert_eq!(av2, array_vec![3]);
236
237 let mut av2 = av.clone();
238 av2.splice(..3, None);
239 assert_eq!(av2, array_vec![]);
240
241 let mut av2 = av.clone();
242 av2.splice(..=1, None);
243 assert_eq!(av2, array_vec![3]);
244 let mut av2 = av.clone();
245 av2.splice(..=2, None);
246 assert_eq!(av2, array_vec![]);
247
248 let mut av2 = av.clone();
249 av2.splice(0.., None);
250 assert_eq!(av2, array_vec![]);
251 let mut av2 = av.clone();
252 av2.splice(1.., None);
253 assert_eq!(av2, array_vec![1]);
254
255 let mut av2 = av.clone();
256 av2.splice(0..2, None);
257 assert_eq!(av2, array_vec![3]);
258
259 let mut av2 = av.clone();
260 av2.splice(0..3, None);
261 assert_eq!(av2, array_vec![]);
262 let mut av2 = av.clone();
263 av2.splice(1..2, None);
264 assert_eq!(av2, array_vec![1, 3]);
265
266 let mut av2 = av.clone();
267 av2.splice(1..3, None);
268 assert_eq!(av2, array_vec![1]);
269
270 let mut av2 = av.clone();
271 av2.splice(0..=1, None);
272 assert_eq!(av2, array_vec![3]);
273
274 let mut av2 = av.clone();
275 av2.splice(0..=2, None);
276 assert_eq!(av2, array_vec![]);
277
278 let mut av2 = av.clone();
279 av2.splice(1..=1, None);
280 assert_eq!(av2, array_vec![1, 3]);
281
282 let mut av2 = av.clone();
283 av2.splice(1..=2, None);
284 assert_eq!(av2, array_vec![1]);
285
286 // splice adds the elements correctly
287 let mut av2 = av.clone();
288 av2.splice(.., 4..=6);
289 assert_eq!(av2, array_vec![4, 5, 6]);
290
291 let mut av2 = av.clone();
292 av2.splice(..2, 4..=6);
293 assert_eq!(av2, array_vec![4, 5, 6, 3]);
294
295 let mut av2 = av.clone();
296 av2.splice(..3, 4..=6);
297 assert_eq!(av2, array_vec![4, 5, 6]);
298
299 let mut av2 = av.clone();
300 av2.splice(..=1, 4..=6);
301 assert_eq!(av2, array_vec![4, 5, 6, 3]);
302
303 let mut av2 = av.clone();
304 av2.splice(..=2, 4..=6);
305 assert_eq!(av2, array_vec![4, 5, 6]);
306
307 let mut av2 = av.clone();
308 av2.splice(0.., 4..=6);
309 assert_eq!(av2, array_vec![4, 5, 6]);
310
311 let mut av2 = av.clone();
312 av2.splice(1.., 4..=6);
313 assert_eq!(av2, array_vec![1, 4, 5, 6]);
314
315 let mut av2 = av.clone();
316 av2.splice(0..2, 4..=6);
317 assert_eq!(av2, array_vec![4, 5, 6, 3]);
318
319 let mut av2 = av.clone();
320 av2.splice(0..3, 4..=6);
321 assert_eq!(av2, array_vec![4, 5, 6]);
322
323 let mut av2 = av.clone();
324 av2.splice(1..2, 4..=6);
325 assert_eq!(av2, array_vec![1, 4, 5, 6, 3]);
326
327 let mut av2 = av.clone();
328 av2.splice(1..3, 4..=6);
329 assert_eq!(av2, array_vec![1, 4, 5, 6]);
330
331 let mut av2 = av.clone();
332 av2.splice(0..=1, 4..=6);
333 assert_eq!(av2, array_vec![4, 5, 6, 3]);
334
335 let mut av2 = av.clone();
336 av2.splice(0..=2, 4..=6);
337 assert_eq!(av2, array_vec![4, 5, 6]);
338
339 let mut av2 = av.clone();
340 av2.splice(1..=1, 4..=6);
341 assert_eq!(av2, array_vec![1, 4, 5, 6, 3]);
342
343 let mut av2 = av.clone();
344 av2.splice(1..=2, 4..=6);
345 assert_eq!(av2, array_vec![1, 4, 5, 6]);
346
347 // splice adds the elements correctly when the replacement is smaller
348 let mut av2 = av.clone();
349 av2.splice(.., Some(4));
350 assert_eq!(av2, array_vec![4]);
351
352 let mut av2 = av.clone();
353 av2.splice(..2, Some(4));
354 assert_eq!(av2, array_vec![4, 3]);
355
356 let mut av2 = av.clone();
357 av2.splice(1.., Some(4));
358 assert_eq!(av2, array_vec![1, 4]);
359
360 let mut av2 = av.clone();
361 av2.splice(1..=1, Some(4));
362 assert_eq!(av2, array_vec![1, 4, 3]);
363 }
364
365 #[test]
iter_last_nth()366 fn iter_last_nth() {
367 let mut av: ArrayVec<[i32; 10]> = Default::default();
368 av.push(1);
369 av.push(2);
370 av.push(3);
371 av.push(4);
372 assert_eq!(av.len(), 4);
373 let mut iter = av.into_iter();
374 assert_eq!(iter.next(), Some(1));
375 assert_eq!(iter.next(), Some(2));
376 assert_eq!(iter.next(), Some(3));
377 assert_eq!(iter.next(), Some(4));
378 assert_eq!(iter.next(), None);
379 assert_eq!(iter.last(), None);
380
381 let mut av: ArrayVec<[i32; 10]> = Default::default();
382 av.push(1);
383 av.push(2);
384 av.push(3);
385
386 assert_eq!(av.into_iter().nth(0), Some(1));
387 }
388
389 #[test]
390 #[cfg(feature = "rustc_1_40")]
reviter()391 fn reviter() {
392 let mut av: ArrayVec<[i32; 10]> = Default::default();
393 av.push(1);
394 av.push(2);
395 av.push(3);
396 av.push(4);
397
398 let mut iter = av.into_iter();
399
400 assert_eq!(iter.next(), Some(1));
401 assert_eq!(iter.next_back(), Some(4));
402 assert_eq!(iter.next(), Some(2));
403 assert_eq!(iter.next_back(), Some(3));
404 assert_eq!(iter.next(), None);
405 assert_eq!(iter.next_back(), None);
406
407 let mut av: ArrayVec<[i32; 32]> = Default::default();
408 av.extend(0..32);
409
410 let mut iter = av.into_iter();
411
412 assert_eq!(iter.nth_back(0), Some(31));
413 assert_eq!(iter.nth_back(2), Some(28));
414 assert_eq!(iter.nth_back(0), Some(27));
415 assert_eq!(iter.nth_back(99), None);
416 assert_eq!(iter.nth_back(99), None);
417 }
418
419 #[cfg(feature = "serde")]
420 #[test]
ArrayVec_ser_de_empty()421 fn ArrayVec_ser_de_empty() {
422 let tv: ArrayVec<[i32; 0]> = Default::default();
423
424 assert_tokens(&tv, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
425 }
426
427 #[cfg(feature = "serde")]
428 #[test]
ArrayVec_ser_de()429 fn ArrayVec_ser_de() {
430 let mut tv: ArrayVec<[i32; 4]> = Default::default();
431 tv.push(1);
432 tv.push(2);
433 tv.push(3);
434 tv.push(4);
435
436 assert_tokens(
437 &tv,
438 &[
439 Token::Seq { len: Some(4) },
440 Token::I32(1),
441 Token::I32(2),
442 Token::I32(3),
443 Token::I32(4),
444 Token::SeqEnd,
445 ],
446 );
447 }
448