extract libcollections tests into libcollectionstest
This commit is contained in:
parent
3ff84fc5fd
commit
6453fcd4cc
34 changed files with 9192 additions and 9098 deletions
|
@ -53,6 +53,7 @@ PKG_FILES := \
|
|||
driver \
|
||||
etc \
|
||||
$(foreach crate,$(CRATES),lib$(crate)) \
|
||||
libcollectionstest \
|
||||
libcoretest \
|
||||
libbacktrace \
|
||||
rt \
|
||||
|
|
|
@ -19,7 +19,11 @@
|
|||
DEPS_coretest :=
|
||||
$(eval $(call RUST_CRATE,coretest))
|
||||
|
||||
TEST_TARGET_CRATES = $(filter-out core unicode,$(TARGET_CRATES)) coretest
|
||||
DEPS_collectionstest :=
|
||||
$(eval $(call RUST_CRATE,collectionstest))
|
||||
|
||||
TEST_TARGET_CRATES = $(filter-out collections core unicode,$(TARGET_CRATES)) \
|
||||
collectionstest coretest
|
||||
TEST_DOC_CRATES = $(DOC_CRATES)
|
||||
TEST_HOST_CRATES = $(filter-out rustc_typeck rustc_borrowck rustc_resolve rustc_trans rustc_lint,\
|
||||
$(HOST_CRATES))
|
||||
|
|
|
@ -693,218 +693,3 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
|
||||
use super::BinaryHeap;
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = [9, 5, 3];
|
||||
let heap = BinaryHeap::from_vec(data);
|
||||
let mut i = 0;
|
||||
for el in &heap {
|
||||
assert_eq!(*el, iterout[i]);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_reverse() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![3, 5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.iter().rev().cloned().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![9, 5, 3];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.into_iter().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_size_hint() {
|
||||
let data = vec![5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let mut it = pq.into_iter();
|
||||
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(it.next(), Some(9));
|
||||
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next(), Some(5));
|
||||
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_reverse() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![3, 5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.into_iter().rev().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_peek_and_pop() {
|
||||
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
|
||||
let mut sorted = data.clone();
|
||||
sorted.sort();
|
||||
let mut heap = BinaryHeap::from_vec(data);
|
||||
while !heap.is_empty() {
|
||||
assert_eq!(heap.peek().unwrap(), sorted.last().unwrap());
|
||||
assert_eq!(heap.pop().unwrap(), sorted.pop().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![2, 4, 9]);
|
||||
assert_eq!(heap.len(), 3);
|
||||
assert!(*heap.peek().unwrap() == 9);
|
||||
heap.push(11);
|
||||
assert_eq!(heap.len(), 4);
|
||||
assert!(*heap.peek().unwrap() == 11);
|
||||
heap.push(5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert!(*heap.peek().unwrap() == 11);
|
||||
heap.push(27);
|
||||
assert_eq!(heap.len(), 6);
|
||||
assert!(*heap.peek().unwrap() == 27);
|
||||
heap.push(3);
|
||||
assert_eq!(heap.len(), 7);
|
||||
assert!(*heap.peek().unwrap() == 27);
|
||||
heap.push(103);
|
||||
assert_eq!(heap.len(), 8);
|
||||
assert!(*heap.peek().unwrap() == 103);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_unique() {
|
||||
let mut heap = BinaryHeap::<Box<_>>::from_vec(vec![box 2, box 4, box 9]);
|
||||
assert_eq!(heap.len(), 3);
|
||||
assert!(*heap.peek().unwrap() == box 9);
|
||||
heap.push(box 11);
|
||||
assert_eq!(heap.len(), 4);
|
||||
assert!(*heap.peek().unwrap() == box 11);
|
||||
heap.push(box 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert!(*heap.peek().unwrap() == box 11);
|
||||
heap.push(box 27);
|
||||
assert_eq!(heap.len(), 6);
|
||||
assert!(*heap.peek().unwrap() == box 27);
|
||||
heap.push(box 3);
|
||||
assert_eq!(heap.len(), 7);
|
||||
assert!(*heap.peek().unwrap() == box 27);
|
||||
heap.push(box 103);
|
||||
assert_eq!(heap.len(), 8);
|
||||
assert!(*heap.peek().unwrap() == box 103);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_pop() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(6), 6);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(0), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(4), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(1), 4);
|
||||
assert_eq!(heap.len(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(6).unwrap(), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(0).unwrap(), 6);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(4).unwrap(), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(1).unwrap(), 4);
|
||||
assert_eq!(heap.len(), 5);
|
||||
}
|
||||
|
||||
fn check_to_vec(mut data: Vec<i32>) {
|
||||
let heap = BinaryHeap::from_vec(data.clone());
|
||||
let mut v = heap.clone().into_vec();
|
||||
v.sort();
|
||||
data.sort();
|
||||
|
||||
assert_eq!(v, data);
|
||||
assert_eq!(heap.into_sorted_vec(), data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_vec() {
|
||||
check_to_vec(vec![]);
|
||||
check_to_vec(vec![5]);
|
||||
check_to_vec(vec![3, 2]);
|
||||
check_to_vec(vec![2, 3]);
|
||||
check_to_vec(vec![5, 1, 2]);
|
||||
check_to_vec(vec![1, 100, 2, 3]);
|
||||
check_to_vec(vec![1, 3, 5, 7, 9, 2, 4, 6, 8, 0]);
|
||||
check_to_vec(vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]);
|
||||
check_to_vec(vec![9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0]);
|
||||
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
check_to_vec(vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
|
||||
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2]);
|
||||
check_to_vec(vec![5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_pop() {
|
||||
let mut heap = BinaryHeap::<i32>::new();
|
||||
assert!(heap.pop().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_peek() {
|
||||
let empty = BinaryHeap::<i32>::new();
|
||||
assert!(empty.peek().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_replace() {
|
||||
let mut heap = BinaryHeap::new();
|
||||
assert!(heap.replace(5).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = vec![9, 8, 7, 6, 5, 4, 3, 2, 1];
|
||||
|
||||
let mut q: BinaryHeap<_> = xs.iter().rev().cloned().collect();
|
||||
|
||||
for &x in &xs {
|
||||
assert_eq!(q.pop().unwrap(), x);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain() {
|
||||
let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect();
|
||||
|
||||
assert_eq!(q.drain().take(5).count(), 5);
|
||||
|
||||
assert!(q.is_empty());
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1599,309 +1599,3 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use prelude::*;
|
||||
use std::iter::range_inclusive;
|
||||
|
||||
use super::BTreeMap;
|
||||
use super::Entry::{Occupied, Vacant};
|
||||
use Bound::{self, Included, Excluded, Unbounded};
|
||||
|
||||
#[test]
|
||||
fn test_basic_large() {
|
||||
let mut map = BTreeMap::new();
|
||||
let size = 10000;
|
||||
assert_eq!(map.len(), 0);
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.insert(i, 10*i), None);
|
||||
assert_eq!(map.len(), i + 1);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.get(&i).unwrap(), &(i*10));
|
||||
}
|
||||
|
||||
for i in size..size*2 {
|
||||
assert_eq!(map.get(&i), None);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.insert(i, 100*i), Some(10*i));
|
||||
assert_eq!(map.len(), size);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.get(&i).unwrap(), &(i*100));
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.remove(&(i*2)), Some(i*200));
|
||||
assert_eq!(map.len(), size - i - 1);
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.get(&(2*i)), None);
|
||||
assert_eq!(map.get(&(2*i+1)).unwrap(), &(i*200 + 100));
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.remove(&(2*i)), None);
|
||||
assert_eq!(map.remove(&(2*i+1)), Some(i*200 + 100));
|
||||
assert_eq!(map.len(), size/2 - i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_small() {
|
||||
let mut map = BTreeMap::new();
|
||||
assert_eq!(map.remove(&1), None);
|
||||
assert_eq!(map.get(&1), None);
|
||||
assert_eq!(map.insert(1, 1), None);
|
||||
assert_eq!(map.get(&1), Some(&1));
|
||||
assert_eq!(map.insert(1, 2), Some(1));
|
||||
assert_eq!(map.get(&1), Some(&2));
|
||||
assert_eq!(map.insert(2, 4), None);
|
||||
assert_eq!(map.get(&2), Some(&4));
|
||||
assert_eq!(map.remove(&1), Some(2));
|
||||
assert_eq!(map.remove(&2), Some(4));
|
||||
assert_eq!(map.remove(&1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T) where T: Iterator<Item=(usize, usize)> {
|
||||
for i in 0..size {
|
||||
assert_eq!(iter.size_hint(), (size - i, Some(size - i)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_rev() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T) where T: Iterator<Item=(usize, usize)> {
|
||||
for i in 0..size {
|
||||
assert_eq!(iter.size_hint(), (size - i, Some(size - i)));
|
||||
assert_eq!(iter.next().unwrap(), (size - i - 1, size - i - 1));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().rev().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().rev().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter().rev());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_mixed() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T)
|
||||
where T: Iterator<Item=(usize, usize)> + DoubleEndedIterator {
|
||||
for i in 0..size / 4 {
|
||||
assert_eq!(iter.size_hint(), (size - i * 2, Some(size - i * 2)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
assert_eq!(iter.next_back().unwrap(), (size - i - 1, size - i - 1));
|
||||
}
|
||||
for i in size / 4..size * 3 / 4 {
|
||||
assert_eq!(iter.size_hint(), (size * 3 / 4 - i, Some(size * 3 / 4 - i)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_small() {
|
||||
let size = 5;
|
||||
|
||||
// Forwards
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
let mut j = 0;
|
||||
for ((&k, &v), i) in map.range(Included(&2), Unbounded).zip(2..size) {
|
||||
assert_eq!(k, i);
|
||||
assert_eq!(v, i);
|
||||
j += 1;
|
||||
}
|
||||
assert_eq!(j, size - 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_1000() {
|
||||
let size = 1000;
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test(map: &BTreeMap<u32, u32>, size: u32, min: Bound<&u32>, max: Bound<&u32>) {
|
||||
let mut kvs = map.range(min, max).map(|(&k, &v)| (k, v));
|
||||
let mut pairs = (0..size).map(|i| (i, i));
|
||||
|
||||
for (kv, pair) in kvs.by_ref().zip(pairs.by_ref()) {
|
||||
assert_eq!(kv, pair);
|
||||
}
|
||||
assert_eq!(kvs.next(), None);
|
||||
assert_eq!(pairs.next(), None);
|
||||
}
|
||||
test(&map, size, Included(&0), Excluded(&size));
|
||||
test(&map, size, Unbounded, Excluded(&size));
|
||||
test(&map, size, Included(&0), Included(&(size - 1)));
|
||||
test(&map, size, Unbounded, Included(&(size - 1)));
|
||||
test(&map, size, Included(&0), Unbounded);
|
||||
test(&map, size, Unbounded, Unbounded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range() {
|
||||
let size = 200;
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
for i in 0..size {
|
||||
for j in i..size {
|
||||
let mut kvs = map.range(Included(&i), Included(&j)).map(|(&k, &v)| (k, v));
|
||||
let mut pairs = range_inclusive(i, j).map(|i| (i, i));
|
||||
|
||||
for (kv, pair) in kvs.by_ref().zip(pairs.by_ref()) {
|
||||
assert_eq!(kv, pair);
|
||||
}
|
||||
assert_eq!(kvs.next(), None);
|
||||
assert_eq!(pairs.next(), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry(){
|
||||
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
|
||||
|
||||
let mut map: BTreeMap<_, _> = xs.iter().cloned().collect();
|
||||
|
||||
// Existing key (insert)
|
||||
match map.entry(1) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
assert_eq!(view.get(), &10);
|
||||
assert_eq!(view.insert(100), 10);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&1).unwrap(), &100);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
|
||||
// Existing key (update)
|
||||
match map.entry(2) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
let v = view.get_mut();
|
||||
*v *= 10;
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&2).unwrap(), &200);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
// Existing key (take)
|
||||
match map.entry(3) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(view) => {
|
||||
assert_eq!(view.remove(), 30);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&3), None);
|
||||
assert_eq!(map.len(), 5);
|
||||
|
||||
|
||||
// Inexistent key (insert)
|
||||
match map.entry(10) {
|
||||
Occupied(_) => unreachable!(),
|
||||
Vacant(view) => {
|
||||
assert_eq!(*view.insert(1000), 1000);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&10).unwrap(), &1000);
|
||||
assert_eq!(map.len(), 6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod bench {
|
||||
use prelude::*;
|
||||
use std::rand::{weak_rng, Rng};
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
use super::BTreeMap;
|
||||
|
||||
map_insert_rand_bench!{insert_rand_100, 100, BTreeMap}
|
||||
map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_insert_seq_bench!{insert_seq_100, 100, BTreeMap}
|
||||
map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_find_rand_bench!{find_rand_100, 100, BTreeMap}
|
||||
map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_find_seq_bench!{find_seq_100, 100, BTreeMap}
|
||||
map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap}
|
||||
|
||||
fn bench_iter(b: &mut Bencher, size: i32) {
|
||||
let mut map = BTreeMap::<i32, i32>::new();
|
||||
let mut rng = weak_rng();
|
||||
|
||||
for _ in 0..size {
|
||||
map.insert(rng.gen(), rng.gen());
|
||||
}
|
||||
|
||||
b.iter(|| {
|
||||
for entry in &map {
|
||||
black_box(entry);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_20(b: &mut Bencher) {
|
||||
bench_iter(b, 20);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_1000(b: &mut Bencher) {
|
||||
bench_iter(b, 1000);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_100000(b: &mut Bencher) {
|
||||
bench_iter(b, 100000);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -742,180 +742,3 @@ impl<'a, T: Ord> Iterator for Union<'a, T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use prelude::*;
|
||||
|
||||
use super::BTreeSet;
|
||||
use std::hash::{self, SipHasher};
|
||||
|
||||
#[test]
|
||||
fn test_clone_eq() {
|
||||
let mut m = BTreeSet::new();
|
||||
|
||||
m.insert(1);
|
||||
m.insert(2);
|
||||
|
||||
assert!(m.clone() == m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = BTreeSet::new();
|
||||
let mut y = BTreeSet::new();
|
||||
|
||||
x.insert(1);
|
||||
x.insert(2);
|
||||
x.insert(3);
|
||||
|
||||
y.insert(3);
|
||||
y.insert(2);
|
||||
y.insert(1);
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
struct Counter<'a, 'b> {
|
||||
i: &'a mut usize,
|
||||
expected: &'b [i32],
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> FnMut<(&'c i32,)> for Counter<'a, 'b> {
|
||||
type Output = bool;
|
||||
|
||||
extern "rust-call" fn call_mut(&mut self, (&x,): (&'c i32,)) -> bool {
|
||||
assert_eq!(x, self.expected[*self.i]);
|
||||
*self.i += 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F) where
|
||||
// FIXME Replace Counter with `Box<FnMut(_) -> _>`
|
||||
F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, Counter) -> bool,
|
||||
{
|
||||
let mut set_a = BTreeSet::new();
|
||||
let mut set_b = BTreeSet::new();
|
||||
|
||||
for x in a { assert!(set_a.insert(*x)) }
|
||||
for y in b { assert!(set_b.insert(*y)) }
|
||||
|
||||
let mut i = 0;
|
||||
f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
|
||||
assert_eq!(i, expected.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersection() {
|
||||
fn check_intersection(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.intersection(y).all(f))
|
||||
}
|
||||
|
||||
check_intersection(&[], &[], &[]);
|
||||
check_intersection(&[1, 2, 3], &[], &[]);
|
||||
check_intersection(&[], &[1, 2, 3], &[]);
|
||||
check_intersection(&[2], &[1, 2, 3], &[2]);
|
||||
check_intersection(&[1, 2, 3], &[2], &[2]);
|
||||
check_intersection(&[11, 1, 3, 77, 103, 5, -5],
|
||||
&[2, 11, 77, -9, -42, 5, 3],
|
||||
&[3, 5, 11, 77]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_difference() {
|
||||
fn check_difference(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.difference(y).all(f))
|
||||
}
|
||||
|
||||
check_difference(&[], &[], &[]);
|
||||
check_difference(&[1, 12], &[], &[1, 12]);
|
||||
check_difference(&[], &[1, 2, 3, 9], &[]);
|
||||
check_difference(&[1, 3, 5, 9, 11],
|
||||
&[3, 9],
|
||||
&[1, 5, 11]);
|
||||
check_difference(&[-5, 11, 22, 33, 40, 42],
|
||||
&[-12, -5, 14, 23, 34, 38, 39, 50],
|
||||
&[11, 22, 33, 40, 42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_symmetric_difference() {
|
||||
fn check_symmetric_difference(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
|
||||
}
|
||||
|
||||
check_symmetric_difference(&[], &[], &[]);
|
||||
check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
|
||||
check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
|
||||
check_symmetric_difference(&[1, 3, 5, 9, 11],
|
||||
&[-2, 3, 9, 14, 22],
|
||||
&[-2, 1, 5, 11, 14, 22]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_union() {
|
||||
fn check_union(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.union(y).all(f))
|
||||
}
|
||||
|
||||
check_union(&[], &[], &[]);
|
||||
check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
|
||||
check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
|
||||
check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
|
||||
&[-2, 1, 5, 9, 13, 19],
|
||||
&[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip() {
|
||||
let mut x = BTreeSet::new();
|
||||
x.insert(5);
|
||||
x.insert(12);
|
||||
x.insert(11);
|
||||
|
||||
let mut y = BTreeSet::new();
|
||||
y.insert("foo");
|
||||
y.insert("bar");
|
||||
|
||||
let x = x;
|
||||
let y = y;
|
||||
let mut z = x.iter().zip(y.iter());
|
||||
|
||||
// FIXME: #5801: this needs a type hint to compile...
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert_eq!(result.unwrap(), (&5, &("bar")));
|
||||
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert_eq!(result.unwrap(), (&11, &("foo")));
|
||||
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let set: BTreeSet<_> = xs.iter().cloned().collect();
|
||||
|
||||
for x in &xs {
|
||||
assert!(set.contains(x));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut set = BTreeSet::new();
|
||||
let empty = BTreeSet::<i32>::new();
|
||||
|
||||
set.insert(1);
|
||||
set.insert(2);
|
||||
|
||||
let set_str = format!("{:?}", set);
|
||||
|
||||
assert_eq!(set_str, "{1, 2}");
|
||||
assert_eq!(format!("{:?}", empty), "{}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -285,241 +285,3 @@ impl<E:CLike> Extend<E> for EnumSet<E> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use self::Foo::*;
|
||||
use prelude::*;
|
||||
use core::mem;
|
||||
|
||||
use super::{EnumSet, CLike};
|
||||
|
||||
#[derive(Copy, PartialEq, Debug)]
|
||||
#[repr(usize)]
|
||||
enum Foo {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
impl CLike for Foo {
|
||||
fn to_usize(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
|
||||
fn from_usize(v: usize) -> Foo {
|
||||
unsafe { mem::transmute(v) }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new() {
|
||||
let e: EnumSet<Foo> = EnumSet::new();
|
||||
assert!(e.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut e = EnumSet::new();
|
||||
assert!(format!("{:?}", e) == "{}");
|
||||
e.insert(A);
|
||||
assert!(format!("{:?}", e) == "{A}");
|
||||
e.insert(C);
|
||||
assert!(format!("{:?}", e) == "{A, C}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_len() {
|
||||
let mut e = EnumSet::new();
|
||||
assert_eq!(e.len(), 0);
|
||||
e.insert(A);
|
||||
e.insert(B);
|
||||
e.insert(C);
|
||||
assert_eq!(e.len(), 3);
|
||||
e.remove(&A);
|
||||
assert_eq!(e.len(), 2);
|
||||
e.clear();
|
||||
assert_eq!(e.len(), 0);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// intersect
|
||||
|
||||
#[test]
|
||||
fn test_two_empties_do_not_intersect() {
|
||||
let e1: EnumSet<Foo> = EnumSet::new();
|
||||
let e2: EnumSet<Foo> = EnumSet::new();
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_does_not_intersect_with_full() {
|
||||
let e1: EnumSet<Foo> = EnumSet::new();
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
e2.insert(C);
|
||||
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disjoint_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(B);
|
||||
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlapping_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
|
||||
assert!(!e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// contains and contains_elem
|
||||
|
||||
#[test]
|
||||
fn test_superset() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
|
||||
let mut e3: EnumSet<Foo> = EnumSet::new();
|
||||
e3.insert(C);
|
||||
|
||||
assert!(e1.is_subset(&e2));
|
||||
assert!(e2.is_superset(&e1));
|
||||
assert!(!e3.is_superset(&e2));
|
||||
assert!(!e2.is_superset(&e3))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_contains() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
assert!(e1.contains(&A));
|
||||
assert!(!e1.contains(&B));
|
||||
assert!(!e1.contains(&C));
|
||||
|
||||
e1.insert(A);
|
||||
e1.insert(B);
|
||||
assert!(e1.contains(&A));
|
||||
assert!(e1.contains(&B));
|
||||
assert!(!e1.contains(&C));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// iter
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
|
||||
let elems: ::vec::Vec<Foo> = e1.iter().collect();
|
||||
assert!(elems.is_empty());
|
||||
|
||||
e1.insert(A);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A], elems);
|
||||
|
||||
e1.insert(C);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,C], elems);
|
||||
|
||||
e1.insert(C);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,C], elems);
|
||||
|
||||
e1.insert(B);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,B,C], elems);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// operators
|
||||
|
||||
#[test]
|
||||
fn test_operators() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
e1.insert(C);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(B);
|
||||
e2.insert(C);
|
||||
|
||||
let e_union = e1 | e2;
|
||||
let elems: ::vec::Vec<_> = e_union.iter().collect();
|
||||
assert_eq!([A,B,C], elems);
|
||||
|
||||
let e_intersection = e1 & e2;
|
||||
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
|
||||
assert_eq!([C], elems);
|
||||
|
||||
// Another way to express intersection
|
||||
let e_intersection = e1 - (e1 - e2);
|
||||
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
|
||||
assert_eq!([C], elems);
|
||||
|
||||
let e_subtract = e1 - e2;
|
||||
let elems: ::vec::Vec<_> = e_subtract.iter().collect();
|
||||
assert_eq!([A], elems);
|
||||
|
||||
// Bitwise XOR of two sets, aka symmetric difference
|
||||
let e_symmetric_diff = e1 ^ e2;
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
|
||||
// Another way to express symmetric difference
|
||||
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
|
||||
// Yet another way to express symmetric difference
|
||||
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_overflow() {
|
||||
#[allow(dead_code)]
|
||||
#[derive(Copy)]
|
||||
#[repr(usize)]
|
||||
enum Bar {
|
||||
V00, V01, V02, V03, V04, V05, V06, V07, V08, V09,
|
||||
V10, V11, V12, V13, V14, V15, V16, V17, V18, V19,
|
||||
V20, V21, V22, V23, V24, V25, V26, V27, V28, V29,
|
||||
V30, V31, V32, V33, V34, V35, V36, V37, V38, V39,
|
||||
V40, V41, V42, V43, V44, V45, V46, V47, V48, V49,
|
||||
V50, V51, V52, V53, V54, V55, V56, V57, V58, V59,
|
||||
V60, V61, V62, V63, V64, V65, V66, V67, V68, V69,
|
||||
}
|
||||
|
||||
impl CLike for Bar {
|
||||
fn to_usize(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
|
||||
fn from_usize(v: usize) -> Bar {
|
||||
unsafe { mem::transmute(v) }
|
||||
}
|
||||
}
|
||||
let mut set = EnumSet::new();
|
||||
set.insert(Bar::V64);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -434,15 +434,3 @@ pub fn format(args: Arguments) -> string::String {
|
|||
let _ = write!(&mut output, "{}", args);
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
use fmt;
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
let s = fmt::format(format_args!("Hello, {}!", "world"));
|
||||
assert_eq!(s.as_slice(), "Hello, world!");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,10 +47,6 @@ extern crate core;
|
|||
extern crate unicode;
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(test)] extern crate test;
|
||||
#[cfg(test)] #[macro_use] extern crate std;
|
||||
#[cfg(test)] #[macro_use] extern crate log;
|
||||
|
||||
pub use binary_heap::BinaryHeap;
|
||||
pub use bit_vec::BitVec;
|
||||
pub use bit_set::BitSet;
|
||||
|
@ -85,8 +81,6 @@ pub use alloc::boxed;
|
|||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
#[cfg(test)] #[macro_use] mod bench;
|
||||
|
||||
pub mod binary_heap;
|
||||
mod bit;
|
||||
mod btree;
|
||||
|
@ -137,47 +131,10 @@ pub mod btree_set {
|
|||
#[doc(hidden)]
|
||||
pub fn fixme_14344_be_sure_to_link_to_collections() {}
|
||||
|
||||
#[cfg(not(test))]
|
||||
mod std {
|
||||
pub use core::ops; // RangeFull
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod prelude {
|
||||
// from core.
|
||||
pub use core::clone::Clone;
|
||||
pub use core::cmp::{PartialEq, Eq, PartialOrd, Ord};
|
||||
pub use core::cmp::Ordering::{Less, Equal, Greater};
|
||||
pub use core::iter::range;
|
||||
pub use core::iter::{FromIterator, Extend, IteratorExt};
|
||||
pub use core::iter::{Iterator, DoubleEndedIterator, RandomAccessIterator};
|
||||
pub use core::iter::{ExactSizeIterator};
|
||||
pub use core::marker::{Copy, Send, Sized, Sync};
|
||||
pub use core::mem::drop;
|
||||
pub use core::ops::{Drop, Fn, FnMut, FnOnce};
|
||||
pub use core::option::Option;
|
||||
pub use core::option::Option::{Some, None};
|
||||
pub use core::ptr::PtrExt;
|
||||
pub use core::result::Result;
|
||||
pub use core::result::Result::{Ok, Err};
|
||||
|
||||
// in core and collections (may differ).
|
||||
pub use slice::{AsSlice, SliceExt};
|
||||
#[cfg(stage0)]
|
||||
pub use str::{Str, StrExt};
|
||||
#[cfg(not(stage0))]
|
||||
pub use str::Str;
|
||||
|
||||
// from other crates.
|
||||
pub use alloc::boxed::Box;
|
||||
|
||||
// from collections.
|
||||
pub use borrow::IntoCow;
|
||||
pub use slice::SliceConcatExt;
|
||||
pub use string::{String, ToString};
|
||||
pub use vec::Vec;
|
||||
}
|
||||
|
||||
/// An endpoint of a range of keys.
|
||||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
||||
pub enum Bound<T> {
|
||||
|
|
|
@ -938,572 +938,3 @@ impl<A: Hash> Hash for LinkedList<A> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
use std::rand;
|
||||
use std::hash::{self, SipHasher};
|
||||
use std::thread;
|
||||
use test::Bencher;
|
||||
use test;
|
||||
|
||||
use super::{LinkedList, Node};
|
||||
|
||||
pub fn check_links<T>(list: &LinkedList<T>) {
|
||||
let mut len = 0;
|
||||
let mut last_ptr: Option<&Node<T>> = None;
|
||||
let mut node_ptr: &Node<T>;
|
||||
match list.list_head {
|
||||
None => { assert_eq!(0, list.length); return }
|
||||
Some(ref node) => node_ptr = &**node,
|
||||
}
|
||||
loop {
|
||||
match (last_ptr, node_ptr.prev.resolve_immut()) {
|
||||
(None , None ) => {}
|
||||
(None , _ ) => panic!("prev link for list_head"),
|
||||
(Some(p), Some(pptr)) => {
|
||||
assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
|
||||
}
|
||||
_ => panic!("prev link is none, not good"),
|
||||
}
|
||||
match node_ptr.next {
|
||||
Some(ref next) => {
|
||||
last_ptr = Some(node_ptr);
|
||||
node_ptr = &**next;
|
||||
len += 1;
|
||||
}
|
||||
None => {
|
||||
len += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(len, list.length);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
let mut m = LinkedList::<Box<_>>::new();
|
||||
assert_eq!(m.pop_front(), None);
|
||||
assert_eq!(m.pop_back(), None);
|
||||
assert_eq!(m.pop_front(), None);
|
||||
m.push_front(box 1);
|
||||
assert_eq!(m.pop_front(), Some(box 1));
|
||||
m.push_back(box 2);
|
||||
m.push_back(box 3);
|
||||
assert_eq!(m.len(), 2);
|
||||
assert_eq!(m.pop_front(), Some(box 2));
|
||||
assert_eq!(m.pop_front(), Some(box 3));
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(m.pop_front(), None);
|
||||
m.push_back(box 1);
|
||||
m.push_back(box 3);
|
||||
m.push_back(box 5);
|
||||
m.push_back(box 7);
|
||||
assert_eq!(m.pop_front(), Some(box 1));
|
||||
|
||||
let mut n = LinkedList::new();
|
||||
n.push_front(2);
|
||||
n.push_front(3);
|
||||
{
|
||||
assert_eq!(n.front().unwrap(), &3);
|
||||
let x = n.front_mut().unwrap();
|
||||
assert_eq!(*x, 3);
|
||||
*x = 0;
|
||||
}
|
||||
{
|
||||
assert_eq!(n.back().unwrap(), &2);
|
||||
let y = n.back_mut().unwrap();
|
||||
assert_eq!(*y, 2);
|
||||
*y = 1;
|
||||
}
|
||||
assert_eq!(n.pop_front(), Some(0));
|
||||
assert_eq!(n.pop_front(), Some(1));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn generate_test() -> LinkedList<i32> {
|
||||
list_from(&[0,1,2,3,4,5,6])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> {
|
||||
v.iter().cloned().collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_append() {
|
||||
// Empty to empty
|
||||
{
|
||||
let mut m = LinkedList::<i32>::new();
|
||||
let mut n = LinkedList::new();
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(n.len(), 0);
|
||||
}
|
||||
// Non-empty to empty
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
let mut n = LinkedList::new();
|
||||
n.push_back(2);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(m.pop_back(), Some(2));
|
||||
assert_eq!(n.len(), 0);
|
||||
check_links(&m);
|
||||
}
|
||||
// Empty to non-empty
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
let mut n = LinkedList::new();
|
||||
m.push_back(2);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(m.pop_back(), Some(2));
|
||||
check_links(&m);
|
||||
}
|
||||
|
||||
// Non-empty to non-empty
|
||||
let v = vec![1,2,3,4,5];
|
||||
let u = vec![9,8,1,2,3,4,5];
|
||||
let mut m = list_from(&v);
|
||||
let mut n = list_from(&u);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
let mut sum = v;
|
||||
sum.push_all(&u);
|
||||
assert_eq!(sum.len(), m.len());
|
||||
for elt in sum {
|
||||
assert_eq!(m.pop_front(), Some(elt))
|
||||
}
|
||||
assert_eq!(n.len(), 0);
|
||||
// let's make sure it's working properly, since we
|
||||
// did some direct changes to private members
|
||||
n.push_back(3);
|
||||
assert_eq!(n.len(), 1);
|
||||
assert_eq!(n.pop_front(), Some(3));
|
||||
check_links(&n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_off() {
|
||||
// singleton
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
m.push_back(1);
|
||||
|
||||
let p = m.split_off(0);
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(p.len(), 1);
|
||||
assert_eq!(p.back(), Some(&1));
|
||||
assert_eq!(p.front(), Some(&1));
|
||||
}
|
||||
|
||||
// not singleton, forwards
|
||||
{
|
||||
let u = vec![1,2,3,4,5];
|
||||
let mut m = list_from(&u);
|
||||
let mut n = m.split_off(2);
|
||||
assert_eq!(m.len(), 2);
|
||||
assert_eq!(n.len(), 3);
|
||||
for elt in 1..3 {
|
||||
assert_eq!(m.pop_front(), Some(elt));
|
||||
}
|
||||
for elt in 3..6 {
|
||||
assert_eq!(n.pop_front(), Some(elt));
|
||||
}
|
||||
}
|
||||
// not singleton, backwards
|
||||
{
|
||||
let u = vec![1,2,3,4,5];
|
||||
let mut m = list_from(&u);
|
||||
let mut n = m.split_off(4);
|
||||
assert_eq!(m.len(), 4);
|
||||
assert_eq!(n.len(), 1);
|
||||
for elt in 1..5 {
|
||||
assert_eq!(m.pop_front(), Some(elt));
|
||||
}
|
||||
for elt in 5..6 {
|
||||
assert_eq!(n.pop_front(), Some(elt));
|
||||
}
|
||||
}
|
||||
|
||||
// no-op on the last index
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
m.push_back(1);
|
||||
|
||||
let p = m.split_off(1);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(p.len(), 0);
|
||||
assert_eq!(m.back(), Some(&1));
|
||||
assert_eq!(m.front(), Some(&1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let m = generate_test();
|
||||
for (i, elt) in m.iter().enumerate() {
|
||||
assert_eq!(i as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().next(), None);
|
||||
n.push_front(4);
|
||||
let mut it = n.iter();
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_clone() {
|
||||
let mut n = LinkedList::new();
|
||||
n.push_back(2);
|
||||
n.push_back(3);
|
||||
n.push_back(4);
|
||||
let mut it = n.iter();
|
||||
it.next();
|
||||
let mut jt = it.clone();
|
||||
assert_eq!(it.next(), jt.next());
|
||||
assert_eq!(it.next_back(), jt.next_back());
|
||||
assert_eq!(it.next(), jt.next());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_double_end() {
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().next(), None);
|
||||
n.push_front(4);
|
||||
n.push_front(5);
|
||||
n.push_front(6);
|
||||
let mut it = n.iter();
|
||||
assert_eq!(it.size_hint(), (3, Some(3)));
|
||||
assert_eq!(it.next().unwrap(), &6);
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(it.next_back().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next_back().unwrap(), &5);
|
||||
assert_eq!(it.next_back(), None);
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rev_iter() {
|
||||
let m = generate_test();
|
||||
for (i, elt) in m.iter().rev().enumerate() {
|
||||
assert_eq!((6 - i) as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().rev().next(), None);
|
||||
n.push_front(4);
|
||||
let mut it = n.iter().rev();
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iter() {
|
||||
let mut m = generate_test();
|
||||
let mut len = m.len();
|
||||
for (i, elt) in m.iter_mut().enumerate() {
|
||||
assert_eq!(i as i32, *elt);
|
||||
len -= 1;
|
||||
}
|
||||
assert_eq!(len, 0);
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().next().is_none());
|
||||
n.push_front(4);
|
||||
n.push_back(5);
|
||||
let mut it = n.iter_mut();
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert!(it.next().is_some());
|
||||
assert!(it.next().is_some());
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_mut_double_end() {
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().next_back().is_none());
|
||||
n.push_front(4);
|
||||
n.push_front(5);
|
||||
n.push_front(6);
|
||||
let mut it = n.iter_mut();
|
||||
assert_eq!(it.size_hint(), (3, Some(3)));
|
||||
assert_eq!(*it.next().unwrap(), 6);
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(*it.next_back().unwrap(), 4);
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(*it.next_back().unwrap(), 5);
|
||||
assert!(it.next_back().is_none());
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_prev() {
|
||||
let mut m = list_from(&[0,2,4,6,8]);
|
||||
let len = m.len();
|
||||
{
|
||||
let mut it = m.iter_mut();
|
||||
it.insert_next(-2);
|
||||
loop {
|
||||
match it.next() {
|
||||
None => break,
|
||||
Some(elt) => {
|
||||
it.insert_next(*elt + 1);
|
||||
match it.peek_next() {
|
||||
Some(x) => assert_eq!(*x, *elt + 2),
|
||||
None => assert_eq!(8, *elt),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
it.insert_next(0);
|
||||
it.insert_next(1);
|
||||
}
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 3 + len * 2);
|
||||
assert_eq!(m.into_iter().collect::<Vec<_>>(), [-2,0,1,2,3,4,5,6,7,8,9,0,1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_rev_iter() {
|
||||
let mut m = generate_test();
|
||||
for (i, elt) in m.iter_mut().rev().enumerate() {
|
||||
assert_eq!((6 - i) as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().rev().next().is_none());
|
||||
n.push_front(4);
|
||||
let mut it = n.iter_mut().rev();
|
||||
assert!(it.next().is_some());
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send() {
|
||||
let n = list_from(&[1,2,3]);
|
||||
thread::spawn(move || {
|
||||
check_links(&n);
|
||||
let a: &[_] = &[&1,&2,&3];
|
||||
assert_eq!(a, n.iter().collect::<Vec<_>>());
|
||||
}).join().ok().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let mut n = list_from(&[]);
|
||||
let mut m = list_from(&[]);
|
||||
assert!(n == m);
|
||||
n.push_front(1);
|
||||
assert!(n != m);
|
||||
m.push_back(1);
|
||||
assert!(n == m);
|
||||
|
||||
let n = list_from(&[2,3,4]);
|
||||
let m = list_from(&[1,2,3]);
|
||||
assert!(n != m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = LinkedList::new();
|
||||
let mut y = LinkedList::new();
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
|
||||
x.push_back(1);
|
||||
x.push_back(2);
|
||||
x.push_back(3);
|
||||
|
||||
y.push_front(3);
|
||||
y.push_front(2);
|
||||
y.push_front(1);
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord() {
|
||||
let n = list_from(&[]);
|
||||
let m = list_from(&[1,2,3]);
|
||||
assert!(n < m);
|
||||
assert!(m > n);
|
||||
assert!(n <= n);
|
||||
assert!(n >= n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord_nan() {
|
||||
let nan = 0.0f64/0.0;
|
||||
let n = list_from(&[nan]);
|
||||
let m = list_from(&[nan]);
|
||||
assert!(!(n < m));
|
||||
assert!(!(n > m));
|
||||
assert!(!(n <= m));
|
||||
assert!(!(n >= m));
|
||||
|
||||
let n = list_from(&[nan]);
|
||||
let one = list_from(&[1.0f64]);
|
||||
assert!(!(n < one));
|
||||
assert!(!(n > one));
|
||||
assert!(!(n <= one));
|
||||
assert!(!(n >= one));
|
||||
|
||||
let u = list_from(&[1.0f64,2.0,nan]);
|
||||
let v = list_from(&[1.0f64,2.0,3.0]);
|
||||
assert!(!(u < v));
|
||||
assert!(!(u > v));
|
||||
assert!(!(u <= v));
|
||||
assert!(!(u >= v));
|
||||
|
||||
let s = list_from(&[1.0f64,2.0,4.0,2.0]);
|
||||
let t = list_from(&[1.0f64,2.0,3.0,2.0]);
|
||||
assert!(!(s < t));
|
||||
assert!(s > one);
|
||||
assert!(!(s <= one));
|
||||
assert!(s >= one);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fuzz() {
|
||||
for _ in 0..25 {
|
||||
fuzz_test(3);
|
||||
fuzz_test(16);
|
||||
fuzz_test(189);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let list: LinkedList<_> = (0..10).collect();
|
||||
assert_eq!(format!("{:?}", list), "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
|
||||
|
||||
let list: LinkedList<_> = vec!["just", "one", "test", "more"].iter().cloned().collect();
|
||||
assert_eq!(format!("{:?}", list), "[\"just\", \"one\", \"test\", \"more\"]");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn fuzz_test(sz: i32) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
let mut v = vec![];
|
||||
for i in 0..sz {
|
||||
check_links(&m);
|
||||
let r: u8 = rand::random();
|
||||
match r % 6 {
|
||||
0 => {
|
||||
m.pop_back();
|
||||
v.pop();
|
||||
}
|
||||
1 => {
|
||||
if !v.is_empty() {
|
||||
m.pop_front();
|
||||
v.remove(0);
|
||||
}
|
||||
}
|
||||
2 | 4 => {
|
||||
m.push_front(-i);
|
||||
v.insert(0, -i);
|
||||
}
|
||||
3 | 5 | _ => {
|
||||
m.push_back(i);
|
||||
v.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check_links(&m);
|
||||
|
||||
let mut i = 0;
|
||||
for (a, &b) in m.into_iter().zip(v.iter()) {
|
||||
i += 1;
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
assert_eq!(i, v.len());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_collect_into(b: &mut test::Bencher) {
|
||||
let v = &[0; 64];
|
||||
b.iter(|| {
|
||||
let _: LinkedList<_> = v.iter().cloned().collect();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_front(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_front(0);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_back(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_back(0);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_back_pop_back(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_back(0);
|
||||
m.pop_back();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_front_pop_front(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_front(0);
|
||||
m.pop_front();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_iter(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_mut(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter_mut().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_rev(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter().rev().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_mut_rev(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter_mut().rev().count() == 128);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1014,450 +1014,3 @@ impl fmt::Write for String {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
use test::Bencher;
|
||||
|
||||
use str::Utf8Error;
|
||||
use core::iter::repeat;
|
||||
use super::{as_string, CowString};
|
||||
|
||||
#[test]
|
||||
fn test_as_string() {
|
||||
let x = "foo";
|
||||
assert_eq!(x, &**as_string(x));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
let owned: Option<::std::string::String> = "string".parse().ok();
|
||||
assert_eq!(owned.as_ref().map(|s| &**s), Some("string"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unsized_to_string() {
|
||||
let s: &str = "abc";
|
||||
let _: String = (*s).to_string();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf8() {
|
||||
let xs = b"hello".to_vec();
|
||||
assert_eq!(String::from_utf8(xs).unwrap(),
|
||||
String::from_str("hello"));
|
||||
|
||||
let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
|
||||
assert_eq!(String::from_utf8(xs).unwrap(),
|
||||
String::from_str("ศไทย中华Việt Nam"));
|
||||
|
||||
let xs = b"hello\xFF".to_vec();
|
||||
let err = String::from_utf8(xs).err().unwrap();
|
||||
assert_eq!(err.utf8_error(), Utf8Error::TooShort);
|
||||
assert_eq!(err.into_bytes(), b"hello\xff".to_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf8_lossy() {
|
||||
let xs = b"hello";
|
||||
let ys: CowString = "hello".into_cow();
|
||||
assert_eq!(String::from_utf8_lossy(xs), ys);
|
||||
|
||||
let xs = "ศไทย中华Việt Nam".as_bytes();
|
||||
let ys: CowString = "ศไทย中华Việt Nam".into_cow();
|
||||
assert_eq!(String::from_utf8_lossy(xs), ys);
|
||||
|
||||
let xs = b"Hello\xC2 There\xFF Goodbye";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow());
|
||||
|
||||
let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow());
|
||||
|
||||
let xs = b"\xF5foo\xF5\x80bar";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow());
|
||||
|
||||
let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow());
|
||||
|
||||
let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow());
|
||||
|
||||
let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
|
||||
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\
|
||||
foo\u{10000}bar").into_cow());
|
||||
|
||||
// surrogates
|
||||
let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
|
||||
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}foo\
|
||||
\u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf16() {
|
||||
let pairs =
|
||||
[(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
|
||||
vec![0xd800, 0xdf45, 0xd800, 0xdf3f,
|
||||
0xd800, 0xdf3b, 0xd800, 0xdf46,
|
||||
0xd800, 0xdf39, 0xd800, 0xdf3b,
|
||||
0xd800, 0xdf30, 0x000a]),
|
||||
|
||||
(String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
|
||||
vec![0xd801, 0xdc12, 0xd801,
|
||||
0xdc49, 0xd801, 0xdc2e, 0xd801,
|
||||
0xdc40, 0xd801, 0xdc32, 0xd801,
|
||||
0xdc4b, 0x0020, 0xd801, 0xdc0f,
|
||||
0xd801, 0xdc32, 0xd801, 0xdc4d,
|
||||
0x000a]),
|
||||
|
||||
(String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
|
||||
vec![0xd800, 0xdf00, 0xd800, 0xdf16,
|
||||
0xd800, 0xdf0b, 0xd800, 0xdf04,
|
||||
0xd800, 0xdf11, 0xd800, 0xdf09,
|
||||
0x00b7, 0xd800, 0xdf0c, 0xd800,
|
||||
0xdf04, 0xd800, 0xdf15, 0xd800,
|
||||
0xdf04, 0xd800, 0xdf0b, 0xd800,
|
||||
0xdf09, 0xd800, 0xdf11, 0x000a ]),
|
||||
|
||||
(String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
|
||||
vec![0xd801, 0xdc8b, 0xd801, 0xdc98,
|
||||
0xd801, 0xdc88, 0xd801, 0xdc91,
|
||||
0xd801, 0xdc9b, 0xd801, 0xdc92,
|
||||
0x0020, 0xd801, 0xdc95, 0xd801,
|
||||
0xdc93, 0x0020, 0xd801, 0xdc88,
|
||||
0xd801, 0xdc9a, 0xd801, 0xdc8d,
|
||||
0x0020, 0xd801, 0xdc8f, 0xd801,
|
||||
0xdc9c, 0xd801, 0xdc92, 0xd801,
|
||||
0xdc96, 0xd801, 0xdc86, 0x0020,
|
||||
0xd801, 0xdc95, 0xd801, 0xdc86,
|
||||
0x000a ]),
|
||||
// Issue #12318, even-numbered non-BMP planes
|
||||
(String::from_str("\u{20000}"),
|
||||
vec![0xD840, 0xDC00])];
|
||||
|
||||
for p in &pairs {
|
||||
let (s, u) = (*p).clone();
|
||||
let s_as_utf16 = s.utf16_units().collect::<Vec<u16>>();
|
||||
let u_as_string = String::from_utf16(&u).unwrap();
|
||||
|
||||
assert!(::unicode::str::is_utf16(&u));
|
||||
assert_eq!(s_as_utf16, u);
|
||||
|
||||
assert_eq!(u_as_string, s);
|
||||
assert_eq!(String::from_utf16_lossy(&u), s);
|
||||
|
||||
assert_eq!(String::from_utf16(&s_as_utf16).unwrap(), s);
|
||||
assert_eq!(u_as_string.utf16_units().collect::<Vec<u16>>(), u);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utf16_invalid() {
|
||||
// completely positive cases tested above.
|
||||
// lead + eof
|
||||
assert!(String::from_utf16(&[0xD800]).is_err());
|
||||
// lead + lead
|
||||
assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
|
||||
|
||||
// isolated trail
|
||||
assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
|
||||
|
||||
// general
|
||||
assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf16_lossy() {
|
||||
// completely positive cases tested above.
|
||||
// lead + eof
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from_str("\u{FFFD}"));
|
||||
// lead + lead
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
|
||||
String::from_str("\u{FFFD}\u{FFFD}"));
|
||||
|
||||
// isolated trail
|
||||
assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from_str("a\u{FFFD}"));
|
||||
|
||||
// general
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
|
||||
String::from_str("\u{FFFD}𐒋\u{FFFD}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_bytes() {
|
||||
let mut s = String::from_str("ABC");
|
||||
unsafe {
|
||||
let mv = s.as_mut_vec();
|
||||
mv.push_all(&[b'D']);
|
||||
}
|
||||
assert_eq!(s, "ABCD");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_str() {
|
||||
let mut s = String::new();
|
||||
s.push_str("");
|
||||
assert_eq!(&s[0..], "");
|
||||
s.push_str("abc");
|
||||
assert_eq!(&s[0..], "abc");
|
||||
s.push_str("ประเทศไทย中华Việt Nam");
|
||||
assert_eq!(&s[0..], "abcประเทศไทย中华Việt Nam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push() {
|
||||
let mut data = String::from_str("ประเทศไทย中");
|
||||
data.push('华');
|
||||
data.push('b'); // 1 byte
|
||||
data.push('¢'); // 2 byte
|
||||
data.push('€'); // 3 byte
|
||||
data.push('𤭢'); // 4 byte
|
||||
assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pop() {
|
||||
let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
|
||||
assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
|
||||
assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
|
||||
assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
|
||||
assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
|
||||
assert_eq!(data.pop().unwrap(), '华');
|
||||
assert_eq!(data, "ประเทศไทย中");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_truncate() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.truncate(5);
|
||||
assert_eq!(s, "12345");
|
||||
s.truncate(3);
|
||||
assert_eq!(s, "123");
|
||||
s.truncate(0);
|
||||
assert_eq!(s, "");
|
||||
|
||||
let mut s = String::from_str("12345");
|
||||
let p = s.as_ptr();
|
||||
s.truncate(3);
|
||||
s.push_str("6");
|
||||
let p_ = s.as_ptr();
|
||||
assert_eq!(p_, p);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_str_truncate_invalid_len() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.truncate(6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_str_truncate_split_codepoint() {
|
||||
let mut s = String::from_str("\u{FC}"); // ü
|
||||
s.truncate(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_clear() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.clear();
|
||||
assert_eq!(s.len(), 0);
|
||||
assert_eq!(s, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_add() {
|
||||
let a = String::from_str("12345");
|
||||
let b = a + "2";
|
||||
let b = b + "2";
|
||||
assert_eq!(b.len(), 7);
|
||||
assert_eq!(b, "1234522");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove() {
|
||||
let mut s = "ศไทย中华Việt Nam; foobar".to_string();;
|
||||
assert_eq!(s.remove(0), 'ศ');
|
||||
assert_eq!(s.len(), 33);
|
||||
assert_eq!(s, "ไทย中华Việt Nam; foobar");
|
||||
assert_eq!(s.remove(17), 'ệ');
|
||||
assert_eq!(s, "ไทย中华Vit Nam; foobar");
|
||||
}
|
||||
|
||||
#[test] #[should_panic]
|
||||
fn remove_bad() {
|
||||
"ศ".to_string().remove(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert() {
|
||||
let mut s = "foobar".to_string();
|
||||
s.insert(0, 'ệ');
|
||||
assert_eq!(s, "ệfoobar");
|
||||
s.insert(6, 'ย');
|
||||
assert_eq!(s, "ệfooยbar");
|
||||
}
|
||||
|
||||
#[test] #[should_panic] fn insert_bad1() { "".to_string().insert(1, 't'); }
|
||||
#[test] #[should_panic] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
|
||||
|
||||
#[test]
|
||||
fn test_slicing() {
|
||||
let s = "foobar".to_string();
|
||||
assert_eq!("foobar", &s[..]);
|
||||
assert_eq!("foo", &s[..3]);
|
||||
assert_eq!("bar", &s[3..]);
|
||||
assert_eq!("oob", &s[1..4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_types() {
|
||||
assert_eq!(1.to_string(), "1");
|
||||
assert_eq!((-1).to_string(), "-1");
|
||||
assert_eq!(200.to_string(), "200");
|
||||
assert_eq!(2.to_string(), "2");
|
||||
assert_eq!(true.to_string(), "true");
|
||||
assert_eq!(false.to_string(), "false");
|
||||
assert_eq!(("hi".to_string()).to_string(), "hi");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vectors() {
|
||||
let x: Vec<i32> = vec![];
|
||||
assert_eq!(format!("{:?}", x), "[]");
|
||||
assert_eq!(format!("{:?}", vec![1]), "[1]");
|
||||
assert_eq!(format!("{:?}", vec![1, 2, 3]), "[1, 2, 3]");
|
||||
assert!(format!("{:?}", vec![vec![], vec![1], vec![1, 1]]) ==
|
||||
"[[], [1], [1, 1]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iterator() {
|
||||
let s = "ศไทย中华Việt Nam".to_string();
|
||||
let t = "ศไทย中华";
|
||||
let u = "Việt Nam";
|
||||
|
||||
let a: String = s.chars().collect();
|
||||
assert_eq!(s, a);
|
||||
|
||||
let mut b = t.to_string();
|
||||
b.extend(u.chars());
|
||||
assert_eq!(s, b);
|
||||
|
||||
let c: String = vec![t, u].into_iter().collect();
|
||||
assert_eq!(s, c);
|
||||
|
||||
let mut d = t.to_string();
|
||||
d.extend(vec![u].into_iter());
|
||||
assert_eq!(s, d);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
String::with_capacity(100)
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_str(b: &mut Bencher) {
|
||||
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
r.push_str(s);
|
||||
});
|
||||
}
|
||||
|
||||
const REPETITIONS: u64 = 10_000;
|
||||
|
||||
#[bench]
|
||||
fn bench_push_str_one_byte(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push_str("a")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_char_one_byte(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push('a')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_char_two_bytes(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS * 2;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push('â')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
|
||||
let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
|
||||
Lorem ipsum dolor sit amet, consectetur. ";
|
||||
|
||||
assert_eq!(100, s.len());
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
|
||||
let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
|
||||
assert_eq!(100, s.len());
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_invalid(b: &mut Bencher) {
|
||||
let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
|
||||
let s = repeat(0xf5).take(100).collect::<Vec<_>>();
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(&s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
|
||||
let s = "Hello there, the quick brown fox jumped over the lazy dog! \
|
||||
Lorem ipsum dolor sit amet, consectetur. ";
|
||||
// ensure our operation produces an exact-size string before we benchmark it
|
||||
let mut r = String::with_capacity(s.len());
|
||||
r.push_str(s);
|
||||
assert_eq!(r.len(), r.capacity());
|
||||
b.iter(|| {
|
||||
let mut r = String::with_capacity(s.len());
|
||||
r.push_str(s);
|
||||
r.shrink_to_fit();
|
||||
r
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1951,991 +1951,3 @@ impl<T,U> Drop for PartialVecZeroSized<T,U> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
use core::mem::size_of;
|
||||
use core::iter::repeat;
|
||||
use test::Bencher;
|
||||
use super::as_vec;
|
||||
|
||||
struct DropCounter<'a> {
|
||||
count: &'a mut u32
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
impl<'a> Drop for DropCounter<'a> {
|
||||
fn drop(&mut self) {
|
||||
*self.count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_vec() {
|
||||
let xs = [1u8, 2u8, 3u8];
|
||||
assert_eq!(&**as_vec(&xs), xs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_vec_dtor() {
|
||||
let (mut count_x, mut count_y) = (0, 0);
|
||||
{
|
||||
let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }];
|
||||
assert_eq!(as_vec(xs).len(), 2);
|
||||
}
|
||||
assert_eq!(count_x, 1);
|
||||
assert_eq!(count_y, 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>,
|
||||
y: Vec<T>
|
||||
}
|
||||
|
||||
let (mut count_x, mut count_y) = (0, 0);
|
||||
{
|
||||
let mut tv = TwoVec {
|
||||
x: Vec::new(),
|
||||
y: Vec::new()
|
||||
};
|
||||
tv.x.push(DropCounter {count: &mut count_x});
|
||||
tv.y.push(DropCounter {count: &mut count_y});
|
||||
|
||||
// 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);
|
||||
for i in 0..3 { w.push(i) }
|
||||
|
||||
assert_eq!(v, w);
|
||||
|
||||
v.extend(3..10);
|
||||
for i in 3..10 { w.push(i) }
|
||||
|
||||
assert_eq!(v, w);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_from_mut() {
|
||||
let mut values = vec![1, 2, 3, 4, 5];
|
||||
{
|
||||
let slice = &mut values[2 ..];
|
||||
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];
|
||||
{
|
||||
let slice = &mut values[.. 2];
|
||||
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![];
|
||||
let w = vec!(1, 2, 3);
|
||||
|
||||
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() {
|
||||
let mut v = vec!();
|
||||
let three: Vec<Box<_>> = vec!(box 1, box 2, box 3);
|
||||
let two: Vec<Box<_>> = vec!(box 4, box 5);
|
||||
// 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]);
|
||||
}
|
||||
|
||||
#[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 {}
|
||||
unsafe { v.set_len(0); }
|
||||
assert_eq!(v.iter_mut().count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partition() {
|
||||
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]));
|
||||
}
|
||||
|
||||
#[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_unsafe_ptrs() {
|
||||
unsafe {
|
||||
// Test on-stack copy-from-buf.
|
||||
let a = [1, 2, 3];
|
||||
let ptr = a.as_ptr();
|
||||
let b = Vec::from_raw_buf(ptr, 3);
|
||||
assert_eq!(b, [1, 2, 3]);
|
||||
|
||||
// Test on-heap copy-from-buf.
|
||||
let c = vec![1, 2, 3, 4, 5];
|
||||
let ptr = c.as_ptr();
|
||||
let d = Vec::from_raw_buf(ptr, 5);
|
||||
assert_eq!(d, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_truncate_drop() {
|
||||
static mut drops: u32 = 0;
|
||||
struct Elem(i32);
|
||||
impl Drop for Elem {
|
||||
fn drop(&mut self) {
|
||||
unsafe { drops += 1; }
|
||||
}
|
||||
}
|
||||
|
||||
let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
|
||||
assert_eq!(unsafe { drops }, 0);
|
||||
v.truncate(3);
|
||||
assert_eq!(unsafe { drops }, 2);
|
||||
v.truncate(0);
|
||||
assert_eq!(unsafe { drops }, 5);
|
||||
}
|
||||
|
||||
#[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];
|
||||
&x[-1..];
|
||||
}
|
||||
|
||||
#[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];
|
||||
&x[-1..4];
|
||||
}
|
||||
|
||||
#[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() {
|
||||
let mut vec= Vec::<i32>::new();
|
||||
vec.swap_remove(0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_unwrap() {
|
||||
let mut vec = Vec::with_capacity(7);
|
||||
vec.push(1);
|
||||
vec.push(2);
|
||||
let ptr = vec.as_ptr();
|
||||
vec = vec.into_iter().into_inner();
|
||||
assert_eq!(vec.as_ptr(), ptr);
|
||||
assert_eq!(vec.capacity(), 7);
|
||||
assert_eq!(vec.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_map_in_place_incompatible_types_fail() {
|
||||
let v = vec![0, 1, 2];
|
||||
v.map_in_place(|_| ());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place() {
|
||||
let v = vec![0, 1, 2];
|
||||
assert_eq!(v.map_in_place(|i: u32| i as i32 - 1), [-1, 0, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place_zero_sized() {
|
||||
let v = vec![(), ()];
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct ZeroSized;
|
||||
assert_eq!(v.map_in_place(|_| ZeroSized), [ZeroSized, ZeroSized]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place_zero_drop_count() {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct Nothing;
|
||||
impl Drop for Nothing { fn drop(&mut self) { } }
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct ZeroSized;
|
||||
impl Drop for ZeroSized {
|
||||
fn drop(&mut self) {
|
||||
DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
const NUM_ELEMENTS: usize = 2;
|
||||
static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
|
||||
|
||||
let v = repeat(Nothing).take(NUM_ELEMENTS).collect::<Vec<_>>();
|
||||
|
||||
DROP_COUNTER.store(0, Ordering::Relaxed);
|
||||
|
||||
let v = v.map_in_place(|_| ZeroSized);
|
||||
assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), 0);
|
||||
drop(v);
|
||||
assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), NUM_ELEMENTS);
|
||||
}
|
||||
|
||||
#[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![];
|
||||
for i in vec.drain() {
|
||||
vec2.push(i);
|
||||
}
|
||||
assert_eq!(vec, []);
|
||||
assert_eq!(vec2, [ 1, 2, 3 ]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain_items_reverse() {
|
||||
let mut vec = vec![1, 2, 3];
|
||||
let mut vec2 = vec![];
|
||||
for i in vec.drain().rev() {
|
||||
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![];
|
||||
for i in vec.drain() {
|
||||
vec2.push(i);
|
||||
}
|
||||
assert_eq!(vec, []);
|
||||
assert_eq!(vec2, [(), (), ()]);
|
||||
}
|
||||
|
||||
#[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]);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_new(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u32> = Vec::new();
|
||||
assert_eq!(v.len(), 0);
|
||||
assert_eq!(v.capacity(), 0);
|
||||
})
|
||||
}
|
||||
|
||||
fn do_bench_with_capacity(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let v: Vec<u32> = Vec::with_capacity(src_len);
|
||||
assert_eq!(v.len(), 0);
|
||||
assert_eq!(v.capacity(), src_len);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0000(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0010(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0100(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_1000(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_fn(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = (0..src_len).collect::<Vec<_>>();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0000(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0010(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0100(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_1000(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_elem(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst: Vec<usize> = repeat(5).take(src_len).collect();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().all(|x| *x == 5));
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0000(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0010(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0100(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_1000(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_slice(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = src.clone()[..].to_vec();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0000(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0010(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0100(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_1000(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_iter(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst: Vec<_> = FromIterator::from_iter(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0000(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0010(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0100(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_1000(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_extend(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.extend(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0010(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0100(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_1000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0010_0010(b: &mut Bencher) {
|
||||
do_bench_extend(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0100_0100(b: &mut Bencher) {
|
||||
do_bench_extend(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_1000_1000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_push_all(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.push_all(&src);
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0010(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0100(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0010_0010(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0100_0100(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_1000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_push_all_move(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.extend(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0010(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0100(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0010_0010(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0100_0100(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_1000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_clone(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<usize> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = src.clone();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0000(b: &mut Bencher) {
|
||||
do_bench_clone(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0010(b: &mut Bencher) {
|
||||
do_bench_clone(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0100(b: &mut Bencher) {
|
||||
do_bench_clone(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_1000(b: &mut Bencher) {
|
||||
do_bench_clone(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_clone_from(b: &mut Bencher, times: usize, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = (times * src_len) as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
|
||||
for _ in 0..times {
|
||||
dst.clone_from(&src);
|
||||
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| dst_len + i == *x));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_1000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 1000, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_1000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 1000, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_1000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 1000, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_1000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 1000, 100)
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1004,510 +1004,3 @@ impl<V> Iterator for IntoIter<V> {
|
|||
impl<V> DoubleEndedIterator for IntoIter<V> {
|
||||
fn next_back(&mut self) -> Option<(usize, V)> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_map {
|
||||
use prelude::*;
|
||||
use core::hash::{hash, SipHasher};
|
||||
|
||||
use super::VecMap;
|
||||
use super::Entry::{Occupied, Vacant};
|
||||
|
||||
#[test]
|
||||
fn test_get_mut() {
|
||||
let mut m = VecMap::new();
|
||||
assert!(m.insert(1, 12).is_none());
|
||||
assert!(m.insert(2, 8).is_none());
|
||||
assert!(m.insert(5, 14).is_none());
|
||||
let new = 100;
|
||||
match m.get_mut(&5) {
|
||||
None => panic!(), Some(x) => *x = new
|
||||
}
|
||||
assert_eq!(m.get(&5), Some(&new));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_len() {
|
||||
let mut map = VecMap::new();
|
||||
assert_eq!(map.len(), 0);
|
||||
assert!(map.is_empty());
|
||||
assert!(map.insert(5, 20).is_none());
|
||||
assert_eq!(map.len(), 1);
|
||||
assert!(!map.is_empty());
|
||||
assert!(map.insert(11, 12).is_none());
|
||||
assert_eq!(map.len(), 2);
|
||||
assert!(!map.is_empty());
|
||||
assert!(map.insert(14, 22).is_none());
|
||||
assert_eq!(map.len(), 3);
|
||||
assert!(!map.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clear() {
|
||||
let mut map = VecMap::new();
|
||||
assert!(map.insert(5, 20).is_none());
|
||||
assert!(map.insert(11, 12).is_none());
|
||||
assert!(map.insert(14, 22).is_none());
|
||||
map.clear();
|
||||
assert!(map.is_empty());
|
||||
assert!(map.get(&5).is_none());
|
||||
assert!(map.get(&11).is_none());
|
||||
assert!(map.get(&14).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert() {
|
||||
let mut m = VecMap::new();
|
||||
assert_eq!(m.insert(1, 2), None);
|
||||
assert_eq!(m.insert(1, 3), Some(2));
|
||||
assert_eq!(m.insert(1, 4), Some(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove() {
|
||||
let mut m = VecMap::new();
|
||||
m.insert(1, 2);
|
||||
assert_eq!(m.remove(&1), Some(2));
|
||||
assert_eq!(m.remove(&1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_keys() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, 'a');
|
||||
map.insert(2, 'b');
|
||||
map.insert(3, 'c');
|
||||
let keys: Vec<_> = map.keys().collect();
|
||||
assert_eq!(keys.len(), 3);
|
||||
assert!(keys.contains(&1));
|
||||
assert!(keys.contains(&2));
|
||||
assert!(keys.contains(&3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_values() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, 'a');
|
||||
map.insert(2, 'b');
|
||||
map.insert(3, 'c');
|
||||
let values: Vec<_> = map.values().cloned().collect();
|
||||
assert_eq!(values.len(), 3);
|
||||
assert!(values.contains(&'a'));
|
||||
assert!(values.contains(&'b'));
|
||||
assert!(values.contains(&'c'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.size_hint(), (0, Some(11)));
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.size_hint(), (0, Some(10)));
|
||||
assert_eq!(it.next().unwrap(), (1, &2));
|
||||
assert_eq!(it.size_hint(), (0, Some(9)));
|
||||
assert_eq!(it.next().unwrap(), (3, &5));
|
||||
assert_eq!(it.size_hint(), (0, Some(7)));
|
||||
assert_eq!(it.next().unwrap(), (6, &10));
|
||||
assert_eq!(it.size_hint(), (0, Some(4)));
|
||||
assert_eq!(it.next().unwrap(), (10, &11));
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_size_hints() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
assert_eq!(m.iter().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
for (k, v) in &mut m {
|
||||
*v += k as isize;
|
||||
}
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.next().unwrap(), (1, &3));
|
||||
assert_eq!(it.next().unwrap(), (3, &8));
|
||||
assert_eq!(it.next().unwrap(), (6, &16));
|
||||
assert_eq!(it.next().unwrap(), (10, &21));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rev_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
let mut it = m.iter().rev();
|
||||
assert_eq!(it.next().unwrap(), (10, &11));
|
||||
assert_eq!(it.next().unwrap(), (6, &10));
|
||||
assert_eq!(it.next().unwrap(), (3, &5));
|
||||
assert_eq!(it.next().unwrap(), (1, &2));
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_rev_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
for (k, v) in m.iter_mut().rev() {
|
||||
*v += k as isize;
|
||||
}
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.next().unwrap(), (1, &3));
|
||||
assert_eq!(it.next().unwrap(), (3, &8));
|
||||
assert_eq!(it.next().unwrap(), (6, &16));
|
||||
assert_eq!(it.next().unwrap(), (10, &21));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter() {
|
||||
let mut m: VecMap<Box<_>> = VecMap::new();
|
||||
m.insert(1, box 2);
|
||||
let mut called = false;
|
||||
for (k, v) in m {
|
||||
assert!(!called);
|
||||
called = true;
|
||||
assert_eq!(k, 1);
|
||||
assert_eq!(v, box 2);
|
||||
}
|
||||
assert!(called);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain_iterator() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, "a");
|
||||
map.insert(3, "c");
|
||||
map.insert(2, "b");
|
||||
|
||||
let vec: Vec<_> = map.drain().collect();
|
||||
|
||||
assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);
|
||||
assert_eq!(map.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_append() {
|
||||
let mut a = VecMap::new();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
|
||||
let mut b = VecMap::new();
|
||||
b.insert(3, "d"); // Overwrite element from a
|
||||
b.insert(4, "e");
|
||||
b.insert(5, "f");
|
||||
|
||||
a.append(&mut b);
|
||||
|
||||
assert_eq!(a.len(), 5);
|
||||
assert_eq!(b.len(), 0);
|
||||
// Capacity shouldn't change for possible reuse
|
||||
assert!(b.capacity() >= 4);
|
||||
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
assert_eq!(a[3], "d");
|
||||
assert_eq!(a[4], "e");
|
||||
assert_eq!(a[5], "f");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_off() {
|
||||
// Split within the key range
|
||||
let mut a = VecMap::new();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(3);
|
||||
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(b.len(), 2);
|
||||
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
|
||||
assert_eq!(b[3], "c");
|
||||
assert_eq!(b[4], "d");
|
||||
|
||||
// Split at 0
|
||||
a.clear();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(0);
|
||||
|
||||
assert_eq!(a.len(), 0);
|
||||
assert_eq!(b.len(), 4);
|
||||
assert_eq!(b[1], "a");
|
||||
assert_eq!(b[2], "b");
|
||||
assert_eq!(b[3], "c");
|
||||
assert_eq!(b[4], "d");
|
||||
|
||||
// Split behind max_key
|
||||
a.clear();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(5);
|
||||
|
||||
assert_eq!(a.len(), 4);
|
||||
assert_eq!(b.len(), 0);
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
assert_eq!(a[3], "c");
|
||||
assert_eq!(a[4], "d");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut map = VecMap::new();
|
||||
let empty = VecMap::<i32>::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(3, 4);
|
||||
|
||||
let map_str = format!("{:?}", map);
|
||||
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
|
||||
assert_eq!(format!("{:?}", empty), "{}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let mut a = VecMap::new();
|
||||
|
||||
a.insert(1, 'x');
|
||||
a.insert(4, 'y');
|
||||
a.insert(6, 'z');
|
||||
|
||||
assert!(a.clone() == a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(a == b);
|
||||
assert!(a.insert(0, 5).is_none());
|
||||
assert!(a != b);
|
||||
assert!(b.insert(0, 4).is_none());
|
||||
assert!(a != b);
|
||||
assert!(a.insert(5, 19).is_none());
|
||||
assert!(a != b);
|
||||
assert!(!b.insert(0, 5).is_none());
|
||||
assert!(a != b);
|
||||
assert!(b.insert(5, 19).is_none());
|
||||
assert!(a == b);
|
||||
|
||||
a = VecMap::new();
|
||||
b = VecMap::with_capacity(1);
|
||||
assert!(a == b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lt() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(!(a < b) && !(b < a));
|
||||
assert!(b.insert(2, 5).is_none());
|
||||
assert!(a < b);
|
||||
assert!(a.insert(2, 7).is_none());
|
||||
assert!(!(a < b) && b < a);
|
||||
assert!(b.insert(1, 0).is_none());
|
||||
assert!(b < a);
|
||||
assert!(a.insert(0, 6).is_none());
|
||||
assert!(a < b);
|
||||
assert!(a.insert(6, 2).is_none());
|
||||
assert!(a < b && !(b < a));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(a <= b && a >= b);
|
||||
assert!(a.insert(1, 1).is_none());
|
||||
assert!(a > b && a >= b);
|
||||
assert!(b < a && b <= a);
|
||||
assert!(b.insert(2, 2).is_none());
|
||||
assert!(b > a && b >= a);
|
||||
assert!(a < b && a <= b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = VecMap::new();
|
||||
let mut y = VecMap::new();
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
x.insert(1, 'a');
|
||||
x.insert(2, 'b');
|
||||
x.insert(3, 'c');
|
||||
|
||||
y.insert(3, 'c');
|
||||
y.insert(2, 'b');
|
||||
y.insert(1, 'a');
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
|
||||
x.insert(1000, 'd');
|
||||
x.remove(&1000);
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
|
||||
|
||||
let map: VecMap<_> = xs.iter().cloned().collect();
|
||||
|
||||
for &(k, v) in &xs {
|
||||
assert_eq!(map.get(&k), Some(&v));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
let mut map = VecMap::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(2, 1);
|
||||
map.insert(3, 4);
|
||||
|
||||
assert_eq!(map[3], 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_index_nonexistent() {
|
||||
let mut map = VecMap::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(2, 1);
|
||||
map.insert(3, 4);
|
||||
|
||||
map[4];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry(){
|
||||
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
|
||||
|
||||
let mut map: VecMap<_> = xs.iter().cloned().collect();
|
||||
|
||||
// Existing key (insert)
|
||||
match map.entry(1) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
assert_eq!(view.get(), &10);
|
||||
assert_eq!(view.insert(100), 10);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&1).unwrap(), &100);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
|
||||
// Existing key (update)
|
||||
match map.entry(2) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
let v = view.get_mut();
|
||||
*v *= 10;
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&2).unwrap(), &200);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
// Existing key (take)
|
||||
match map.entry(3) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(view) => {
|
||||
assert_eq!(view.remove(), 30);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&3), None);
|
||||
assert_eq!(map.len(), 5);
|
||||
|
||||
|
||||
// Inexistent key (insert)
|
||||
match map.entry(10) {
|
||||
Occupied(_) => unreachable!(),
|
||||
Vacant(view) => {
|
||||
assert_eq!(*view.insert(1000), 1000);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&10).unwrap(), &1000);
|
||||
assert_eq!(map.len(), 6);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod bench {
|
||||
use super::VecMap;
|
||||
|
||||
map_insert_rand_bench!{insert_rand_100, 100, VecMap}
|
||||
map_insert_rand_bench!{insert_rand_10_000, 10_000, VecMap}
|
||||
|
||||
map_insert_seq_bench!{insert_seq_100, 100, VecMap}
|
||||
map_insert_seq_bench!{insert_seq_10_000, 10_000, VecMap}
|
||||
|
||||
map_find_rand_bench!{find_rand_100, 100, VecMap}
|
||||
map_find_rand_bench!{find_rand_10_000, 10_000, VecMap}
|
||||
|
||||
map_find_seq_bench!{find_seq_100, 100, VecMap}
|
||||
map_find_seq_bench!{find_seq_10_000, 10_000, VecMap}
|
||||
}
|
||||
|
|
|
@ -66,11 +66,11 @@ macro_rules! map_find_rand_bench {
|
|||
($name: ident, $n: expr, $map: ident) => (
|
||||
#[bench]
|
||||
pub fn $name(b: &mut ::test::Bencher) {
|
||||
use std::rand;
|
||||
use std::iter::IteratorExt;
|
||||
use std::rand::Rng;
|
||||
use std::rand;
|
||||
use std::vec::Vec;
|
||||
use test::black_box;
|
||||
use vec::Vec;
|
||||
|
||||
let mut map = $map::new();
|
||||
let n: usize = $n;
|
219
src/libcollectionstest/binary_heap.rs
Normal file
219
src/libcollectionstest/binary_heap.rs
Normal file
|
@ -0,0 +1,219 @@
|
|||
// Copyright 2013-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.
|
||||
|
||||
use std::collections::BinaryHeap;
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = [9, 5, 3];
|
||||
let heap = BinaryHeap::from_vec(data);
|
||||
let mut i = 0;
|
||||
for el in &heap {
|
||||
assert_eq!(*el, iterout[i]);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_reverse() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![3, 5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.iter().rev().cloned().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![9, 5, 3];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.into_iter().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_size_hint() {
|
||||
let data = vec![5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let mut it = pq.into_iter();
|
||||
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(it.next(), Some(9));
|
||||
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next(), Some(5));
|
||||
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_reverse() {
|
||||
let data = vec![5, 9, 3];
|
||||
let iterout = vec![3, 5, 9];
|
||||
let pq = BinaryHeap::from_vec(data);
|
||||
|
||||
let v: Vec<_> = pq.into_iter().rev().collect();
|
||||
assert_eq!(v, iterout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_peek_and_pop() {
|
||||
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];
|
||||
let mut sorted = data.clone();
|
||||
sorted.sort();
|
||||
let mut heap = BinaryHeap::from_vec(data);
|
||||
while !heap.is_empty() {
|
||||
assert_eq!(heap.peek().unwrap(), sorted.last().unwrap());
|
||||
assert_eq!(heap.pop().unwrap(), sorted.pop().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![2, 4, 9]);
|
||||
assert_eq!(heap.len(), 3);
|
||||
assert!(*heap.peek().unwrap() == 9);
|
||||
heap.push(11);
|
||||
assert_eq!(heap.len(), 4);
|
||||
assert!(*heap.peek().unwrap() == 11);
|
||||
heap.push(5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert!(*heap.peek().unwrap() == 11);
|
||||
heap.push(27);
|
||||
assert_eq!(heap.len(), 6);
|
||||
assert!(*heap.peek().unwrap() == 27);
|
||||
heap.push(3);
|
||||
assert_eq!(heap.len(), 7);
|
||||
assert!(*heap.peek().unwrap() == 27);
|
||||
heap.push(103);
|
||||
assert_eq!(heap.len(), 8);
|
||||
assert!(*heap.peek().unwrap() == 103);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_unique() {
|
||||
let mut heap = BinaryHeap::<Box<_>>::from_vec(vec![box 2, box 4, box 9]);
|
||||
assert_eq!(heap.len(), 3);
|
||||
assert!(*heap.peek().unwrap() == box 9);
|
||||
heap.push(box 11);
|
||||
assert_eq!(heap.len(), 4);
|
||||
assert!(*heap.peek().unwrap() == box 11);
|
||||
heap.push(box 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert!(*heap.peek().unwrap() == box 11);
|
||||
heap.push(box 27);
|
||||
assert_eq!(heap.len(), 6);
|
||||
assert!(*heap.peek().unwrap() == box 27);
|
||||
heap.push(box 3);
|
||||
assert_eq!(heap.len(), 7);
|
||||
assert!(*heap.peek().unwrap() == box 27);
|
||||
heap.push(box 103);
|
||||
assert_eq!(heap.len(), 8);
|
||||
assert!(*heap.peek().unwrap() == box 103);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_pop() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(6), 6);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(0), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(4), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.push_pop(1), 4);
|
||||
assert_eq!(heap.len(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace() {
|
||||
let mut heap = BinaryHeap::from_vec(vec![5, 5, 2, 1, 3]);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(6).unwrap(), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(0).unwrap(), 6);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(4).unwrap(), 5);
|
||||
assert_eq!(heap.len(), 5);
|
||||
assert_eq!(heap.replace(1).unwrap(), 4);
|
||||
assert_eq!(heap.len(), 5);
|
||||
}
|
||||
|
||||
fn check_to_vec(mut data: Vec<i32>) {
|
||||
let heap = BinaryHeap::from_vec(data.clone());
|
||||
let mut v = heap.clone().into_vec();
|
||||
v.sort();
|
||||
data.sort();
|
||||
|
||||
assert_eq!(v, data);
|
||||
assert_eq!(heap.into_sorted_vec(), data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_vec() {
|
||||
check_to_vec(vec![]);
|
||||
check_to_vec(vec![5]);
|
||||
check_to_vec(vec![3, 2]);
|
||||
check_to_vec(vec![2, 3]);
|
||||
check_to_vec(vec![5, 1, 2]);
|
||||
check_to_vec(vec![1, 100, 2, 3]);
|
||||
check_to_vec(vec![1, 3, 5, 7, 9, 2, 4, 6, 8, 0]);
|
||||
check_to_vec(vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]);
|
||||
check_to_vec(vec![9, 11, 9, 9, 9, 9, 11, 2, 3, 4, 11, 9, 0, 0, 0, 0]);
|
||||
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||
check_to_vec(vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]);
|
||||
check_to_vec(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 1, 2]);
|
||||
check_to_vec(vec![5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_pop() {
|
||||
let mut heap = BinaryHeap::<i32>::new();
|
||||
assert!(heap.pop().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_peek() {
|
||||
let empty = BinaryHeap::<i32>::new();
|
||||
assert!(empty.peek().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_replace() {
|
||||
let mut heap = BinaryHeap::new();
|
||||
assert!(heap.replace(5).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = vec![9, 8, 7, 6, 5, 4, 3, 2, 1];
|
||||
|
||||
let mut q: BinaryHeap<_> = xs.iter().rev().cloned().collect();
|
||||
|
||||
for &x in &xs {
|
||||
assert_eq!(q.pop().unwrap(), x);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain() {
|
||||
let mut q: BinaryHeap<_> = [9, 8, 7, 6, 5, 4, 3, 2, 1].iter().cloned().collect();
|
||||
|
||||
assert_eq!(q.drain().take(5).count(), 5);
|
||||
|
||||
assert!(q.is_empty());
|
||||
}
|
12
src/libcollectionstest/bit/mod.rs
Normal file
12
src/libcollectionstest/bit/mod.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
// Copyright 2012-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.
|
||||
|
||||
mod set;
|
||||
mod vec;
|
441
src/libcollectionstest/bit/set.rs
Normal file
441
src/libcollectionstest/bit/set.rs
Normal file
|
@ -0,0 +1,441 @@
|
|||
// Copyright 2012-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.
|
||||
|
||||
use std::cmp::Ordering::{Equal, Greater, Less};
|
||||
use std::collections::{BitSet, BitVec};
|
||||
use std::iter::range_step;
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_show() {
|
||||
let mut s = BitSet::new();
|
||||
s.insert(1);
|
||||
s.insert(10);
|
||||
s.insert(50);
|
||||
s.insert(2);
|
||||
assert_eq!("{1, 2, 10, 50}", format!("{:?}", s));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_from_usizes() {
|
||||
let usizes = vec![0, 2, 2, 3];
|
||||
let a: BitSet = usizes.into_iter().collect();
|
||||
let mut b = BitSet::new();
|
||||
b.insert(0);
|
||||
b.insert(2);
|
||||
b.insert(3);
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_iterator() {
|
||||
let usizes = vec![0, 2, 2, 3];
|
||||
let bit_vec: BitSet = usizes.into_iter().collect();
|
||||
|
||||
let idxs: Vec<_> = bit_vec.iter().collect();
|
||||
assert_eq!(idxs, [0, 2, 3]);
|
||||
|
||||
let long: BitSet = (0..10000).filter(|&n| n % 2 == 0).collect();
|
||||
let real: Vec<_> = range_step(0, 10000, 2).collect();
|
||||
|
||||
let idxs: Vec<_> = long.iter().collect();
|
||||
assert_eq!(idxs, real);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_frombit_vec_init() {
|
||||
let bools = [true, false];
|
||||
let lengths = [10, 64, 100];
|
||||
for &b in &bools {
|
||||
for &l in &lengths {
|
||||
let bitset = BitSet::from_bit_vec(BitVec::from_elem(l, b));
|
||||
assert_eq!(bitset.contains(&1), b);
|
||||
assert_eq!(bitset.contains(&(l-1)), b);
|
||||
assert!(!bitset.contains(&l));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_masking() {
|
||||
let b = BitVec::from_elem(140, true);
|
||||
let mut bs = BitSet::from_bit_vec(b);
|
||||
assert!(bs.contains(&139));
|
||||
assert!(!bs.contains(&140));
|
||||
assert!(bs.insert(150));
|
||||
assert!(!bs.contains(&140));
|
||||
assert!(!bs.contains(&149));
|
||||
assert!(bs.contains(&150));
|
||||
assert!(!bs.contains(&151));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_basic() {
|
||||
let mut b = BitSet::new();
|
||||
assert!(b.insert(3));
|
||||
assert!(!b.insert(3));
|
||||
assert!(b.contains(&3));
|
||||
assert!(b.insert(4));
|
||||
assert!(!b.insert(4));
|
||||
assert!(b.contains(&3));
|
||||
assert!(b.insert(400));
|
||||
assert!(!b.insert(400));
|
||||
assert!(b.contains(&400));
|
||||
assert_eq!(b.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_intersection() {
|
||||
let mut a = BitSet::new();
|
||||
let mut b = BitSet::new();
|
||||
|
||||
assert!(a.insert(11));
|
||||
assert!(a.insert(1));
|
||||
assert!(a.insert(3));
|
||||
assert!(a.insert(77));
|
||||
assert!(a.insert(103));
|
||||
assert!(a.insert(5));
|
||||
|
||||
assert!(b.insert(2));
|
||||
assert!(b.insert(11));
|
||||
assert!(b.insert(77));
|
||||
assert!(b.insert(5));
|
||||
assert!(b.insert(3));
|
||||
|
||||
let expected = [3, 5, 11, 77];
|
||||
let actual: Vec<_> = a.intersection(&b).collect();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_difference() {
|
||||
let mut a = BitSet::new();
|
||||
let mut b = BitSet::new();
|
||||
|
||||
assert!(a.insert(1));
|
||||
assert!(a.insert(3));
|
||||
assert!(a.insert(5));
|
||||
assert!(a.insert(200));
|
||||
assert!(a.insert(500));
|
||||
|
||||
assert!(b.insert(3));
|
||||
assert!(b.insert(200));
|
||||
|
||||
let expected = [1, 5, 500];
|
||||
let actual: Vec<_> = a.difference(&b).collect();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_symmetric_difference() {
|
||||
let mut a = BitSet::new();
|
||||
let mut b = BitSet::new();
|
||||
|
||||
assert!(a.insert(1));
|
||||
assert!(a.insert(3));
|
||||
assert!(a.insert(5));
|
||||
assert!(a.insert(9));
|
||||
assert!(a.insert(11));
|
||||
|
||||
assert!(b.insert(3));
|
||||
assert!(b.insert(9));
|
||||
assert!(b.insert(14));
|
||||
assert!(b.insert(220));
|
||||
|
||||
let expected = [1, 5, 11, 14, 220];
|
||||
let actual: Vec<_> = a.symmetric_difference(&b).collect();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_union() {
|
||||
let mut a = BitSet::new();
|
||||
let mut b = BitSet::new();
|
||||
assert!(a.insert(1));
|
||||
assert!(a.insert(3));
|
||||
assert!(a.insert(5));
|
||||
assert!(a.insert(9));
|
||||
assert!(a.insert(11));
|
||||
assert!(a.insert(160));
|
||||
assert!(a.insert(19));
|
||||
assert!(a.insert(24));
|
||||
assert!(a.insert(200));
|
||||
|
||||
assert!(b.insert(1));
|
||||
assert!(b.insert(5));
|
||||
assert!(b.insert(9));
|
||||
assert!(b.insert(13));
|
||||
assert!(b.insert(19));
|
||||
|
||||
let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160, 200];
|
||||
let actual: Vec<_> = a.union(&b).collect();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_subset() {
|
||||
let mut set1 = BitSet::new();
|
||||
let mut set2 = BitSet::new();
|
||||
|
||||
assert!(set1.is_subset(&set2)); // {} {}
|
||||
set2.insert(100);
|
||||
assert!(set1.is_subset(&set2)); // {} { 1 }
|
||||
set2.insert(200);
|
||||
assert!(set1.is_subset(&set2)); // {} { 1, 2 }
|
||||
set1.insert(200);
|
||||
assert!(set1.is_subset(&set2)); // { 2 } { 1, 2 }
|
||||
set1.insert(300);
|
||||
assert!(!set1.is_subset(&set2)); // { 2, 3 } { 1, 2 }
|
||||
set2.insert(300);
|
||||
assert!(set1.is_subset(&set2)); // { 2, 3 } { 1, 2, 3 }
|
||||
set2.insert(400);
|
||||
assert!(set1.is_subset(&set2)); // { 2, 3 } { 1, 2, 3, 4 }
|
||||
set2.remove(&100);
|
||||
assert!(set1.is_subset(&set2)); // { 2, 3 } { 2, 3, 4 }
|
||||
set2.remove(&300);
|
||||
assert!(!set1.is_subset(&set2)); // { 2, 3 } { 2, 4 }
|
||||
set1.remove(&300);
|
||||
assert!(set1.is_subset(&set2)); // { 2 } { 2, 4 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_is_disjoint() {
|
||||
let a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b01000000]));
|
||||
let c = BitSet::new();
|
||||
let d = BitSet::from_bit_vec(BitVec::from_bytes(&[0b00110000]));
|
||||
|
||||
assert!(!a.is_disjoint(&d));
|
||||
assert!(!d.is_disjoint(&a));
|
||||
|
||||
assert!(a.is_disjoint(&b));
|
||||
assert!(a.is_disjoint(&c));
|
||||
assert!(b.is_disjoint(&a));
|
||||
assert!(b.is_disjoint(&c));
|
||||
assert!(c.is_disjoint(&a));
|
||||
assert!(c.is_disjoint(&b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_union_with() {
|
||||
//a should grow to include larger elements
|
||||
let mut a = BitSet::new();
|
||||
a.insert(0);
|
||||
let mut b = BitSet::new();
|
||||
b.insert(5);
|
||||
let expected = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10000100]));
|
||||
a.union_with(&b);
|
||||
assert_eq!(a, expected);
|
||||
|
||||
// Standard
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let mut b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b01100010]));
|
||||
let c = a.clone();
|
||||
a.union_with(&b);
|
||||
b.union_with(&c);
|
||||
assert_eq!(a.len(), 4);
|
||||
assert_eq!(b.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_intersect_with() {
|
||||
// Explicitly 0'ed bits
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let mut b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b00000000]));
|
||||
let c = a.clone();
|
||||
a.intersect_with(&b);
|
||||
b.intersect_with(&c);
|
||||
assert!(a.is_empty());
|
||||
assert!(b.is_empty());
|
||||
|
||||
// Uninitialized bits should behave like 0's
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let mut b = BitSet::new();
|
||||
let c = a.clone();
|
||||
a.intersect_with(&b);
|
||||
b.intersect_with(&c);
|
||||
assert!(a.is_empty());
|
||||
assert!(b.is_empty());
|
||||
|
||||
// Standard
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let mut b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b01100010]));
|
||||
let c = a.clone();
|
||||
a.intersect_with(&b);
|
||||
b.intersect_with(&c);
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(b.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_difference_with() {
|
||||
// Explicitly 0'ed bits
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b00000000]));
|
||||
let b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
a.difference_with(&b);
|
||||
assert!(a.is_empty());
|
||||
|
||||
// Uninitialized bits should behave like 0's
|
||||
let mut a = BitSet::new();
|
||||
let b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b11111111]));
|
||||
a.difference_with(&b);
|
||||
assert!(a.is_empty());
|
||||
|
||||
// Standard
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let mut b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b01100010]));
|
||||
let c = a.clone();
|
||||
a.difference_with(&b);
|
||||
b.difference_with(&c);
|
||||
assert_eq!(a.len(), 1);
|
||||
assert_eq!(b.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_symmetric_difference_with() {
|
||||
//a should grow to include larger elements
|
||||
let mut a = BitSet::new();
|
||||
a.insert(0);
|
||||
a.insert(1);
|
||||
let mut b = BitSet::new();
|
||||
b.insert(1);
|
||||
b.insert(5);
|
||||
let expected = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10000100]));
|
||||
a.symmetric_difference_with(&b);
|
||||
assert_eq!(a, expected);
|
||||
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let b = BitSet::new();
|
||||
let c = a.clone();
|
||||
a.symmetric_difference_with(&b);
|
||||
assert_eq!(a, c);
|
||||
|
||||
// Standard
|
||||
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b11100010]));
|
||||
let mut b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b01101010]));
|
||||
let c = a.clone();
|
||||
a.symmetric_difference_with(&b);
|
||||
b.symmetric_difference_with(&c);
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(b.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_eq() {
|
||||
let a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b00000000]));
|
||||
let c = BitSet::new();
|
||||
|
||||
assert!(a == a);
|
||||
assert!(a != b);
|
||||
assert!(a != c);
|
||||
assert!(b == b);
|
||||
assert!(b == c);
|
||||
assert!(c == c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_set_cmp() {
|
||||
let a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100010]));
|
||||
let b = BitSet::from_bit_vec(BitVec::from_bytes(&[0b00000000]));
|
||||
let c = BitSet::new();
|
||||
|
||||
assert_eq!(a.cmp(&b), Greater);
|
||||
assert_eq!(a.cmp(&c), Greater);
|
||||
assert_eq!(b.cmp(&a), Less);
|
||||
assert_eq!(b.cmp(&c), Equal);
|
||||
assert_eq!(c.cmp(&a), Less);
|
||||
assert_eq!(c.cmp(&b), Equal);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_remove() {
|
||||
let mut a = BitSet::new();
|
||||
|
||||
assert!(a.insert(1));
|
||||
assert!(a.remove(&1));
|
||||
|
||||
assert!(a.insert(100));
|
||||
assert!(a.remove(&100));
|
||||
|
||||
assert!(a.insert(1000));
|
||||
assert!(a.remove(&1000));
|
||||
a.shrink_to_fit();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_clone() {
|
||||
let mut a = BitSet::new();
|
||||
|
||||
assert!(a.insert(1));
|
||||
assert!(a.insert(100));
|
||||
assert!(a.insert(1000));
|
||||
|
||||
let mut b = a.clone();
|
||||
|
||||
assert!(a == b);
|
||||
|
||||
assert!(b.remove(&1));
|
||||
assert!(a.contains(&1));
|
||||
|
||||
assert!(a.remove(&1000));
|
||||
assert!(b.contains(&1000));
|
||||
}
|
||||
|
||||
mod bench {
|
||||
use std::collections::{BitSet, BitVec};
|
||||
use std::rand::{Rng, self};
|
||||
use std::u32;
|
||||
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
const BENCH_BITS : usize = 1 << 14;
|
||||
|
||||
fn rng() -> rand::IsaacRng {
|
||||
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
||||
rand::SeedableRng::from_seed(seed)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vecset_small(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = BitSet::new();
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec.insert((r.next_u32() as usize) % u32::BITS as usize);
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vecset_big(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = BitSet::new();
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec.insert((r.next_u32() as usize) % BENCH_BITS);
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vecset_iter(b: &mut Bencher) {
|
||||
let bit_vec = BitSet::from_bit_vec(BitVec::from_fn(BENCH_BITS,
|
||||
|idx| {idx % 3 == 0}));
|
||||
b.iter(|| {
|
||||
let mut sum = 0;
|
||||
for idx in &bit_vec {
|
||||
sum += idx as usize;
|
||||
}
|
||||
sum
|
||||
})
|
||||
}
|
||||
}
|
729
src/libcollectionstest/bit/vec.rs
Normal file
729
src/libcollectionstest/bit/vec.rs
Normal file
|
@ -0,0 +1,729 @@
|
|||
// Copyright 2012-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.
|
||||
|
||||
use std::collections::BitVec;
|
||||
use std::u32;
|
||||
|
||||
#[test]
|
||||
fn test_to_str() {
|
||||
let zerolen = BitVec::new();
|
||||
assert_eq!(format!("{:?}", zerolen), "");
|
||||
|
||||
let eightbits = BitVec::from_elem(8, false);
|
||||
assert_eq!(format!("{:?}", eightbits), "00000000")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_0_elements() {
|
||||
let act = BitVec::new();
|
||||
let exp = Vec::new();
|
||||
assert!(act.eq_vec(&exp));
|
||||
assert!(act.none() && act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_1_element() {
|
||||
let mut act = BitVec::from_elem(1, false);
|
||||
assert!(act.eq_vec(&[false]));
|
||||
assert!(act.none() && !act.all());
|
||||
act = BitVec::from_elem(1, true);
|
||||
assert!(act.eq_vec(&[true]));
|
||||
assert!(!act.none() && act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2_elements() {
|
||||
let mut b = BitVec::from_elem(2, false);
|
||||
b.set(0, true);
|
||||
b.set(1, false);
|
||||
assert_eq!(format!("{:?}", b), "10");
|
||||
assert!(!b.none() && !b.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_10_elements() {
|
||||
let mut act;
|
||||
// all 0
|
||||
|
||||
act = BitVec::from_elem(10, false);
|
||||
assert!((act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false])));
|
||||
assert!(act.none() && !act.all());
|
||||
// all 1
|
||||
|
||||
act = BitVec::from_elem(10, true);
|
||||
assert!((act.eq_vec(&[true, true, true, true, true, true, true, true, true, true])));
|
||||
assert!(!act.none() && act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(10, false);
|
||||
act.set(0, true);
|
||||
act.set(1, true);
|
||||
act.set(2, true);
|
||||
act.set(3, true);
|
||||
act.set(4, true);
|
||||
assert!((act.eq_vec(&[true, true, true, true, true, false, false, false, false, false])));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(10, false);
|
||||
act.set(5, true);
|
||||
act.set(6, true);
|
||||
act.set(7, true);
|
||||
act.set(8, true);
|
||||
act.set(9, true);
|
||||
assert!((act.eq_vec(&[false, false, false, false, false, true, true, true, true, true])));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(10, false);
|
||||
act.set(0, true);
|
||||
act.set(3, true);
|
||||
act.set(6, true);
|
||||
act.set(9, true);
|
||||
assert!((act.eq_vec(&[true, false, false, true, false, false, true, false, false, true])));
|
||||
assert!(!act.none() && !act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_31_elements() {
|
||||
let mut act;
|
||||
// all 0
|
||||
|
||||
act = BitVec::from_elem(31, false);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false]));
|
||||
assert!(act.none() && !act.all());
|
||||
// all 1
|
||||
|
||||
act = BitVec::from_elem(31, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true]));
|
||||
assert!(!act.none() && act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(31, false);
|
||||
act.set(0, true);
|
||||
act.set(1, true);
|
||||
act.set(2, true);
|
||||
act.set(3, true);
|
||||
act.set(4, true);
|
||||
act.set(5, true);
|
||||
act.set(6, true);
|
||||
act.set(7, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(31, false);
|
||||
act.set(16, true);
|
||||
act.set(17, true);
|
||||
act.set(18, true);
|
||||
act.set(19, true);
|
||||
act.set(20, true);
|
||||
act.set(21, true);
|
||||
act.set(22, true);
|
||||
act.set(23, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, true, true, true, true, true, true, true,
|
||||
false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(31, false);
|
||||
act.set(24, true);
|
||||
act.set(25, true);
|
||||
act.set(26, true);
|
||||
act.set(27, true);
|
||||
act.set(28, true);
|
||||
act.set(29, true);
|
||||
act.set(30, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, true, true, true, true, true, true, true]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(31, false);
|
||||
act.set(3, true);
|
||||
act.set(17, true);
|
||||
act.set(30, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, true, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, true]));
|
||||
assert!(!act.none() && !act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_32_elements() {
|
||||
let mut act;
|
||||
// all 0
|
||||
|
||||
act = BitVec::from_elem(32, false);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false]));
|
||||
assert!(act.none() && !act.all());
|
||||
// all 1
|
||||
|
||||
act = BitVec::from_elem(32, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true, true]));
|
||||
assert!(!act.none() && act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(32, false);
|
||||
act.set(0, true);
|
||||
act.set(1, true);
|
||||
act.set(2, true);
|
||||
act.set(3, true);
|
||||
act.set(4, true);
|
||||
act.set(5, true);
|
||||
act.set(6, true);
|
||||
act.set(7, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(32, false);
|
||||
act.set(16, true);
|
||||
act.set(17, true);
|
||||
act.set(18, true);
|
||||
act.set(19, true);
|
||||
act.set(20, true);
|
||||
act.set(21, true);
|
||||
act.set(22, true);
|
||||
act.set(23, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, true, true, true, true, true, true, true,
|
||||
false, false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(32, false);
|
||||
act.set(24, true);
|
||||
act.set(25, true);
|
||||
act.set(26, true);
|
||||
act.set(27, true);
|
||||
act.set(28, true);
|
||||
act.set(29, true);
|
||||
act.set(30, true);
|
||||
act.set(31, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, true, true, true, true, true, true, true, true]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(32, false);
|
||||
act.set(3, true);
|
||||
act.set(17, true);
|
||||
act.set(30, true);
|
||||
act.set(31, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, true, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, true, true]));
|
||||
assert!(!act.none() && !act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_33_elements() {
|
||||
let mut act;
|
||||
// all 0
|
||||
|
||||
act = BitVec::from_elem(33, false);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false]));
|
||||
assert!(act.none() && !act.all());
|
||||
// all 1
|
||||
|
||||
act = BitVec::from_elem(33, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true, true, true, true, true, true, true, true, true,
|
||||
true, true, true, true, true, true, true]));
|
||||
assert!(!act.none() && act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(33, false);
|
||||
act.set(0, true);
|
||||
act.set(1, true);
|
||||
act.set(2, true);
|
||||
act.set(3, true);
|
||||
act.set(4, true);
|
||||
act.set(5, true);
|
||||
act.set(6, true);
|
||||
act.set(7, true);
|
||||
assert!(act.eq_vec(
|
||||
&[true, true, true, true, true, true, true, true, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(33, false);
|
||||
act.set(16, true);
|
||||
act.set(17, true);
|
||||
act.set(18, true);
|
||||
act.set(19, true);
|
||||
act.set(20, true);
|
||||
act.set(21, true);
|
||||
act.set(22, true);
|
||||
act.set(23, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, true, true, true, true, true, true, true,
|
||||
false, false, false, false, false, false, false, false, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(33, false);
|
||||
act.set(24, true);
|
||||
act.set(25, true);
|
||||
act.set(26, true);
|
||||
act.set(27, true);
|
||||
act.set(28, true);
|
||||
act.set(29, true);
|
||||
act.set(30, true);
|
||||
act.set(31, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, false, false, false, false, false,
|
||||
false, false, true, true, true, true, true, true, true, true, false]));
|
||||
assert!(!act.none() && !act.all());
|
||||
// mixed
|
||||
|
||||
act = BitVec::from_elem(33, false);
|
||||
act.set(3, true);
|
||||
act.set(17, true);
|
||||
act.set(30, true);
|
||||
act.set(31, true);
|
||||
act.set(32, true);
|
||||
assert!(act.eq_vec(
|
||||
&[false, false, false, true, false, false, false, false, false, false, false, false,
|
||||
false, false, false, false, false, true, false, false, false, false, false, false,
|
||||
false, false, false, false, false, false, true, true, true]));
|
||||
assert!(!act.none() && !act.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_equal_differing_sizes() {
|
||||
let v0 = BitVec::from_elem(10, false);
|
||||
let v1 = BitVec::from_elem(11, false);
|
||||
assert!(v0 != v1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_equal_greatly_differing_sizes() {
|
||||
let v0 = BitVec::from_elem(10, false);
|
||||
let v1 = BitVec::from_elem(110, false);
|
||||
assert!(v0 != v1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_equal_sneaky_small() {
|
||||
let mut a = BitVec::from_elem(1, false);
|
||||
a.set(0, true);
|
||||
|
||||
let mut b = BitVec::from_elem(1, true);
|
||||
b.set(0, true);
|
||||
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_equal_sneaky_big() {
|
||||
let mut a = BitVec::from_elem(100, false);
|
||||
for i in 0..100 {
|
||||
a.set(i, true);
|
||||
}
|
||||
|
||||
let mut b = BitVec::from_elem(100, true);
|
||||
for i in 0..100 {
|
||||
b.set(i, true);
|
||||
}
|
||||
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bytes() {
|
||||
let bit_vec = BitVec::from_bytes(&[0b10110110, 0b00000000, 0b11111111]);
|
||||
let str = concat!("10110110", "00000000", "11111111");
|
||||
assert_eq!(format!("{:?}", bit_vec), str);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_bytes() {
|
||||
let mut bv = BitVec::from_elem(3, true);
|
||||
bv.set(1, false);
|
||||
assert_eq!(bv.to_bytes(), [0b10100000]);
|
||||
|
||||
let mut bv = BitVec::from_elem(9, false);
|
||||
bv.set(2, true);
|
||||
bv.set(8, true);
|
||||
assert_eq!(bv.to_bytes(), [0b00100000, 0b10000000]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_bools() {
|
||||
let bools = vec![true, false, true, true];
|
||||
let bit_vec: BitVec = bools.iter().map(|n| *n).collect();
|
||||
assert_eq!(format!("{:?}", bit_vec), "1011");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_bools() {
|
||||
let bools = vec![false, false, true, false, false, true, true, false];
|
||||
assert_eq!(BitVec::from_bytes(&[0b00100110]).iter().collect::<Vec<bool>>(), bools);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_iterator() {
|
||||
let bools = vec![true, false, true, true];
|
||||
let bit_vec: BitVec = bools.iter().map(|n| *n).collect();
|
||||
|
||||
assert_eq!(bit_vec.iter().collect::<Vec<bool>>(), bools);
|
||||
|
||||
let long: Vec<_> = (0..10000).map(|i| i % 2 == 0).collect();
|
||||
let bit_vec: BitVec = long.iter().map(|n| *n).collect();
|
||||
assert_eq!(bit_vec.iter().collect::<Vec<bool>>(), long)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_small_difference() {
|
||||
let mut b1 = BitVec::from_elem(3, false);
|
||||
let mut b2 = BitVec::from_elem(3, false);
|
||||
b1.set(0, true);
|
||||
b1.set(1, true);
|
||||
b2.set(1, true);
|
||||
b2.set(2, true);
|
||||
assert!(b1.difference(&b2));
|
||||
assert!(b1[0]);
|
||||
assert!(!b1[1]);
|
||||
assert!(!b1[2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_difference() {
|
||||
let mut b1 = BitVec::from_elem(100, false);
|
||||
let mut b2 = BitVec::from_elem(100, false);
|
||||
b1.set(0, true);
|
||||
b1.set(40, true);
|
||||
b2.set(40, true);
|
||||
b2.set(80, true);
|
||||
assert!(b1.difference(&b2));
|
||||
assert!(b1[0]);
|
||||
assert!(!b1[40]);
|
||||
assert!(!b1[80]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_small_clear() {
|
||||
let mut b = BitVec::from_elem(14, true);
|
||||
assert!(!b.none() && b.all());
|
||||
b.clear();
|
||||
assert!(b.none() && !b.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_clear() {
|
||||
let mut b = BitVec::from_elem(140, true);
|
||||
assert!(!b.none() && b.all());
|
||||
b.clear();
|
||||
assert!(b.none() && !b.all());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_lt() {
|
||||
let mut a = BitVec::from_elem(5, false);
|
||||
let mut b = BitVec::from_elem(5, false);
|
||||
|
||||
assert!(!(a < b) && !(b < a));
|
||||
b.set(2, true);
|
||||
assert!(a < b);
|
||||
a.set(3, true);
|
||||
assert!(a < b);
|
||||
a.set(2, true);
|
||||
assert!(!(a < b) && b < a);
|
||||
b.set(0, true);
|
||||
assert!(a < b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord() {
|
||||
let mut a = BitVec::from_elem(5, false);
|
||||
let mut b = BitVec::from_elem(5, false);
|
||||
|
||||
assert!(a <= b && a >= b);
|
||||
a.set(1, true);
|
||||
assert!(a > b && a >= b);
|
||||
assert!(b < a && b <= a);
|
||||
b.set(1, true);
|
||||
b.set(2, true);
|
||||
assert!(b > a && b >= a);
|
||||
assert!(a < b && a <= b);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_small_bit_vec_tests() {
|
||||
let v = BitVec::from_bytes(&[0]);
|
||||
assert!(!v.all());
|
||||
assert!(!v.any());
|
||||
assert!(v.none());
|
||||
|
||||
let v = BitVec::from_bytes(&[0b00010100]);
|
||||
assert!(!v.all());
|
||||
assert!(v.any());
|
||||
assert!(!v.none());
|
||||
|
||||
let v = BitVec::from_bytes(&[0xFF]);
|
||||
assert!(v.all());
|
||||
assert!(v.any());
|
||||
assert!(!v.none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_bit_vec_tests() {
|
||||
let v = BitVec::from_bytes(&[ // 88 bits
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0]);
|
||||
assert!(!v.all());
|
||||
assert!(!v.any());
|
||||
assert!(v.none());
|
||||
|
||||
let v = BitVec::from_bytes(&[ // 88 bits
|
||||
0, 0, 0b00010100, 0,
|
||||
0, 0, 0, 0b00110100,
|
||||
0, 0, 0]);
|
||||
assert!(!v.all());
|
||||
assert!(v.any());
|
||||
assert!(!v.none());
|
||||
|
||||
let v = BitVec::from_bytes(&[ // 88 bits
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF]);
|
||||
assert!(v.all());
|
||||
assert!(v.any());
|
||||
assert!(!v.none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_push_pop() {
|
||||
let mut s = BitVec::from_elem(5 * u32::BITS as usize - 2, false);
|
||||
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
|
||||
assert_eq!(s[5 * u32::BITS as usize - 3], false);
|
||||
s.push(true);
|
||||
s.push(true);
|
||||
assert_eq!(s[5 * u32::BITS as usize - 2], true);
|
||||
assert_eq!(s[5 * u32::BITS as usize - 1], true);
|
||||
// Here the internal vector will need to be extended
|
||||
s.push(false);
|
||||
assert_eq!(s[5 * u32::BITS as usize], false);
|
||||
s.push(false);
|
||||
assert_eq!(s[5 * u32::BITS as usize + 1], false);
|
||||
assert_eq!(s.len(), 5 * u32::BITS as usize + 2);
|
||||
// Pop it all off
|
||||
assert_eq!(s.pop(), Some(false));
|
||||
assert_eq!(s.pop(), Some(false));
|
||||
assert_eq!(s.pop(), Some(true));
|
||||
assert_eq!(s.pop(), Some(true));
|
||||
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_truncate() {
|
||||
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
|
||||
|
||||
assert_eq!(s, BitVec::from_elem(5 * u32::BITS as usize, true));
|
||||
assert_eq!(s.len(), 5 * u32::BITS as usize);
|
||||
s.truncate(4 * u32::BITS as usize);
|
||||
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
|
||||
assert_eq!(s.len(), 4 * u32::BITS as usize);
|
||||
// Truncating to a size > s.len() should be a noop
|
||||
s.truncate(5 * u32::BITS as usize);
|
||||
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
|
||||
assert_eq!(s.len(), 4 * u32::BITS as usize);
|
||||
s.truncate(3 * u32::BITS as usize - 10);
|
||||
assert_eq!(s, BitVec::from_elem(3 * u32::BITS as usize - 10, true));
|
||||
assert_eq!(s.len(), 3 * u32::BITS as usize - 10);
|
||||
s.truncate(0);
|
||||
assert_eq!(s, BitVec::from_elem(0, true));
|
||||
assert_eq!(s.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_reserve() {
|
||||
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
|
||||
// Check capacity
|
||||
assert!(s.capacity() >= 5 * u32::BITS as usize);
|
||||
s.reserve(2 * u32::BITS as usize);
|
||||
assert!(s.capacity() >= 7 * u32::BITS as usize);
|
||||
s.reserve(7 * u32::BITS as usize);
|
||||
assert!(s.capacity() >= 12 * u32::BITS as usize);
|
||||
s.reserve_exact(7 * u32::BITS as usize);
|
||||
assert!(s.capacity() >= 12 * u32::BITS as usize);
|
||||
s.reserve(7 * u32::BITS as usize + 1);
|
||||
assert!(s.capacity() >= 12 * u32::BITS as usize + 1);
|
||||
// Check that length hasn't changed
|
||||
assert_eq!(s.len(), 5 * u32::BITS as usize);
|
||||
s.push(true);
|
||||
s.push(false);
|
||||
s.push(true);
|
||||
assert_eq!(s[5 * u32::BITS as usize - 1], true);
|
||||
assert_eq!(s[5 * u32::BITS as usize - 0], true);
|
||||
assert_eq!(s[5 * u32::BITS as usize + 1], false);
|
||||
assert_eq!(s[5 * u32::BITS as usize + 2], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_grow() {
|
||||
let mut bit_vec = BitVec::from_bytes(&[0b10110110, 0b00000000, 0b10101010]);
|
||||
bit_vec.grow(32, true);
|
||||
assert_eq!(bit_vec, BitVec::from_bytes(&[0b10110110, 0b00000000, 0b10101010,
|
||||
0xFF, 0xFF, 0xFF, 0xFF]));
|
||||
bit_vec.grow(64, false);
|
||||
assert_eq!(bit_vec, BitVec::from_bytes(&[0b10110110, 0b00000000, 0b10101010,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0]));
|
||||
bit_vec.grow(16, true);
|
||||
assert_eq!(bit_vec, BitVec::from_bytes(&[0b10110110, 0b00000000, 0b10101010,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bit_vec_extend() {
|
||||
let mut bit_vec = BitVec::from_bytes(&[0b10110110, 0b00000000, 0b11111111]);
|
||||
let ext = BitVec::from_bytes(&[0b01001001, 0b10010010, 0b10111101]);
|
||||
bit_vec.extend(ext.iter());
|
||||
assert_eq!(bit_vec, BitVec::from_bytes(&[0b10110110, 0b00000000, 0b11111111,
|
||||
0b01001001, 0b10010010, 0b10111101]));
|
||||
}
|
||||
|
||||
mod bench {
|
||||
use std::collections::BitVec;
|
||||
use std::u32;
|
||||
use std::rand::{Rng, self};
|
||||
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
const BENCH_BITS : usize = 1 << 14;
|
||||
|
||||
fn rng() -> rand::IsaacRng {
|
||||
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
||||
rand::SeedableRng::from_seed(seed)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_usize_small(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = 0 as usize;
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS as usize);
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_set_big_fixed(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = BitVec::from_elem(BENCH_BITS, false);
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec.set((r.next_u32() as usize) % BENCH_BITS, true);
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_set_big_variable(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = BitVec::from_elem(BENCH_BITS, false);
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec.set((r.next_u32() as usize) % BENCH_BITS, r.gen());
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_set_small(b: &mut Bencher) {
|
||||
let mut r = rng();
|
||||
let mut bit_vec = BitVec::from_elem(u32::BITS as usize, false);
|
||||
b.iter(|| {
|
||||
for _ in 0..100 {
|
||||
bit_vec.set((r.next_u32() as usize) % u32::BITS as usize, true);
|
||||
}
|
||||
black_box(&bit_vec);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vec_big_union(b: &mut Bencher) {
|
||||
let mut b1 = BitVec::from_elem(BENCH_BITS, false);
|
||||
let b2 = BitVec::from_elem(BENCH_BITS, false);
|
||||
b.iter(|| {
|
||||
b1.union(&b2)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vec_small_iter(b: &mut Bencher) {
|
||||
let bit_vec = BitVec::from_elem(u32::BITS as usize, false);
|
||||
b.iter(|| {
|
||||
let mut sum = 0;
|
||||
for _ in 0..10 {
|
||||
for pres in &bit_vec {
|
||||
sum += pres as usize;
|
||||
}
|
||||
}
|
||||
sum
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bit_vec_big_iter(b: &mut Bencher) {
|
||||
let bit_vec = BitVec::from_elem(BENCH_BITS, false);
|
||||
b.iter(|| {
|
||||
let mut sum = 0;
|
||||
for pres in &bit_vec {
|
||||
sum += pres as usize;
|
||||
}
|
||||
sum
|
||||
})
|
||||
}
|
||||
}
|
299
src/libcollectionstest/btree/map.rs
Normal file
299
src/libcollectionstest/btree/map.rs
Normal file
|
@ -0,0 +1,299 @@
|
|||
// 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.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::Bound::{Excluded, Included, Unbounded, self};
|
||||
use std::collections::btree_map::Entry::{Occupied, Vacant};
|
||||
use std::iter::range_inclusive;
|
||||
|
||||
#[test]
|
||||
fn test_basic_large() {
|
||||
let mut map = BTreeMap::new();
|
||||
let size = 10000;
|
||||
assert_eq!(map.len(), 0);
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.insert(i, 10*i), None);
|
||||
assert_eq!(map.len(), i + 1);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.get(&i).unwrap(), &(i*10));
|
||||
}
|
||||
|
||||
for i in size..size*2 {
|
||||
assert_eq!(map.get(&i), None);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.insert(i, 100*i), Some(10*i));
|
||||
assert_eq!(map.len(), size);
|
||||
}
|
||||
|
||||
for i in 0..size {
|
||||
assert_eq!(map.get(&i).unwrap(), &(i*100));
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.remove(&(i*2)), Some(i*200));
|
||||
assert_eq!(map.len(), size - i - 1);
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.get(&(2*i)), None);
|
||||
assert_eq!(map.get(&(2*i+1)).unwrap(), &(i*200 + 100));
|
||||
}
|
||||
|
||||
for i in 0..size/2 {
|
||||
assert_eq!(map.remove(&(2*i)), None);
|
||||
assert_eq!(map.remove(&(2*i+1)), Some(i*200 + 100));
|
||||
assert_eq!(map.len(), size/2 - i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_small() {
|
||||
let mut map = BTreeMap::new();
|
||||
assert_eq!(map.remove(&1), None);
|
||||
assert_eq!(map.get(&1), None);
|
||||
assert_eq!(map.insert(1, 1), None);
|
||||
assert_eq!(map.get(&1), Some(&1));
|
||||
assert_eq!(map.insert(1, 2), Some(1));
|
||||
assert_eq!(map.get(&1), Some(&2));
|
||||
assert_eq!(map.insert(2, 4), None);
|
||||
assert_eq!(map.get(&2), Some(&4));
|
||||
assert_eq!(map.remove(&1), Some(2));
|
||||
assert_eq!(map.remove(&2), Some(4));
|
||||
assert_eq!(map.remove(&1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T) where T: Iterator<Item=(usize, usize)> {
|
||||
for i in 0..size {
|
||||
assert_eq!(iter.size_hint(), (size - i, Some(size - i)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_rev() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T) where T: Iterator<Item=(usize, usize)> {
|
||||
for i in 0..size {
|
||||
assert_eq!(iter.size_hint(), (size - i, Some(size - i)));
|
||||
assert_eq!(iter.next().unwrap(), (size - i - 1, size - i - 1));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().rev().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().rev().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter().rev());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_mixed() {
|
||||
let size = 10000;
|
||||
|
||||
// Forwards
|
||||
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test<T>(size: usize, mut iter: T)
|
||||
where T: Iterator<Item=(usize, usize)> + DoubleEndedIterator {
|
||||
for i in 0..size / 4 {
|
||||
assert_eq!(iter.size_hint(), (size - i * 2, Some(size - i * 2)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
assert_eq!(iter.next_back().unwrap(), (size - i - 1, size - i - 1));
|
||||
}
|
||||
for i in size / 4..size * 3 / 4 {
|
||||
assert_eq!(iter.size_hint(), (size * 3 / 4 - i, Some(size * 3 / 4 - i)));
|
||||
assert_eq!(iter.next().unwrap(), (i, i));
|
||||
}
|
||||
assert_eq!(iter.size_hint(), (0, Some(0)));
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
test(size, map.iter().map(|(&k, &v)| (k, v)));
|
||||
test(size, map.iter_mut().map(|(&k, &mut v)| (k, v)));
|
||||
test(size, map.into_iter());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_small() {
|
||||
let size = 5;
|
||||
|
||||
// Forwards
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
let mut j = 0;
|
||||
for ((&k, &v), i) in map.range(Included(&2), Unbounded).zip(2..size) {
|
||||
assert_eq!(k, i);
|
||||
assert_eq!(v, i);
|
||||
j += 1;
|
||||
}
|
||||
assert_eq!(j, size - 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_1000() {
|
||||
let size = 1000;
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
fn test(map: &BTreeMap<u32, u32>, size: u32, min: Bound<&u32>, max: Bound<&u32>) {
|
||||
let mut kvs = map.range(min, max).map(|(&k, &v)| (k, v));
|
||||
let mut pairs = (0..size).map(|i| (i, i));
|
||||
|
||||
for (kv, pair) in kvs.by_ref().zip(pairs.by_ref()) {
|
||||
assert_eq!(kv, pair);
|
||||
}
|
||||
assert_eq!(kvs.next(), None);
|
||||
assert_eq!(pairs.next(), None);
|
||||
}
|
||||
test(&map, size, Included(&0), Excluded(&size));
|
||||
test(&map, size, Unbounded, Excluded(&size));
|
||||
test(&map, size, Included(&0), Included(&(size - 1)));
|
||||
test(&map, size, Unbounded, Included(&(size - 1)));
|
||||
test(&map, size, Included(&0), Unbounded);
|
||||
test(&map, size, Unbounded, Unbounded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range() {
|
||||
let size = 200;
|
||||
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
|
||||
|
||||
for i in 0..size {
|
||||
for j in i..size {
|
||||
let mut kvs = map.range(Included(&i), Included(&j)).map(|(&k, &v)| (k, v));
|
||||
let mut pairs = range_inclusive(i, j).map(|i| (i, i));
|
||||
|
||||
for (kv, pair) in kvs.by_ref().zip(pairs.by_ref()) {
|
||||
assert_eq!(kv, pair);
|
||||
}
|
||||
assert_eq!(kvs.next(), None);
|
||||
assert_eq!(pairs.next(), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry(){
|
||||
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
|
||||
|
||||
let mut map: BTreeMap<_, _> = xs.iter().cloned().collect();
|
||||
|
||||
// Existing key (insert)
|
||||
match map.entry(1) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
assert_eq!(view.get(), &10);
|
||||
assert_eq!(view.insert(100), 10);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&1).unwrap(), &100);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
|
||||
// Existing key (update)
|
||||
match map.entry(2) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
let v = view.get_mut();
|
||||
*v *= 10;
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&2).unwrap(), &200);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
// Existing key (take)
|
||||
match map.entry(3) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(view) => {
|
||||
assert_eq!(view.remove(), 30);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&3), None);
|
||||
assert_eq!(map.len(), 5);
|
||||
|
||||
|
||||
// Inexistent key (insert)
|
||||
match map.entry(10) {
|
||||
Occupied(_) => unreachable!(),
|
||||
Vacant(view) => {
|
||||
assert_eq!(*view.insert(1000), 1000);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&10).unwrap(), &1000);
|
||||
assert_eq!(map.len(), 6);
|
||||
}
|
||||
|
||||
mod bench {
|
||||
use std::collections::BTreeMap;
|
||||
use std::rand::{Rng, weak_rng};
|
||||
|
||||
use test::{Bencher, black_box};
|
||||
|
||||
map_insert_rand_bench!{insert_rand_100, 100, BTreeMap}
|
||||
map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_insert_seq_bench!{insert_seq_100, 100, BTreeMap}
|
||||
map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_find_rand_bench!{find_rand_100, 100, BTreeMap}
|
||||
map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap}
|
||||
|
||||
map_find_seq_bench!{find_seq_100, 100, BTreeMap}
|
||||
map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap}
|
||||
|
||||
fn bench_iter(b: &mut Bencher, size: i32) {
|
||||
let mut map = BTreeMap::<i32, i32>::new();
|
||||
let mut rng = weak_rng();
|
||||
|
||||
for _ in 0..size {
|
||||
map.insert(rng.gen(), rng.gen());
|
||||
}
|
||||
|
||||
b.iter(|| {
|
||||
for entry in &map {
|
||||
black_box(entry);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_20(b: &mut Bencher) {
|
||||
bench_iter(b, 20);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_1000(b: &mut Bencher) {
|
||||
bench_iter(b, 1000);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
pub fn iter_100000(b: &mut Bencher) {
|
||||
bench_iter(b, 100000);
|
||||
}
|
||||
}
|
12
src/libcollectionstest/btree/mod.rs
Normal file
12
src/libcollectionstest/btree/mod.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
// 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.
|
||||
|
||||
mod map;
|
||||
mod set;
|
180
src/libcollectionstest/btree/set.rs
Normal file
180
src/libcollectionstest/btree/set.rs
Normal file
|
@ -0,0 +1,180 @@
|
|||
// 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.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::hash::{SipHasher, self};
|
||||
|
||||
#[test]
|
||||
fn test_clone_eq() {
|
||||
let mut m = BTreeSet::new();
|
||||
|
||||
m.insert(1);
|
||||
m.insert(2);
|
||||
|
||||
assert!(m.clone() == m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = BTreeSet::new();
|
||||
let mut y = BTreeSet::new();
|
||||
|
||||
x.insert(1);
|
||||
x.insert(2);
|
||||
x.insert(3);
|
||||
|
||||
y.insert(3);
|
||||
y.insert(2);
|
||||
y.insert(1);
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
struct Counter<'a, 'b> {
|
||||
i: &'a mut usize,
|
||||
expected: &'b [i32],
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> FnMut<(&'c i32,)> for Counter<'a, 'b> {
|
||||
type Output = bool;
|
||||
|
||||
extern "rust-call" fn call_mut(&mut self, (&x,): (&'c i32,)) -> bool {
|
||||
assert_eq!(x, self.expected[*self.i]);
|
||||
*self.i += 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn check<F>(a: &[i32], b: &[i32], expected: &[i32], f: F) where
|
||||
// FIXME Replace Counter with `Box<FnMut(_) -> _>`
|
||||
F: FnOnce(&BTreeSet<i32>, &BTreeSet<i32>, Counter) -> bool,
|
||||
{
|
||||
let mut set_a = BTreeSet::new();
|
||||
let mut set_b = BTreeSet::new();
|
||||
|
||||
for x in a { assert!(set_a.insert(*x)) }
|
||||
for y in b { assert!(set_b.insert(*y)) }
|
||||
|
||||
let mut i = 0;
|
||||
f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
|
||||
assert_eq!(i, expected.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_intersection() {
|
||||
fn check_intersection(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.intersection(y).all(f))
|
||||
}
|
||||
|
||||
check_intersection(&[], &[], &[]);
|
||||
check_intersection(&[1, 2, 3], &[], &[]);
|
||||
check_intersection(&[], &[1, 2, 3], &[]);
|
||||
check_intersection(&[2], &[1, 2, 3], &[2]);
|
||||
check_intersection(&[1, 2, 3], &[2], &[2]);
|
||||
check_intersection(&[11, 1, 3, 77, 103, 5, -5],
|
||||
&[2, 11, 77, -9, -42, 5, 3],
|
||||
&[3, 5, 11, 77]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_difference() {
|
||||
fn check_difference(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.difference(y).all(f))
|
||||
}
|
||||
|
||||
check_difference(&[], &[], &[]);
|
||||
check_difference(&[1, 12], &[], &[1, 12]);
|
||||
check_difference(&[], &[1, 2, 3, 9], &[]);
|
||||
check_difference(&[1, 3, 5, 9, 11],
|
||||
&[3, 9],
|
||||
&[1, 5, 11]);
|
||||
check_difference(&[-5, 11, 22, 33, 40, 42],
|
||||
&[-12, -5, 14, 23, 34, 38, 39, 50],
|
||||
&[11, 22, 33, 40, 42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_symmetric_difference() {
|
||||
fn check_symmetric_difference(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
|
||||
}
|
||||
|
||||
check_symmetric_difference(&[], &[], &[]);
|
||||
check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
|
||||
check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
|
||||
check_symmetric_difference(&[1, 3, 5, 9, 11],
|
||||
&[-2, 3, 9, 14, 22],
|
||||
&[-2, 1, 5, 11, 14, 22]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_union() {
|
||||
fn check_union(a: &[i32], b: &[i32], expected: &[i32]) {
|
||||
check(a, b, expected, |x, y, f| x.union(y).all(f))
|
||||
}
|
||||
|
||||
check_union(&[], &[], &[]);
|
||||
check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
|
||||
check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
|
||||
check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
|
||||
&[-2, 1, 5, 9, 13, 19],
|
||||
&[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zip() {
|
||||
let mut x = BTreeSet::new();
|
||||
x.insert(5);
|
||||
x.insert(12);
|
||||
x.insert(11);
|
||||
|
||||
let mut y = BTreeSet::new();
|
||||
y.insert("foo");
|
||||
y.insert("bar");
|
||||
|
||||
let x = x;
|
||||
let y = y;
|
||||
let mut z = x.iter().zip(y.iter());
|
||||
|
||||
// FIXME: #5801: this needs a type hint to compile...
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert_eq!(result.unwrap(), (&5, &("bar")));
|
||||
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert_eq!(result.unwrap(), (&11, &("foo")));
|
||||
|
||||
let result: Option<(&usize, & &'static str)> = z.next();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
let set: BTreeSet<_> = xs.iter().cloned().collect();
|
||||
|
||||
for x in &xs {
|
||||
assert!(set.contains(x));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut set = BTreeSet::new();
|
||||
let empty = BTreeSet::<i32>::new();
|
||||
|
||||
set.insert(1);
|
||||
set.insert(2);
|
||||
|
||||
let set_str = format!("{:?}", set);
|
||||
|
||||
assert_eq!(set_str, "{1, 2}");
|
||||
assert_eq!(format!("{:?}", empty), "{}");
|
||||
}
|
240
src/libcollectionstest/enum_set.rs
Normal file
240
src/libcollectionstest/enum_set.rs
Normal file
|
@ -0,0 +1,240 @@
|
|||
// Copyright 2012 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.
|
||||
|
||||
use std::collections::EnumSet;
|
||||
|
||||
#[derive(Copy, PartialEq, Debug)]
|
||||
#[repr(usize)]
|
||||
enum Foo {
|
||||
A, B, C
|
||||
}
|
||||
|
||||
impl CLike for Foo {
|
||||
fn to_usize(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
|
||||
fn from_usize(v: usize) -> Foo {
|
||||
unsafe { mem::transmute(v) }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new() {
|
||||
let e: EnumSet<Foo> = EnumSet::new();
|
||||
assert!(e.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut e = EnumSet::new();
|
||||
assert!(format!("{:?}", e) == "{}");
|
||||
e.insert(A);
|
||||
assert!(format!("{:?}", e) == "{A}");
|
||||
e.insert(C);
|
||||
assert!(format!("{:?}", e) == "{A, C}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_len() {
|
||||
let mut e = EnumSet::new();
|
||||
assert_eq!(e.len(), 0);
|
||||
e.insert(A);
|
||||
e.insert(B);
|
||||
e.insert(C);
|
||||
assert_eq!(e.len(), 3);
|
||||
e.remove(&A);
|
||||
assert_eq!(e.len(), 2);
|
||||
e.clear();
|
||||
assert_eq!(e.len(), 0);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// intersect
|
||||
|
||||
#[test]
|
||||
fn test_two_empties_do_not_intersect() {
|
||||
let e1: EnumSet<Foo> = EnumSet::new();
|
||||
let e2: EnumSet<Foo> = EnumSet::new();
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_does_not_intersect_with_full() {
|
||||
let e1: EnumSet<Foo> = EnumSet::new();
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
e2.insert(C);
|
||||
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disjoint_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(B);
|
||||
|
||||
assert!(e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlapping_intersects() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
|
||||
assert!(!e1.is_disjoint(&e2));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// contains and contains_elem
|
||||
|
||||
#[test]
|
||||
fn test_superset() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(A);
|
||||
e2.insert(B);
|
||||
|
||||
let mut e3: EnumSet<Foo> = EnumSet::new();
|
||||
e3.insert(C);
|
||||
|
||||
assert!(e1.is_subset(&e2));
|
||||
assert!(e2.is_superset(&e1));
|
||||
assert!(!e3.is_superset(&e2));
|
||||
assert!(!e2.is_superset(&e3))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_contains() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
assert!(e1.contains(&A));
|
||||
assert!(!e1.contains(&B));
|
||||
assert!(!e1.contains(&C));
|
||||
|
||||
e1.insert(A);
|
||||
e1.insert(B);
|
||||
assert!(e1.contains(&A));
|
||||
assert!(e1.contains(&B));
|
||||
assert!(!e1.contains(&C));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// iter
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
|
||||
let elems: ::vec::Vec<Foo> = e1.iter().collect();
|
||||
assert!(elems.is_empty());
|
||||
|
||||
e1.insert(A);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A], elems);
|
||||
|
||||
e1.insert(C);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,C], elems);
|
||||
|
||||
e1.insert(C);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,C], elems);
|
||||
|
||||
e1.insert(B);
|
||||
let elems: ::vec::Vec<_> = e1.iter().collect();
|
||||
assert_eq!([A,B,C], elems);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// operators
|
||||
|
||||
#[test]
|
||||
fn test_operators() {
|
||||
let mut e1: EnumSet<Foo> = EnumSet::new();
|
||||
e1.insert(A);
|
||||
e1.insert(C);
|
||||
|
||||
let mut e2: EnumSet<Foo> = EnumSet::new();
|
||||
e2.insert(B);
|
||||
e2.insert(C);
|
||||
|
||||
let e_union = e1 | e2;
|
||||
let elems: ::vec::Vec<_> = e_union.iter().collect();
|
||||
assert_eq!([A,B,C], elems);
|
||||
|
||||
let e_intersection = e1 & e2;
|
||||
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
|
||||
assert_eq!([C], elems);
|
||||
|
||||
// Another way to express intersection
|
||||
let e_intersection = e1 - (e1 - e2);
|
||||
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
|
||||
assert_eq!([C], elems);
|
||||
|
||||
let e_subtract = e1 - e2;
|
||||
let elems: ::vec::Vec<_> = e_subtract.iter().collect();
|
||||
assert_eq!([A], elems);
|
||||
|
||||
// Bitwise XOR of two sets, aka symmetric difference
|
||||
let e_symmetric_diff = e1 ^ e2;
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
|
||||
// Another way to express symmetric difference
|
||||
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
|
||||
// Yet another way to express symmetric difference
|
||||
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
|
||||
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
|
||||
assert_eq!([A,B], elems);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_overflow() {
|
||||
#[allow(dead_code)]
|
||||
#[derive(Copy)]
|
||||
#[repr(usize)]
|
||||
enum Bar {
|
||||
V00, V01, V02, V03, V04, V05, V06, V07, V08, V09,
|
||||
V10, V11, V12, V13, V14, V15, V16, V17, V18, V19,
|
||||
V20, V21, V22, V23, V24, V25, V26, V27, V28, V29,
|
||||
V30, V31, V32, V33, V34, V35, V36, V37, V38, V39,
|
||||
V40, V41, V42, V43, V44, V45, V46, V47, V48, V49,
|
||||
V50, V51, V52, V53, V54, V55, V56, V57, V58, V59,
|
||||
V60, V61, V62, V63, V64, V65, V66, V67, V68, V69,
|
||||
}
|
||||
|
||||
impl CLike for Bar {
|
||||
fn to_usize(&self) -> usize {
|
||||
*self as usize
|
||||
}
|
||||
|
||||
fn from_usize(v: usize) -> Bar {
|
||||
unsafe { mem::transmute(v) }
|
||||
}
|
||||
}
|
||||
let mut set = EnumSet::new();
|
||||
set.insert(Bar::V64);
|
||||
}
|
17
src/libcollectionstest/fmt.rs
Normal file
17
src/libcollectionstest/fmt.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2013-2015 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.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[test]
|
||||
fn test_format() {
|
||||
let s = fmt::format(format_args!("Hello, {}!", "world"));
|
||||
assert_eq!(s.as_slice(), "Hello, world!");
|
||||
}
|
44
src/libcollectionstest/lib.rs
Normal file
44
src/libcollectionstest/lib.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2015 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.
|
||||
|
||||
#![allow(deprecated)]
|
||||
#![feature(box_syntax)]
|
||||
#![feature(collections)]
|
||||
#![feature(core)]
|
||||
#![feature(hash)]
|
||||
#![feature(rand)]
|
||||
#![feature(rustc_private)]
|
||||
#![feature(str_words)]
|
||||
#![feature(test)]
|
||||
#![feature(unboxed_closures)]
|
||||
#![feature(unicode)]
|
||||
#![feature(unsafe_destructor)]
|
||||
|
||||
#[macro_use] extern crate log;
|
||||
|
||||
extern crate collections;
|
||||
extern crate test;
|
||||
extern crate unicode;
|
||||
|
||||
#[cfg(test)] #[macro_use] mod bench;
|
||||
|
||||
mod binary_heap;
|
||||
mod bit;
|
||||
mod btree;
|
||||
// FIXME(japaric) privacy
|
||||
//mod enum_set;
|
||||
mod fmt;
|
||||
mod linked_list;
|
||||
mod slice;
|
||||
mod str;
|
||||
mod string;
|
||||
mod vec_deque;
|
||||
mod vec_map;
|
||||
mod vec;
|
589
src/libcollectionstest/linked_list.rs
Normal file
589
src/libcollectionstest/linked_list.rs
Normal file
|
@ -0,0 +1,589 @@
|
|||
// Copyright 2012-2015 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.
|
||||
|
||||
use std::collections::LinkedList;
|
||||
use std::hash::{SipHasher, self};
|
||||
|
||||
use test;
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
pub fn check_links<T>(list: &LinkedList<T>) {
|
||||
let mut len = 0;
|
||||
let mut last_ptr: Option<&Node<T>> = None;
|
||||
let mut node_ptr: &Node<T>;
|
||||
match list.list_head {
|
||||
None => { assert_eq!(0, list.length); return }
|
||||
Some(ref node) => node_ptr = &**node,
|
||||
}
|
||||
loop {
|
||||
match (last_ptr, node_ptr.prev.resolve_immut()) {
|
||||
(None , None ) => {}
|
||||
(None , _ ) => panic!("prev link for list_head"),
|
||||
(Some(p), Some(pptr)) => {
|
||||
assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
|
||||
}
|
||||
_ => panic!("prev link is none, not good"),
|
||||
}
|
||||
match node_ptr.next {
|
||||
Some(ref next) => {
|
||||
last_ptr = Some(node_ptr);
|
||||
node_ptr = &**next;
|
||||
len += 1;
|
||||
}
|
||||
None => {
|
||||
len += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(len, list.length);
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
let mut m = LinkedList::<Box<_>>::new();
|
||||
assert_eq!(m.pop_front(), None);
|
||||
assert_eq!(m.pop_back(), None);
|
||||
assert_eq!(m.pop_front(), None);
|
||||
m.push_front(box 1);
|
||||
assert_eq!(m.pop_front(), Some(box 1));
|
||||
m.push_back(box 2);
|
||||
m.push_back(box 3);
|
||||
assert_eq!(m.len(), 2);
|
||||
assert_eq!(m.pop_front(), Some(box 2));
|
||||
assert_eq!(m.pop_front(), Some(box 3));
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(m.pop_front(), None);
|
||||
m.push_back(box 1);
|
||||
m.push_back(box 3);
|
||||
m.push_back(box 5);
|
||||
m.push_back(box 7);
|
||||
assert_eq!(m.pop_front(), Some(box 1));
|
||||
|
||||
let mut n = LinkedList::new();
|
||||
n.push_front(2);
|
||||
n.push_front(3);
|
||||
{
|
||||
assert_eq!(n.front().unwrap(), &3);
|
||||
let x = n.front_mut().unwrap();
|
||||
assert_eq!(*x, 3);
|
||||
*x = 0;
|
||||
}
|
||||
{
|
||||
assert_eq!(n.back().unwrap(), &2);
|
||||
let y = n.back_mut().unwrap();
|
||||
assert_eq!(*y, 2);
|
||||
*y = 1;
|
||||
}
|
||||
assert_eq!(n.pop_front(), Some(0));
|
||||
assert_eq!(n.pop_front(), Some(1));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn generate_test() -> LinkedList<i32> {
|
||||
list_from(&[0,1,2,3,4,5,6])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn list_from<T: Clone>(v: &[T]) -> LinkedList<T> {
|
||||
v.iter().cloned().collect()
|
||||
}
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
#[test]
|
||||
fn test_append() {
|
||||
// Empty to empty
|
||||
{
|
||||
let mut m = LinkedList::<i32>::new();
|
||||
let mut n = LinkedList::new();
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(n.len(), 0);
|
||||
}
|
||||
// Non-empty to empty
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
let mut n = LinkedList::new();
|
||||
n.push_back(2);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(m.pop_back(), Some(2));
|
||||
assert_eq!(n.len(), 0);
|
||||
check_links(&m);
|
||||
}
|
||||
// Empty to non-empty
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
let mut n = LinkedList::new();
|
||||
m.push_back(2);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(m.pop_back(), Some(2));
|
||||
check_links(&m);
|
||||
}
|
||||
|
||||
// Non-empty to non-empty
|
||||
let v = vec![1,2,3,4,5];
|
||||
let u = vec![9,8,1,2,3,4,5];
|
||||
let mut m = list_from(&v);
|
||||
let mut n = list_from(&u);
|
||||
m.append(&mut n);
|
||||
check_links(&m);
|
||||
let mut sum = v;
|
||||
sum.push_all(&u);
|
||||
assert_eq!(sum.len(), m.len());
|
||||
for elt in sum {
|
||||
assert_eq!(m.pop_front(), Some(elt))
|
||||
}
|
||||
assert_eq!(n.len(), 0);
|
||||
// let's make sure it's working properly, since we
|
||||
// did some direct changes to private members
|
||||
n.push_back(3);
|
||||
assert_eq!(n.len(), 1);
|
||||
assert_eq!(n.pop_front(), Some(3));
|
||||
check_links(&n);
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_split_off() {
|
||||
// singleton
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
m.push_back(1);
|
||||
|
||||
let p = m.split_off(0);
|
||||
assert_eq!(m.len(), 0);
|
||||
assert_eq!(p.len(), 1);
|
||||
assert_eq!(p.back(), Some(&1));
|
||||
assert_eq!(p.front(), Some(&1));
|
||||
}
|
||||
|
||||
// not singleton, forwards
|
||||
{
|
||||
let u = vec![1,2,3,4,5];
|
||||
let mut m = list_from(&u);
|
||||
let mut n = m.split_off(2);
|
||||
assert_eq!(m.len(), 2);
|
||||
assert_eq!(n.len(), 3);
|
||||
for elt in 1..3 {
|
||||
assert_eq!(m.pop_front(), Some(elt));
|
||||
}
|
||||
for elt in 3..6 {
|
||||
assert_eq!(n.pop_front(), Some(elt));
|
||||
}
|
||||
}
|
||||
// not singleton, backwards
|
||||
{
|
||||
let u = vec![1,2,3,4,5];
|
||||
let mut m = list_from(&u);
|
||||
let mut n = m.split_off(4);
|
||||
assert_eq!(m.len(), 4);
|
||||
assert_eq!(n.len(), 1);
|
||||
for elt in 1..5 {
|
||||
assert_eq!(m.pop_front(), Some(elt));
|
||||
}
|
||||
for elt in 5..6 {
|
||||
assert_eq!(n.pop_front(), Some(elt));
|
||||
}
|
||||
}
|
||||
|
||||
// no-op on the last index
|
||||
{
|
||||
let mut m = LinkedList::new();
|
||||
m.push_back(1);
|
||||
|
||||
let p = m.split_off(1);
|
||||
assert_eq!(m.len(), 1);
|
||||
assert_eq!(p.len(), 0);
|
||||
assert_eq!(m.back(), Some(&1));
|
||||
assert_eq!(m.front(), Some(&1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let m = generate_test();
|
||||
for (i, elt) in m.iter().enumerate() {
|
||||
assert_eq!(i as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().next(), None);
|
||||
n.push_front(4);
|
||||
let mut it = n.iter();
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_clone() {
|
||||
let mut n = LinkedList::new();
|
||||
n.push_back(2);
|
||||
n.push_back(3);
|
||||
n.push_back(4);
|
||||
let mut it = n.iter();
|
||||
it.next();
|
||||
let mut jt = it.clone();
|
||||
assert_eq!(it.next(), jt.next());
|
||||
assert_eq!(it.next_back(), jt.next_back());
|
||||
assert_eq!(it.next(), jt.next());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_double_end() {
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().next(), None);
|
||||
n.push_front(4);
|
||||
n.push_front(5);
|
||||
n.push_front(6);
|
||||
let mut it = n.iter();
|
||||
assert_eq!(it.size_hint(), (3, Some(3)));
|
||||
assert_eq!(it.next().unwrap(), &6);
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(it.next_back().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next_back().unwrap(), &5);
|
||||
assert_eq!(it.next_back(), None);
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rev_iter() {
|
||||
let m = generate_test();
|
||||
for (i, elt) in m.iter().rev().enumerate() {
|
||||
assert_eq!((6 - i) as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert_eq!(n.iter().rev().next(), None);
|
||||
n.push_front(4);
|
||||
let mut it = n.iter().rev();
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(it.next().unwrap(), &4);
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert_eq!(it.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iter() {
|
||||
let mut m = generate_test();
|
||||
let mut len = m.len();
|
||||
for (i, elt) in m.iter_mut().enumerate() {
|
||||
assert_eq!(i as i32, *elt);
|
||||
len -= 1;
|
||||
}
|
||||
assert_eq!(len, 0);
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().next().is_none());
|
||||
n.push_front(4);
|
||||
n.push_back(5);
|
||||
let mut it = n.iter_mut();
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert!(it.next().is_some());
|
||||
assert!(it.next().is_some());
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_mut_double_end() {
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().next_back().is_none());
|
||||
n.push_front(4);
|
||||
n.push_front(5);
|
||||
n.push_front(6);
|
||||
let mut it = n.iter_mut();
|
||||
assert_eq!(it.size_hint(), (3, Some(3)));
|
||||
assert_eq!(*it.next().unwrap(), 6);
|
||||
assert_eq!(it.size_hint(), (2, Some(2)));
|
||||
assert_eq!(*it.next_back().unwrap(), 4);
|
||||
assert_eq!(it.size_hint(), (1, Some(1)));
|
||||
assert_eq!(*it.next_back().unwrap(), 5);
|
||||
assert!(it.next_back().is_none());
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
#[test]
|
||||
fn test_insert_prev() {
|
||||
let mut m = list_from(&[0,2,4,6,8]);
|
||||
let len = m.len();
|
||||
{
|
||||
let mut it = m.iter_mut();
|
||||
it.insert_next(-2);
|
||||
loop {
|
||||
match it.next() {
|
||||
None => break,
|
||||
Some(elt) => {
|
||||
it.insert_next(*elt + 1);
|
||||
match it.peek_next() {
|
||||
Some(x) => assert_eq!(*x, *elt + 2),
|
||||
None => assert_eq!(8, *elt),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
it.insert_next(0);
|
||||
it.insert_next(1);
|
||||
}
|
||||
check_links(&m);
|
||||
assert_eq!(m.len(), 3 + len * 2);
|
||||
assert_eq!(m.into_iter().collect::<Vec<_>>(), [-2,0,1,2,3,4,5,6,7,8,9,0,1]);
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_mut_rev_iter() {
|
||||
let mut m = generate_test();
|
||||
for (i, elt) in m.iter_mut().rev().enumerate() {
|
||||
assert_eq!((6 - i) as i32, *elt);
|
||||
}
|
||||
let mut n = LinkedList::new();
|
||||
assert!(n.iter_mut().rev().next().is_none());
|
||||
n.push_front(4);
|
||||
let mut it = n.iter_mut().rev();
|
||||
assert!(it.next().is_some());
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
#[test]
|
||||
fn test_send() {
|
||||
let n = list_from(&[1,2,3]);
|
||||
thread::spawn(move || {
|
||||
check_links(&n);
|
||||
let a: &[_] = &[&1,&2,&3];
|
||||
assert_eq!(a, n.iter().collect::<Vec<_>>());
|
||||
}).join().ok().unwrap();
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let mut n = list_from(&[]);
|
||||
let mut m = list_from(&[]);
|
||||
assert!(n == m);
|
||||
n.push_front(1);
|
||||
assert!(n != m);
|
||||
m.push_back(1);
|
||||
assert!(n == m);
|
||||
|
||||
let n = list_from(&[2,3,4]);
|
||||
let m = list_from(&[1,2,3]);
|
||||
assert!(n != m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = LinkedList::new();
|
||||
let mut y = LinkedList::new();
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
|
||||
x.push_back(1);
|
||||
x.push_back(2);
|
||||
x.push_back(3);
|
||||
|
||||
y.push_front(3);
|
||||
y.push_front(2);
|
||||
y.push_front(1);
|
||||
|
||||
assert!(hash::hash::<_, SipHasher>(&x) == hash::hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord() {
|
||||
let n = list_from(&[]);
|
||||
let m = list_from(&[1,2,3]);
|
||||
assert!(n < m);
|
||||
assert!(m > n);
|
||||
assert!(n <= n);
|
||||
assert!(n >= n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord_nan() {
|
||||
let nan = 0.0f64/0.0;
|
||||
let n = list_from(&[nan]);
|
||||
let m = list_from(&[nan]);
|
||||
assert!(!(n < m));
|
||||
assert!(!(n > m));
|
||||
assert!(!(n <= m));
|
||||
assert!(!(n >= m));
|
||||
|
||||
let n = list_from(&[nan]);
|
||||
let one = list_from(&[1.0f64]);
|
||||
assert!(!(n < one));
|
||||
assert!(!(n > one));
|
||||
assert!(!(n <= one));
|
||||
assert!(!(n >= one));
|
||||
|
||||
let u = list_from(&[1.0f64,2.0,nan]);
|
||||
let v = list_from(&[1.0f64,2.0,3.0]);
|
||||
assert!(!(u < v));
|
||||
assert!(!(u > v));
|
||||
assert!(!(u <= v));
|
||||
assert!(!(u >= v));
|
||||
|
||||
let s = list_from(&[1.0f64,2.0,4.0,2.0]);
|
||||
let t = list_from(&[1.0f64,2.0,3.0,2.0]);
|
||||
assert!(!(s < t));
|
||||
assert!(s > one);
|
||||
assert!(!(s <= one));
|
||||
assert!(s >= one);
|
||||
}
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
#[test]
|
||||
fn test_fuzz() {
|
||||
for _ in 0..25 {
|
||||
fuzz_test(3);
|
||||
fuzz_test(16);
|
||||
fuzz_test(189);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let list: LinkedList<_> = (0..10).collect();
|
||||
assert_eq!(format!("{:?}", list), "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
|
||||
|
||||
let list: LinkedList<_> = vec!["just", "one", "test", "more"].iter().cloned().collect();
|
||||
assert_eq!(format!("{:?}", list), "[\"just\", \"one\", \"test\", \"more\"]");
|
||||
}
|
||||
|
||||
// FIXME(japaric) privacy
|
||||
/*
|
||||
#[cfg(test)]
|
||||
fn fuzz_test(sz: i32) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
let mut v = vec![];
|
||||
for i in 0..sz {
|
||||
check_links(&m);
|
||||
let r: u8 = rand::random();
|
||||
match r % 6 {
|
||||
0 => {
|
||||
m.pop_back();
|
||||
v.pop();
|
||||
}
|
||||
1 => {
|
||||
if !v.is_empty() {
|
||||
m.pop_front();
|
||||
v.remove(0);
|
||||
}
|
||||
}
|
||||
2 | 4 => {
|
||||
m.push_front(-i);
|
||||
v.insert(0, -i);
|
||||
}
|
||||
3 | 5 | _ => {
|
||||
m.push_back(i);
|
||||
v.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check_links(&m);
|
||||
|
||||
let mut i = 0;
|
||||
for (a, &b) in m.into_iter().zip(v.iter()) {
|
||||
i += 1;
|
||||
assert_eq!(a, b);
|
||||
}
|
||||
assert_eq!(i, v.len());
|
||||
}
|
||||
*/
|
||||
|
||||
#[bench]
|
||||
fn bench_collect_into(b: &mut test::Bencher) {
|
||||
let v = &[0; 64];
|
||||
b.iter(|| {
|
||||
let _: LinkedList<_> = v.iter().cloned().collect();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_front(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_front(0);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_back(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_back(0);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_back_pop_back(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_back(0);
|
||||
m.pop_back();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_front_pop_front(b: &mut test::Bencher) {
|
||||
let mut m: LinkedList<_> = LinkedList::new();
|
||||
b.iter(|| {
|
||||
m.push_front(0);
|
||||
m.pop_front();
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_iter(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_mut(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter_mut().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_rev(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter().rev().count() == 128);
|
||||
})
|
||||
}
|
||||
#[bench]
|
||||
fn bench_iter_mut_rev(b: &mut test::Bencher) {
|
||||
let v = &[0; 128];
|
||||
let mut m: LinkedList<_> = v.iter().cloned().collect();
|
||||
b.iter(|| {
|
||||
assert!(m.iter_mut().rev().count() == 128);
|
||||
})
|
||||
}
|
1627
src/libcollectionstest/slice.rs
Normal file
1627
src/libcollectionstest/slice.rs
Normal file
File diff suppressed because it is too large
Load diff
1668
src/libcollectionstest/str.rs
Normal file
1668
src/libcollectionstest/str.rs
Normal file
File diff suppressed because it is too large
Load diff
453
src/libcollectionstest/string.rs
Normal file
453
src/libcollectionstest/string.rs
Normal file
|
@ -0,0 +1,453 @@
|
|||
// 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.
|
||||
|
||||
use std::borrow::IntoCow;
|
||||
use std::iter::repeat;
|
||||
use std::str::Utf8Error;
|
||||
use std::string::{CowString, as_string};
|
||||
|
||||
use test::Bencher;
|
||||
|
||||
#[test]
|
||||
fn test_as_string() {
|
||||
let x = "foo";
|
||||
assert_eq!(x, &**as_string(x));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
let owned: Option<::std::string::String> = "string".parse().ok();
|
||||
assert_eq!(owned.as_ref().map(|s| &**s), Some("string"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unsized_to_string() {
|
||||
let s: &str = "abc";
|
||||
let _: String = (*s).to_string();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf8() {
|
||||
let xs = b"hello".to_vec();
|
||||
assert_eq!(String::from_utf8(xs).unwrap(),
|
||||
String::from_str("hello"));
|
||||
|
||||
let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
|
||||
assert_eq!(String::from_utf8(xs).unwrap(),
|
||||
String::from_str("ศไทย中华Việt Nam"));
|
||||
|
||||
let xs = b"hello\xFF".to_vec();
|
||||
let err = String::from_utf8(xs).err().unwrap();
|
||||
assert_eq!(err.utf8_error(), Utf8Error::TooShort);
|
||||
assert_eq!(err.into_bytes(), b"hello\xff".to_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf8_lossy() {
|
||||
let xs = b"hello";
|
||||
let ys: CowString = "hello".into_cow();
|
||||
assert_eq!(String::from_utf8_lossy(xs), ys);
|
||||
|
||||
let xs = "ศไทย中华Việt Nam".as_bytes();
|
||||
let ys: CowString = "ศไทย中华Việt Nam".into_cow();
|
||||
assert_eq!(String::from_utf8_lossy(xs), ys);
|
||||
|
||||
let xs = b"Hello\xC2 There\xFF Goodbye";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow());
|
||||
|
||||
let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow());
|
||||
|
||||
let xs = b"\xF5foo\xF5\x80bar";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow());
|
||||
|
||||
let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow());
|
||||
|
||||
let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
|
||||
assert_eq!(String::from_utf8_lossy(xs),
|
||||
String::from_str("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow());
|
||||
|
||||
let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
|
||||
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\
|
||||
foo\u{10000}bar").into_cow());
|
||||
|
||||
// surrogates
|
||||
let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
|
||||
assert_eq!(String::from_utf8_lossy(xs), String::from_str("\u{FFFD}\u{FFFD}\u{FFFD}foo\
|
||||
\u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf16() {
|
||||
let pairs =
|
||||
[(String::from_str("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
|
||||
vec![0xd800, 0xdf45, 0xd800, 0xdf3f,
|
||||
0xd800, 0xdf3b, 0xd800, 0xdf46,
|
||||
0xd800, 0xdf39, 0xd800, 0xdf3b,
|
||||
0xd800, 0xdf30, 0x000a]),
|
||||
|
||||
(String::from_str("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
|
||||
vec![0xd801, 0xdc12, 0xd801,
|
||||
0xdc49, 0xd801, 0xdc2e, 0xd801,
|
||||
0xdc40, 0xd801, 0xdc32, 0xd801,
|
||||
0xdc4b, 0x0020, 0xd801, 0xdc0f,
|
||||
0xd801, 0xdc32, 0xd801, 0xdc4d,
|
||||
0x000a]),
|
||||
|
||||
(String::from_str("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
|
||||
vec![0xd800, 0xdf00, 0xd800, 0xdf16,
|
||||
0xd800, 0xdf0b, 0xd800, 0xdf04,
|
||||
0xd800, 0xdf11, 0xd800, 0xdf09,
|
||||
0x00b7, 0xd800, 0xdf0c, 0xd800,
|
||||
0xdf04, 0xd800, 0xdf15, 0xd800,
|
||||
0xdf04, 0xd800, 0xdf0b, 0xd800,
|
||||
0xdf09, 0xd800, 0xdf11, 0x000a ]),
|
||||
|
||||
(String::from_str("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
|
||||
vec![0xd801, 0xdc8b, 0xd801, 0xdc98,
|
||||
0xd801, 0xdc88, 0xd801, 0xdc91,
|
||||
0xd801, 0xdc9b, 0xd801, 0xdc92,
|
||||
0x0020, 0xd801, 0xdc95, 0xd801,
|
||||
0xdc93, 0x0020, 0xd801, 0xdc88,
|
||||
0xd801, 0xdc9a, 0xd801, 0xdc8d,
|
||||
0x0020, 0xd801, 0xdc8f, 0xd801,
|
||||
0xdc9c, 0xd801, 0xdc92, 0xd801,
|
||||
0xdc96, 0xd801, 0xdc86, 0x0020,
|
||||
0xd801, 0xdc95, 0xd801, 0xdc86,
|
||||
0x000a ]),
|
||||
// Issue #12318, even-numbered non-BMP planes
|
||||
(String::from_str("\u{20000}"),
|
||||
vec![0xD840, 0xDC00])];
|
||||
|
||||
for p in &pairs {
|
||||
let (s, u) = (*p).clone();
|
||||
let s_as_utf16 = s.utf16_units().collect::<Vec<u16>>();
|
||||
let u_as_string = String::from_utf16(&u).unwrap();
|
||||
|
||||
assert!(::unicode::str::is_utf16(&u));
|
||||
assert_eq!(s_as_utf16, u);
|
||||
|
||||
assert_eq!(u_as_string, s);
|
||||
assert_eq!(String::from_utf16_lossy(&u), s);
|
||||
|
||||
assert_eq!(String::from_utf16(&s_as_utf16).unwrap(), s);
|
||||
assert_eq!(u_as_string.utf16_units().collect::<Vec<u16>>(), u);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_utf16_invalid() {
|
||||
// completely positive cases tested above.
|
||||
// lead + eof
|
||||
assert!(String::from_utf16(&[0xD800]).is_err());
|
||||
// lead + lead
|
||||
assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
|
||||
|
||||
// isolated trail
|
||||
assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
|
||||
|
||||
// general
|
||||
assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_utf16_lossy() {
|
||||
// completely positive cases tested above.
|
||||
// lead + eof
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from_str("\u{FFFD}"));
|
||||
// lead + lead
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]),
|
||||
String::from_str("\u{FFFD}\u{FFFD}"));
|
||||
|
||||
// isolated trail
|
||||
assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from_str("a\u{FFFD}"));
|
||||
|
||||
// general
|
||||
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
|
||||
String::from_str("\u{FFFD}𐒋\u{FFFD}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_bytes() {
|
||||
let mut s = String::from_str("ABC");
|
||||
unsafe {
|
||||
let mv = s.as_mut_vec();
|
||||
mv.push_all(&[b'D']);
|
||||
}
|
||||
assert_eq!(s, "ABCD");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push_str() {
|
||||
let mut s = String::new();
|
||||
s.push_str("");
|
||||
assert_eq!(&s[0..], "");
|
||||
s.push_str("abc");
|
||||
assert_eq!(&s[0..], "abc");
|
||||
s.push_str("ประเทศไทย中华Việt Nam");
|
||||
assert_eq!(&s[0..], "abcประเทศไทย中华Việt Nam");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_push() {
|
||||
let mut data = String::from_str("ประเทศไทย中");
|
||||
data.push('华');
|
||||
data.push('b'); // 1 byte
|
||||
data.push('¢'); // 2 byte
|
||||
data.push('€'); // 3 byte
|
||||
data.push('𤭢'); // 4 byte
|
||||
assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pop() {
|
||||
let mut data = String::from_str("ประเทศไทย中华b¢€𤭢");
|
||||
assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
|
||||
assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
|
||||
assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
|
||||
assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
|
||||
assert_eq!(data.pop().unwrap(), '华');
|
||||
assert_eq!(data, "ประเทศไทย中");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_truncate() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.truncate(5);
|
||||
assert_eq!(s, "12345");
|
||||
s.truncate(3);
|
||||
assert_eq!(s, "123");
|
||||
s.truncate(0);
|
||||
assert_eq!(s, "");
|
||||
|
||||
let mut s = String::from_str("12345");
|
||||
let p = s.as_ptr();
|
||||
s.truncate(3);
|
||||
s.push_str("6");
|
||||
let p_ = s.as_ptr();
|
||||
assert_eq!(p_, p);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_str_truncate_invalid_len() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.truncate(6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_str_truncate_split_codepoint() {
|
||||
let mut s = String::from_str("\u{FC}"); // ü
|
||||
s.truncate(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_clear() {
|
||||
let mut s = String::from_str("12345");
|
||||
s.clear();
|
||||
assert_eq!(s.len(), 0);
|
||||
assert_eq!(s, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_str_add() {
|
||||
let a = String::from_str("12345");
|
||||
let b = a + "2";
|
||||
let b = b + "2";
|
||||
assert_eq!(b.len(), 7);
|
||||
assert_eq!(b, "1234522");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove() {
|
||||
let mut s = "ศไทย中华Việt Nam; foobar".to_string();;
|
||||
assert_eq!(s.remove(0), 'ศ');
|
||||
assert_eq!(s.len(), 33);
|
||||
assert_eq!(s, "ไทย中华Việt Nam; foobar");
|
||||
assert_eq!(s.remove(17), 'ệ');
|
||||
assert_eq!(s, "ไทย中华Vit Nam; foobar");
|
||||
}
|
||||
|
||||
#[test] #[should_panic]
|
||||
fn remove_bad() {
|
||||
"ศ".to_string().remove(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert() {
|
||||
let mut s = "foobar".to_string();
|
||||
s.insert(0, 'ệ');
|
||||
assert_eq!(s, "ệfoobar");
|
||||
s.insert(6, 'ย');
|
||||
assert_eq!(s, "ệfooยbar");
|
||||
}
|
||||
|
||||
#[test] #[should_panic] fn insert_bad1() { "".to_string().insert(1, 't'); }
|
||||
#[test] #[should_panic] fn insert_bad2() { "ệ".to_string().insert(1, 't'); }
|
||||
|
||||
#[test]
|
||||
fn test_slicing() {
|
||||
let s = "foobar".to_string();
|
||||
assert_eq!("foobar", &s[..]);
|
||||
assert_eq!("foo", &s[..3]);
|
||||
assert_eq!("bar", &s[3..]);
|
||||
assert_eq!("oob", &s[1..4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_types() {
|
||||
assert_eq!(1.to_string(), "1");
|
||||
assert_eq!((-1).to_string(), "-1");
|
||||
assert_eq!(200.to_string(), "200");
|
||||
assert_eq!(2.to_string(), "2");
|
||||
assert_eq!(true.to_string(), "true");
|
||||
assert_eq!(false.to_string(), "false");
|
||||
assert_eq!(("hi".to_string()).to_string(), "hi");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vectors() {
|
||||
let x: Vec<i32> = vec![];
|
||||
assert_eq!(format!("{:?}", x), "[]");
|
||||
assert_eq!(format!("{:?}", vec![1]), "[1]");
|
||||
assert_eq!(format!("{:?}", vec![1, 2, 3]), "[1, 2, 3]");
|
||||
assert!(format!("{:?}", vec![vec![], vec![1], vec![1, 1]]) ==
|
||||
"[[], [1], [1, 1]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iterator() {
|
||||
let s = "ศไทย中华Việt Nam".to_string();
|
||||
let t = "ศไทย中华";
|
||||
let u = "Việt Nam";
|
||||
|
||||
let a: String = s.chars().collect();
|
||||
assert_eq!(s, a);
|
||||
|
||||
let mut b = t.to_string();
|
||||
b.extend(u.chars());
|
||||
assert_eq!(s, b);
|
||||
|
||||
let c: String = vec![t, u].into_iter().collect();
|
||||
assert_eq!(s, c);
|
||||
|
||||
let mut d = t.to_string();
|
||||
d.extend(vec![u].into_iter());
|
||||
assert_eq!(s, d);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
String::with_capacity(100)
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_str(b: &mut Bencher) {
|
||||
let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb";
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
r.push_str(s);
|
||||
});
|
||||
}
|
||||
|
||||
const REPETITIONS: u64 = 10_000;
|
||||
|
||||
#[bench]
|
||||
fn bench_push_str_one_byte(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push_str("a")
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_char_one_byte(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push('a')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_char_two_bytes(b: &mut Bencher) {
|
||||
b.bytes = REPETITIONS * 2;
|
||||
b.iter(|| {
|
||||
let mut r = String::new();
|
||||
for _ in 0..REPETITIONS {
|
||||
r.push('â')
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
|
||||
let s = b"Hello there, the quick brown fox jumped over the lazy dog! \
|
||||
Lorem ipsum dolor sit amet, consectetur. ";
|
||||
|
||||
assert_eq!(100, s.len());
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_multibyte(b: &mut Bencher) {
|
||||
let s = "𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰".as_bytes();
|
||||
assert_eq!(100, s.len());
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_invalid(b: &mut Bencher) {
|
||||
let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn from_utf8_lossy_100_invalid(b: &mut Bencher) {
|
||||
let s = repeat(0xf5).take(100).collect::<Vec<_>>();
|
||||
b.iter(|| {
|
||||
let _ = String::from_utf8_lossy(&s);
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_exact_size_shrink_to_fit(b: &mut Bencher) {
|
||||
let s = "Hello there, the quick brown fox jumped over the lazy dog! \
|
||||
Lorem ipsum dolor sit amet, consectetur. ";
|
||||
// ensure our operation produces an exact-size string before we benchmark it
|
||||
let mut r = String::with_capacity(s.len());
|
||||
r.push_str(s);
|
||||
assert_eq!(r.len(), r.capacity());
|
||||
b.iter(|| {
|
||||
let mut r = String::with_capacity(s.len());
|
||||
r.push_str(s);
|
||||
r.shrink_to_fit();
|
||||
r
|
||||
});
|
||||
}
|
994
src/libcollectionstest/vec.rs
Normal file
994
src/libcollectionstest/vec.rs
Normal file
|
@ -0,0 +1,994 @@
|
|||
// 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.
|
||||
|
||||
use std::iter::{FromIterator, repeat};
|
||||
use std::mem::size_of;
|
||||
use std::vec::as_vec;
|
||||
|
||||
use test::Bencher;
|
||||
|
||||
struct DropCounter<'a> {
|
||||
count: &'a mut u32
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
impl<'a> Drop for DropCounter<'a> {
|
||||
fn drop(&mut self) {
|
||||
*self.count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_vec() {
|
||||
let xs = [1u8, 2u8, 3u8];
|
||||
assert_eq!(&**as_vec(&xs), xs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_vec_dtor() {
|
||||
let (mut count_x, mut count_y) = (0, 0);
|
||||
{
|
||||
let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }];
|
||||
assert_eq!(as_vec(xs).len(), 2);
|
||||
}
|
||||
assert_eq!(count_x, 1);
|
||||
assert_eq!(count_y, 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>,
|
||||
y: Vec<T>
|
||||
}
|
||||
|
||||
let (mut count_x, mut count_y) = (0, 0);
|
||||
{
|
||||
let mut tv = TwoVec {
|
||||
x: Vec::new(),
|
||||
y: Vec::new()
|
||||
};
|
||||
tv.x.push(DropCounter {count: &mut count_x});
|
||||
tv.y.push(DropCounter {count: &mut count_y});
|
||||
|
||||
// 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);
|
||||
for i in 0..3 { w.push(i) }
|
||||
|
||||
assert_eq!(v, w);
|
||||
|
||||
v.extend(3..10);
|
||||
for i in 3..10 { w.push(i) }
|
||||
|
||||
assert_eq!(v, w);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_from_mut() {
|
||||
let mut values = vec![1, 2, 3, 4, 5];
|
||||
{
|
||||
let slice = &mut values[2 ..];
|
||||
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];
|
||||
{
|
||||
let slice = &mut values[.. 2];
|
||||
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![];
|
||||
let w = vec!(1, 2, 3);
|
||||
|
||||
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() {
|
||||
let mut v = vec!();
|
||||
let three: Vec<Box<_>> = vec!(box 1, box 2, box 3);
|
||||
let two: Vec<Box<_>> = vec!(box 4, box 5);
|
||||
// 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]);
|
||||
}
|
||||
|
||||
#[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 {}
|
||||
unsafe { v.set_len(0); }
|
||||
assert_eq!(v.iter_mut().count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partition() {
|
||||
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]));
|
||||
}
|
||||
|
||||
#[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_unsafe_ptrs() {
|
||||
unsafe {
|
||||
// Test on-stack copy-from-buf.
|
||||
let a = [1, 2, 3];
|
||||
let ptr = a.as_ptr();
|
||||
let b = Vec::from_raw_buf(ptr, 3);
|
||||
assert_eq!(b, [1, 2, 3]);
|
||||
|
||||
// Test on-heap copy-from-buf.
|
||||
let c = vec![1, 2, 3, 4, 5];
|
||||
let ptr = c.as_ptr();
|
||||
let d = Vec::from_raw_buf(ptr, 5);
|
||||
assert_eq!(d, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_truncate_drop() {
|
||||
static mut drops: u32 = 0;
|
||||
struct Elem(i32);
|
||||
impl Drop for Elem {
|
||||
fn drop(&mut self) {
|
||||
unsafe { drops += 1; }
|
||||
}
|
||||
}
|
||||
|
||||
let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
|
||||
assert_eq!(unsafe { drops }, 0);
|
||||
v.truncate(3);
|
||||
assert_eq!(unsafe { drops }, 2);
|
||||
v.truncate(0);
|
||||
assert_eq!(unsafe { drops }, 5);
|
||||
}
|
||||
|
||||
#[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];
|
||||
&x[-1..];
|
||||
}
|
||||
|
||||
#[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];
|
||||
&x[-1..4];
|
||||
}
|
||||
|
||||
#[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() {
|
||||
let mut vec= Vec::<i32>::new();
|
||||
vec.swap_remove(0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter_unwrap() {
|
||||
let mut vec = Vec::with_capacity(7);
|
||||
vec.push(1);
|
||||
vec.push(2);
|
||||
let ptr = vec.as_ptr();
|
||||
vec = vec.into_iter().into_inner();
|
||||
assert_eq!(vec.as_ptr(), ptr);
|
||||
assert_eq!(vec.capacity(), 7);
|
||||
assert_eq!(vec.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_map_in_place_incompatible_types_fail() {
|
||||
let v = vec![0, 1, 2];
|
||||
v.map_in_place(|_| ());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place() {
|
||||
let v = vec![0, 1, 2];
|
||||
assert_eq!(v.map_in_place(|i: u32| i as i32 - 1), [-1, 0, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place_zero_sized() {
|
||||
let v = vec![(), ()];
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct ZeroSized;
|
||||
assert_eq!(v.map_in_place(|_| ZeroSized), [ZeroSized, ZeroSized]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_in_place_zero_drop_count() {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct Nothing;
|
||||
impl Drop for Nothing { fn drop(&mut self) { } }
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct ZeroSized;
|
||||
impl Drop for ZeroSized {
|
||||
fn drop(&mut self) {
|
||||
DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
const NUM_ELEMENTS: usize = 2;
|
||||
static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
|
||||
|
||||
let v = repeat(Nothing).take(NUM_ELEMENTS).collect::<Vec<_>>();
|
||||
|
||||
DROP_COUNTER.store(0, Ordering::Relaxed);
|
||||
|
||||
let v = v.map_in_place(|_| ZeroSized);
|
||||
assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), 0);
|
||||
drop(v);
|
||||
assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), NUM_ELEMENTS);
|
||||
}
|
||||
|
||||
#[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![];
|
||||
for i in vec.drain() {
|
||||
vec2.push(i);
|
||||
}
|
||||
assert_eq!(vec, []);
|
||||
assert_eq!(vec2, [ 1, 2, 3 ]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain_items_reverse() {
|
||||
let mut vec = vec![1, 2, 3];
|
||||
let mut vec2 = vec![];
|
||||
for i in vec.drain().rev() {
|
||||
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![];
|
||||
for i in vec.drain() {
|
||||
vec2.push(i);
|
||||
}
|
||||
assert_eq!(vec, []);
|
||||
assert_eq!(vec2, [(), (), ()]);
|
||||
}
|
||||
|
||||
#[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]);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_new(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u32> = Vec::new();
|
||||
assert_eq!(v.len(), 0);
|
||||
assert_eq!(v.capacity(), 0);
|
||||
})
|
||||
}
|
||||
|
||||
fn do_bench_with_capacity(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let v: Vec<u32> = Vec::with_capacity(src_len);
|
||||
assert_eq!(v.len(), 0);
|
||||
assert_eq!(v.capacity(), src_len);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0000(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0010(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_0100(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_with_capacity_1000(b: &mut Bencher) {
|
||||
do_bench_with_capacity(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_fn(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = (0..src_len).collect::<Vec<_>>();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0000(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0010(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_0100(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_fn_1000(b: &mut Bencher) {
|
||||
do_bench_from_fn(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_elem(b: &mut Bencher, src_len: usize) {
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst: Vec<usize> = repeat(5).take(src_len).collect();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().all(|x| *x == 5));
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0000(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0010(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_0100(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_elem_1000(b: &mut Bencher) {
|
||||
do_bench_from_elem(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_slice(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = src.clone()[..].to_vec();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0000(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0010(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_0100(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_slice_1000(b: &mut Bencher) {
|
||||
do_bench_from_slice(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_from_iter(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst: Vec<_> = FromIterator::from_iter(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0000(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0010(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_0100(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_from_iter_1000(b: &mut Bencher) {
|
||||
do_bench_from_iter(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_extend(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.extend(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0010(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_0100(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0000_1000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0010_0010(b: &mut Bencher) {
|
||||
do_bench_extend(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_0100_0100(b: &mut Bencher) {
|
||||
do_bench_extend(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_extend_1000_1000(b: &mut Bencher) {
|
||||
do_bench_extend(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_push_all(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.push_all(&src);
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0010(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_0100(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0010_0010(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_0100_0100(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_1000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_push_all_move(b: &mut Bencher, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..dst_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
dst.extend(src.clone().into_iter());
|
||||
assert_eq!(dst.len(), dst_len + src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0010(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_0100(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0010_0010(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_0100_0100(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_push_all_move_1000_1000(b: &mut Bencher) {
|
||||
do_bench_push_all_move(b, 1000, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_clone(b: &mut Bencher, src_len: usize) {
|
||||
let src: Vec<usize> = FromIterator::from_iter(0..src_len);
|
||||
|
||||
b.bytes = src_len as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let dst = src.clone();
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0000(b: &mut Bencher) {
|
||||
do_bench_clone(b, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0010(b: &mut Bencher) {
|
||||
do_bench_clone(b, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_0100(b: &mut Bencher) {
|
||||
do_bench_clone(b, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_1000(b: &mut Bencher) {
|
||||
do_bench_clone(b, 1000)
|
||||
}
|
||||
|
||||
fn do_bench_clone_from(b: &mut Bencher, times: usize, dst_len: usize, src_len: usize) {
|
||||
let dst: Vec<_> = FromIterator::from_iter(0..src_len);
|
||||
let src: Vec<_> = FromIterator::from_iter(dst_len..dst_len + src_len);
|
||||
|
||||
b.bytes = (times * src_len) as u64;
|
||||
|
||||
b.iter(|| {
|
||||
let mut dst = dst.clone();
|
||||
|
||||
for _ in 0..times {
|
||||
dst.clone_from(&src);
|
||||
|
||||
assert_eq!(dst.len(), src_len);
|
||||
assert!(dst.iter().enumerate().all(|(i, x)| dst_len + i == *x));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_1000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 1000, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0010_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 10, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_0100_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 100, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_01_1000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 1, 1000, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 0, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_1000_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 1000, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 100)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_1000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 1000)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0010_0000(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 10, 0)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_0100_0010(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 100, 10)
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_clone_from_10_1000_0100(b: &mut Bencher) {
|
||||
do_bench_clone_from(b, 10, 1000, 100)
|
||||
}
|
1150
src/libcollectionstest/vec_deque.rs
Normal file
1150
src/libcollectionstest/vec_deque.rs
Normal file
File diff suppressed because it is too large
Load diff
510
src/libcollectionstest/vec_map.rs
Normal file
510
src/libcollectionstest/vec_map.rs
Normal file
|
@ -0,0 +1,510 @@
|
|||
// Copyright 2012-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.
|
||||
|
||||
use std::collections::VecMap;
|
||||
use std::collections::vec_map::Entry::{Occupied, Vacant};
|
||||
use std::hash::{SipHasher, hash};
|
||||
|
||||
#[test]
|
||||
fn test_get_mut() {
|
||||
let mut m = VecMap::new();
|
||||
assert!(m.insert(1, 12).is_none());
|
||||
assert!(m.insert(2, 8).is_none());
|
||||
assert!(m.insert(5, 14).is_none());
|
||||
let new = 100;
|
||||
match m.get_mut(&5) {
|
||||
None => panic!(), Some(x) => *x = new
|
||||
}
|
||||
assert_eq!(m.get(&5), Some(&new));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_len() {
|
||||
let mut map = VecMap::new();
|
||||
assert_eq!(map.len(), 0);
|
||||
assert!(map.is_empty());
|
||||
assert!(map.insert(5, 20).is_none());
|
||||
assert_eq!(map.len(), 1);
|
||||
assert!(!map.is_empty());
|
||||
assert!(map.insert(11, 12).is_none());
|
||||
assert_eq!(map.len(), 2);
|
||||
assert!(!map.is_empty());
|
||||
assert!(map.insert(14, 22).is_none());
|
||||
assert_eq!(map.len(), 3);
|
||||
assert!(!map.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clear() {
|
||||
let mut map = VecMap::new();
|
||||
assert!(map.insert(5, 20).is_none());
|
||||
assert!(map.insert(11, 12).is_none());
|
||||
assert!(map.insert(14, 22).is_none());
|
||||
map.clear();
|
||||
assert!(map.is_empty());
|
||||
assert!(map.get(&5).is_none());
|
||||
assert!(map.get(&11).is_none());
|
||||
assert!(map.get(&14).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert() {
|
||||
let mut m = VecMap::new();
|
||||
assert_eq!(m.insert(1, 2), None);
|
||||
assert_eq!(m.insert(1, 3), Some(2));
|
||||
assert_eq!(m.insert(1, 4), Some(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove() {
|
||||
let mut m = VecMap::new();
|
||||
m.insert(1, 2);
|
||||
assert_eq!(m.remove(&1), Some(2));
|
||||
assert_eq!(m.remove(&1), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_keys() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, 'a');
|
||||
map.insert(2, 'b');
|
||||
map.insert(3, 'c');
|
||||
let keys: Vec<_> = map.keys().collect();
|
||||
assert_eq!(keys.len(), 3);
|
||||
assert!(keys.contains(&1));
|
||||
assert!(keys.contains(&2));
|
||||
assert!(keys.contains(&3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_values() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, 'a');
|
||||
map.insert(2, 'b');
|
||||
map.insert(3, 'c');
|
||||
let values: Vec<_> = map.values().cloned().collect();
|
||||
assert_eq!(values.len(), 3);
|
||||
assert!(values.contains(&'a'));
|
||||
assert!(values.contains(&'b'));
|
||||
assert!(values.contains(&'c'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.size_hint(), (0, Some(11)));
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.size_hint(), (0, Some(10)));
|
||||
assert_eq!(it.next().unwrap(), (1, &2));
|
||||
assert_eq!(it.size_hint(), (0, Some(9)));
|
||||
assert_eq!(it.next().unwrap(), (3, &5));
|
||||
assert_eq!(it.size_hint(), (0, Some(7)));
|
||||
assert_eq!(it.next().unwrap(), (6, &10));
|
||||
assert_eq!(it.size_hint(), (0, Some(4)));
|
||||
assert_eq!(it.next().unwrap(), (10, &11));
|
||||
assert_eq!(it.size_hint(), (0, Some(0)));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_size_hints() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
assert_eq!(m.iter().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
|
||||
assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
for (k, v) in &mut m {
|
||||
*v += k as isize;
|
||||
}
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.next().unwrap(), (1, &3));
|
||||
assert_eq!(it.next().unwrap(), (3, &8));
|
||||
assert_eq!(it.next().unwrap(), (6, &16));
|
||||
assert_eq!(it.next().unwrap(), (10, &21));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rev_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
let mut it = m.iter().rev();
|
||||
assert_eq!(it.next().unwrap(), (10, &11));
|
||||
assert_eq!(it.next().unwrap(), (6, &10));
|
||||
assert_eq!(it.next().unwrap(), (3, &5));
|
||||
assert_eq!(it.next().unwrap(), (1, &2));
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_rev_iterator() {
|
||||
let mut m = VecMap::new();
|
||||
|
||||
assert!(m.insert(0, 1).is_none());
|
||||
assert!(m.insert(1, 2).is_none());
|
||||
assert!(m.insert(3, 5).is_none());
|
||||
assert!(m.insert(6, 10).is_none());
|
||||
assert!(m.insert(10, 11).is_none());
|
||||
|
||||
for (k, v) in m.iter_mut().rev() {
|
||||
*v += k as isize;
|
||||
}
|
||||
|
||||
let mut it = m.iter();
|
||||
assert_eq!(it.next().unwrap(), (0, &1));
|
||||
assert_eq!(it.next().unwrap(), (1, &3));
|
||||
assert_eq!(it.next().unwrap(), (3, &8));
|
||||
assert_eq!(it.next().unwrap(), (6, &16));
|
||||
assert_eq!(it.next().unwrap(), (10, &21));
|
||||
assert!(it.next().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_iter() {
|
||||
let mut m: VecMap<Box<_>> = VecMap::new();
|
||||
m.insert(1, box 2);
|
||||
let mut called = false;
|
||||
for (k, v) in m {
|
||||
assert!(!called);
|
||||
called = true;
|
||||
assert_eq!(k, 1);
|
||||
assert_eq!(v, box 2);
|
||||
}
|
||||
assert!(called);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drain_iterator() {
|
||||
let mut map = VecMap::new();
|
||||
map.insert(1, "a");
|
||||
map.insert(3, "c");
|
||||
map.insert(2, "b");
|
||||
|
||||
let vec: Vec<_> = map.drain().collect();
|
||||
|
||||
assert_eq!(vec, [(1, "a"), (2, "b"), (3, "c")]);
|
||||
assert_eq!(map.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_append() {
|
||||
let mut a = VecMap::new();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
|
||||
let mut b = VecMap::new();
|
||||
b.insert(3, "d"); // Overwrite element from a
|
||||
b.insert(4, "e");
|
||||
b.insert(5, "f");
|
||||
|
||||
a.append(&mut b);
|
||||
|
||||
assert_eq!(a.len(), 5);
|
||||
assert_eq!(b.len(), 0);
|
||||
// Capacity shouldn't change for possible reuse
|
||||
assert!(b.capacity() >= 4);
|
||||
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
assert_eq!(a[3], "d");
|
||||
assert_eq!(a[4], "e");
|
||||
assert_eq!(a[5], "f");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_off() {
|
||||
// Split within the key range
|
||||
let mut a = VecMap::new();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(3);
|
||||
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(b.len(), 2);
|
||||
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
|
||||
assert_eq!(b[3], "c");
|
||||
assert_eq!(b[4], "d");
|
||||
|
||||
// Split at 0
|
||||
a.clear();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(0);
|
||||
|
||||
assert_eq!(a.len(), 0);
|
||||
assert_eq!(b.len(), 4);
|
||||
assert_eq!(b[1], "a");
|
||||
assert_eq!(b[2], "b");
|
||||
assert_eq!(b[3], "c");
|
||||
assert_eq!(b[4], "d");
|
||||
|
||||
// Split behind max_key
|
||||
a.clear();
|
||||
a.insert(1, "a");
|
||||
a.insert(2, "b");
|
||||
a.insert(3, "c");
|
||||
a.insert(4, "d");
|
||||
|
||||
let b = a.split_off(5);
|
||||
|
||||
assert_eq!(a.len(), 4);
|
||||
assert_eq!(b.len(), 0);
|
||||
assert_eq!(a[1], "a");
|
||||
assert_eq!(a[2], "b");
|
||||
assert_eq!(a[3], "c");
|
||||
assert_eq!(a[4], "d");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_show() {
|
||||
let mut map = VecMap::new();
|
||||
let empty = VecMap::<i32>::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(3, 4);
|
||||
|
||||
let map_str = format!("{:?}", map);
|
||||
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
|
||||
assert_eq!(format!("{:?}", empty), "{}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone() {
|
||||
let mut a = VecMap::new();
|
||||
|
||||
a.insert(1, 'x');
|
||||
a.insert(4, 'y');
|
||||
a.insert(6, 'z');
|
||||
|
||||
assert!(a.clone() == a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(a == b);
|
||||
assert!(a.insert(0, 5).is_none());
|
||||
assert!(a != b);
|
||||
assert!(b.insert(0, 4).is_none());
|
||||
assert!(a != b);
|
||||
assert!(a.insert(5, 19).is_none());
|
||||
assert!(a != b);
|
||||
assert!(!b.insert(0, 5).is_none());
|
||||
assert!(a != b);
|
||||
assert!(b.insert(5, 19).is_none());
|
||||
assert!(a == b);
|
||||
|
||||
a = VecMap::new();
|
||||
b = VecMap::with_capacity(1);
|
||||
assert!(a == b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lt() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(!(a < b) && !(b < a));
|
||||
assert!(b.insert(2, 5).is_none());
|
||||
assert!(a < b);
|
||||
assert!(a.insert(2, 7).is_none());
|
||||
assert!(!(a < b) && b < a);
|
||||
assert!(b.insert(1, 0).is_none());
|
||||
assert!(b < a);
|
||||
assert!(a.insert(0, 6).is_none());
|
||||
assert!(a < b);
|
||||
assert!(a.insert(6, 2).is_none());
|
||||
assert!(a < b && !(b < a));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord() {
|
||||
let mut a = VecMap::new();
|
||||
let mut b = VecMap::new();
|
||||
|
||||
assert!(a <= b && a >= b);
|
||||
assert!(a.insert(1, 1).is_none());
|
||||
assert!(a > b && a >= b);
|
||||
assert!(b < a && b <= a);
|
||||
assert!(b.insert(2, 2).is_none());
|
||||
assert!(b > a && b >= a);
|
||||
assert!(a < b && a <= b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
let mut x = VecMap::new();
|
||||
let mut y = VecMap::new();
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
x.insert(1, 'a');
|
||||
x.insert(2, 'b');
|
||||
x.insert(3, 'c');
|
||||
|
||||
y.insert(3, 'c');
|
||||
y.insert(2, 'b');
|
||||
y.insert(1, 'a');
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
|
||||
x.insert(1000, 'd');
|
||||
x.remove(&1000);
|
||||
|
||||
assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_iter() {
|
||||
let xs = vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
|
||||
|
||||
let map: VecMap<_> = xs.iter().cloned().collect();
|
||||
|
||||
for &(k, v) in &xs {
|
||||
assert_eq!(map.get(&k), Some(&v));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
let mut map = VecMap::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(2, 1);
|
||||
map.insert(3, 4);
|
||||
|
||||
assert_eq!(map[3], 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_index_nonexistent() {
|
||||
let mut map = VecMap::new();
|
||||
|
||||
map.insert(1, 2);
|
||||
map.insert(2, 1);
|
||||
map.insert(3, 4);
|
||||
|
||||
map[4];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entry(){
|
||||
let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
|
||||
|
||||
let mut map: VecMap<_> = xs.iter().cloned().collect();
|
||||
|
||||
// Existing key (insert)
|
||||
match map.entry(1) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
assert_eq!(view.get(), &10);
|
||||
assert_eq!(view.insert(100), 10);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&1).unwrap(), &100);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
|
||||
// Existing key (update)
|
||||
match map.entry(2) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(mut view) => {
|
||||
let v = view.get_mut();
|
||||
*v *= 10;
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&2).unwrap(), &200);
|
||||
assert_eq!(map.len(), 6);
|
||||
|
||||
// Existing key (take)
|
||||
match map.entry(3) {
|
||||
Vacant(_) => unreachable!(),
|
||||
Occupied(view) => {
|
||||
assert_eq!(view.remove(), 30);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&3), None);
|
||||
assert_eq!(map.len(), 5);
|
||||
|
||||
|
||||
// Inexistent key (insert)
|
||||
match map.entry(10) {
|
||||
Occupied(_) => unreachable!(),
|
||||
Vacant(view) => {
|
||||
assert_eq!(*view.insert(1000), 1000);
|
||||
}
|
||||
}
|
||||
assert_eq!(map.get(&10).unwrap(), &1000);
|
||||
assert_eq!(map.len(), 6);
|
||||
}
|
||||
|
||||
mod bench {
|
||||
use std::collections::VecMap;
|
||||
|
||||
map_insert_rand_bench!{insert_rand_100, 100, VecMap}
|
||||
map_insert_rand_bench!{insert_rand_10_000, 10_000, VecMap}
|
||||
|
||||
map_insert_seq_bench!{insert_seq_100, 100, VecMap}
|
||||
map_insert_seq_bench!{insert_seq_10_000, 10_000, VecMap}
|
||||
|
||||
map_find_rand_bench!{find_rand_100, 100, VecMap}
|
||||
map_find_rand_bench!{find_rand_10_000, 10_000, VecMap}
|
||||
|
||||
map_find_seq_bench!{find_seq_100, 100, VecMap}
|
||||
map_find_seq_bench!{find_seq_10_000, 10_000, VecMap}
|
||||
}
|
Loading…
Add table
Reference in a new issue