2013-02-16 16:55:55 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-03-21 18:14:02 -05:00
|
|
|
//! A double-ended queue implemented as a circular buffer
|
2013-07-11 09:17:51 -05:00
|
|
|
//!
|
|
|
|
//! RingBuf implements the trait Deque. It should be imported with `use
|
|
|
|
//! extra::container::Deque`.
|
2013-05-17 17:28:44 -05:00
|
|
|
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
use std::num;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::vec;
|
2013-07-29 19:06:49 -05:00
|
|
|
use std::iterator::{FromIterator, Invert, RandomAccessIterator, Extendable};
|
2013-05-05 23:42:54 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
use container::Deque;
|
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
static INITIAL_CAPACITY: uint = 8u; // 2^3
|
|
|
|
static MINIMUM_CAPACITY: uint = 2u;
|
2013-02-16 16:55:55 -06:00
|
|
|
|
2013-07-11 09:17:51 -05:00
|
|
|
/// RingBuf is a circular buffer that implements Deque.
|
2013-07-06 08:27:32 -05:00
|
|
|
#[deriving(Clone)]
|
2013-07-10 08:27:14 -05:00
|
|
|
pub struct RingBuf<T> {
|
2013-02-16 16:55:55 -06:00
|
|
|
priv nelts: uint,
|
|
|
|
priv lo: uint,
|
|
|
|
priv elts: ~[Option<T>]
|
2012-01-11 05:49:33 -06:00
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<T> Container for RingBuf<T> {
|
|
|
|
/// Return the number of elements in the RingBuf
|
2013-06-23 22:44:11 -05:00
|
|
|
fn len(&self) -> uint { self.nelts }
|
2013-02-16 16:55:55 -06:00
|
|
|
}
|
2010-07-20 20:03:09 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<T> Mutable for RingBuf<T> {
|
|
|
|
/// Clear the RingBuf, removing all values.
|
2013-02-16 17:55:25 -06:00
|
|
|
fn clear(&mut self) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in self.elts.mut_iter() { *x = None }
|
2013-02-16 17:55:25 -06:00
|
|
|
self.nelts = 0;
|
|
|
|
self.lo = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<T> Deque<T> for RingBuf<T> {
|
|
|
|
/// Return a reference to the first element in the RingBuf
|
|
|
|
fn front<'a>(&'a self) -> Option<&'a T> {
|
|
|
|
if self.nelts > 0 { Some(self.get(0)) } else { None }
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Return a mutable reference to the first element in the RingBuf
|
|
|
|
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
|
|
|
if self.nelts > 0 { Some(self.get_mut(0)) } else { None }
|
2013-04-10 15:14:06 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Return a reference to the last element in the RingBuf
|
|
|
|
fn back<'a>(&'a self) -> Option<&'a T> {
|
|
|
|
if self.nelts > 0 { Some(self.get(self.nelts - 1)) } else { None }
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
2013-04-10 15:14:06 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Return a mutable reference to the last element in the RingBuf
|
|
|
|
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
|
|
|
if self.nelts > 0 { Some(self.get_mut(self.nelts - 1)) } else { None }
|
2013-04-10 15:14:06 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Remove and return the first element in the RingBuf, or None if it is empty
|
|
|
|
fn pop_front(&mut self) -> Option<T> {
|
2013-07-17 16:41:50 -05:00
|
|
|
let result = self.elts[self.lo].take();
|
2013-07-10 08:27:14 -05:00
|
|
|
if result.is_some() {
|
|
|
|
self.lo = (self.lo + 1u) % self.elts.len();
|
|
|
|
self.nelts -= 1u;
|
|
|
|
}
|
2013-02-16 18:43:29 -06:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Remove and return the last element in the RingBuf, or None if it is empty
|
|
|
|
fn pop_back(&mut self) -> Option<T> {
|
|
|
|
if self.nelts > 0 {
|
|
|
|
self.nelts -= 1;
|
|
|
|
let hi = self.raw_index(self.nelts);
|
2013-07-17 16:41:50 -05:00
|
|
|
self.elts[hi].take()
|
2013-07-10 08:27:14 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2013-02-16 18:43:29 -06:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Prepend an element to the RingBuf
|
|
|
|
fn push_front(&mut self, t: T) {
|
2013-07-05 22:42:45 -05:00
|
|
|
if self.nelts == self.elts.len() {
|
2013-07-05 22:42:45 -05:00
|
|
|
grow(self.nelts, &mut self.lo, &mut self.elts);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
2013-02-16 16:55:55 -06:00
|
|
|
if self.lo == 0u {
|
|
|
|
self.lo = self.elts.len() - 1u;
|
|
|
|
} else { self.lo -= 1u; }
|
|
|
|
self.elts[self.lo] = Some(t);
|
|
|
|
self.nelts += 1u;
|
|
|
|
}
|
2011-07-12 16:20:15 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Append an element to the RingBuf
|
|
|
|
fn push_back(&mut self, t: T) {
|
2013-07-05 22:42:45 -05:00
|
|
|
if self.nelts == self.elts.len() {
|
2013-07-05 22:42:45 -05:00
|
|
|
grow(self.nelts, &mut self.lo, &mut self.elts);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
2013-07-05 22:42:45 -05:00
|
|
|
let hi = self.raw_index(self.nelts);
|
|
|
|
self.elts[hi] = Some(t);
|
2013-02-16 16:55:55 -06:00
|
|
|
self.nelts += 1u;
|
2010-07-20 20:03:09 -05:00
|
|
|
}
|
2013-07-10 08:27:14 -05:00
|
|
|
}
|
2013-05-27 13:47:38 -05:00
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<T> RingBuf<T> {
|
|
|
|
/// Create an empty RingBuf
|
|
|
|
pub fn new() -> RingBuf<T> {
|
|
|
|
RingBuf::with_capacity(INITIAL_CAPACITY)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an empty RingBuf with space for at least `n` elements.
|
|
|
|
pub fn with_capacity(n: uint) -> RingBuf<T> {
|
|
|
|
RingBuf{nelts: 0, lo: 0,
|
|
|
|
elts: vec::from_fn(num::max(MINIMUM_CAPACITY, n), |_| None)}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve an element in the RingBuf by index
|
|
|
|
///
|
|
|
|
/// Fails if there is no element with the given index
|
|
|
|
pub fn get<'a>(&'a self, i: uint) -> &'a T {
|
|
|
|
let idx = self.raw_index(i);
|
|
|
|
match self.elts[idx] {
|
|
|
|
None => fail!(),
|
|
|
|
Some(ref v) => v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve an element in the RingBuf by index
|
|
|
|
///
|
|
|
|
/// Fails if there is no element with the given index
|
|
|
|
pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
|
|
|
|
let idx = self.raw_index(i);
|
|
|
|
match self.elts[idx] {
|
|
|
|
None => fail!(),
|
|
|
|
Some(ref mut v) => v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return index in underlying vec for a given logical element index
|
|
|
|
fn raw_index(&self, idx: uint) -> uint {
|
|
|
|
raw_index(self.lo, self.elts.len(), idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserve capacity for exactly `n` elements in the given RingBuf,
|
2013-05-27 13:47:38 -05:00
|
|
|
/// doing nothing if `self`'s capacity is already equal to or greater
|
|
|
|
/// than the requested capacity
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * n - The number of elements to reserve space for
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reserve(&mut self, n: uint) {
|
2013-06-27 09:40:47 -05:00
|
|
|
self.elts.reserve(n);
|
2013-05-27 13:47:38 -05:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// Reserve capacity for at least `n` elements in the given RingBuf,
|
2013-05-27 13:47:38 -05:00
|
|
|
/// over-allocating in case the caller needs to reserve additional
|
|
|
|
/// space.
|
|
|
|
///
|
|
|
|
/// Do nothing if `self`'s capacity is already equal to or greater
|
|
|
|
/// than the requested capacity.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * n - The number of elements to reserve space for
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn reserve_at_least(&mut self, n: uint) {
|
2013-06-27 09:40:47 -05:00
|
|
|
self.elts.reserve_at_least(n);
|
2013-05-27 13:47:38 -05:00
|
|
|
}
|
2013-06-25 14:08:47 -05:00
|
|
|
|
|
|
|
/// Front-to-back iterator.
|
2013-07-10 08:27:14 -05:00
|
|
|
pub fn iter<'a>(&'a self) -> RingBufIterator<'a, T> {
|
2013-07-29 13:16:26 -05:00
|
|
|
RingBufIterator{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts}
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
|
2013-07-15 18:13:26 -05:00
|
|
|
/// Back-to-front iterator.
|
2013-07-27 16:41:20 -05:00
|
|
|
pub fn rev_iter<'a>(&'a self) -> Invert<RingBufIterator<'a, T>> {
|
2013-07-15 18:13:26 -05:00
|
|
|
self.iter().invert()
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-15 18:13:26 -05:00
|
|
|
/// Front-to-back iterator which returns mutable values.
|
|
|
|
pub fn mut_iter<'a>(&'a mut self) -> RingBufMutIterator<'a, T> {
|
2013-07-29 13:16:26 -05:00
|
|
|
RingBufMutIterator{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts}
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Back-to-front iterator which returns mutable values.
|
2013-07-27 16:41:20 -05:00
|
|
|
pub fn mut_rev_iter<'a>(&'a mut self) -> Invert<RingBufMutIterator<'a, T>> {
|
2013-07-15 18:13:26 -05:00
|
|
|
self.mut_iter().invert()
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 10:38:29 -05:00
|
|
|
macro_rules! iterator {
|
2013-07-15 18:13:26 -05:00
|
|
|
(impl $name:ident -> $elem:ty, $getter:ident) => {
|
2013-06-26 10:38:29 -05:00
|
|
|
impl<'self, T> Iterator<$elem> for $name<'self, T> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<$elem> {
|
2013-07-29 13:16:26 -05:00
|
|
|
if self.index == self.rindex {
|
2013-06-26 10:38:29 -05:00
|
|
|
return None;
|
|
|
|
}
|
2013-07-05 22:42:45 -05:00
|
|
|
let raw_index = raw_index(self.lo, self.elts.len(), self.index);
|
2013-07-15 18:13:26 -05:00
|
|
|
self.index += 1;
|
2013-07-29 13:16:26 -05:00
|
|
|
Some(self.elts[raw_index] . $getter ())
|
2013-06-26 10:38:29 -05:00
|
|
|
}
|
2013-07-14 15:30:22 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-07-29 13:16:26 -05:00
|
|
|
let len = self.rindex - self.index;
|
|
|
|
(len, Some(len))
|
2013-07-14 15:30:22 -05:00
|
|
|
}
|
2013-06-26 10:38:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 18:13:26 -05:00
|
|
|
macro_rules! iterator_rev {
|
|
|
|
(impl $name:ident -> $elem:ty, $getter:ident) => {
|
|
|
|
impl<'self, T> DoubleEndedIterator<$elem> for $name<'self, T> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<$elem> {
|
2013-07-29 13:16:26 -05:00
|
|
|
if self.index == self.rindex {
|
2013-07-15 18:13:26 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.rindex -= 1;
|
2013-07-29 13:16:26 -05:00
|
|
|
let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
|
|
|
|
Some(self.elts[raw_index] . $getter ())
|
2013-07-15 18:13:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-29 13:16:26 -05:00
|
|
|
|
2013-07-15 18:13:26 -05:00
|
|
|
/// RingBuf iterator
|
|
|
|
pub struct RingBufIterator<'self, T> {
|
2013-07-05 22:42:45 -05:00
|
|
|
priv lo: uint,
|
|
|
|
priv index: uint,
|
2013-07-15 18:13:26 -05:00
|
|
|
priv rindex: uint,
|
2013-07-05 22:42:45 -05:00
|
|
|
priv elts: &'self [Option<T>],
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-07-15 18:13:26 -05:00
|
|
|
iterator!{impl RingBufIterator -> &'self T, get_ref}
|
|
|
|
iterator_rev!{impl RingBufIterator -> &'self T, get_ref}
|
2013-06-26 10:38:29 -05:00
|
|
|
|
2013-07-29 13:16:26 -05:00
|
|
|
impl<'self, T> RandomAccessIterator<&'self T> for RingBufIterator<'self, T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint { self.rindex - self.index }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn idx(&self, j: uint) -> Option<&'self T> {
|
|
|
|
if j >= self.indexable() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
|
|
|
|
Some(self.elts[raw_index].get_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
/// RingBuf mutable iterator
|
|
|
|
pub struct RingBufMutIterator<'self, T> {
|
2013-07-05 22:42:45 -05:00
|
|
|
priv lo: uint,
|
|
|
|
priv index: uint,
|
2013-07-15 18:13:26 -05:00
|
|
|
priv rindex: uint,
|
2013-07-05 22:42:45 -05:00
|
|
|
priv elts: &'self mut [Option<T>],
|
2013-06-25 14:08:47 -05:00
|
|
|
}
|
2013-07-15 18:13:26 -05:00
|
|
|
iterator!{impl RingBufMutIterator -> &'self mut T, get_mut_ref}
|
|
|
|
iterator_rev!{impl RingBufMutIterator -> &'self mut T, get_mut_ref}
|
2012-01-11 05:49:33 -06:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
/// Grow is only called on full elts, so nelts is also len(elts), unlike
|
|
|
|
/// elsewhere.
|
2013-07-05 22:42:45 -05:00
|
|
|
fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut ~[Option<T>]) {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(nelts, elts.len());
|
2013-07-05 22:42:45 -05:00
|
|
|
let lo = *loptr;
|
|
|
|
let newlen = nelts * 2;
|
2013-07-05 22:42:45 -05:00
|
|
|
elts.reserve(newlen);
|
2013-02-16 16:55:55 -06:00
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
/* fill with None */
|
2013-08-03 11:45:23 -05:00
|
|
|
for _ in range(elts.len(), elts.capacity()) {
|
2013-07-05 22:42:45 -05:00
|
|
|
elts.push(None);
|
|
|
|
}
|
2013-07-05 22:42:45 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Move the shortest half into the newly reserved area.
|
|
|
|
lo ---->|
|
|
|
|
nelts ----------->|
|
|
|
|
[o o o|o o o o o]
|
|
|
|
A [. . .|o o o o o o o o|. . . . .]
|
|
|
|
B [o o o|. . . . . . . .|o o o o o]
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert!(newlen - nelts/2 >= nelts);
|
|
|
|
if lo <= (nelts - lo) { // A
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, lo) {
|
2013-07-05 22:42:45 -05:00
|
|
|
elts.swap(i, nelts + i);
|
|
|
|
}
|
|
|
|
} else { // B
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(lo, nelts) {
|
2013-07-05 22:42:45 -05:00
|
|
|
elts.swap(i, newlen - nelts + i);
|
|
|
|
}
|
|
|
|
*loptr += newlen - nelts;
|
2013-02-16 16:55:55 -06:00
|
|
|
}
|
|
|
|
}
|
2013-01-22 10:44:24 -06:00
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
/// Return index in underlying vec for a given logical element index
|
|
|
|
fn raw_index(lo: uint, len: uint, index: uint) -> uint {
|
|
|
|
if lo >= len - index {
|
|
|
|
lo + index - len
|
|
|
|
} else {
|
|
|
|
lo + index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<A: Eq> Eq for RingBuf<A> {
|
|
|
|
fn eq(&self, other: &RingBuf<A>) -> bool {
|
2013-07-06 08:27:32 -05:00
|
|
|
self.nelts == other.nelts &&
|
|
|
|
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
|
|
|
|
}
|
2013-07-10 08:27:14 -05:00
|
|
|
fn ne(&self, other: &RingBuf<A>) -> bool {
|
2013-07-06 08:27:32 -05:00
|
|
|
!self.eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
impl<A, T: Iterator<A>> FromIterator<A, T> for RingBuf<A> {
|
|
|
|
fn from_iterator(iterator: &mut T) -> RingBuf<A> {
|
2013-07-29 19:06:49 -05:00
|
|
|
let (lower, _) = iterator.size_hint();
|
|
|
|
let mut deq = RingBuf::with_capacity(lower);
|
|
|
|
deq.extend(iterator);
|
|
|
|
deq
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, T: Iterator<A>> Extendable<A, T> for RingBuf<A> {
|
|
|
|
fn extend(&mut self, iterator: &mut T) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for elt in *iterator {
|
2013-07-29 19:06:49 -05:00
|
|
|
self.push_back(elt);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-02-16 16:55:55 -06:00
|
|
|
use super::*;
|
2013-07-02 14:47:32 -05:00
|
|
|
use std::clone::Clone;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cmp::Eq;
|
2013-07-05 22:42:45 -05:00
|
|
|
use extra::test;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 0u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(17);
|
|
|
|
d.push_front(42);
|
|
|
|
d.push_back(137);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 3u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_back(137);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 4u);
|
2013-07-10 08:27:14 -05:00
|
|
|
debug!(d.front());
|
|
|
|
assert_eq!(*d.front().unwrap(), 42);
|
|
|
|
debug!(d.back());
|
|
|
|
assert_eq!(*d.back().unwrap(), 137);
|
|
|
|
let mut i = d.pop_front();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(i, Some(42));
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(i, Some(137));
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(i, Some(137));
|
2012-01-17 21:05:07 -06:00
|
|
|
i = d.pop_back();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(i);
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(i, Some(17));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 0u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_back(3);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 1u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(2);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 2u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_back(4);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 3u);
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(1);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(d.len(), 4u);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(d.get(0));
|
|
|
|
debug!(d.get(1));
|
|
|
|
debug!(d.get(2));
|
|
|
|
debug!(d.get(3));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(*d.get(0), 1);
|
|
|
|
assert_eq!(*d.get(1), 2);
|
|
|
|
assert_eq!(*d.get(2), 3);
|
|
|
|
assert_eq!(*d.get(3), 4);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_boxes() {
|
|
|
|
let a: @int = @5;
|
|
|
|
let b: @int = @72;
|
|
|
|
let c: @int = @64;
|
|
|
|
let d: @int = @175;
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut deq = RingBuf::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(a);
|
|
|
|
deq.push_front(b);
|
|
|
|
deq.push_back(c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_back(d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(deq.front(), Some(&b));
|
|
|
|
assert_eq!(deq.back(), Some(&d));
|
|
|
|
assert_eq!(deq.pop_front(), Some(b));
|
|
|
|
assert_eq!(deq.pop_back(), Some(d));
|
|
|
|
assert_eq!(deq.pop_back(), Some(c));
|
|
|
|
assert_eq!(deq.pop_back(), Some(a));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_back(c);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 1);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(b);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 2);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_back(d);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(a);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
|
|
|
assert_eq!(*deq.get(0), a);
|
|
|
|
assert_eq!(*deq.get(1), b);
|
|
|
|
assert_eq!(*deq.get(2), c);
|
|
|
|
assert_eq!(*deq.get(3), d);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-05-01 18:32:37 -05:00
|
|
|
#[cfg(test)]
|
2013-07-02 14:47:32 -05:00
|
|
|
fn test_parameterized<T:Clone + Eq>(a: T, b: T, c: T, d: T) {
|
2013-07-12 23:05:59 -05:00
|
|
|
let mut deq = RingBuf::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_front(a.clone());
|
|
|
|
deq.push_front(b.clone());
|
|
|
|
deq.push_back(c.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_back(d.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
2013-07-12 23:05:59 -05:00
|
|
|
assert_eq!((*deq.front().get()).clone(), b.clone());
|
|
|
|
assert_eq!((*deq.back().get()).clone(), d.clone());
|
|
|
|
assert_eq!(deq.pop_front().get(), b.clone());
|
|
|
|
assert_eq!(deq.pop_back().get(), d.clone());
|
|
|
|
assert_eq!(deq.pop_back().get(), c.clone());
|
|
|
|
assert_eq!(deq.pop_back().get(), a.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 0);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_back(c.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 1);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_front(b.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 2);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_back(d.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 3);
|
2013-07-12 23:05:59 -05:00
|
|
|
deq.push_front(a.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(deq.len(), 4);
|
2013-07-02 14:47:32 -05:00
|
|
|
assert_eq!((*deq.get(0)).clone(), a.clone());
|
|
|
|
assert_eq!((*deq.get(1)).clone(), b.clone());
|
|
|
|
assert_eq!((*deq.get(2)).clone(), c.clone());
|
|
|
|
assert_eq!((*deq.get(3)).clone(), d.clone());
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
#[test]
|
2013-07-10 08:27:14 -05:00
|
|
|
fn test_push_front_grow() {
|
|
|
|
let mut deq = RingBuf::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 66) {
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(i);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
assert_eq!(deq.len(), 66);
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 66) {
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(*deq.get(i), 65 - i);
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut deq = RingBuf::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 66) {
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_back(i);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 66) {
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(*deq.get(i), i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_new(b: &mut test::BenchHarness) {
|
|
|
|
do b.iter {
|
2013-07-10 08:27:14 -05:00
|
|
|
let _ = RingBuf::new::<u64>();
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2013-07-10 08:27:14 -05:00
|
|
|
fn bench_push_back(b: &mut test::BenchHarness) {
|
|
|
|
let mut deq = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
do b.iter {
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_back(0);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2013-07-10 08:27:14 -05:00
|
|
|
fn bench_push_front(b: &mut test::BenchHarness) {
|
|
|
|
let mut deq = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
do b.iter {
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(0);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_grow(b: &mut test::BenchHarness) {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut deq = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
do b.iter {
|
2013-07-31 21:18:19 -05:00
|
|
|
do 65.times {
|
2013-07-10 08:27:14 -05:00
|
|
|
deq.push_front(1);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
enum Taggy {
|
|
|
|
One(int),
|
|
|
|
Two(int, int),
|
|
|
|
Three(int, int, int),
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-08-11 09:08:42 -05:00
|
|
|
enum Taggypar<T> {
|
2013-07-02 14:47:32 -05:00
|
|
|
Onepar(int),
|
|
|
|
Twopar(int, int),
|
|
|
|
Threepar(int, int, int),
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-01-22 10:44:24 -06:00
|
|
|
struct RecCy {
|
|
|
|
x: int,
|
|
|
|
y: int,
|
2013-01-25 18:57:39 -06:00
|
|
|
t: Taggy
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-13 00:09:55 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_int() {
|
|
|
|
test_parameterized::<int>(5, 72, 64, 175);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_at_int() {
|
|
|
|
test_parameterized::<@int>(@5, @72, @64, @175);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_param_taggy() {
|
2013-06-26 17:14:35 -05:00
|
|
|
test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
|
2012-09-13 00:09:55 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_param_taggypar() {
|
|
|
|
test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
|
2012-08-11 09:08:42 -05:00
|
|
|
Twopar::<int>(1, 2),
|
|
|
|
Threepar::<int>(1, 2, 3),
|
|
|
|
Twopar::<int>(17, 42));
|
2012-09-13 00:09:55 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-09-13 00:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn test_param_reccy() {
|
2013-01-22 10:44:24 -06:00
|
|
|
let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
|
|
|
|
let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
|
|
|
|
let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
|
|
|
|
let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
|
2012-09-13 00:09:55 -05:00
|
|
|
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2013-03-29 20:02:44 -05:00
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
#[test]
|
|
|
|
fn test_with_capacity() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::with_capacity(0);
|
|
|
|
d.push_back(1);
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(d.len(), 1);
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::with_capacity(50);
|
|
|
|
d.push_back(1);
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(d.len(), 1);
|
|
|
|
}
|
|
|
|
|
2013-05-27 13:47:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_reserve() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
d.push_back(0u64);
|
2013-05-27 13:47:38 -05:00
|
|
|
d.reserve(50);
|
2013-06-27 09:40:47 -05:00
|
|
|
assert_eq!(d.elts.capacity(), 50);
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
d.push_back(0u32);
|
2013-05-27 13:47:38 -05:00
|
|
|
d.reserve(50);
|
2013-06-27 09:40:47 -05:00
|
|
|
assert_eq!(d.elts.capacity(), 50);
|
2013-05-27 13:47:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reserve_at_least() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
d.push_back(0u64);
|
2013-05-27 13:47:38 -05:00
|
|
|
d.reserve_at_least(50);
|
2013-06-27 09:40:47 -05:00
|
|
|
assert_eq!(d.elts.capacity(), 64);
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
d.push_back(0u32);
|
2013-05-27 13:47:38 -05:00
|
|
|
d.reserve_at_least(50);
|
2013-06-27 09:40:47 -05:00
|
|
|
assert_eq!(d.elts.capacity(), 64);
|
2013-05-27 13:47:38 -05:00
|
|
|
}
|
|
|
|
|
2013-06-26 09:04:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iter() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(d.iter().next(), None);
|
2013-07-14 15:30:22 -05:00
|
|
|
assert_eq!(d.iter().size_hint(), (0, Some(0)));
|
2013-07-05 22:42:45 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0, 5) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_back(i);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.iter().collect::<~[&int]>(), ~[&0,&1,&2,&3,&4]);
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(6, 9) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(i);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.iter().collect::<~[&int]>(), ~[&8,&7,&6,&0,&1,&2,&3,&4]);
|
2013-07-14 15:30:22 -05:00
|
|
|
|
|
|
|
let mut it = d.iter();
|
|
|
|
let mut len = d.len();
|
|
|
|
loop {
|
|
|
|
match it.next() {
|
|
|
|
None => break,
|
|
|
|
_ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rev_iter() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(d.rev_iter().next(), None);
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0, 5) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_back(i);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0]);
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(6, 9) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(i);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2013-06-26 17:14:35 -05:00
|
|
|
assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0,&6,&7,&8]);
|
2013-06-26 09:04:44 -05:00
|
|
|
}
|
2013-07-05 22:42:45 -05:00
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
assert!(d.mut_iter().next().is_none());
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 3) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(i);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in d.mut_iter().enumerate() {
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(*elt, 2 - i);
|
|
|
|
*elt = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut it = d.mut_iter();
|
|
|
|
assert_eq!(*it.next().unwrap(), 0);
|
|
|
|
assert_eq!(*it.next().unwrap(), 1);
|
|
|
|
assert_eq!(*it.next().unwrap(), 2);
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_rev_iter() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
2013-07-05 22:42:45 -05:00
|
|
|
assert!(d.mut_rev_iter().next().is_none());
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 3) {
|
2013-07-10 08:27:14 -05:00
|
|
|
d.push_front(i);
|
2013-07-05 22:42:45 -05:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in d.mut_rev_iter().enumerate() {
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(*elt, i);
|
|
|
|
*elt = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut it = d.mut_rev_iter();
|
|
|
|
assert_eq!(*it.next().unwrap(), 0);
|
|
|
|
assert_eq!(*it.next().unwrap(), 1);
|
|
|
|
assert_eq!(*it.next().unwrap(), 2);
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 22:42:45 -05:00
|
|
|
#[test]
|
|
|
|
fn test_from_iterator() {
|
|
|
|
use std::iterator;
|
|
|
|
let v = ~[1,2,3,4,5,6,7];
|
2013-07-10 08:27:14 -05:00
|
|
|
let deq: RingBuf<int> = v.iter().transform(|&x| x).collect();
|
2013-07-05 22:42:45 -05:00
|
|
|
let u: ~[int] = deq.iter().transform(|&x| x).collect();
|
|
|
|
assert_eq!(u, v);
|
|
|
|
|
|
|
|
let mut seq = iterator::Counter::new(0u, 2).take_(256);
|
2013-07-10 08:27:14 -05:00
|
|
|
let deq: RingBuf<uint> = seq.collect();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, &x) in deq.iter().enumerate() {
|
2013-07-05 22:42:45 -05:00
|
|
|
assert_eq!(2*i, x);
|
|
|
|
}
|
|
|
|
assert_eq!(deq.len(), 256);
|
|
|
|
}
|
2013-07-06 08:27:32 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
d.push_front(17);
|
|
|
|
d.push_front(42);
|
|
|
|
d.push_back(137);
|
|
|
|
d.push_back(137);
|
2013-07-06 08:27:32 -05:00
|
|
|
assert_eq!(d.len(), 4u);
|
|
|
|
let mut e = d.clone();
|
|
|
|
assert_eq!(e.len(), 4u);
|
|
|
|
while !d.is_empty() {
|
|
|
|
assert_eq!(d.pop_back(), e.pop_back());
|
|
|
|
}
|
|
|
|
assert_eq!(d.len(), 0u);
|
|
|
|
assert_eq!(e.len(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2013-07-10 08:27:14 -05:00
|
|
|
let mut d = RingBuf::new();
|
|
|
|
assert_eq!(&d, &RingBuf::with_capacity(0));
|
|
|
|
d.push_front(137);
|
|
|
|
d.push_front(17);
|
|
|
|
d.push_front(42);
|
|
|
|
d.push_back(137);
|
|
|
|
let mut e = RingBuf::with_capacity(0);
|
|
|
|
e.push_back(42);
|
|
|
|
e.push_back(17);
|
|
|
|
e.push_back(137);
|
|
|
|
e.push_back(137);
|
2013-07-06 08:27:32 -05:00
|
|
|
assert_eq!(&e, &d);
|
|
|
|
e.pop_back();
|
2013-07-10 08:27:14 -05:00
|
|
|
e.push_back(0);
|
2013-07-06 08:27:32 -05:00
|
|
|
assert!(e != d);
|
|
|
|
e.clear();
|
2013-07-10 08:27:14 -05:00
|
|
|
assert_eq!(e, RingBuf::new());
|
2013-07-06 08:27:32 -05:00
|
|
|
}
|
2012-07-03 12:52:32 -05:00
|
|
|
}
|