blob: c61fcd0c76bbd8220b319aaab445ba52517f7403 [file] [log] [blame]
Daniel Micayb47e1e92013-02-16 22:55:551// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
Graydon Hoare00c856c2012-12-04 00:48:012// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
Daniel Micayb2060172013-03-21 23:14:0211//! A double-ended queue implemented as a circular buffer
blake2-ppc0f9b9a52013-07-11 14:17:5112//!
13//! RingBuf implements the trait Deque. It should be imported with `use
Scott Jenkinsedc7ad12014-03-30 12:35:5414//! collections::deque::Deque`.
Patrick Waltonf3723cf2013-05-17 22:28:4415
Michael Darakanandabf1464c2014-02-06 07:34:3316use std::cmp;
Sean Chalmers292ed3e2014-01-23 19:41:5717use std::iter::{Rev, RandomAccessIterator};
Alex Crichton998fece2013-05-06 04:42:5418
HeroesGraved81bb442014-02-03 05:56:4919use deque::Deque;
blake2-ppc70523712013-07-10 13:27:1420
blake2-ppc0ff5c172013-07-06 03:42:4521static INITIAL_CAPACITY: uint = 8u; // 2^3
22static MINIMUM_CAPACITY: uint = 2u;
Daniel Micayb47e1e92013-02-16 22:55:5523
blake2-ppc0f9b9a52013-07-11 14:17:5124/// RingBuf is a circular buffer that implements Deque.
blake2-ppc10c76982013-07-06 13:27:3225#[deriving(Clone)]
blake2-ppc70523712013-07-10 13:27:1426pub struct RingBuf<T> {
Alex Crichton8ad7e542014-03-27 22:10:0427 nelts: uint,
28 lo: uint,
Huon Wilson4b9a7a22014-04-05 05:45:4229 elts: Vec<Option<T>>
Marijn Haverbeke26610db2012-01-11 11:49:3330}
Roy Frostig9c818892010-07-21 01:03:0931
blake2-ppc70523712013-07-10 13:27:1432impl<T> Container for RingBuf<T> {
33 /// Return the number of elements in the RingBuf
Alex Crichtonb29c3682013-06-24 03:44:1134 fn len(&self) -> uint { self.nelts }
Daniel Micayb47e1e92013-02-16 22:55:5535}
Roy Frostig9c818892010-07-21 01:03:0936
blake2-ppc70523712013-07-10 13:27:1437impl<T> Mutable for RingBuf<T> {
38 /// Clear the RingBuf, removing all values.
Daniel Micayed7c9c42013-02-16 23:55:2539 fn clear(&mut self) {
Daniel Micay100894552013-08-03 16:45:2340 for x in self.elts.mut_iter() { *x = None }
Daniel Micayed7c9c42013-02-16 23:55:2541 self.nelts = 0;
42 self.lo = 0;
Daniel Micayed7c9c42013-02-16 23:55:2543 }
44}
45
blake2-ppc70523712013-07-10 13:27:1446impl<T> Deque<T> for RingBuf<T> {
47 /// Return a reference to the first element in the RingBuf
48 fn front<'a>(&'a self) -> Option<&'a T> {
49 if self.nelts > 0 { Some(self.get(0)) } else { None }
blake2-ppc0ff5c172013-07-06 03:42:4550 }
51
blake2-ppc70523712013-07-10 13:27:1452 /// Return a mutable reference to the first element in the RingBuf
53 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
54 if self.nelts > 0 { Some(self.get_mut(0)) } else { None }
Niko Matsakis03396472013-04-10 20:14:0655 }
56
blake2-ppc70523712013-07-10 13:27:1457 /// Return a reference to the last element in the RingBuf
58 fn back<'a>(&'a self) -> Option<&'a T> {
59 if self.nelts > 0 { Some(self.get(self.nelts - 1)) } else { None }
60 }
Niko Matsakis03396472013-04-10 20:14:0661
blake2-ppc70523712013-07-10 13:27:1462 /// Return a mutable reference to the last element in the RingBuf
63 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
64 if self.nelts > 0 { Some(self.get_mut(self.nelts - 1)) } else { None }
65 }
66
67 /// Remove and return the first element in the RingBuf, or None if it is empty
68 fn pop_front(&mut self) -> Option<T> {
Huon Wilson4b9a7a22014-04-05 05:45:4269 let result = self.elts.get_mut(self.lo).take();
blake2-ppc70523712013-07-10 13:27:1470 if result.is_some() {
71 self.lo = (self.lo + 1u) % self.elts.len();
72 self.nelts -= 1u;
blake2-ppc5d72f3f2013-07-06 03:42:4573 }
Daniel Micay61906612013-02-17 00:43:2974 result
75 }
76
blake2-ppc70523712013-07-10 13:27:1477 /// Remove and return the last element in the RingBuf, or None if it is empty
78 fn pop_back(&mut self) -> Option<T> {
79 if self.nelts > 0 {
80 self.nelts -= 1;
81 let hi = self.raw_index(self.nelts);
Huon Wilson4b9a7a22014-04-05 05:45:4282 self.elts.get_mut(hi).take()
blake2-ppc70523712013-07-10 13:27:1483 } else {
84 None
85 }
blake2-ppc5d72f3f2013-07-06 03:42:4586 }
87
blake2-ppc70523712013-07-10 13:27:1488 /// Prepend an element to the RingBuf
89 fn push_front(&mut self, t: T) {
blake2-ppc40ce0b72013-07-06 03:42:4590 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:4591 grow(self.nelts, &mut self.lo, &mut self.elts);
blake2-ppc40ce0b72013-07-06 03:42:4592 }
Daniel Micayb47e1e92013-02-16 22:55:5593 if self.lo == 0u {
94 self.lo = self.elts.len() - 1u;
95 } else { self.lo -= 1u; }
Huon Wilson4b9a7a22014-04-05 05:45:4296 *self.elts.get_mut(self.lo) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:5597 self.nelts += 1u;
Erick Tryzelaare84576b2013-01-22 16:44:2498 }
Marijn Haverbeke26610db2012-01-11 11:49:3399
blake2-ppc70523712013-07-10 13:27:14100 /// Append an element to the RingBuf
101 fn push_back(&mut self, t: T) {
blake2-ppc5d72f3f2013-07-06 03:42:45102 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:45103 grow(self.nelts, &mut self.lo, &mut self.elts);
Graydon Hoare2880ecd2010-09-22 22:44:13104 }
blake2-ppc5d72f3f2013-07-06 03:42:45105 let hi = self.raw_index(self.nelts);
Huon Wilson4b9a7a22014-04-05 05:45:42106 *self.elts.get_mut(hi) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:55107 self.nelts += 1u;
Graydon Hoarece729932011-06-15 18:19:50108 }
blake2-ppc70523712013-07-10 13:27:14109}
Tim Chevalier77de84b2013-05-27 18:47:38110
blake2-ppc70523712013-07-10 13:27:14111impl<T> RingBuf<T> {
112 /// Create an empty RingBuf
113 pub fn new() -> RingBuf<T> {
114 RingBuf::with_capacity(INITIAL_CAPACITY)
115 }
116
117 /// Create an empty RingBuf with space for at least `n` elements.
118 pub fn with_capacity(n: uint) -> RingBuf<T> {
119 RingBuf{nelts: 0, lo: 0,
Huon Wilson4b9a7a22014-04-05 05:45:42120 elts: Vec::from_fn(cmp::max(MINIMUM_CAPACITY, n), |_| None)}
blake2-ppc70523712013-07-10 13:27:14121 }
122
123 /// Retrieve an element in the RingBuf by index
124 ///
125 /// Fails if there is no element with the given index
126 pub fn get<'a>(&'a self, i: uint) -> &'a T {
127 let idx = self.raw_index(i);
Huon Wilson4b9a7a22014-04-05 05:45:42128 match *self.elts.get(idx) {
Alex Crichtondaf5f5a2013-10-21 20:08:31129 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14130 Some(ref v) => v
131 }
132 }
133
134 /// Retrieve an element in the RingBuf by index
135 ///
136 /// Fails if there is no element with the given index
137 pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
138 let idx = self.raw_index(i);
Huon Wilson4b9a7a22014-04-05 05:45:42139 match *self.elts.get_mut(idx) {
Alex Crichtondaf5f5a2013-10-21 20:08:31140 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14141 Some(ref mut v) => v
142 }
143 }
144
blake2-ppc57757a82013-09-26 07:19:26145 /// Swap elements at indices `i` and `j`
146 ///
147 /// `i` and `j` may be equal.
148 ///
149 /// Fails if there is no element with the given index
150 pub fn swap(&mut self, i: uint, j: uint) {
151 assert!(i < self.len());
152 assert!(j < self.len());
153 let ri = self.raw_index(i);
154 let rj = self.raw_index(j);
Huon Wilson4b9a7a22014-04-05 05:45:42155 self.elts.as_mut_slice().swap(ri, rj);
blake2-ppc57757a82013-09-26 07:19:26156 }
157
blake2-ppc70523712013-07-10 13:27:14158 /// Return index in underlying vec for a given logical element index
159 fn raw_index(&self, idx: uint) -> uint {
160 raw_index(self.lo, self.elts.len(), idx)
161 }
162
163 /// Reserve capacity for exactly `n` elements in the given RingBuf,
Tim Chevalier77de84b2013-05-27 18:47:38164 /// doing nothing if `self`'s capacity is already equal to or greater
165 /// than the requested capacity
166 ///
167 /// # Arguments
168 ///
169 /// * n - The number of elements to reserve space for
David Manescu65f35782014-01-31 13:03:20170 pub fn reserve_exact(&mut self, n: uint) {
171 self.elts.reserve_exact(n);
Tim Chevalier77de84b2013-05-27 18:47:38172 }
173
blake2-ppc70523712013-07-10 13:27:14174 /// Reserve capacity for at least `n` elements in the given RingBuf,
Tim Chevalier77de84b2013-05-27 18:47:38175 /// over-allocating in case the caller needs to reserve additional
176 /// space.
177 ///
178 /// Do nothing if `self`'s capacity is already equal to or greater
179 /// than the requested capacity.
180 ///
181 /// # Arguments
182 ///
183 /// * n - The number of elements to reserve space for
David Manescu65f35782014-01-31 13:03:20184 pub fn reserve(&mut self, n: uint) {
185 self.elts.reserve(n);
Tim Chevalier77de84b2013-05-27 18:47:38186 }
Jed Estep4f7a7422013-06-25 19:08:47187
188 /// Front-to-back iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24189 pub fn iter<'a>(&'a self) -> Items<'a, T> {
Huon Wilson4b9a7a22014-04-05 05:45:42190 Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
blake2-ppc3385e792013-07-15 23:13:26191 }
192
193 /// Back-to-front iterator.
Sean Chalmers292ed3e2014-01-23 19:41:57194 pub fn rev_iter<'a>(&'a self) -> Rev<Items<'a, T>> {
195 self.iter().rev()
Jed Estep4f7a7422013-06-25 19:08:47196 }
Corey Richardsonf8ae9b02013-06-26 22:14:35197
Jed Estep4f7a7422013-06-25 19:08:47198 /// Front-to-back iterator which returns mutable values.
Palmer Cox3fd8c8b2014-01-15 03:32:24199 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39200 let start_index = raw_index(self.lo, self.elts.len(), 0);
201 let end_index = raw_index(self.lo, self.elts.len(), self.nelts);
202
203 // Divide up the array
204 if end_index <= start_index {
205 // Items to iterate goes from:
206 // start_index to self.elts.len()
207 // and then
208 // 0 to end_index
Guillaume Pinot25bb1a42013-12-01 17:19:39209 let (temp, remaining1) = self.elts.mut_split_at(start_index);
210 let (remaining2, _) = temp.mut_split_at(end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24211 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39212 remaining2: remaining2,
213 nelts: self.nelts }
214 } else {
215 // Items to iterate goes from start_index to end_index:
Guillaume Pinot25bb1a42013-12-01 17:19:39216 let (empty, elts) = self.elts.mut_split_at(0);
Niko Matsakisbc4164d2013-11-16 22:29:39217 let remaining1 = elts.mut_slice(start_index, end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24218 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39219 remaining2: empty,
220 nelts: self.nelts }
221 }
Jed Estep4f7a7422013-06-25 19:08:47222 }
223
224 /// Back-to-front iterator which returns mutable values.
Sean Chalmers292ed3e2014-01-23 19:41:57225 pub fn mut_rev_iter<'a>(&'a mut self) -> Rev<MutItems<'a, T>> {
226 self.mut_iter().rev()
Jed Estep4f7a7422013-06-25 19:08:47227 }
228}
229
blake2-ppc70523712013-07-10 13:27:14230/// RingBuf iterator
Palmer Cox3fd8c8b2014-01-15 03:32:24231pub struct Items<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04232 lo: uint,
233 index: uint,
234 rindex: uint,
235 elts: &'a [Option<T>],
Jed Estep4f7a7422013-06-25 19:08:47236}
Niko Matsakisbc4164d2013-11-16 22:29:39237
Palmer Cox3fd8c8b2014-01-15 03:32:24238impl<'a, T> Iterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39239 #[inline]
Erik Price5731ca32013-12-10 07:16:18240 fn next(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39241 if self.index == self.rindex {
242 return None;
243 }
244 let raw_index = raw_index(self.lo, self.elts.len(), self.index);
245 self.index += 1;
246 Some(self.elts[raw_index].get_ref())
247 }
248
249 #[inline]
250 fn size_hint(&self) -> (uint, Option<uint>) {
251 let len = self.rindex - self.index;
252 (len, Some(len))
253 }
254}
255
Palmer Cox3fd8c8b2014-01-15 03:32:24256impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39257 #[inline]
Erik Price5731ca32013-12-10 07:16:18258 fn next_back(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39259 if self.index == self.rindex {
260 return None;
261 }
262 self.rindex -= 1;
263 let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
264 Some(self.elts[raw_index].get_ref())
265 }
266}
Jed Estep35314c92013-06-26 15:38:29267
Palmer Cox3fd8c8b2014-01-15 03:32:24268impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24269
Palmer Cox3fd8c8b2014-01-15 03:32:24270impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> {
blake2-ppcf6862132013-07-29 18:16:26271 #[inline]
272 fn indexable(&self) -> uint { self.rindex - self.index }
273
274 #[inline]
Erik Price5731ca32013-12-10 07:16:18275 fn idx(&self, j: uint) -> Option<&'a T> {
blake2-ppcf6862132013-07-29 18:16:26276 if j >= self.indexable() {
277 None
278 } else {
279 let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
280 Some(self.elts[raw_index].get_ref())
281 }
282 }
283}
284
blake2-ppc70523712013-07-10 13:27:14285/// RingBuf mutable iterator
Palmer Cox3fd8c8b2014-01-15 03:32:24286pub struct MutItems<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04287 remaining1: &'a mut [Option<T>],
288 remaining2: &'a mut [Option<T>],
289 nelts: uint,
Jed Estep4f7a7422013-06-25 19:08:47290}
Niko Matsakisbc4164d2013-11-16 22:29:39291
Palmer Cox3fd8c8b2014-01-15 03:32:24292impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39293 #[inline]
Erik Price5731ca32013-12-10 07:16:18294 fn next(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39295 if self.nelts == 0 {
296 return None;
297 }
298 let r = if self.remaining1.len() > 0 {
299 &mut self.remaining1
300 } else {
301 assert!(self.remaining2.len() > 0);
302 &mut self.remaining2
303 };
304 self.nelts -= 1;
Nathaniel Hermand451c152014-01-25 17:00:46305 Some(r.mut_shift_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39306 }
307
308 #[inline]
309 fn size_hint(&self) -> (uint, Option<uint>) {
310 (self.nelts, Some(self.nelts))
311 }
312}
313
Palmer Cox3fd8c8b2014-01-15 03:32:24314impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39315 #[inline]
Erik Price5731ca32013-12-10 07:16:18316 fn next_back(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39317 if self.nelts == 0 {
318 return None;
319 }
320 let r = if self.remaining2.len() > 0 {
321 &mut self.remaining2
322 } else {
323 assert!(self.remaining1.len() > 0);
324 &mut self.remaining1
325 };
326 self.nelts -= 1;
Nathaniel Herman33960342014-01-25 20:33:31327 Some(r.mut_pop_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39328 }
329}
Daniel Micayb47e1e92013-02-16 22:55:55330
Palmer Cox3fd8c8b2014-01-15 03:32:24331impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24332
Daniel Micayb47e1e92013-02-16 22:55:55333/// Grow is only called on full elts, so nelts is also len(elts), unlike
334/// elsewhere.
Huon Wilson4b9a7a22014-04-05 05:45:42335fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
Corey Richardsoncc57ca02013-05-19 02:02:45336 assert_eq!(nelts, elts.len());
blake2-ppc8a326762013-07-06 03:42:45337 let lo = *loptr;
338 let newlen = nelts * 2;
blake2-ppc40ce0b72013-07-06 03:42:45339 elts.reserve(newlen);
Daniel Micayb47e1e92013-02-16 22:55:55340
blake2-ppc40ce0b72013-07-06 03:42:45341 /* fill with None */
Daniel Micay100894552013-08-03 16:45:23342 for _ in range(elts.len(), elts.capacity()) {
blake2-ppc40ce0b72013-07-06 03:42:45343 elts.push(None);
Daniel Micayb47e1e92013-02-16 22:55:55344 }
blake2-ppc8a326762013-07-06 03:42:45345
346 /*
347 Move the shortest half into the newly reserved area.
348 lo ---->|
349 nelts ----------->|
350 [o o o|o o o o o]
351 A [. . .|o o o o o o o o|. . . . .]
352 B [o o o|. . . . . . . .|o o o o o]
353 */
354
355 assert!(newlen - nelts/2 >= nelts);
356 if lo <= (nelts - lo) { // A
Daniel Micay100894552013-08-03 16:45:23357 for i in range(0u, lo) {
Huon Wilson4b9a7a22014-04-05 05:45:42358 elts.as_mut_slice().swap(i, nelts + i);
blake2-ppc8a326762013-07-06 03:42:45359 }
360 } else { // B
Daniel Micay100894552013-08-03 16:45:23361 for i in range(lo, nelts) {
Huon Wilson4b9a7a22014-04-05 05:45:42362 elts.as_mut_slice().swap(i, newlen - nelts + i);
blake2-ppc8a326762013-07-06 03:42:45363 }
364 *loptr += newlen - nelts;
blake2-ppc40ce0b72013-07-06 03:42:45365 }
Daniel Micayb47e1e92013-02-16 22:55:55366}
367
blake2-ppc75015c32013-07-06 03:42:45368/// Return index in underlying vec for a given logical element index
369fn raw_index(lo: uint, len: uint, index: uint) -> uint {
370 if lo >= len - index {
371 lo + index - len
372 } else {
373 lo + index
374 }
375}
376
blake2-ppc70523712013-07-10 13:27:14377impl<A: Eq> Eq for RingBuf<A> {
378 fn eq(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32379 self.nelts == other.nelts &&
380 self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
381 }
blake2-ppc70523712013-07-10 13:27:14382 fn ne(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32383 !self.eq(other)
384 }
385}
386
Huon Wilson53487a02013-08-13 13:08:14387impl<A> FromIterator<A> for RingBuf<A> {
Brian Andersonee052192014-03-31 04:45:55388 fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
blake2-ppcf8ae5262013-07-30 00:06:49389 let (lower, _) = iterator.size_hint();
390 let mut deq = RingBuf::with_capacity(lower);
391 deq.extend(iterator);
blake2-ppc08dc72f2013-07-06 03:42:45392 deq
393 }
394}
395
Huon Wilson53487a02013-08-13 13:08:14396impl<A> Extendable<A> for RingBuf<A> {
Marvin Löbel6200e762014-03-20 13:12:56397 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
398 for elt in iterator {
blake2-ppcf8ae5262013-07-30 00:06:49399 self.push_back(elt);
400 }
401 }
402}
403
Brian Anderson6e27b272012-01-18 03:05:07404#[cfg(test)]
405mod tests {
Liigo Zhuang53b9d1a2014-02-14 01:49:11406 extern crate test;
407 use self::test::BenchHarness;
HeroesGraved81bb442014-02-03 05:56:49408 use deque::Deque;
Patrick Walton99b33f72013-07-02 19:47:32409 use std::clone::Clone;
Corey Richardson1662bd32013-06-28 22:32:26410 use std::cmp::Eq;
Alex Crichton02882fb2014-02-28 09:23:06411 use std::fmt::Show;
Alex Crichtonf47e4b22014-01-07 06:33:50412 use super::RingBuf;
Patrick Waltonfa5ee932012-12-28 02:24:18413
Brian Anderson6e27b272012-01-18 03:05:07414 #[test]
415 fn test_simple() {
blake2-ppc70523712013-07-10 13:27:14416 let mut d = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45417 assert_eq!(d.len(), 0u);
blake2-ppc70523712013-07-10 13:27:14418 d.push_front(17);
419 d.push_front(42);
420 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45421 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14422 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45423 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31424 debug!("{:?}", d.front());
blake2-ppc70523712013-07-10 13:27:14425 assert_eq!(*d.front().unwrap(), 42);
Alex Crichtondaf5f5a2013-10-21 20:08:31426 debug!("{:?}", d.back());
blake2-ppc70523712013-07-10 13:27:14427 assert_eq!(*d.back().unwrap(), 137);
428 let mut i = d.pop_front();
Alex Crichtondaf5f5a2013-10-21 20:08:31429 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14430 assert_eq!(i, Some(42));
Brian Anderson6e27b272012-01-18 03:05:07431 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31432 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14433 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07434 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31435 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14436 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07437 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31438 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14439 assert_eq!(i, Some(17));
Corey Richardsoncc57ca02013-05-19 02:02:45440 assert_eq!(d.len(), 0u);
blake2-ppc70523712013-07-10 13:27:14441 d.push_back(3);
Corey Richardsoncc57ca02013-05-19 02:02:45442 assert_eq!(d.len(), 1u);
blake2-ppc70523712013-07-10 13:27:14443 d.push_front(2);
Corey Richardsoncc57ca02013-05-19 02:02:45444 assert_eq!(d.len(), 2u);
blake2-ppc70523712013-07-10 13:27:14445 d.push_back(4);
Corey Richardsoncc57ca02013-05-19 02:02:45446 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14447 d.push_front(1);
Corey Richardsoncc57ca02013-05-19 02:02:45448 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31449 debug!("{:?}", d.get(0));
450 debug!("{:?}", d.get(1));
451 debug!("{:?}", d.get(2));
452 debug!("{:?}", d.get(3));
Corey Richardsoncc57ca02013-05-19 02:02:45453 assert_eq!(*d.get(0), 1);
454 assert_eq!(*d.get(1), 2);
455 assert_eq!(*d.get(2), 3);
456 assert_eq!(*d.get(3), 4);
Brian Anderson6e27b272012-01-18 03:05:07457 }
458
Kevin Cantuc43426e2012-09-13 05:09:55459 #[test]
460 fn test_boxes() {
461 let a: @int = @5;
462 let b: @int = @72;
463 let c: @int = @64;
464 let d: @int = @175;
465
blake2-ppc70523712013-07-10 13:27:14466 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45467 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14468 deq.push_front(a);
469 deq.push_front(b);
470 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45471 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14472 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45473 assert_eq!(deq.len(), 4);
blake2-ppc70523712013-07-10 13:27:14474 assert_eq!(deq.front(), Some(&b));
475 assert_eq!(deq.back(), Some(&d));
476 assert_eq!(deq.pop_front(), Some(b));
477 assert_eq!(deq.pop_back(), Some(d));
478 assert_eq!(deq.pop_back(), Some(c));
479 assert_eq!(deq.pop_back(), Some(a));
Corey Richardsoncc57ca02013-05-19 02:02:45480 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14481 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45482 assert_eq!(deq.len(), 1);
blake2-ppc70523712013-07-10 13:27:14483 deq.push_front(b);
Corey Richardsoncc57ca02013-05-19 02:02:45484 assert_eq!(deq.len(), 2);
blake2-ppc70523712013-07-10 13:27:14485 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45486 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14487 deq.push_front(a);
Corey Richardsoncc57ca02013-05-19 02:02:45488 assert_eq!(deq.len(), 4);
489 assert_eq!(*deq.get(0), a);
490 assert_eq!(*deq.get(1), b);
491 assert_eq!(*deq.get(2), c);
492 assert_eq!(*deq.get(3), d);
Daniel Micay373c0722013-02-17 00:10:10493 }
494
Felix S. Klock IIa636f512013-05-01 23:32:37495 #[cfg(test)]
Alex Crichton02882fb2014-02-28 09:23:06496 fn test_parameterized<T:Clone + Eq + Show>(a: T, b: T, c: T, d: T) {
Patrick Waltondc4bf172013-07-13 04:05:59497 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45498 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59499 deq.push_front(a.clone());
500 deq.push_front(b.clone());
501 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45502 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59503 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45504 assert_eq!(deq.len(), 4);
Marvin Löbel0ac7a212013-08-03 23:59:24505 assert_eq!((*deq.front().unwrap()).clone(), b.clone());
506 assert_eq!((*deq.back().unwrap()).clone(), d.clone());
507 assert_eq!(deq.pop_front().unwrap(), b.clone());
508 assert_eq!(deq.pop_back().unwrap(), d.clone());
509 assert_eq!(deq.pop_back().unwrap(), c.clone());
510 assert_eq!(deq.pop_back().unwrap(), a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45511 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59512 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45513 assert_eq!(deq.len(), 1);
Patrick Waltondc4bf172013-07-13 04:05:59514 deq.push_front(b.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45515 assert_eq!(deq.len(), 2);
Patrick Waltondc4bf172013-07-13 04:05:59516 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45517 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59518 deq.push_front(a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45519 assert_eq!(deq.len(), 4);
Patrick Walton99b33f72013-07-02 19:47:32520 assert_eq!((*deq.get(0)).clone(), a.clone());
521 assert_eq!((*deq.get(1)).clone(), b.clone());
522 assert_eq!((*deq.get(2)).clone(), c.clone());
523 assert_eq!((*deq.get(3)).clone(), d.clone());
Brian Anderson6e27b272012-01-18 03:05:07524 }
525
blake2-ppc81933ed2013-07-06 03:42:45526 #[test]
blake2-ppc70523712013-07-10 13:27:14527 fn test_push_front_grow() {
528 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23529 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14530 deq.push_front(i);
blake2-ppc81933ed2013-07-06 03:42:45531 }
532 assert_eq!(deq.len(), 66);
533
Daniel Micay100894552013-08-03 16:45:23534 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45535 assert_eq!(*deq.get(i), 65 - i);
536 }
537
blake2-ppc70523712013-07-10 13:27:14538 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23539 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14540 deq.push_back(i);
blake2-ppc81933ed2013-07-06 03:42:45541 }
542
Daniel Micay100894552013-08-03 16:45:23543 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45544 assert_eq!(*deq.get(i), i);
545 }
546 }
547
548 #[bench]
549 fn bench_new(b: &mut test::BenchHarness) {
Patrick Walton38efa172013-11-22 03:20:48550 b.iter(|| {
Patrick Walton86939432013-08-08 18:38:10551 let _: RingBuf<u64> = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48552 })
blake2-ppc81933ed2013-07-06 03:42:45553 }
554
555 #[bench]
blake2-ppc70523712013-07-10 13:27:14556 fn bench_push_back(b: &mut test::BenchHarness) {
557 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48558 b.iter(|| {
blake2-ppc70523712013-07-10 13:27:14559 deq.push_back(0);
Patrick Walton38efa172013-11-22 03:20:48560 })
blake2-ppc81933ed2013-07-06 03:42:45561 }
562
563 #[bench]
blake2-ppc70523712013-07-10 13:27:14564 fn bench_push_front(b: &mut test::BenchHarness) {
565 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48566 b.iter(|| {
blake2-ppc70523712013-07-10 13:27:14567 deq.push_front(0);
Patrick Walton38efa172013-11-22 03:20:48568 })
blake2-ppc81933ed2013-07-06 03:42:45569 }
570
571 #[bench]
572 fn bench_grow(b: &mut test::BenchHarness) {
blake2-ppc70523712013-07-10 13:27:14573 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48574 b.iter(|| {
Brendan Zabarauskas729060d2014-01-30 00:20:34575 for _ in range(0, 65) {
blake2-ppc70523712013-07-10 13:27:14576 deq.push_front(1);
Brendan Zabarauskas729060d2014-01-30 00:20:34577 }
Patrick Walton38efa172013-11-22 03:20:48578 })
blake2-ppc81933ed2013-07-06 03:42:45579 }
580
Alex Crichton02882fb2014-02-28 09:23:06581 #[deriving(Clone, Eq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32582 enum Taggy {
583 One(int),
584 Two(int, int),
585 Three(int, int, int),
Brian Anderson6e27b272012-01-18 03:05:07586 }
587
Alex Crichton02882fb2014-02-28 09:23:06588 #[deriving(Clone, Eq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32589 enum Taggypar<T> {
590 Onepar(int),
591 Twopar(int, int),
592 Threepar(int, int, int),
593 }
594
Alex Crichton02882fb2014-02-28 09:23:06595 #[deriving(Clone, Eq, Show)]
Erick Tryzelaare84576b2013-01-22 16:44:24596 struct RecCy {
597 x: int,
598 y: int,
Patrick Waltoneb4d39e2013-01-26 00:57:39599 t: Taggy
Patrick Walton9117dcb2012-09-20 01:00:26600 }
Kevin Cantuc43426e2012-09-13 05:09:55601
602 #[test]
603 fn test_param_int() {
604 test_parameterized::<int>(5, 72, 64, 175);
605 }
606
607 #[test]
608 fn test_param_at_int() {
609 test_parameterized::<@int>(@5, @72, @64, @175);
610 }
611
612 #[test]
613 fn test_param_taggy() {
Corey Richardsonf8ae9b02013-06-26 22:14:35614 test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55615 }
616
617 #[test]
618 fn test_param_taggypar() {
619 test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
Ben Striegela605fd02012-08-11 14:08:42620 Twopar::<int>(1, 2),
621 Threepar::<int>(1, 2, 3),
622 Twopar::<int>(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55623 }
Brian Anderson6e27b272012-01-18 03:05:07624
Kevin Cantuc43426e2012-09-13 05:09:55625 #[test]
626 fn test_param_reccy() {
Erick Tryzelaare84576b2013-01-22 16:44:24627 let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
628 let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
629 let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
630 let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
Kevin Cantuc43426e2012-09-13 05:09:55631 test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
Brian Anderson6e27b272012-01-18 03:05:07632 }
Erick Tryzelaar909d8f02013-03-30 01:02:44633
634 #[test]
blake2-ppc0ff5c172013-07-06 03:42:45635 fn test_with_capacity() {
blake2-ppc70523712013-07-10 13:27:14636 let mut d = RingBuf::with_capacity(0);
637 d.push_back(1);
blake2-ppc0ff5c172013-07-06 03:42:45638 assert_eq!(d.len(), 1);
blake2-ppc70523712013-07-10 13:27:14639 let mut d = RingBuf::with_capacity(50);
640 d.push_back(1);
blake2-ppc0ff5c172013-07-06 03:42:45641 assert_eq!(d.len(), 1);
642 }
643
644 #[test]
David Manescu65f35782014-01-31 13:03:20645 fn test_reserve_exact() {
blake2-ppc70523712013-07-10 13:27:14646 let mut d = RingBuf::new();
647 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20648 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47649 assert_eq!(d.elts.capacity(), 50);
blake2-ppc70523712013-07-10 13:27:14650 let mut d = RingBuf::new();
651 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20652 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47653 assert_eq!(d.elts.capacity(), 50);
Tim Chevalier77de84b2013-05-27 18:47:38654 }
655
656 #[test]
David Manescu65f35782014-01-31 13:03:20657 fn test_reserve() {
blake2-ppc70523712013-07-10 13:27:14658 let mut d = RingBuf::new();
659 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20660 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47661 assert_eq!(d.elts.capacity(), 64);
blake2-ppc70523712013-07-10 13:27:14662 let mut d = RingBuf::new();
663 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20664 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47665 assert_eq!(d.elts.capacity(), 64);
Tim Chevalier77de84b2013-05-27 18:47:38666 }
667
Jed Estep096fb792013-06-26 14:04:44668 #[test]
blake2-ppc57757a82013-09-26 07:19:26669 fn test_swap() {
670 let mut d: RingBuf<int> = range(0, 5).collect();
671 d.pop_front();
672 d.swap(0, 3);
Huon Wilson4b9a7a22014-04-05 05:45:42673 assert_eq!(d.iter().map(|&x|x).collect::<Vec<int>>(), vec!(4, 2, 3, 1));
blake2-ppc57757a82013-09-26 07:19:26674 }
675
676 #[test]
Jed Estep096fb792013-06-26 14:04:44677 fn test_iter() {
blake2-ppc70523712013-07-10 13:27:14678 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45679 assert_eq!(d.iter().next(), None);
blake2-ppc9ccf4432013-07-14 20:30:22680 assert_eq!(d.iter().size_hint(), (0, Some(0)));
blake2-ppcf88d5322013-07-06 03:42:45681
Daniel Micay100894552013-08-03 16:45:23682 for i in range(0, 5) {
blake2-ppc70523712013-07-10 13:27:14683 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44684 }
Huon Wilson4b9a7a22014-04-05 05:45:42685 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), &[&0,&1,&2,&3,&4]);
Corey Richardsonf8ae9b02013-06-26 22:14:35686
Daniel Micay100894552013-08-03 16:45:23687 for i in range(6, 9) {
blake2-ppc70523712013-07-10 13:27:14688 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44689 }
Huon Wilson4b9a7a22014-04-05 05:45:42690 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), &[&8,&7,&6,&0,&1,&2,&3,&4]);
blake2-ppc9ccf4432013-07-14 20:30:22691
692 let mut it = d.iter();
693 let mut len = d.len();
694 loop {
695 match it.next() {
696 None => break,
697 _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
698 }
699 }
Jed Estep096fb792013-06-26 14:04:44700 }
701
702 #[test]
703 fn test_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14704 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45705 assert_eq!(d.rev_iter().next(), None);
706
Daniel Micay100894552013-08-03 16:45:23707 for i in range(0, 5) {
blake2-ppc70523712013-07-10 13:27:14708 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44709 }
Huon Wilson4b9a7a22014-04-05 05:45:42710 assert_eq!(d.rev_iter().collect::<Vec<&int>>().as_slice(), &[&4,&3,&2,&1,&0]);
Corey Richardsonf8ae9b02013-06-26 22:14:35711
Daniel Micay100894552013-08-03 16:45:23712 for i in range(6, 9) {
blake2-ppc70523712013-07-10 13:27:14713 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44714 }
Huon Wilson4b9a7a22014-04-05 05:45:42715 assert_eq!(d.rev_iter().collect::<Vec<&int>>().as_slice(), &[&4,&3,&2,&1,&0,&6,&7,&8]);
Jed Estep096fb792013-06-26 14:04:44716 }
blake2-ppc08dc72f2013-07-06 03:42:45717
718 #[test]
Niko Matsakisbc4164d2013-11-16 22:29:39719 fn test_mut_rev_iter_wrap() {
720 let mut d = RingBuf::with_capacity(3);
721 assert!(d.mut_rev_iter().next().is_none());
722
723 d.push_back(1);
724 d.push_back(2);
725 d.push_back(3);
726 assert_eq!(d.pop_front(), Some(1));
727 d.push_back(4);
728
Huon Wilson4b9a7a22014-04-05 05:45:42729 assert_eq!(d.mut_rev_iter().map(|x| *x).collect::<Vec<int>>(),
730 vec!(4, 3, 2));
Niko Matsakisbc4164d2013-11-16 22:29:39731 }
732
733 #[test]
blake2-ppcf88d5322013-07-06 03:42:45734 fn test_mut_iter() {
blake2-ppc70523712013-07-10 13:27:14735 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45736 assert!(d.mut_iter().next().is_none());
737
Daniel Micay100894552013-08-03 16:45:23738 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14739 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45740 }
741
Daniel Micay100894552013-08-03 16:45:23742 for (i, elt) in d.mut_iter().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45743 assert_eq!(*elt, 2 - i);
744 *elt = i;
745 }
746
747 {
748 let mut it = d.mut_iter();
749 assert_eq!(*it.next().unwrap(), 0);
750 assert_eq!(*it.next().unwrap(), 1);
751 assert_eq!(*it.next().unwrap(), 2);
752 assert!(it.next().is_none());
753 }
754 }
755
756 #[test]
757 fn test_mut_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14758 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45759 assert!(d.mut_rev_iter().next().is_none());
760
Daniel Micay100894552013-08-03 16:45:23761 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14762 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45763 }
764
Daniel Micay100894552013-08-03 16:45:23765 for (i, elt) in d.mut_rev_iter().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45766 assert_eq!(*elt, i);
767 *elt = i;
768 }
769
770 {
771 let mut it = d.mut_rev_iter();
772 assert_eq!(*it.next().unwrap(), 0);
773 assert_eq!(*it.next().unwrap(), 1);
774 assert_eq!(*it.next().unwrap(), 2);
775 assert!(it.next().is_none());
776 }
777 }
778
779 #[test]
Brian Andersonee052192014-03-31 04:45:55780 fn test_from_iter() {
Daniel Micay6919cf52013-09-08 15:01:16781 use std::iter;
Huon Wilson4b9a7a22014-04-05 05:45:42782 let v = vec!(1,2,3,4,5,6,7);
Erick Tryzelaar68f40d22013-08-10 03:09:47783 let deq: RingBuf<int> = v.iter().map(|&x| x).collect();
Huon Wilson4b9a7a22014-04-05 05:45:42784 let u: Vec<int> = deq.iter().map(|&x| x).collect();
blake2-ppc08dc72f2013-07-06 03:42:45785 assert_eq!(u, v);
786
Daniel Micay6919cf52013-09-08 15:01:16787 let mut seq = iter::count(0u, 2).take(256);
blake2-ppc70523712013-07-10 13:27:14788 let deq: RingBuf<uint> = seq.collect();
Daniel Micay100894552013-08-03 16:45:23789 for (i, &x) in deq.iter().enumerate() {
blake2-ppc08dc72f2013-07-06 03:42:45790 assert_eq!(2*i, x);
791 }
792 assert_eq!(deq.len(), 256);
793 }
blake2-ppc10c76982013-07-06 13:27:32794
795 #[test]
796 fn test_clone() {
blake2-ppc70523712013-07-10 13:27:14797 let mut d = RingBuf::new();
798 d.push_front(17);
799 d.push_front(42);
800 d.push_back(137);
801 d.push_back(137);
blake2-ppc10c76982013-07-06 13:27:32802 assert_eq!(d.len(), 4u);
803 let mut e = d.clone();
804 assert_eq!(e.len(), 4u);
805 while !d.is_empty() {
806 assert_eq!(d.pop_back(), e.pop_back());
807 }
808 assert_eq!(d.len(), 0u);
809 assert_eq!(e.len(), 0u);
810 }
811
812 #[test]
813 fn test_eq() {
blake2-ppc70523712013-07-10 13:27:14814 let mut d = RingBuf::new();
Alex Crichton02882fb2014-02-28 09:23:06815 assert!(d == RingBuf::with_capacity(0));
blake2-ppc70523712013-07-10 13:27:14816 d.push_front(137);
817 d.push_front(17);
818 d.push_front(42);
819 d.push_back(137);
820 let mut e = RingBuf::with_capacity(0);
821 e.push_back(42);
822 e.push_back(17);
823 e.push_back(137);
824 e.push_back(137);
Alex Crichton02882fb2014-02-28 09:23:06825 assert!(&e == &d);
blake2-ppc10c76982013-07-06 13:27:32826 e.pop_back();
blake2-ppc70523712013-07-10 13:27:14827 e.push_back(0);
blake2-ppc10c76982013-07-06 13:27:32828 assert!(e != d);
829 e.clear();
Alex Crichton02882fb2014-02-28 09:23:06830 assert!(e == RingBuf::new());
blake2-ppc10c76982013-07-06 13:27:32831 }
Michael Sullivanc854d6e2012-07-03 17:52:32832}