2015-03-10 23:58:16 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-09-26 11:26:49 -05:00
|
|
|
use std::ascii::AsciiExt;
|
2016-02-03 06:57:25 -06:00
|
|
|
use std::borrow::Cow;
|
2015-03-10 23:58:16 -05:00
|
|
|
use std::mem::size_of;
|
2016-12-22 11:15:13 -06:00
|
|
|
use std::panic;
|
2016-08-16 19:45:07 -05:00
|
|
|
use std::vec::{Drain, IntoIter};
|
2015-03-10 23:58:16 -05:00
|
|
|
|
|
|
|
struct DropCounter<'a> {
|
2016-05-22 13:27:13 -05:00
|
|
|
count: &'a mut u32,
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for DropCounter<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
*self.count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_small_vec_struct() {
|
|
|
|
assert!(size_of::<Vec<u8>>() == size_of::<usize>() * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_drop() {
|
|
|
|
struct TwoVec<T> {
|
|
|
|
x: Vec<T>,
|
2016-05-22 13:27:13 -05:00
|
|
|
y: Vec<T>,
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let (mut count_x, mut count_y) = (0, 0);
|
|
|
|
{
|
|
|
|
let mut tv = TwoVec {
|
|
|
|
x: Vec::new(),
|
2016-05-22 13:27:13 -05:00
|
|
|
y: Vec::new(),
|
2015-03-10 23:58:16 -05:00
|
|
|
};
|
2016-05-22 13:27:13 -05:00
|
|
|
tv.x.push(DropCounter { count: &mut count_x });
|
|
|
|
tv.y.push(DropCounter { count: &mut count_y });
|
2015-03-10 23:58:16 -05:00
|
|
|
|
|
|
|
// If Vec had a drop flag, here is where it would be zeroed.
|
|
|
|
// Instead, it should rely on its internal state to prevent
|
|
|
|
// doing anything significant when dropped multiple times.
|
|
|
|
drop(tv.x);
|
|
|
|
|
|
|
|
// Here tv goes out of scope, tv.y should be dropped, but not tv.x.
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(count_x, 1);
|
|
|
|
assert_eq!(count_y, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reserve() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
assert_eq!(v.capacity(), 0);
|
|
|
|
|
|
|
|
v.reserve(2);
|
|
|
|
assert!(v.capacity() >= 2);
|
|
|
|
|
|
|
|
for i in 0..16 {
|
|
|
|
v.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(v.capacity() >= 16);
|
|
|
|
v.reserve(16);
|
|
|
|
assert!(v.capacity() >= 32);
|
|
|
|
|
|
|
|
v.push(16);
|
|
|
|
|
|
|
|
v.reserve(16);
|
|
|
|
assert!(v.capacity() >= 33)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
let mut w = Vec::new();
|
|
|
|
|
|
|
|
v.extend(0..3);
|
2016-05-22 13:27:13 -05:00
|
|
|
for i in 0..3 {
|
|
|
|
w.push(i)
|
|
|
|
}
|
2015-03-10 23:58:16 -05:00
|
|
|
|
|
|
|
assert_eq!(v, w);
|
|
|
|
|
|
|
|
v.extend(3..10);
|
2016-05-22 13:27:13 -05:00
|
|
|
for i in 3..10 {
|
|
|
|
w.push(i)
|
|
|
|
}
|
2015-03-10 23:58:16 -05:00
|
|
|
|
|
|
|
assert_eq!(v, w);
|
2016-04-08 22:36:54 -05:00
|
|
|
|
|
|
|
v.extend(w.clone()); // specializes to `append`
|
|
|
|
assert!(v.iter().eq(w.iter().chain(w.iter())));
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
2015-06-03 05:38:42 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extend_ref() {
|
|
|
|
let mut v = vec![1, 2];
|
|
|
|
v.extend(&[3, 4, 5]);
|
|
|
|
|
|
|
|
assert_eq!(v.len(), 5);
|
|
|
|
assert_eq!(v, [1, 2, 3, 4, 5]);
|
|
|
|
|
|
|
|
let w = vec![6, 7];
|
|
|
|
v.extend(&w);
|
|
|
|
|
|
|
|
assert_eq!(v.len(), 7);
|
|
|
|
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:58:16 -05:00
|
|
|
#[test]
|
|
|
|
fn test_slice_from_mut() {
|
|
|
|
let mut values = vec![1, 2, 3, 4, 5];
|
|
|
|
{
|
2016-05-22 13:27:13 -05:00
|
|
|
let slice = &mut values[2..];
|
2015-03-10 23:58:16 -05:00
|
|
|
assert!(slice == [3, 4, 5]);
|
|
|
|
for p in slice {
|
|
|
|
*p += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(values == [1, 2, 5, 6, 7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slice_to_mut() {
|
|
|
|
let mut values = vec![1, 2, 3, 4, 5];
|
|
|
|
{
|
2016-05-22 13:27:13 -05:00
|
|
|
let slice = &mut values[..2];
|
2015-03-10 23:58:16 -05:00
|
|
|
assert!(slice == [1, 2]);
|
|
|
|
for p in slice {
|
|
|
|
*p += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(values == [2, 3, 3, 4, 5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_at_mut() {
|
|
|
|
let mut values = vec![1, 2, 3, 4, 5];
|
|
|
|
{
|
|
|
|
let (left, right) = values.split_at_mut(2);
|
|
|
|
{
|
|
|
|
let left: &[_] = left;
|
|
|
|
assert!(&left[..left.len()] == &[1, 2]);
|
|
|
|
}
|
|
|
|
for p in left {
|
|
|
|
*p += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let right: &[_] = right;
|
|
|
|
assert!(&right[..right.len()] == &[3, 4, 5]);
|
|
|
|
}
|
|
|
|
for p in right {
|
|
|
|
*p += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(values, [2, 3, 5, 6, 7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone() {
|
|
|
|
let v: Vec<i32> = vec![];
|
2016-05-22 13:27:13 -05:00
|
|
|
let w = vec![1, 2, 3];
|
2015-03-10 23:58:16 -05:00
|
|
|
|
|
|
|
assert_eq!(v, v.clone());
|
|
|
|
|
|
|
|
let z = w.clone();
|
|
|
|
assert_eq!(w, z);
|
|
|
|
// they should be disjoint in memory.
|
|
|
|
assert!(w.as_ptr() != z.as_ptr())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone_from() {
|
2016-05-22 13:27:13 -05:00
|
|
|
let mut v = vec![];
|
|
|
|
let three: Vec<Box<_>> = vec![box 1, box 2, box 3];
|
|
|
|
let two: Vec<Box<_>> = vec![box 4, box 5];
|
2015-03-10 23:58:16 -05:00
|
|
|
// zero, long
|
|
|
|
v.clone_from(&three);
|
|
|
|
assert_eq!(v, three);
|
|
|
|
|
|
|
|
// equal
|
|
|
|
v.clone_from(&three);
|
|
|
|
assert_eq!(v, three);
|
|
|
|
|
|
|
|
// long, short
|
|
|
|
v.clone_from(&two);
|
|
|
|
assert_eq!(v, two);
|
|
|
|
|
|
|
|
// short, long
|
|
|
|
v.clone_from(&three);
|
|
|
|
assert_eq!(v, three)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_retain() {
|
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
|
|
|
vec.retain(|&x| x % 2 == 0);
|
|
|
|
assert_eq!(vec, [2, 4]);
|
|
|
|
}
|
|
|
|
|
2016-09-26 11:17:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_dedup() {
|
|
|
|
fn case(a: Vec<i32>, b: Vec<i32>) {
|
|
|
|
let mut v = a;
|
|
|
|
v.dedup();
|
|
|
|
assert_eq!(v, b);
|
|
|
|
}
|
|
|
|
case(vec![], vec![]);
|
|
|
|
case(vec![1], vec![1]);
|
|
|
|
case(vec![1, 1], vec![1]);
|
|
|
|
case(vec![1, 2, 3], vec![1, 2, 3]);
|
|
|
|
case(vec![1, 1, 2, 3], vec![1, 2, 3]);
|
|
|
|
case(vec![1, 2, 2, 3], vec![1, 2, 3]);
|
|
|
|
case(vec![1, 2, 3, 3], vec![1, 2, 3]);
|
|
|
|
case(vec![1, 1, 2, 2, 2, 3, 3], vec![1, 2, 3]);
|
|
|
|
}
|
|
|
|
|
2016-09-26 11:26:49 -05:00
|
|
|
#[test]
|
|
|
|
fn test_dedup_by_key() {
|
|
|
|
fn case(a: Vec<i32>, b: Vec<i32>) {
|
|
|
|
let mut v = a;
|
|
|
|
v.dedup_by_key(|i| *i / 10);
|
|
|
|
assert_eq!(v, b);
|
|
|
|
}
|
|
|
|
case(vec![], vec![]);
|
|
|
|
case(vec![10], vec![10]);
|
|
|
|
case(vec![10, 11], vec![10]);
|
|
|
|
case(vec![10, 20, 30], vec![10, 20, 30]);
|
|
|
|
case(vec![10, 11, 20, 30], vec![10, 20, 30]);
|
|
|
|
case(vec![10, 20, 21, 30], vec![10, 20, 30]);
|
|
|
|
case(vec![10, 20, 30, 31], vec![10, 20, 30]);
|
|
|
|
case(vec![10, 11, 20, 21, 22, 30, 31], vec![10, 20, 30]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dedup_by() {
|
|
|
|
let mut vec = vec!["foo", "bar", "Bar", "baz", "bar"];
|
|
|
|
vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
|
|
|
|
|
|
|
|
assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
|
|
|
|
}
|
|
|
|
|
2016-09-26 11:17:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_dedup_unique() {
|
|
|
|
let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
|
|
|
|
v0.dedup();
|
|
|
|
let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
|
|
|
|
v1.dedup();
|
|
|
|
let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
|
|
|
|
v2.dedup();
|
|
|
|
// If the boxed pointers were leaked or otherwise misused, valgrind
|
|
|
|
// and/or rt should raise errors.
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:58:16 -05:00
|
|
|
#[test]
|
|
|
|
fn zero_sized_values() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
assert_eq!(v.len(), 0);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.len(), 1);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.len(), 2);
|
|
|
|
assert_eq!(v.pop(), Some(()));
|
|
|
|
assert_eq!(v.pop(), Some(()));
|
|
|
|
assert_eq!(v.pop(), None);
|
|
|
|
|
|
|
|
assert_eq!(v.iter().count(), 0);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.iter().count(), 1);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.iter().count(), 2);
|
|
|
|
|
|
|
|
for &() in &v {}
|
|
|
|
|
|
|
|
assert_eq!(v.iter_mut().count(), 2);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.iter_mut().count(), 3);
|
|
|
|
v.push(());
|
|
|
|
assert_eq!(v.iter_mut().count(), 4);
|
|
|
|
|
|
|
|
for &mut () in &mut v {}
|
2016-05-22 13:27:13 -05:00
|
|
|
unsafe {
|
|
|
|
v.set_len(0);
|
|
|
|
}
|
2015-03-10 23:58:16 -05:00
|
|
|
assert_eq!(v.iter_mut().count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_partition() {
|
2016-05-22 13:27:13 -05:00
|
|
|
assert_eq!(vec![].into_iter().partition(|x: &i32| *x < 3),
|
|
|
|
(vec![], vec![]));
|
|
|
|
assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 4),
|
|
|
|
(vec![1, 2, 3], vec![]));
|
|
|
|
assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 2),
|
|
|
|
(vec![1], vec![2, 3]));
|
|
|
|
assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 0),
|
|
|
|
(vec![], vec![1, 2, 3]));
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zip_unzip() {
|
|
|
|
let z1 = vec![(1, 4), (2, 5), (3, 6)];
|
|
|
|
|
|
|
|
let (left, right): (Vec<_>, Vec<_>) = z1.iter().cloned().unzip();
|
|
|
|
|
|
|
|
assert_eq!((1, 4), (left[0], right[0]));
|
|
|
|
assert_eq!((2, 5), (left[1], right[1]));
|
|
|
|
assert_eq!((3, 6), (left[2], right[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec_truncate_drop() {
|
2016-10-14 07:07:18 -05:00
|
|
|
static mut DROPS: u32 = 0;
|
2015-03-10 23:58:16 -05:00
|
|
|
struct Elem(i32);
|
|
|
|
impl Drop for Elem {
|
|
|
|
fn drop(&mut self) {
|
2016-05-22 13:27:13 -05:00
|
|
|
unsafe {
|
2016-10-14 07:07:18 -05:00
|
|
|
DROPS += 1;
|
2016-05-22 13:27:13 -05:00
|
|
|
}
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
|
2016-10-14 07:07:18 -05:00
|
|
|
assert_eq!(unsafe { DROPS }, 0);
|
2015-03-10 23:58:16 -05:00
|
|
|
v.truncate(3);
|
2016-10-14 07:07:18 -05:00
|
|
|
assert_eq!(unsafe { DROPS }, 2);
|
2015-03-10 23:58:16 -05:00
|
|
|
v.truncate(0);
|
2016-10-14 07:07:18 -05:00
|
|
|
assert_eq!(unsafe { DROPS }, 5);
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_vec_truncate_fail() {
|
|
|
|
struct BadElem(i32);
|
|
|
|
impl Drop for BadElem {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let BadElem(ref mut x) = *self;
|
|
|
|
if *x == 0xbadbeef {
|
|
|
|
panic!("BadElem panic: 0xbadbeef")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut v = vec![BadElem(1), BadElem(2), BadElem(0xbadbeef), BadElem(4)];
|
|
|
|
v.truncate(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
|
|
|
let vec = vec![1, 2, 3];
|
|
|
|
assert!(vec[1] == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_index_out_of_bounds() {
|
|
|
|
let vec = vec![1, 2, 3];
|
|
|
|
let _ = vec[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_slice_out_of_bounds_1() {
|
|
|
|
let x = vec![1, 2, 3, 4, 5];
|
2015-08-11 19:27:05 -05:00
|
|
|
&x[!0..];
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_slice_out_of_bounds_2() {
|
|
|
|
let x = vec![1, 2, 3, 4, 5];
|
|
|
|
&x[..6];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_slice_out_of_bounds_3() {
|
|
|
|
let x = vec![1, 2, 3, 4, 5];
|
2015-08-11 19:27:05 -05:00
|
|
|
&x[!0..4];
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_slice_out_of_bounds_4() {
|
|
|
|
let x = vec![1, 2, 3, 4, 5];
|
|
|
|
&x[1..6];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_slice_out_of_bounds_5() {
|
|
|
|
let x = vec![1, 2, 3, 4, 5];
|
|
|
|
&x[3..2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_swap_remove_empty() {
|
2016-05-22 13:27:13 -05:00
|
|
|
let mut vec = Vec::<i32>::new();
|
2015-03-10 23:58:16 -05:00
|
|
|
vec.swap_remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_items() {
|
|
|
|
let vec = vec![1, 2, 3];
|
|
|
|
let mut vec2 = vec![];
|
|
|
|
for i in vec {
|
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec2, [1, 2, 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_items_reverse() {
|
|
|
|
let vec = vec![1, 2, 3];
|
|
|
|
let mut vec2 = vec![];
|
|
|
|
for i in vec.into_iter().rev() {
|
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec2, [3, 2, 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_items_zero_sized() {
|
|
|
|
let vec = vec![(), (), ()];
|
|
|
|
let mut vec2 = vec![];
|
|
|
|
for i in vec {
|
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec2, [(), (), ()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_drain_items() {
|
|
|
|
let mut vec = vec![1, 2, 3];
|
|
|
|
let mut vec2 = vec![];
|
2015-04-27 12:37:13 -05:00
|
|
|
for i in vec.drain(..) {
|
2015-03-10 23:58:16 -05:00
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec, []);
|
2016-05-22 13:27:13 -05:00
|
|
|
assert_eq!(vec2, [1, 2, 3]);
|
2015-03-10 23:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_drain_items_reverse() {
|
|
|
|
let mut vec = vec![1, 2, 3];
|
|
|
|
let mut vec2 = vec![];
|
2015-04-27 12:37:13 -05:00
|
|
|
for i in vec.drain(..).rev() {
|
2015-03-10 23:58:16 -05:00
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec, []);
|
|
|
|
assert_eq!(vec2, [3, 2, 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_drain_items_zero_sized() {
|
|
|
|
let mut vec = vec![(), (), ()];
|
|
|
|
let mut vec2 = vec![];
|
2015-04-27 12:37:13 -05:00
|
|
|
for i in vec.drain(..) {
|
2015-03-10 23:58:16 -05:00
|
|
|
vec2.push(i);
|
|
|
|
}
|
|
|
|
assert_eq!(vec, []);
|
|
|
|
assert_eq!(vec2, [(), (), ()]);
|
|
|
|
}
|
|
|
|
|
2015-04-27 12:37:13 -05:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_drain_out_of_bounds() {
|
|
|
|
let mut v = vec![1, 2, 3, 4, 5];
|
|
|
|
v.drain(5..6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_drain_range() {
|
|
|
|
let mut v = vec![1, 2, 3, 4, 5];
|
|
|
|
for _ in v.drain(4..) {
|
|
|
|
}
|
|
|
|
assert_eq!(v, &[1, 2, 3, 4]);
|
|
|
|
|
|
|
|
let mut v: Vec<_> = (1..6).map(|x| x.to_string()).collect();
|
|
|
|
for _ in v.drain(1..4) {
|
|
|
|
}
|
|
|
|
assert_eq!(v, &[1.to_string(), 5.to_string()]);
|
|
|
|
|
|
|
|
let mut v: Vec<_> = (1..6).map(|x| x.to_string()).collect();
|
|
|
|
for _ in v.drain(1..4).rev() {
|
|
|
|
}
|
|
|
|
assert_eq!(v, &[1.to_string(), 5.to_string()]);
|
|
|
|
|
|
|
|
let mut v: Vec<_> = vec![(); 5];
|
|
|
|
for _ in v.drain(1..4).rev() {
|
|
|
|
}
|
|
|
|
assert_eq!(v, &[(), ()]);
|
|
|
|
}
|
|
|
|
|
2015-03-10 23:58:16 -05:00
|
|
|
#[test]
|
|
|
|
fn test_into_boxed_slice() {
|
|
|
|
let xs = vec![1, 2, 3];
|
|
|
|
let ys = xs.into_boxed_slice();
|
|
|
|
assert_eq!(&*ys, [1, 2, 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_append() {
|
|
|
|
let mut vec = vec![1, 2, 3];
|
|
|
|
let mut vec2 = vec![4, 5, 6];
|
|
|
|
vec.append(&mut vec2);
|
|
|
|
assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
|
|
|
|
assert_eq!(vec2, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_off() {
|
|
|
|
let mut vec = vec![1, 2, 3, 4, 5, 6];
|
|
|
|
let vec2 = vec.split_off(4);
|
|
|
|
assert_eq!(vec, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(vec2, [5, 6]);
|
|
|
|
}
|
|
|
|
|
2016-08-06 23:30:17 -05:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter_as_slice() {
|
|
|
|
let vec = vec!['a', 'b', 'c'];
|
|
|
|
let mut into_iter = vec.into_iter();
|
|
|
|
assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
|
|
|
|
let _ = into_iter.next().unwrap();
|
|
|
|
assert_eq!(into_iter.as_slice(), &['b', 'c']);
|
|
|
|
let _ = into_iter.next().unwrap();
|
|
|
|
let _ = into_iter.next().unwrap();
|
|
|
|
assert_eq!(into_iter.as_slice(), &[]);
|
|
|
|
}
|
|
|
|
|
2016-08-07 09:08:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter_as_mut_slice() {
|
|
|
|
let vec = vec!['a', 'b', 'c'];
|
|
|
|
let mut into_iter = vec.into_iter();
|
|
|
|
assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
|
|
|
|
into_iter.as_mut_slice()[0] = 'x';
|
|
|
|
into_iter.as_mut_slice()[1] = 'y';
|
|
|
|
assert_eq!(into_iter.next().unwrap(), 'x');
|
|
|
|
assert_eq!(into_iter.as_slice(), &['y', 'c']);
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:45:12 -05:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter_debug() {
|
|
|
|
let vec = vec!['a', 'b', 'c'];
|
|
|
|
let into_iter = vec.into_iter();
|
|
|
|
let debug = format!("{:?}", into_iter);
|
|
|
|
assert_eq!(debug, "IntoIter(['a', 'b', 'c'])");
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:01:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter_count() {
|
|
|
|
assert_eq!(vec![1, 2, 3].into_iter().count(), 3);
|
|
|
|
}
|
|
|
|
|
2016-02-16 09:00:38 -06:00
|
|
|
#[test]
|
|
|
|
fn test_into_iter_clone() {
|
2016-05-22 13:27:13 -05:00
|
|
|
fn iter_equal<I: Iterator<Item = i32>>(it: I, slice: &[i32]) {
|
2016-02-16 09:00:38 -06:00
|
|
|
let v: Vec<i32> = it.collect();
|
|
|
|
assert_eq!(&v[..], slice);
|
|
|
|
}
|
|
|
|
let mut it = vec![1, 2, 3].into_iter();
|
|
|
|
iter_equal(it.clone(), &[1, 2, 3]);
|
|
|
|
assert_eq!(it.next(), Some(1));
|
|
|
|
let mut it = it.rev();
|
|
|
|
iter_equal(it.clone(), &[3, 2]);
|
|
|
|
assert_eq!(it.next(), Some(3));
|
|
|
|
iter_equal(it.clone(), &[2]);
|
|
|
|
assert_eq!(it.next(), Some(2));
|
|
|
|
iter_equal(it.clone(), &[]);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2016-02-03 06:57:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_cow_from() {
|
|
|
|
let borrowed: &[_] = &["borrowed", "(slice)"];
|
|
|
|
let owned = vec!["owned", "(vec)"];
|
|
|
|
match (Cow::from(owned.clone()), Cow::from(borrowed)) {
|
|
|
|
(Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
|
|
|
|
_ => panic!("invalid `Cow::from`"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implement `From<Cow<str>> for String` and `From<Cow<[T]>> for Vec<T>`.
Motivation: the `selectors` crate is generic over a string type,
in order to support all of `String`, `string_cache::Atom`, and
`gecko_string_cache::Atom`. Multiple trait bounds are used
for the various operations done with these strings.
One of these operations is creating a string (as efficiently as possible,
re-using an existing memory allocation if possible) from `Cow<str>`.
The `std::convert::From` trait seems natural for this, but
the relevant implementation was missing before this PR.
To work around this I’ve added a `FromCowStr` trait in `selectors`,
but with trait coherence that means one of `selectors` or `string_cache`
needs to depend on the other to implement this trait.
Using a trait from `std` would solve this.
The `Vec<T>` implementation is just added for consistency.
I also tried a more general
`impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O`,
but (the compiler thinks?) it conflicts with `From<T> for T` the impl
(after moving all of `collections::borrow` into `core::borrow`
to work around trait coherence).
2016-10-21 09:51:59 -05:00
|
|
|
#[test]
|
|
|
|
fn test_from_cow() {
|
|
|
|
let borrowed: &[_] = &["borrowed", "(slice)"];
|
|
|
|
let owned = vec!["owned", "(vec)"];
|
|
|
|
assert_eq!(Vec::from(Cow::Borrowed(borrowed)), vec!["borrowed", "(slice)"]);
|
|
|
|
assert_eq!(Vec::from(Cow::Owned(owned)), vec!["owned", "(vec)"]);
|
|
|
|
}
|
|
|
|
|
2016-07-21 03:03:40 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn assert_covariance() {
|
2016-10-16 05:18:22 -05:00
|
|
|
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> {
|
|
|
|
d
|
|
|
|
}
|
|
|
|
fn into_iter<'new>(i: IntoIter<&'static str>) -> IntoIter<&'new str> {
|
|
|
|
i
|
|
|
|
}
|
2016-07-21 03:03:40 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 11:15:13 -06:00
|
|
|
#[test]
|
|
|
|
fn test_placement() {
|
|
|
|
let mut vec = vec![1];
|
|
|
|
assert_eq!(vec.place_back() <- 2, &2);
|
|
|
|
assert_eq!(vec.len(), 2);
|
|
|
|
assert_eq!(vec.place_back() <- 3, &3);
|
|
|
|
assert_eq!(vec.len(), 3);
|
|
|
|
assert_eq!(&vec, &[1, 2, 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_placement_panic() {
|
|
|
|
let mut vec = vec![1, 2, 3];
|
|
|
|
fn mkpanic() -> usize { panic!() }
|
|
|
|
let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| { vec.place_back() <- mkpanic(); }));
|
|
|
|
assert_eq!(vec.len(), 3);
|
|
|
|
}
|