1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std
::cmp
::Ordering
::{Equal, Greater, Less}
;
13 use std
::__rand
::{Rng, thread_rng}
;
16 fn square(n
: usize) -> usize { n * n }
18 fn is_odd(n
: &usize) -> bool { *n % 2 == 1 }
22 // Test on-stack from_fn.
23 let mut v
: Vec
<_
> = (0..3).map(square
).collect();
26 assert_eq
!(v
.len(), 3);
32 // Test on-heap from_fn.
33 v
= (0..5).map(square
).collect();
36 assert_eq
!(v
.len(), 5);
47 // Test on-stack from_elem.
48 let mut v
= vec
![10, 10];
51 assert_eq
!(v
.len(), 2);
56 // Test on-heap from_elem.
71 let xs
: [i32; 0] = [];
72 assert
!(xs
.is_empty());
73 assert
!(![0].is_empty());
77 fn test_len_divzero() {
80 let v1
: &[Z
] = &[[]];
81 let v2
: &[Z
] = &[[], []];
82 assert_eq
!(mem
::size_of
::<Z
>(), 0);
83 assert_eq
!(v0
.len(), 0);
84 assert_eq
!(v1
.len(), 1);
85 assert_eq
!(v2
.len(), 2);
91 assert_eq
!(a
.get(1), None
);
93 assert_eq
!(a
.get(1).unwrap(), &12);
95 assert_eq
!(a
.get(1).unwrap(), &12);
101 assert_eq
!(a
.first(), None
);
103 assert_eq
!(a
.first().unwrap(), &11);
105 assert_eq
!(a
.first().unwrap(), &11);
109 fn test_first_mut() {
111 assert_eq
!(a
.first_mut(), None
);
113 assert_eq
!(*a
.first_mut().unwrap(), 11);
115 assert_eq
!(*a
.first_mut().unwrap(), 11);
119 fn test_split_first() {
120 let mut a
= vec
![11];
122 assert
!(b
.split_first().is_none());
123 assert_eq
!(a
.split_first(), Some((&11, b
)));
125 let b
: &[i32] = &[12];
126 assert_eq
!(a
.split_first(), Some((&11, b
)));
130 fn test_split_first_mut() {
131 let mut a
= vec
![11];
132 let b
: &mut [i32] = &mut [];
133 assert
!(b
.split_first_mut().is_none());
134 assert
!(a
.split_first_mut() == Some((&mut 11, b
)));
136 let b
: &mut [_
] = &mut [12];
137 assert
!(a
.split_first_mut() == Some((&mut 11, b
)));
141 fn test_split_last() {
142 let mut a
= vec
![11];
144 assert
!(b
.split_last().is_none());
145 assert_eq
!(a
.split_last(), Some((&11, b
)));
148 assert_eq
!(a
.split_last(), Some((&12, b
)));
152 fn test_split_last_mut() {
153 let mut a
= vec
![11];
154 let b
: &mut [i32] = &mut [];
155 assert
!(b
.split_last_mut().is_none());
156 assert
!(a
.split_last_mut() == Some((&mut 11, b
)));
159 let b
: &mut [_
] = &mut [11];
160 assert
!(a
.split_last_mut() == Some((&mut 12, b
)));
166 assert_eq
!(a
.last(), None
);
168 assert_eq
!(a
.last().unwrap(), &11);
170 assert_eq
!(a
.last().unwrap(), &12);
176 assert_eq
!(a
.last_mut(), None
);
178 assert_eq
!(*a
.last_mut().unwrap(), 11);
180 assert_eq
!(*a
.last_mut().unwrap(), 12);
185 // Test fixed length vector.
186 let vec_fixed
= [1, 2, 3, 4];
187 let v_a
= vec_fixed
[1..vec_fixed
.len()].to_vec();
188 assert_eq
!(v_a
.len(), 3);
190 assert_eq
!(v_a
[0], 2);
191 assert_eq
!(v_a
[1], 3);
192 assert_eq
!(v_a
[2], 4);
195 let vec_stack
: &[_
] = &[1, 2, 3];
196 let v_b
= vec_stack
[1..3].to_vec();
197 assert_eq
!(v_b
.len(), 2);
199 assert_eq
!(v_b
[0], 2);
200 assert_eq
!(v_b
[1], 3);
203 let vec_unique
= vec
![1, 2, 3, 4, 5, 6];
204 let v_d
= vec_unique
[1..6].to_vec();
205 assert_eq
!(v_d
.len(), 5);
207 assert_eq
!(v_d
[0], 2);
208 assert_eq
!(v_d
[1], 3);
209 assert_eq
!(v_d
[2], 4);
210 assert_eq
!(v_d
[3], 5);
211 assert_eq
!(v_d
[4], 6);
215 fn test_slice_from() {
216 let vec
: &[_
] = &[1, 2, 3, 4];
217 assert_eq
!(&vec
[..], vec
);
218 let b
: &[_
] = &[3, 4];
219 assert_eq
!(&vec
[2..], b
);
221 assert_eq
!(&vec
[4..], b
);
226 let vec
: &[_
] = &[1, 2, 3, 4];
227 assert_eq
!(&vec
[..4], vec
);
228 let b
: &[_
] = &[1, 2];
229 assert_eq
!(&vec
[..2], b
);
231 assert_eq
!(&vec
[..0], b
);
239 assert_eq
!(v
.len(), 0);
240 assert_eq
!(e
, Some(5));
248 fn test_swap_remove() {
249 let mut v
= vec
![1, 2, 3, 4, 5];
250 let mut e
= v
.swap_remove(0);
252 assert_eq
!(v
, [5, 2, 3, 4]);
253 e
= v
.swap_remove(3);
255 assert_eq
!(v
, [5, 2, 3]);
260 fn test_swap_remove_fail() {
262 let _
= v
.swap_remove(0);
263 let _
= v
.swap_remove(0);
267 fn test_swap_remove_noncopyable() {
268 // Tests that we don't accidentally run destructors twice.
269 let mut v
: Vec
<Box
<_
>> = Vec
::new();
273 let mut _e
= v
.swap_remove(0);
274 assert_eq
!(v
.len(), 2);
275 _e
= v
.swap_remove(1);
276 assert_eq
!(v
.len(), 1);
277 _e
= v
.swap_remove(0);
278 assert_eq
!(v
.len(), 0);
283 // Test on-stack push().
286 assert_eq
!(v
.len(), 1);
289 // Test on-heap push().
291 assert_eq
!(v
.len(), 2);
298 let mut v
: Vec
<Box
<_
>> = vec
![box 6,box 5,box 4];
301 assert_eq
!(v
.len(), 1);
302 assert_eq
!(*(v
[0]), 6);
303 // If the unsafe block didn't drop things properly, we blow up here.
308 let mut v
: Vec
<Box
<_
>> = vec
![box 6,box 5,box 4];
310 assert_eq
!(v
.len(), 0);
311 // If the unsafe block didn't drop things properly, we blow up here.
316 fn case(a
: Vec
<i32>, b
: Vec
<i32>) {
321 case(vec
![], vec
![]);
322 case(vec
![1], vec
![1]);
323 case(vec
![1,1], vec
![1]);
324 case(vec
![1,2,3], vec
![1,2,3]);
325 case(vec
![1,1,2,3], vec
![1,2,3]);
326 case(vec
![1,2,2,3], vec
![1,2,3]);
327 case(vec
![1,2,3,3], vec
![1,2,3]);
328 case(vec
![1,1,2,2,2,3,3], vec
![1,2,3]);
332 fn test_dedup_unique() {
333 let mut v0
: Vec
<Box
<_
>> = vec
![box 1, box 1, box 2, box 3];
335 let mut v1
: Vec
<Box
<_
>> = vec
![box 1, box 2, box 2, box 3];
337 let mut v2
: Vec
<Box
<_
>> = vec
![box 1, box 2, box 3, box 3];
340 * If the boxed pointers were leaked or otherwise misused, valgrind
341 * and/or rt should raise errors.
346 fn test_dedup_shared() {
347 let mut v0
: Vec
<Box
<_
>> = vec
![box 1, box 1, box 2, box 3];
349 let mut v1
: Vec
<Box
<_
>> = vec
![box 1, box 2, box 2, box 3];
351 let mut v2
: Vec
<Box
<_
>> = vec
![box 1, box 2, box 3, box 3];
354 * If the pointers were leaked or otherwise misused, valgrind and/or
355 * rt should raise errors.
361 let mut v
= vec
![1, 2, 3, 4, 5];
363 assert_eq
!(v
, [1, 3, 5]);
367 fn test_binary_search() {
368 assert_eq
!([1,2,3,4,5].binary_search(&5).ok(), Some(4));
369 assert_eq
!([1,2,3,4,5].binary_search(&4).ok(), Some(3));
370 assert_eq
!([1,2,3,4,5].binary_search(&3).ok(), Some(2));
371 assert_eq
!([1,2,3,4,5].binary_search(&2).ok(), Some(1));
372 assert_eq
!([1,2,3,4,5].binary_search(&1).ok(), Some(0));
374 assert_eq
!([2,4,6,8,10].binary_search(&1).ok(), None
);
375 assert_eq
!([2,4,6,8,10].binary_search(&5).ok(), None
);
376 assert_eq
!([2,4,6,8,10].binary_search(&4).ok(), Some(1));
377 assert_eq
!([2,4,6,8,10].binary_search(&10).ok(), Some(4));
379 assert_eq
!([2,4,6,8].binary_search(&1).ok(), None
);
380 assert_eq
!([2,4,6,8].binary_search(&5).ok(), None
);
381 assert_eq
!([2,4,6,8].binary_search(&4).ok(), Some(1));
382 assert_eq
!([2,4,6,8].binary_search(&8).ok(), Some(3));
384 assert_eq
!([2,4,6].binary_search(&1).ok(), None
);
385 assert_eq
!([2,4,6].binary_search(&5).ok(), None
);
386 assert_eq
!([2,4,6].binary_search(&4).ok(), Some(1));
387 assert_eq
!([2,4,6].binary_search(&6).ok(), Some(2));
389 assert_eq
!([2,4].binary_search(&1).ok(), None
);
390 assert_eq
!([2,4].binary_search(&5).ok(), None
);
391 assert_eq
!([2,4].binary_search(&2).ok(), Some(0));
392 assert_eq
!([2,4].binary_search(&4).ok(), Some(1));
394 assert_eq
!([2].binary_search(&1).ok(), None
);
395 assert_eq
!([2].binary_search(&5).ok(), None
);
396 assert_eq
!([2].binary_search(&2).ok(), Some(0));
398 assert_eq
!([].binary_search(&1).ok(), None
);
399 assert_eq
!([].binary_search(&5).ok(), None
);
401 assert
!([1,1,1,1,1].binary_search(&1).ok() != None
);
402 assert
!([1,1,1,1,2].binary_search(&1).ok() != None
);
403 assert
!([1,1,1,2,2].binary_search(&1).ok() != None
);
404 assert
!([1,1,2,2,2].binary_search(&1).ok() != None
);
405 assert_eq
!([1,2,2,2,2].binary_search(&1).ok(), Some(0));
407 assert_eq
!([1,2,3,4,5].binary_search(&6).ok(), None
);
408 assert_eq
!([1,2,3,4,5].binary_search(&0).ok(), None
);
413 let mut v
= vec
![10, 20];
414 assert_eq
!(v
[0], 10);
415 assert_eq
!(v
[1], 20);
417 assert_eq
!(v
[0], 20);
418 assert_eq
!(v
[1], 10);
420 let mut v3
= Vec
::<i32>::new();
422 assert
!(v3
.is_empty());
429 let mut v
: Vec
<_
> = thread_rng().gen_iter
::<i32>().take(len
).collect();
430 let mut v1
= v
.clone();
433 assert
!(v
.windows(2).all(|w
| w
[0] <= w
[1]));
435 v1
.sort_by(|a
, b
| a
.cmp(b
));
436 assert
!(v1
.windows(2).all(|w
| w
[0] <= w
[1]));
438 v1
.sort_by(|a
, b
| b
.cmp(a
));
439 assert
!(v1
.windows(2).all(|w
| w
[0] >= w
[1]));
444 let mut v
: [i32; 0] = [];
447 let mut v
= [0xDEADBEEFu64];
449 assert
!(v
== [0xDEADBEEF]);
453 fn test_sort_stability() {
456 let mut counts
= [0; 10];
458 // create a vector like [(6, 1), (5, 1), (6, 2), ...],
459 // where the first item of each tuple is random, but
460 // the second item represents which occurrence of that
461 // number this element is, i.e. the second elements
462 // will occur in sorted order.
463 let mut v
: Vec
<_
> = (0..len
).map(|_
| {
464 let n
= thread_rng().gen
::<usize>() % 10;
469 // only sort on the first element, so an unstable sort
470 // may mix up the counts.
471 v
.sort_by(|&(a
,_
), &(b
,_
)| a
.cmp(&b
));
473 // this comparison includes the count (the second item
474 // of the tuple), so elements with equal first items
475 // will need to be ordered with increasing
476 // counts... i.e. exactly asserting that this sort is
478 assert
!(v
.windows(2).all(|w
| w
[0] <= w
[1]));
485 let v
: [Vec
<i32>; 0] = [];
488 let d
= [vec
![1], vec
![2, 3]].concat();
489 assert_eq
!(d
, [1, 2, 3]);
491 let v
: &[&[_
]] = &[&[1], &[2, 3]];
492 assert_eq
!(v
.join(&0), [1, 0, 2, 3]);
493 let v
: &[&[_
]] = &[&[1], &[2], &[3]];
494 assert_eq
!(v
.join(&0), [1, 0, 2, 0, 3]);
499 let v
: [Vec
<i32>; 0] = [];
500 assert_eq
!(v
.join(&0), []);
501 assert_eq
!([vec
![1], vec
![2, 3]].join(&0), [1, 0, 2, 3]);
502 assert_eq
!([vec
![1], vec
![2], vec
![3]].join(&0), [1, 0, 2, 0, 3]);
504 let v
: [&[_
]; 2] = [&[1], &[2, 3]];
505 assert_eq
!(v
.join(&0), [1, 0, 2, 3]);
506 let v
: [&[_
]; 3] = [&[1], &[2], &[3]];
507 assert_eq
!(v
.join(&0), [1, 0, 2, 0, 3]);
512 let mut a
= vec
![1, 2, 4];
514 assert_eq
!(a
, [1, 2, 3, 4]);
516 let mut a
= vec
![1, 2, 3];
518 assert_eq
!(a
, [0, 1, 2, 3]);
520 let mut a
= vec
![1, 2, 3];
522 assert_eq
!(a
, [1, 2, 3, 4]);
531 fn test_insert_oob() {
532 let mut a
= vec
![1, 2, 3];
538 let mut a
= vec
![1, 2, 3, 4];
540 assert_eq
!(a
.remove(2), 3);
541 assert_eq
!(a
, [1, 2, 4]);
543 assert_eq
!(a
.remove(2), 4);
544 assert_eq
!(a
, [1, 2]);
546 assert_eq
!(a
.remove(0), 1);
549 assert_eq
!(a
.remove(0), 2);
555 fn test_remove_fail() {
565 assert
!(v
.capacity() >= 11);
570 let v
= vec
![1, 2, 3, 4, 5];
572 assert_eq
!(v
.len(), 2);
577 macro_rules
! assert_order
{
578 (Greater
, $a
:expr
, $b
:expr
) => {
579 assert_eq
!($a
.cmp($b
), Greater
);
582 (Less
, $a
:expr
, $b
:expr
) => {
583 assert_eq
!($a
.cmp($b
), Less
);
586 (Equal
, $a
:expr
, $b
:expr
) => {
587 assert_eq
!($a
.cmp($b
), Equal
);
593 fn test_total_ord_u8() {
594 let c
= &[1u8, 2, 3];
595 assert_order
!(Greater
, &[1u8, 2, 3, 4][..], &c
[..]);
596 let c
= &[1u8, 2, 3, 4];
597 assert_order
!(Less
, &[1u8, 2, 3][..], &c
[..]);
598 let c
= &[1u8, 2, 3, 6];
599 assert_order
!(Equal
, &[1u8, 2, 3, 6][..], &c
[..]);
600 let c
= &[1u8, 2, 3, 4, 5, 6];
601 assert_order
!(Less
, &[1u8, 2, 3, 4, 5, 5, 5, 5][..], &c
[..]);
602 let c
= &[1u8, 2, 3, 4];
603 assert_order
!(Greater
, &[2u8, 2][..], &c
[..]);
608 fn test_total_ord_i32() {
610 assert_order
!(Greater
, &[1, 2, 3, 4][..], &c
[..]);
611 let c
= &[1, 2, 3, 4];
612 assert_order
!(Less
, &[1, 2, 3][..], &c
[..]);
613 let c
= &[1, 2, 3, 6];
614 assert_order
!(Equal
, &[1, 2, 3, 6][..], &c
[..]);
615 let c
= &[1, 2, 3, 4, 5, 6];
616 assert_order
!(Less
, &[1, 2, 3, 4, 5, 5, 5, 5][..], &c
[..]);
617 let c
= &[1, 2, 3, 4];
618 assert_order
!(Greater
, &[2, 2][..], &c
[..]);
623 let xs
= [1, 2, 5, 10, 11];
624 let mut it
= xs
.iter();
625 assert_eq
!(it
.size_hint(), (5, Some(5)));
626 assert_eq
!(it
.next().unwrap(), &1);
627 assert_eq
!(it
.size_hint(), (4, Some(4)));
628 assert_eq
!(it
.next().unwrap(), &2);
629 assert_eq
!(it
.size_hint(), (3, Some(3)));
630 assert_eq
!(it
.next().unwrap(), &5);
631 assert_eq
!(it
.size_hint(), (2, Some(2)));
632 assert_eq
!(it
.next().unwrap(), &10);
633 assert_eq
!(it
.size_hint(), (1, Some(1)));
634 assert_eq
!(it
.next().unwrap(), &11);
635 assert_eq
!(it
.size_hint(), (0, Some(0)));
636 assert
!(it
.next().is_none());
640 fn test_iter_size_hints() {
641 let mut xs
= [1, 2, 5, 10, 11];
642 assert_eq
!(xs
.iter().size_hint(), (5, Some(5)));
643 assert_eq
!(xs
.iter_mut().size_hint(), (5, Some(5)));
647 fn test_iter_clone() {
649 let mut it
= xs
.iter();
651 let mut jt
= it
.clone();
652 assert_eq
!(it
.next(), jt
.next());
653 assert_eq
!(it
.next(), jt
.next());
654 assert_eq
!(it
.next(), jt
.next());
658 fn test_mut_iterator() {
659 let mut xs
= [1, 2, 3, 4, 5];
663 assert
!(xs
== [2, 3, 4, 5, 6])
667 fn test_rev_iterator() {
669 let xs
= [1, 2, 5, 10, 11];
670 let ys
= [11, 10, 5, 2, 1];
672 for &x
in xs
.iter().rev() {
673 assert_eq
!(x
, ys
[i
]);
680 fn test_mut_rev_iterator() {
681 let mut xs
= [1, 2, 3, 4, 5];
682 for (i
,x
) in xs
.iter_mut().rev().enumerate() {
685 assert
!(xs
== [5, 5, 5, 5, 5])
689 fn test_move_iterator() {
690 let xs
= vec
![1,2,3,4,5];
691 assert_eq
!(xs
.into_iter().fold(0, |a
: usize, b
: usize| 10*a
+ b
), 12345);
695 fn test_move_rev_iterator() {
696 let xs
= vec
![1,2,3,4,5];
697 assert_eq
!(xs
.into_iter().rev().fold(0, |a
: usize, b
: usize| 10*a
+ b
), 54321);
701 fn test_splitator() {
702 let xs
= &[1,2,3,4,5];
704 let splits
: &[&[_
]] = &[&[1], &[3], &[5]];
705 assert_eq
!(xs
.split(|x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
707 let splits
: &[&[_
]] = &[&[], &[2,3,4,5]];
708 assert_eq
!(xs
.split(|x
| *x
== 1).collect
::<Vec
<_
>>(),
710 let splits
: &[&[_
]] = &[&[1,2,3,4], &[]];
711 assert_eq
!(xs
.split(|x
| *x
== 5).collect
::<Vec
<_
>>(),
713 let splits
: &[&[_
]] = &[&[1,2,3,4,5]];
714 assert_eq
!(xs
.split(|x
| *x
== 10).collect
::<Vec
<_
>>(),
716 let splits
: &[&[_
]] = &[&[], &[], &[], &[], &[], &[]];
717 assert_eq
!(xs
.split(|_
| true).collect
::<Vec
<&[i32]>>(),
720 let xs
: &[i32] = &[];
721 let splits
: &[&[i32]] = &[&[]];
722 assert_eq
!(xs
.split(|x
| *x
== 5).collect
::<Vec
<&[i32]>>(), splits
);
726 fn test_splitnator() {
727 let xs
= &[1,2,3,4,5];
729 let splits
: &[&[_
]] = &[&[1,2,3,4,5]];
730 assert_eq
!(xs
.splitn(1, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
732 let splits
: &[&[_
]] = &[&[1], &[3,4,5]];
733 assert_eq
!(xs
.splitn(2, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
735 let splits
: &[&[_
]] = &[&[], &[], &[], &[4,5]];
736 assert_eq
!(xs
.splitn(4, |_
| true).collect
::<Vec
<_
>>(),
739 let xs
: &[i32] = &[];
740 let splits
: &[&[i32]] = &[&[]];
741 assert_eq
!(xs
.splitn(2, |x
| *x
== 5).collect
::<Vec
<_
>>(), splits
);
745 fn test_splitnator_mut() {
746 let xs
= &mut [1,2,3,4,5];
748 let splits
: &[&mut[_
]] = &[&mut [1,2,3,4,5]];
749 assert_eq
!(xs
.splitn_mut(1, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
751 let splits
: &[&mut[_
]] = &[&mut [1], &mut [3,4,5]];
752 assert_eq
!(xs
.splitn_mut(2, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
754 let splits
: &[&mut[_
]] = &[&mut [], &mut [], &mut [], &mut [4,5]];
755 assert_eq
!(xs
.splitn_mut(4, |_
| true).collect
::<Vec
<_
>>(),
758 let xs
: &mut [i32] = &mut [];
759 let splits
: &[&mut[i32]] = &[&mut []];
760 assert_eq
!(xs
.splitn_mut(2, |x
| *x
== 5).collect
::<Vec
<_
>>(),
765 fn test_rsplitator() {
766 let xs
= &[1,2,3,4,5];
768 let splits
: &[&[_
]] = &[&[5], &[3], &[1]];
769 assert_eq
!(xs
.split(|x
| *x
% 2 == 0).rev().collect
::<Vec
<_
>>(),
771 let splits
: &[&[_
]] = &[&[2,3,4,5], &[]];
772 assert_eq
!(xs
.split(|x
| *x
== 1).rev().collect
::<Vec
<_
>>(),
774 let splits
: &[&[_
]] = &[&[], &[1,2,3,4]];
775 assert_eq
!(xs
.split(|x
| *x
== 5).rev().collect
::<Vec
<_
>>(),
777 let splits
: &[&[_
]] = &[&[1,2,3,4,5]];
778 assert_eq
!(xs
.split(|x
| *x
== 10).rev().collect
::<Vec
<_
>>(),
781 let xs
: &[i32] = &[];
782 let splits
: &[&[i32]] = &[&[]];
783 assert_eq
!(xs
.split(|x
| *x
== 5).rev().collect
::<Vec
<&[i32]>>(), splits
);
787 fn test_rsplitnator() {
788 let xs
= &[1,2,3,4,5];
790 let splits
: &[&[_
]] = &[&[1,2,3,4,5]];
791 assert_eq
!(xs
.rsplitn(1, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
793 let splits
: &[&[_
]] = &[&[5], &[1,2,3]];
794 assert_eq
!(xs
.rsplitn(2, |x
| *x
% 2 == 0).collect
::<Vec
<_
>>(),
796 let splits
: &[&[_
]] = &[&[], &[], &[], &[1,2]];
797 assert_eq
!(xs
.rsplitn(4, |_
| true).collect
::<Vec
<_
>>(),
800 let xs
: &[i32] = &[];
801 let splits
: &[&[i32]] = &[&[]];
802 assert_eq
!(xs
.rsplitn(2, |x
| *x
== 5).collect
::<Vec
<&[i32]>>(), splits
);
803 assert
!(xs
.rsplitn(0, |x
| *x
% 2 == 0).next().is_none());
807 fn test_windowsator() {
810 let wins
: &[&[_
]] = &[&[1,2], &[2,3], &[3,4]];
811 assert_eq
!(v
.windows(2).collect
::<Vec
<_
>>(), wins
);
813 let wins
: &[&[_
]] = &[&[1,2,3], &[2,3,4]];
814 assert_eq
!(v
.windows(3).collect
::<Vec
<_
>>(), wins
);
815 assert
!(v
.windows(6).next().is_none());
817 let wins
: &[&[_
]] = &[&[3,4], &[2,3], &[1,2]];
818 assert_eq
!(v
.windows(2).rev().collect
::<Vec
<&[_
]>>(), wins
);
823 fn test_windowsator_0() {
825 let _it
= v
.windows(0);
829 fn test_chunksator() {
830 let v
= &[1,2,3,4,5];
832 assert_eq
!(v
.chunks(2).len(), 3);
834 let chunks
: &[&[_
]] = &[&[1,2], &[3,4], &[5]];
835 assert_eq
!(v
.chunks(2).collect
::<Vec
<_
>>(), chunks
);
836 let chunks
: &[&[_
]] = &[&[1,2,3], &[4,5]];
837 assert_eq
!(v
.chunks(3).collect
::<Vec
<_
>>(), chunks
);
838 let chunks
: &[&[_
]] = &[&[1,2,3,4,5]];
839 assert_eq
!(v
.chunks(6).collect
::<Vec
<_
>>(), chunks
);
841 let chunks
: &[&[_
]] = &[&[5], &[3,4], &[1,2]];
842 assert_eq
!(v
.chunks(2).rev().collect
::<Vec
<_
>>(), chunks
);
847 fn test_chunksator_0() {
849 let _it
= v
.chunks(0);
853 fn test_reverse_part() {
854 let mut values
= [1,2,3,4,5];
855 values
[1..4].reverse();
856 assert
!(values
== [1,4,3,2,5]);
861 macro_rules
! test_show_vec
{
862 ($x
:expr
, $x_str
:expr
) => ({
863 let (x
, x_str
) = ($x
, $x_str
);
864 assert_eq
!(format
!("{:?}", x
), x_str
);
865 assert_eq
!(format
!("{:?}", x
), x_str
);
868 let empty
= Vec
::<i32>::new();
869 test_show_vec
!(empty
, "[]");
870 test_show_vec
!(vec
![1], "[1]");
871 test_show_vec
!(vec
![1, 2, 3], "[1, 2, 3]");
872 test_show_vec
!(vec
![vec
![], vec
![1], vec
![1, 1]],
873 "[[], [1], [1, 1]]");
875 let empty_mut
: &mut [i32] = &mut[];
876 test_show_vec
!(empty_mut
, "[]");
878 test_show_vec
!(v
, "[1]");
879 let v
= &mut[1, 2, 3];
880 test_show_vec
!(v
, "[1, 2, 3]");
881 let v
: &mut[&mut[_
]] = &mut[&mut[], &mut[1], &mut[1, 1]];
882 test_show_vec
!(v
, "[[], [1], [1, 1]]");
886 fn test_vec_default() {
889 let v
: $ty
= Default
::default();
890 assert
!(v
.is_empty());
900 fn test_overflow_does_not_cause_segfault() {
909 fn test_overflow_does_not_cause_segfault_managed() {
910 let mut v
= vec
![Rc
::new(1)];
916 fn test_mut_split_at() {
917 let mut values
= [1,2,3,4,5];
919 let (left
, right
) = values
.split_at_mut(2);
921 let left
: &[_
] = left
;
922 assert
!(left
[..left
.len()] == [1, 2]);
929 let right
: &[_
] = right
;
930 assert
!(right
[..right
.len()] == [3, 4, 5]);
937 assert
!(values
== [2, 3, 5, 6, 7]);
940 #[derive(Clone, PartialEq)]
944 fn test_iter_zero_sized() {
945 let mut v
= vec
![Foo
, Foo
, Foo
];
946 assert_eq
!(v
.len(), 3);
973 let xs
: [Foo
; 3] = [Foo
, Foo
, Foo
];
983 fn test_shrink_to_fit() {
984 let mut xs
= vec
![0, 1, 2, 3];
988 assert_eq
!(xs
.capacity(), 128);
990 assert_eq
!(xs
.capacity(), 100);
991 assert_eq
!(xs
, (0..100).collect
::<Vec
<_
>>());
995 fn test_starts_with() {
996 assert
!(b
"foobar".starts_with(b
"foo"));
997 assert
!(!b
"foobar".starts_with(b
"oob"));
998 assert
!(!b
"foobar".starts_with(b
"bar"));
999 assert
!(!b
"foo".starts_with(b
"foobar"));
1000 assert
!(!b
"bar".starts_with(b
"foobar"));
1001 assert
!(b
"foobar".starts_with(b
"foobar"));
1002 let empty
: &[u8] = &[];
1003 assert
!(empty
.starts_with(empty
));
1004 assert
!(!empty
.starts_with(b
"foo"));
1005 assert
!(b
"foobar".starts_with(empty
));
1009 fn test_ends_with() {
1010 assert
!(b
"foobar".ends_with(b
"bar"));
1011 assert
!(!b
"foobar".ends_with(b
"oba"));
1012 assert
!(!b
"foobar".ends_with(b
"foo"));
1013 assert
!(!b
"foo".ends_with(b
"foobar"));
1014 assert
!(!b
"bar".ends_with(b
"foobar"));
1015 assert
!(b
"foobar".ends_with(b
"foobar"));
1016 let empty
: &[u8] = &[];
1017 assert
!(empty
.ends_with(empty
));
1018 assert
!(!empty
.ends_with(b
"foo"));
1019 assert
!(b
"foobar".ends_with(empty
));
1023 fn test_mut_splitator() {
1024 let mut xs
= [0,1,0,2,3,0,0,4,5,0];
1025 assert_eq
!(xs
.split_mut(|x
| *x
== 0).count(), 6);
1026 for slice
in xs
.split_mut(|x
| *x
== 0) {
1029 assert
!(xs
== [0,1,0,3,2,0,0,5,4,0]);
1031 let mut xs
= [0,1,0,2,3,0,0,4,5,0,6,7];
1032 for slice
in xs
.split_mut(|x
| *x
== 0).take(5) {
1035 assert
!(xs
== [0,1,0,3,2,0,0,5,4,0,6,7]);
1039 fn test_mut_splitator_rev() {
1040 let mut xs
= [1,2,0,3,4,0,0,5,6,0];
1041 for slice
in xs
.split_mut(|x
| *x
== 0).rev().take(4) {
1044 assert
!(xs
== [1,2,0,4,3,0,0,6,5,0]);
1049 let mut v
= [0,1,2];
1050 assert_eq
!(v
.get_mut(3), None
);
1051 v
.get_mut(1).map(|e
| *e
= 7);
1052 assert_eq
!(v
[1], 7);
1054 assert_eq
!(v
.get_mut(2), Some(&mut x
));
1058 fn test_mut_chunks() {
1059 let mut v
= [0, 1, 2, 3, 4, 5, 6];
1060 assert_eq
!(v
.chunks_mut(2).len(), 4);
1061 for (i
, chunk
) in v
.chunks_mut(3).enumerate() {
1066 let result
= [0, 0, 0, 1, 1, 1, 2];
1067 assert
!(v
== result
);
1071 fn test_mut_chunks_rev() {
1072 let mut v
= [0, 1, 2, 3, 4, 5, 6];
1073 for (i
, chunk
) in v
.chunks_mut(3).rev().enumerate() {
1078 let result
= [2, 2, 2, 1, 1, 1, 0];
1079 assert
!(v
== result
);
1084 fn test_mut_chunks_0() {
1085 let mut v
= [1, 2, 3, 4];
1086 let _it
= v
.chunks_mut(0);
1090 fn test_mut_last() {
1091 let mut x
= [1, 2, 3, 4, 5];
1092 let h
= x
.last_mut();
1093 assert_eq
!(*h
.unwrap(), 5);
1095 let y
: &mut [i32] = &mut [];
1096 assert
!(y
.last_mut().is_none());
1101 let xs
: Box
<_
> = box [1, 2, 3];
1102 let ys
= xs
.to_vec();
1103 assert_eq
!(ys
, [1, 2, 3]);
1107 fn test_box_slice_clone() {
1108 let data
= vec
![vec
![0, 1], vec
![0], vec
![1]];
1109 let data2
= data
.clone().into_boxed_slice().clone().to_vec();
1111 assert_eq
!(data
, data2
);
1115 fn test_box_slice_clone_panics() {
1117 use std
::sync
::atomic
::{AtomicUsize, Ordering}
;
1118 use std
::thread
::spawn
;
1121 count
: Arc
<AtomicUsize
>,
1125 impl Drop
for Canary
{
1126 fn drop(&mut self) {
1127 self.count
.fetch_add(1, Ordering
::SeqCst
);
1131 impl Clone
for Canary
{
1132 fn clone(&self) -> Self {
1133 if self.panics { panic!() }
1136 count
: self.count
.clone(),
1142 let drop_count
= Arc
::new(AtomicUsize
::new(0));
1143 let canary
= Canary { count: drop_count.clone(), panics: false }
;
1144 let panic
= Canary { count: drop_count.clone(), panics: true }
;
1147 // When xs is dropped, +5.
1148 let xs
= vec
![canary
.clone(), canary
.clone(), canary
.clone(),
1149 panic
, canary
].into_boxed_slice();
1151 // When panic is cloned, +3.
1153 }).join().unwrap_err();
1156 assert_eq
!(drop_count
.load(Ordering
::SeqCst
), 8);
1160 fn test_copy_from_slice() {
1161 let src
= [0, 1, 2, 3, 4, 5];
1162 let mut dst
= [0; 6];
1163 dst
.copy_from_slice(&src
);
1164 assert_eq
!(src
, dst
)
1168 #[should_panic(expected = "destination and source slices have different lengths")]
1169 fn test_copy_from_slice_dst_longer() {
1170 let src
= [0, 1, 2, 3];
1171 let mut dst
= [0; 5];
1172 dst
.copy_from_slice(&src
);
1176 #[should_panic(expected = "destination and source slices have different lengths")]
1177 fn test_copy_from_slice_dst_shorter() {
1178 let src
= [0, 1, 2, 3];
1179 let mut dst
= [0; 3];
1180 dst
.copy_from_slice(&src
);
1184 use std
::{mem, ptr}
;
1185 use std
::__rand
::{Rng, thread_rng}
;
1187 use test
::{Bencher, black_box}
;
1190 fn iterator(b
: &mut Bencher
) {
1191 // peculiar numbers to stop LLVM from optimising the summation
1193 let v
: Vec
<_
> = (0..100).map(|i
| i ^
(i
<< 1) ^
(i
>> 1)).collect();
1200 // sum == 11806, to stop dead code elimination.
1201 if sum
== 0 {panic!()}
1206 fn mut_iterator(b
: &mut Bencher
) {
1207 let mut v
= vec
![0; 100];
1219 fn concat(b
: &mut Bencher
) {
1220 let xss
: Vec
<Vec
<i32>> =
1221 (0..100).map(|i
| (0..i
).collect()).collect();
1228 fn join(b
: &mut Bencher
) {
1229 let xss
: Vec
<Vec
<i32>> =
1230 (0..100).map(|i
| (0..i
).collect()).collect();
1237 fn push(b
: &mut Bencher
) {
1238 let mut vec
= Vec
::<i32>::new();
1246 fn starts_with_same_vector(b
: &mut Bencher
) {
1247 let vec
: Vec
<_
> = (0..100).collect();
1249 vec
.starts_with(&vec
)
1254 fn starts_with_single_element(b
: &mut Bencher
) {
1255 let vec
: Vec
<_
> = vec
![0];
1257 vec
.starts_with(&vec
)
1262 fn starts_with_diff_one_element_at_end(b
: &mut Bencher
) {
1263 let vec
: Vec
<_
> = (0..100).collect();
1264 let mut match_vec
: Vec
<_
> = (0..99).collect();
1267 vec
.starts_with(&match_vec
)
1272 fn ends_with_same_vector(b
: &mut Bencher
) {
1273 let vec
: Vec
<_
> = (0..100).collect();
1280 fn ends_with_single_element(b
: &mut Bencher
) {
1281 let vec
: Vec
<_
> = vec
![0];
1288 fn ends_with_diff_one_element_at_beginning(b
: &mut Bencher
) {
1289 let vec
: Vec
<_
> = (0..100).collect();
1290 let mut match_vec
: Vec
<_
> = (0..100).collect();
1293 vec
.starts_with(&match_vec
)
1298 fn contains_last_element(b
: &mut Bencher
) {
1299 let vec
: Vec
<_
> = (0..100).collect();
1306 fn zero_1kb_from_elem(b
: &mut Bencher
) {
1313 fn zero_1kb_set_memory(b
: &mut Bencher
) {
1315 let mut v
= Vec
::<u8>::with_capacity(1024);
1317 let vp
= v
.as_mut_ptr();
1318 ptr
::write_bytes(vp
, 0, 1024);
1326 fn zero_1kb_loop_set(b
: &mut Bencher
) {
1328 let mut v
= Vec
::<u8>::with_capacity(1024);
1339 fn zero_1kb_mut_iter(b
: &mut Bencher
) {
1341 let mut v
= Vec
::<u8>::with_capacity(1024);
1353 fn random_inserts(b
: &mut Bencher
) {
1354 let mut rng
= thread_rng();
1356 let mut v
= vec
![(0, 0); 30];
1359 v
.insert(rng
.gen
::<usize>() % (l
+ 1),
1365 fn random_removes(b
: &mut Bencher
) {
1366 let mut rng
= thread_rng();
1368 let mut v
= vec
![(0, 0); 130];
1371 v
.remove(rng
.gen
::<usize>() % l
);
1377 fn sort_random_small(b
: &mut Bencher
) {
1378 let mut rng
= thread_rng();
1380 let mut v
: Vec
<_
> = rng
.gen_iter
::<u64>().take(5).collect();
1383 b
.bytes
= 5 * mem
::size_of
::<u64>() as u64;
1387 fn sort_random_medium(b
: &mut Bencher
) {
1388 let mut rng
= thread_rng();
1390 let mut v
: Vec
<_
> = rng
.gen_iter
::<u64>().take(100).collect();
1393 b
.bytes
= 100 * mem
::size_of
::<u64>() as u64;
1397 fn sort_random_large(b
: &mut Bencher
) {
1398 let mut rng
= thread_rng();
1400 let mut v
: Vec
<_
> = rng
.gen_iter
::<u64>().take(10000).collect();
1403 b
.bytes
= 10000 * mem
::size_of
::<u64>() as u64;
1407 fn sort_sorted(b
: &mut Bencher
) {
1408 let mut v
: Vec
<_
> = (0..10000).collect();
1412 b
.bytes
= (v
.len() * mem
::size_of_val(&v
[0])) as u64;
1415 type BigSortable
= (u64, u64, u64, u64);
1418 fn sort_big_random_small(b
: &mut Bencher
) {
1419 let mut rng
= thread_rng();
1421 let mut v
= rng
.gen_iter
::<BigSortable
>().take(5)
1422 .collect
::<Vec
<BigSortable
>>();
1425 b
.bytes
= 5 * mem
::size_of
::<BigSortable
>() as u64;
1429 fn sort_big_random_medium(b
: &mut Bencher
) {
1430 let mut rng
= thread_rng();
1432 let mut v
= rng
.gen_iter
::<BigSortable
>().take(100)
1433 .collect
::<Vec
<BigSortable
>>();
1436 b
.bytes
= 100 * mem
::size_of
::<BigSortable
>() as u64;
1440 fn sort_big_random_large(b
: &mut Bencher
) {
1441 let mut rng
= thread_rng();
1443 let mut v
= rng
.gen_iter
::<BigSortable
>().take(10000)
1444 .collect
::<Vec
<BigSortable
>>();
1447 b
.bytes
= 10000 * mem
::size_of
::<BigSortable
>() as u64;
1451 fn sort_big_sorted(b
: &mut Bencher
) {
1452 let mut v
: Vec
<BigSortable
> = (0..10000).map(|i
| (i
, i
, i
, i
)).collect();
1456 b
.bytes
= (v
.len() * mem
::size_of_val(&v
[0])) as u64;