blob: 05dda93039861aa0910c72380676b9dd27d39951 [file] [log] [blame]
Jonathan S03609e52014-04-21 04:59:121// Copyright 2012-2014 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
Alex Crichtonda070392014-06-06 23:33:4414//! collections::Deque`.
Patrick Waltonf3723cf2013-05-17 22:28:4415
Alex Crichton6a585372014-05-30 01:50:1216use core::prelude::*;
17
18use core::cmp;
Tom Jakubowskid6a39412014-06-09 07:30:0419use core::default::Default;
Alex Crichton6a585372014-05-30 01:50:1220use core::fmt;
21use core::iter::RandomAccessIterator;
Alex Crichton998fece2013-05-06 04:42:5422
Brian Andersond36a8f32014-07-11 17:12:3823use {Deque, Collection, Mutable, MutableSeq};
Alex Crichton6a585372014-05-30 01:50:1224use vec::Vec;
blake2-ppc70523712013-07-10 13:27:1425
blake2-ppc0ff5c172013-07-06 03:42:4526static INITIAL_CAPACITY: uint = 8u; // 2^3
27static MINIMUM_CAPACITY: uint = 2u;
Daniel Micayb47e1e92013-02-16 22:55:5528
blake2-ppc0f9b9a52013-07-11 14:17:5129/// RingBuf is a circular buffer that implements Deque.
blake2-ppc10c76982013-07-06 13:27:3230#[deriving(Clone)]
blake2-ppc70523712013-07-10 13:27:1431pub struct RingBuf<T> {
Alex Crichton8ad7e542014-03-27 22:10:0432 nelts: uint,
33 lo: uint,
Huon Wilson4b9a7a22014-04-05 05:45:4234 elts: Vec<Option<T>>
Marijn Haverbeke26610db2012-01-11 11:49:3335}
Roy Frostig9c818892010-07-21 01:03:0936
Brian Anderson50942c72014-05-19 18:32:0937impl<T> Collection for RingBuf<T> {
blake2-ppc70523712013-07-10 13:27:1438 /// Return the number of elements in the RingBuf
Alex Crichtonb29c3682013-06-24 03:44:1139 fn len(&self) -> uint { self.nelts }
Daniel Micayb47e1e92013-02-16 22:55:5540}
Roy Frostig9c818892010-07-21 01:03:0941
blake2-ppc70523712013-07-10 13:27:1442impl<T> Mutable for RingBuf<T> {
43 /// Clear the RingBuf, removing all values.
Daniel Micayed7c9c42013-02-16 23:55:2544 fn clear(&mut self) {
Daniel Micay100894552013-08-03 16:45:2345 for x in self.elts.mut_iter() { *x = None }
Daniel Micayed7c9c42013-02-16 23:55:2546 self.nelts = 0;
47 self.lo = 0;
Daniel Micayed7c9c42013-02-16 23:55:2548 }
49}
50
blake2-ppc70523712013-07-10 13:27:1451impl<T> Deque<T> for RingBuf<T> {
52 /// Return a reference to the first element in the RingBuf
53 fn front<'a>(&'a self) -> Option<&'a T> {
54 if self.nelts > 0 { Some(self.get(0)) } else { None }
blake2-ppc0ff5c172013-07-06 03:42:4555 }
56
blake2-ppc70523712013-07-10 13:27:1457 /// Return a mutable reference to the first element in the RingBuf
58 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
59 if self.nelts > 0 { Some(self.get_mut(0)) } else { None }
Niko Matsakis03396472013-04-10 20:14:0660 }
61
blake2-ppc70523712013-07-10 13:27:1462 /// Return a reference to the last element in the RingBuf
63 fn back<'a>(&'a self) -> Option<&'a T> {
64 if self.nelts > 0 { Some(self.get(self.nelts - 1)) } else { None }
65 }
Niko Matsakis03396472013-04-10 20:14:0666
blake2-ppc70523712013-07-10 13:27:1467 /// Return a mutable reference to the last element in the RingBuf
68 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
Cameron Zwarich159e27a2014-06-14 03:48:0969 let nelts = self.nelts;
70 if nelts > 0 { Some(self.get_mut(nelts - 1)) } else { None }
blake2-ppc70523712013-07-10 13:27:1471 }
72
73 /// Remove and return the first element in the RingBuf, or None if it is empty
74 fn pop_front(&mut self) -> Option<T> {
Huon Wilson4b9a7a22014-04-05 05:45:4275 let result = self.elts.get_mut(self.lo).take();
blake2-ppc70523712013-07-10 13:27:1476 if result.is_some() {
77 self.lo = (self.lo + 1u) % self.elts.len();
78 self.nelts -= 1u;
blake2-ppc5d72f3f2013-07-06 03:42:4579 }
Daniel Micay61906612013-02-17 00:43:2980 result
81 }
82
blake2-ppc70523712013-07-10 13:27:1483 /// Remove and return the last element in the RingBuf, or None if it is empty
84 fn pop_back(&mut self) -> Option<T> {
85 if self.nelts > 0 {
86 self.nelts -= 1;
87 let hi = self.raw_index(self.nelts);
Huon Wilson4b9a7a22014-04-05 05:45:4288 self.elts.get_mut(hi).take()
blake2-ppc70523712013-07-10 13:27:1489 } else {
90 None
91 }
blake2-ppc5d72f3f2013-07-06 03:42:4592 }
93
blake2-ppc70523712013-07-10 13:27:1494 /// Prepend an element to the RingBuf
95 fn push_front(&mut self, t: T) {
blake2-ppc40ce0b72013-07-06 03:42:4596 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:4597 grow(self.nelts, &mut self.lo, &mut self.elts);
blake2-ppc40ce0b72013-07-06 03:42:4598 }
Daniel Micayb47e1e92013-02-16 22:55:5599 if self.lo == 0u {
100 self.lo = self.elts.len() - 1u;
101 } else { self.lo -= 1u; }
Huon Wilson4b9a7a22014-04-05 05:45:42102 *self.elts.get_mut(self.lo) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:55103 self.nelts += 1u;
Erick Tryzelaare84576b2013-01-22 16:44:24104 }
Marijn Haverbeke26610db2012-01-11 11:49:33105
blake2-ppc70523712013-07-10 13:27:14106 /// Append an element to the RingBuf
107 fn push_back(&mut self, t: T) {
blake2-ppc5d72f3f2013-07-06 03:42:45108 if self.nelts == self.elts.len() {
blake2-ppc8a326762013-07-06 03:42:45109 grow(self.nelts, &mut self.lo, &mut self.elts);
Graydon Hoare2880ecd2010-09-22 22:44:13110 }
blake2-ppc5d72f3f2013-07-06 03:42:45111 let hi = self.raw_index(self.nelts);
Huon Wilson4b9a7a22014-04-05 05:45:42112 *self.elts.get_mut(hi) = Some(t);
Daniel Micayb47e1e92013-02-16 22:55:55113 self.nelts += 1u;
Graydon Hoarece729932011-06-15 18:19:50114 }
blake2-ppc70523712013-07-10 13:27:14115}
Tim Chevalier77de84b2013-05-27 18:47:38116
Brian Andersond36a8f32014-07-11 17:12:38117impl<T> MutableSeq<T> for RingBuf<T> {
118 fn push(&mut self, t: T) { self.push_back(t) }
119 fn pop(&mut self) -> Option<T> { self.pop_back() }
120}
121
Tom Jakubowskid6a39412014-06-09 07:30:04122impl<T> Default for RingBuf<T> {
123 #[inline]
124 fn default() -> RingBuf<T> { RingBuf::new() }
125}
126
blake2-ppc70523712013-07-10 13:27:14127impl<T> RingBuf<T> {
128 /// Create an empty RingBuf
129 pub fn new() -> RingBuf<T> {
130 RingBuf::with_capacity(INITIAL_CAPACITY)
131 }
132
133 /// Create an empty RingBuf with space for at least `n` elements.
134 pub fn with_capacity(n: uint) -> RingBuf<T> {
135 RingBuf{nelts: 0, lo: 0,
Huon Wilson4b9a7a22014-04-05 05:45:42136 elts: Vec::from_fn(cmp::max(MINIMUM_CAPACITY, n), |_| None)}
blake2-ppc70523712013-07-10 13:27:14137 }
138
139 /// Retrieve an element in the RingBuf by index
140 ///
141 /// Fails if there is no element with the given index
142 pub fn get<'a>(&'a self, i: uint) -> &'a T {
143 let idx = self.raw_index(i);
Huon Wilson4b9a7a22014-04-05 05:45:42144 match *self.elts.get(idx) {
Alex Crichtondaf5f5a2013-10-21 20:08:31145 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14146 Some(ref v) => v
147 }
148 }
149
150 /// Retrieve an element in the RingBuf by index
151 ///
152 /// Fails if there is no element with the given index
153 pub fn get_mut<'a>(&'a mut self, i: uint) -> &'a mut T {
154 let idx = self.raw_index(i);
Huon Wilson4b9a7a22014-04-05 05:45:42155 match *self.elts.get_mut(idx) {
Alex Crichtondaf5f5a2013-10-21 20:08:31156 None => fail!(),
blake2-ppc70523712013-07-10 13:27:14157 Some(ref mut v) => v
158 }
159 }
160
blake2-ppc57757a82013-09-26 07:19:26161 /// Swap elements at indices `i` and `j`
162 ///
163 /// `i` and `j` may be equal.
164 ///
165 /// Fails if there is no element with the given index
166 pub fn swap(&mut self, i: uint, j: uint) {
167 assert!(i < self.len());
168 assert!(j < self.len());
169 let ri = self.raw_index(i);
170 let rj = self.raw_index(j);
Huon Wilson4b9a7a22014-04-05 05:45:42171 self.elts.as_mut_slice().swap(ri, rj);
blake2-ppc57757a82013-09-26 07:19:26172 }
173
blake2-ppc70523712013-07-10 13:27:14174 /// Return index in underlying vec for a given logical element index
175 fn raw_index(&self, idx: uint) -> uint {
176 raw_index(self.lo, self.elts.len(), idx)
177 }
178
179 /// Reserve capacity for exactly `n` elements in the given RingBuf,
Tim Chevalier77de84b2013-05-27 18:47:38180 /// doing nothing if `self`'s capacity is already equal to or greater
181 /// than the requested capacity
182 ///
183 /// # Arguments
184 ///
185 /// * n - The number of elements to reserve space for
David Manescu65f35782014-01-31 13:03:20186 pub fn reserve_exact(&mut self, n: uint) {
187 self.elts.reserve_exact(n);
Tim Chevalier77de84b2013-05-27 18:47:38188 }
189
blake2-ppc70523712013-07-10 13:27:14190 /// Reserve capacity for at least `n` elements in the given RingBuf,
Tim Chevalier77de84b2013-05-27 18:47:38191 /// over-allocating in case the caller needs to reserve additional
192 /// space.
193 ///
194 /// Do nothing if `self`'s capacity is already equal to or greater
195 /// than the requested capacity.
196 ///
197 /// # Arguments
198 ///
199 /// * n - The number of elements to reserve space for
David Manescu65f35782014-01-31 13:03:20200 pub fn reserve(&mut self, n: uint) {
201 self.elts.reserve(n);
Tim Chevalier77de84b2013-05-27 18:47:38202 }
Jed Estep4f7a7422013-06-25 19:08:47203
204 /// Front-to-back iterator.
Palmer Cox3fd8c8b2014-01-15 03:32:24205 pub fn iter<'a>(&'a self) -> Items<'a, T> {
Huon Wilson4b9a7a22014-04-05 05:45:42206 Items{index: 0, rindex: self.nelts, lo: self.lo, elts: self.elts.as_slice()}
blake2-ppc3385e792013-07-15 23:13:26207 }
208
Jed Estep4f7a7422013-06-25 19:08:47209 /// Front-to-back iterator which returns mutable values.
Palmer Cox3fd8c8b2014-01-15 03:32:24210 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39211 let start_index = raw_index(self.lo, self.elts.len(), 0);
212 let end_index = raw_index(self.lo, self.elts.len(), self.nelts);
213
214 // Divide up the array
215 if end_index <= start_index {
216 // Items to iterate goes from:
217 // start_index to self.elts.len()
218 // and then
219 // 0 to end_index
Guillaume Pinot25bb1a42013-12-01 17:19:39220 let (temp, remaining1) = self.elts.mut_split_at(start_index);
221 let (remaining2, _) = temp.mut_split_at(end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24222 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39223 remaining2: remaining2,
224 nelts: self.nelts }
225 } else {
226 // Items to iterate goes from start_index to end_index:
Guillaume Pinot25bb1a42013-12-01 17:19:39227 let (empty, elts) = self.elts.mut_split_at(0);
Niko Matsakisbc4164d2013-11-16 22:29:39228 let remaining1 = elts.mut_slice(start_index, end_index);
Palmer Cox3fd8c8b2014-01-15 03:32:24229 MutItems { remaining1: remaining1,
Niko Matsakisbc4164d2013-11-16 22:29:39230 remaining2: empty,
231 nelts: self.nelts }
232 }
Jed Estep4f7a7422013-06-25 19:08:47233 }
Jed Estep4f7a7422013-06-25 19:08:47234}
235
blake2-ppc70523712013-07-10 13:27:14236/// RingBuf iterator
Palmer Cox3fd8c8b2014-01-15 03:32:24237pub struct Items<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04238 lo: uint,
239 index: uint,
240 rindex: uint,
241 elts: &'a [Option<T>],
Jed Estep4f7a7422013-06-25 19:08:47242}
Niko Matsakisbc4164d2013-11-16 22:29:39243
Palmer Cox3fd8c8b2014-01-15 03:32:24244impl<'a, T> Iterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39245 #[inline]
Erik Price5731ca32013-12-10 07:16:18246 fn next(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39247 if self.index == self.rindex {
248 return None;
249 }
250 let raw_index = raw_index(self.lo, self.elts.len(), self.index);
251 self.index += 1;
252 Some(self.elts[raw_index].get_ref())
253 }
254
255 #[inline]
256 fn size_hint(&self) -> (uint, Option<uint>) {
257 let len = self.rindex - self.index;
258 (len, Some(len))
259 }
260}
261
Palmer Cox3fd8c8b2014-01-15 03:32:24262impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39263 #[inline]
Erik Price5731ca32013-12-10 07:16:18264 fn next_back(&mut self) -> Option<&'a T> {
Niko Matsakisbc4164d2013-11-16 22:29:39265 if self.index == self.rindex {
266 return None;
267 }
268 self.rindex -= 1;
269 let raw_index = raw_index(self.lo, self.elts.len(), self.rindex);
270 Some(self.elts[raw_index].get_ref())
271 }
272}
Jed Estep35314c92013-06-26 15:38:29273
Palmer Cox3fd8c8b2014-01-15 03:32:24274impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24275
Palmer Cox3fd8c8b2014-01-15 03:32:24276impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> {
blake2-ppcf6862132013-07-29 18:16:26277 #[inline]
278 fn indexable(&self) -> uint { self.rindex - self.index }
279
280 #[inline]
Alex Crichtonf4083a22014-04-22 05:15:42281 fn idx(&mut self, j: uint) -> Option<&'a T> {
blake2-ppcf6862132013-07-29 18:16:26282 if j >= self.indexable() {
283 None
284 } else {
285 let raw_index = raw_index(self.lo, self.elts.len(), self.index + j);
286 Some(self.elts[raw_index].get_ref())
287 }
288 }
289}
290
blake2-ppc70523712013-07-10 13:27:14291/// RingBuf mutable iterator
Palmer Cox3fd8c8b2014-01-15 03:32:24292pub struct MutItems<'a, T> {
Alex Crichton8ad7e542014-03-27 22:10:04293 remaining1: &'a mut [Option<T>],
294 remaining2: &'a mut [Option<T>],
295 nelts: uint,
Jed Estep4f7a7422013-06-25 19:08:47296}
Niko Matsakisbc4164d2013-11-16 22:29:39297
Palmer Cox3fd8c8b2014-01-15 03:32:24298impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39299 #[inline]
Erik Price5731ca32013-12-10 07:16:18300 fn next(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39301 if self.nelts == 0 {
302 return None;
303 }
304 let r = if self.remaining1.len() > 0 {
305 &mut self.remaining1
306 } else {
307 assert!(self.remaining2.len() > 0);
308 &mut self.remaining2
309 };
310 self.nelts -= 1;
Nathaniel Hermand451c152014-01-25 17:00:46311 Some(r.mut_shift_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39312 }
313
314 #[inline]
315 fn size_hint(&self) -> (uint, Option<uint>) {
316 (self.nelts, Some(self.nelts))
317 }
318}
319
Palmer Cox3fd8c8b2014-01-15 03:32:24320impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
Niko Matsakisbc4164d2013-11-16 22:29:39321 #[inline]
Erik Price5731ca32013-12-10 07:16:18322 fn next_back(&mut self) -> Option<&'a mut T> {
Niko Matsakisbc4164d2013-11-16 22:29:39323 if self.nelts == 0 {
324 return None;
325 }
326 let r = if self.remaining2.len() > 0 {
327 &mut self.remaining2
328 } else {
329 assert!(self.remaining1.len() > 0);
330 &mut self.remaining1
331 };
332 self.nelts -= 1;
Nathaniel Herman33960342014-01-25 20:33:31333 Some(r.mut_pop_ref().unwrap().get_mut_ref())
Niko Matsakisbc4164d2013-11-16 22:29:39334 }
335}
Daniel Micayb47e1e92013-02-16 22:55:55336
Palmer Cox3fd8c8b2014-01-15 03:32:24337impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
blake2-ppc7c369ee72013-09-01 16:20:24338
Daniel Micayb47e1e92013-02-16 22:55:55339/// Grow is only called on full elts, so nelts is also len(elts), unlike
340/// elsewhere.
Huon Wilson4b9a7a22014-04-05 05:45:42341fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
Corey Richardsoncc57ca02013-05-19 02:02:45342 assert_eq!(nelts, elts.len());
blake2-ppc8a326762013-07-06 03:42:45343 let lo = *loptr;
344 let newlen = nelts * 2;
blake2-ppc40ce0b72013-07-06 03:42:45345 elts.reserve(newlen);
Daniel Micayb47e1e92013-02-16 22:55:55346
blake2-ppc40ce0b72013-07-06 03:42:45347 /* fill with None */
Daniel Micay100894552013-08-03 16:45:23348 for _ in range(elts.len(), elts.capacity()) {
blake2-ppc40ce0b72013-07-06 03:42:45349 elts.push(None);
Daniel Micayb47e1e92013-02-16 22:55:55350 }
blake2-ppc8a326762013-07-06 03:42:45351
352 /*
353 Move the shortest half into the newly reserved area.
354 lo ---->|
355 nelts ----------->|
356 [o o o|o o o o o]
357 A [. . .|o o o o o o o o|. . . . .]
358 B [o o o|. . . . . . . .|o o o o o]
359 */
360
361 assert!(newlen - nelts/2 >= nelts);
362 if lo <= (nelts - lo) { // A
Daniel Micay100894552013-08-03 16:45:23363 for i in range(0u, lo) {
Huon Wilson4b9a7a22014-04-05 05:45:42364 elts.as_mut_slice().swap(i, nelts + i);
blake2-ppc8a326762013-07-06 03:42:45365 }
366 } else { // B
Daniel Micay100894552013-08-03 16:45:23367 for i in range(lo, nelts) {
Huon Wilson4b9a7a22014-04-05 05:45:42368 elts.as_mut_slice().swap(i, newlen - nelts + i);
blake2-ppc8a326762013-07-06 03:42:45369 }
370 *loptr += newlen - nelts;
blake2-ppc40ce0b72013-07-06 03:42:45371 }
Daniel Micayb47e1e92013-02-16 22:55:55372}
373
blake2-ppc75015c32013-07-06 03:42:45374/// Return index in underlying vec for a given logical element index
375fn raw_index(lo: uint, len: uint, index: uint) -> uint {
376 if lo >= len - index {
377 lo + index - len
378 } else {
379 lo + index
380 }
381}
382
Alex Crichton748bc3c2014-05-30 00:45:07383impl<A: PartialEq> PartialEq for RingBuf<A> {
blake2-ppc70523712013-07-10 13:27:14384 fn eq(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32385 self.nelts == other.nelts &&
386 self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
387 }
blake2-ppc70523712013-07-10 13:27:14388 fn ne(&self, other: &RingBuf<A>) -> bool {
blake2-ppc10c76982013-07-06 13:27:32389 !self.eq(other)
390 }
391}
392
Huon Wilson53487a02013-08-13 13:08:14393impl<A> FromIterator<A> for RingBuf<A> {
Brian Andersonee052192014-03-31 04:45:55394 fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
blake2-ppcf8ae5262013-07-30 00:06:49395 let (lower, _) = iterator.size_hint();
396 let mut deq = RingBuf::with_capacity(lower);
397 deq.extend(iterator);
blake2-ppc08dc72f2013-07-06 03:42:45398 deq
399 }
400}
401
Huon Wilson53487a02013-08-13 13:08:14402impl<A> Extendable<A> for RingBuf<A> {
Marvin Löbel6200e762014-03-20 13:12:56403 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
404 for elt in iterator {
blake2-ppcf8ae5262013-07-30 00:06:49405 self.push_back(elt);
406 }
407 }
408}
409
Alex Crichton6a585372014-05-30 01:50:12410impl<T: fmt::Show> fmt::Show for RingBuf<T> {
Adolfo OchagavĂ­a8e4e3ab2014-06-04 14:15:04411 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
412 try!(write!(f, "["));
413
414 for (i, e) in self.iter().enumerate() {
415 if i != 0 { try!(write!(f, ", ")); }
416 try!(write!(f, "{}", *e));
417 }
418
419 write!(f, "]")
420 }
421}
422
Brian Anderson6e27b272012-01-18 03:05:07423#[cfg(test)]
424mod tests {
Alex Crichton02882fb2014-02-28 09:23:06425 use std::fmt::Show;
Alex Crichton760b93a2014-05-30 02:03:06426 use std::prelude::*;
Alex Crichtonade807c2014-06-12 02:33:52427 use std::gc::{GC, Gc};
Alex Crichton760b93a2014-05-30 02:03:06428 use test::Bencher;
429 use test;
430
Alex Crichtonda070392014-06-06 23:33:44431 use {Deque, Mutable};
Alex Crichtonf47e4b22014-01-07 06:33:50432 use super::RingBuf;
Alex Crichton760b93a2014-05-30 02:03:06433 use vec::Vec;
Patrick Waltonfa5ee932012-12-28 02:24:18434
Brian Anderson6e27b272012-01-18 03:05:07435 #[test]
436 fn test_simple() {
blake2-ppc70523712013-07-10 13:27:14437 let mut d = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45438 assert_eq!(d.len(), 0u);
Niko Matsakis9e3d0b02014-04-21 21:58:52439 d.push_front(17i);
440 d.push_front(42i);
blake2-ppc70523712013-07-10 13:27:14441 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45442 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14443 d.push_back(137);
Corey Richardsoncc57ca02013-05-19 02:02:45444 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31445 debug!("{:?}", d.front());
blake2-ppc70523712013-07-10 13:27:14446 assert_eq!(*d.front().unwrap(), 42);
Alex Crichtondaf5f5a2013-10-21 20:08:31447 debug!("{:?}", d.back());
blake2-ppc70523712013-07-10 13:27:14448 assert_eq!(*d.back().unwrap(), 137);
449 let mut i = d.pop_front();
Alex Crichtondaf5f5a2013-10-21 20:08:31450 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14451 assert_eq!(i, Some(42));
Brian Anderson6e27b272012-01-18 03:05:07452 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31453 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14454 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07455 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31456 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14457 assert_eq!(i, Some(137));
Brian Anderson6e27b272012-01-18 03:05:07458 i = d.pop_back();
Alex Crichtondaf5f5a2013-10-21 20:08:31459 debug!("{:?}", i);
blake2-ppc70523712013-07-10 13:27:14460 assert_eq!(i, Some(17));
Corey Richardsoncc57ca02013-05-19 02:02:45461 assert_eq!(d.len(), 0u);
blake2-ppc70523712013-07-10 13:27:14462 d.push_back(3);
Corey Richardsoncc57ca02013-05-19 02:02:45463 assert_eq!(d.len(), 1u);
blake2-ppc70523712013-07-10 13:27:14464 d.push_front(2);
Corey Richardsoncc57ca02013-05-19 02:02:45465 assert_eq!(d.len(), 2u);
blake2-ppc70523712013-07-10 13:27:14466 d.push_back(4);
Corey Richardsoncc57ca02013-05-19 02:02:45467 assert_eq!(d.len(), 3u);
blake2-ppc70523712013-07-10 13:27:14468 d.push_front(1);
Corey Richardsoncc57ca02013-05-19 02:02:45469 assert_eq!(d.len(), 4u);
Alex Crichtondaf5f5a2013-10-21 20:08:31470 debug!("{:?}", d.get(0));
471 debug!("{:?}", d.get(1));
472 debug!("{:?}", d.get(2));
473 debug!("{:?}", d.get(3));
Corey Richardsoncc57ca02013-05-19 02:02:45474 assert_eq!(*d.get(0), 1);
475 assert_eq!(*d.get(1), 2);
476 assert_eq!(*d.get(2), 3);
477 assert_eq!(*d.get(3), 4);
Brian Anderson6e27b272012-01-18 03:05:07478 }
479
Kevin Cantuc43426e2012-09-13 05:09:55480 #[test]
481 fn test_boxes() {
Alex Crichtonade807c2014-06-12 02:33:52482 let a: Gc<int> = box(GC) 5;
483 let b: Gc<int> = box(GC) 72;
484 let c: Gc<int> = box(GC) 64;
485 let d: Gc<int> = box(GC) 175;
Kevin Cantuc43426e2012-09-13 05:09:55486
blake2-ppc70523712013-07-10 13:27:14487 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45488 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14489 deq.push_front(a);
490 deq.push_front(b);
491 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45492 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14493 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45494 assert_eq!(deq.len(), 4);
blake2-ppc70523712013-07-10 13:27:14495 assert_eq!(deq.front(), Some(&b));
496 assert_eq!(deq.back(), Some(&d));
497 assert_eq!(deq.pop_front(), Some(b));
498 assert_eq!(deq.pop_back(), Some(d));
499 assert_eq!(deq.pop_back(), Some(c));
500 assert_eq!(deq.pop_back(), Some(a));
Corey Richardsoncc57ca02013-05-19 02:02:45501 assert_eq!(deq.len(), 0);
blake2-ppc70523712013-07-10 13:27:14502 deq.push_back(c);
Corey Richardsoncc57ca02013-05-19 02:02:45503 assert_eq!(deq.len(), 1);
blake2-ppc70523712013-07-10 13:27:14504 deq.push_front(b);
Corey Richardsoncc57ca02013-05-19 02:02:45505 assert_eq!(deq.len(), 2);
blake2-ppc70523712013-07-10 13:27:14506 deq.push_back(d);
Corey Richardsoncc57ca02013-05-19 02:02:45507 assert_eq!(deq.len(), 3);
blake2-ppc70523712013-07-10 13:27:14508 deq.push_front(a);
Corey Richardsoncc57ca02013-05-19 02:02:45509 assert_eq!(deq.len(), 4);
510 assert_eq!(*deq.get(0), a);
511 assert_eq!(*deq.get(1), b);
512 assert_eq!(*deq.get(2), c);
513 assert_eq!(*deq.get(3), d);
Daniel Micay373c0722013-02-17 00:10:10514 }
515
Felix S. Klock IIa636f512013-05-01 23:32:37516 #[cfg(test)]
Alex Crichton748bc3c2014-05-30 00:45:07517 fn test_parameterized<T:Clone + PartialEq + Show>(a: T, b: T, c: T, d: T) {
Patrick Waltondc4bf172013-07-13 04:05:59518 let mut deq = RingBuf::new();
Corey Richardsoncc57ca02013-05-19 02:02:45519 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59520 deq.push_front(a.clone());
521 deq.push_front(b.clone());
522 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45523 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59524 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45525 assert_eq!(deq.len(), 4);
Marvin Löbel0ac7a212013-08-03 23:59:24526 assert_eq!((*deq.front().unwrap()).clone(), b.clone());
527 assert_eq!((*deq.back().unwrap()).clone(), d.clone());
528 assert_eq!(deq.pop_front().unwrap(), b.clone());
529 assert_eq!(deq.pop_back().unwrap(), d.clone());
530 assert_eq!(deq.pop_back().unwrap(), c.clone());
531 assert_eq!(deq.pop_back().unwrap(), a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45532 assert_eq!(deq.len(), 0);
Patrick Waltondc4bf172013-07-13 04:05:59533 deq.push_back(c.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45534 assert_eq!(deq.len(), 1);
Patrick Waltondc4bf172013-07-13 04:05:59535 deq.push_front(b.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45536 assert_eq!(deq.len(), 2);
Patrick Waltondc4bf172013-07-13 04:05:59537 deq.push_back(d.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45538 assert_eq!(deq.len(), 3);
Patrick Waltondc4bf172013-07-13 04:05:59539 deq.push_front(a.clone());
Corey Richardsoncc57ca02013-05-19 02:02:45540 assert_eq!(deq.len(), 4);
Patrick Walton99b33f72013-07-02 19:47:32541 assert_eq!((*deq.get(0)).clone(), a.clone());
542 assert_eq!((*deq.get(1)).clone(), b.clone());
543 assert_eq!((*deq.get(2)).clone(), c.clone());
544 assert_eq!((*deq.get(3)).clone(), d.clone());
Brian Anderson6e27b272012-01-18 03:05:07545 }
546
blake2-ppc81933ed2013-07-06 03:42:45547 #[test]
blake2-ppc70523712013-07-10 13:27:14548 fn test_push_front_grow() {
549 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23550 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14551 deq.push_front(i);
blake2-ppc81933ed2013-07-06 03:42:45552 }
553 assert_eq!(deq.len(), 66);
554
Daniel Micay100894552013-08-03 16:45:23555 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45556 assert_eq!(*deq.get(i), 65 - i);
557 }
558
blake2-ppc70523712013-07-10 13:27:14559 let mut deq = RingBuf::new();
Daniel Micay100894552013-08-03 16:45:23560 for i in range(0u, 66) {
blake2-ppc70523712013-07-10 13:27:14561 deq.push_back(i);
blake2-ppc81933ed2013-07-06 03:42:45562 }
563
Daniel Micay100894552013-08-03 16:45:23564 for i in range(0u, 66) {
blake2-ppc81933ed2013-07-06 03:42:45565 assert_eq!(*deq.get(i), i);
566 }
567 }
568
569 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35570 fn bench_new(b: &mut test::Bencher) {
Patrick Walton38efa172013-11-22 03:20:48571 b.iter(|| {
Patrick Walton86939432013-08-08 18:38:10572 let _: RingBuf<u64> = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48573 })
blake2-ppc81933ed2013-07-06 03:42:45574 }
575
576 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35577 fn bench_push_back(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14578 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48579 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25580 deq.push_back(0i);
Patrick Walton38efa172013-11-22 03:20:48581 })
blake2-ppc81933ed2013-07-06 03:42:45582 }
583
584 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35585 fn bench_push_front(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14586 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48587 b.iter(|| {
Patrick Waltona5bb0a32014-06-27 19:30:25588 deq.push_front(0i);
Patrick Walton38efa172013-11-22 03:20:48589 })
blake2-ppc81933ed2013-07-06 03:42:45590 }
591
592 #[bench]
Liigo Zhuang408f4842014-04-01 01:16:35593 fn bench_grow(b: &mut test::Bencher) {
blake2-ppc70523712013-07-10 13:27:14594 let mut deq = RingBuf::new();
Patrick Walton38efa172013-11-22 03:20:48595 b.iter(|| {
Niko Matsakis9e3d0b02014-04-21 21:58:52596 for _ in range(0i, 65) {
Patrick Waltona5bb0a32014-06-27 19:30:25597 deq.push_front(1i);
Brendan Zabarauskas729060d2014-01-30 00:20:34598 }
Patrick Walton38efa172013-11-22 03:20:48599 })
blake2-ppc81933ed2013-07-06 03:42:45600 }
601
Alex Crichton748bc3c2014-05-30 00:45:07602 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32603 enum Taggy {
604 One(int),
605 Two(int, int),
606 Three(int, int, int),
Brian Anderson6e27b272012-01-18 03:05:07607 }
608
Alex Crichton748bc3c2014-05-30 00:45:07609 #[deriving(Clone, PartialEq, Show)]
Patrick Walton99b33f72013-07-02 19:47:32610 enum Taggypar<T> {
611 Onepar(int),
612 Twopar(int, int),
613 Threepar(int, int, int),
614 }
615
Alex Crichton748bc3c2014-05-30 00:45:07616 #[deriving(Clone, PartialEq, Show)]
Erick Tryzelaare84576b2013-01-22 16:44:24617 struct RecCy {
618 x: int,
619 y: int,
Patrick Waltoneb4d39e2013-01-26 00:57:39620 t: Taggy
Patrick Walton9117dcb2012-09-20 01:00:26621 }
Kevin Cantuc43426e2012-09-13 05:09:55622
623 #[test]
624 fn test_param_int() {
625 test_parameterized::<int>(5, 72, 64, 175);
626 }
627
628 #[test]
629 fn test_param_at_int() {
Alex Crichtonade807c2014-06-12 02:33:52630 test_parameterized::<Gc<int>>(box(GC) 5, box(GC) 72,
631 box(GC) 64, box(GC) 175);
Kevin Cantuc43426e2012-09-13 05:09:55632 }
633
634 #[test]
635 fn test_param_taggy() {
Corey Richardsonf8ae9b02013-06-26 22:14:35636 test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55637 }
638
639 #[test]
640 fn test_param_taggypar() {
641 test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
Ben Striegela605fd02012-08-11 14:08:42642 Twopar::<int>(1, 2),
643 Threepar::<int>(1, 2, 3),
644 Twopar::<int>(17, 42));
Kevin Cantuc43426e2012-09-13 05:09:55645 }
Brian Anderson6e27b272012-01-18 03:05:07646
Kevin Cantuc43426e2012-09-13 05:09:55647 #[test]
648 fn test_param_reccy() {
Erick Tryzelaare84576b2013-01-22 16:44:24649 let reccy1 = RecCy { x: 1, y: 2, t: One(1) };
650 let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) };
651 let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) };
652 let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) };
Kevin Cantuc43426e2012-09-13 05:09:55653 test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
Brian Anderson6e27b272012-01-18 03:05:07654 }
Erick Tryzelaar909d8f02013-03-30 01:02:44655
656 #[test]
blake2-ppc0ff5c172013-07-06 03:42:45657 fn test_with_capacity() {
blake2-ppc70523712013-07-10 13:27:14658 let mut d = RingBuf::with_capacity(0);
Patrick Waltona5bb0a32014-06-27 19:30:25659 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45660 assert_eq!(d.len(), 1);
blake2-ppc70523712013-07-10 13:27:14661 let mut d = RingBuf::with_capacity(50);
Patrick Waltona5bb0a32014-06-27 19:30:25662 d.push_back(1i);
blake2-ppc0ff5c172013-07-06 03:42:45663 assert_eq!(d.len(), 1);
664 }
665
666 #[test]
David Manescu65f35782014-01-31 13:03:20667 fn test_reserve_exact() {
blake2-ppc70523712013-07-10 13:27:14668 let mut d = RingBuf::new();
669 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20670 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47671 assert_eq!(d.elts.capacity(), 50);
blake2-ppc70523712013-07-10 13:27:14672 let mut d = RingBuf::new();
673 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20674 d.reserve_exact(50);
Huon Wilson32d65592013-06-27 14:40:47675 assert_eq!(d.elts.capacity(), 50);
Tim Chevalier77de84b2013-05-27 18:47:38676 }
677
678 #[test]
David Manescu65f35782014-01-31 13:03:20679 fn test_reserve() {
blake2-ppc70523712013-07-10 13:27:14680 let mut d = RingBuf::new();
681 d.push_back(0u64);
David Manescu65f35782014-01-31 13:03:20682 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47683 assert_eq!(d.elts.capacity(), 64);
blake2-ppc70523712013-07-10 13:27:14684 let mut d = RingBuf::new();
685 d.push_back(0u32);
David Manescu65f35782014-01-31 13:03:20686 d.reserve(50);
Huon Wilson32d65592013-06-27 14:40:47687 assert_eq!(d.elts.capacity(), 64);
Tim Chevalier77de84b2013-05-27 18:47:38688 }
689
Jed Estep096fb792013-06-26 14:04:44690 #[test]
blake2-ppc57757a82013-09-26 07:19:26691 fn test_swap() {
Niko Matsakis9e3d0b02014-04-21 21:58:52692 let mut d: RingBuf<int> = range(0i, 5).collect();
blake2-ppc57757a82013-09-26 07:19:26693 d.pop_front();
694 d.swap(0, 3);
Huon Wilson4b9a7a22014-04-05 05:45:42695 assert_eq!(d.iter().map(|&x|x).collect::<Vec<int>>(), vec!(4, 2, 3, 1));
blake2-ppc57757a82013-09-26 07:19:26696 }
697
698 #[test]
Jed Estep096fb792013-06-26 14:04:44699 fn test_iter() {
blake2-ppc70523712013-07-10 13:27:14700 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45701 assert_eq!(d.iter().next(), None);
blake2-ppc9ccf4432013-07-14 20:30:22702 assert_eq!(d.iter().size_hint(), (0, Some(0)));
blake2-ppcf88d5322013-07-06 03:42:45703
Niko Matsakis9e3d0b02014-04-21 21:58:52704 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14705 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44706 }
Huon Wilson4b9a7a22014-04-05 05:45:42707 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), &[&0,&1,&2,&3,&4]);
Corey Richardsonf8ae9b02013-06-26 22:14:35708
Niko Matsakis9e3d0b02014-04-21 21:58:52709 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14710 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44711 }
Huon Wilson4b9a7a22014-04-05 05:45:42712 assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), &[&8,&7,&6,&0,&1,&2,&3,&4]);
blake2-ppc9ccf4432013-07-14 20:30:22713
714 let mut it = d.iter();
715 let mut len = d.len();
716 loop {
717 match it.next() {
718 None => break,
719 _ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
720 }
721 }
Jed Estep096fb792013-06-26 14:04:44722 }
723
724 #[test]
725 fn test_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14726 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12727 assert_eq!(d.iter().rev().next(), None);
blake2-ppcf88d5322013-07-06 03:42:45728
Niko Matsakis9e3d0b02014-04-21 21:58:52729 for i in range(0i, 5) {
blake2-ppc70523712013-07-10 13:27:14730 d.push_back(i);
Jed Estep096fb792013-06-26 14:04:44731 }
Jonathan S03609e52014-04-21 04:59:12732 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), &[&4,&3,&2,&1,&0]);
Corey Richardsonf8ae9b02013-06-26 22:14:35733
Niko Matsakis9e3d0b02014-04-21 21:58:52734 for i in range(6i, 9) {
blake2-ppc70523712013-07-10 13:27:14735 d.push_front(i);
Jed Estep096fb792013-06-26 14:04:44736 }
Jonathan S03609e52014-04-21 04:59:12737 assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), &[&4,&3,&2,&1,&0,&6,&7,&8]);
Jed Estep096fb792013-06-26 14:04:44738 }
blake2-ppc08dc72f2013-07-06 03:42:45739
740 #[test]
Niko Matsakisbc4164d2013-11-16 22:29:39741 fn test_mut_rev_iter_wrap() {
742 let mut d = RingBuf::with_capacity(3);
Jonathan S03609e52014-04-21 04:59:12743 assert!(d.mut_iter().rev().next().is_none());
Niko Matsakisbc4164d2013-11-16 22:29:39744
Niko Matsakis9e3d0b02014-04-21 21:58:52745 d.push_back(1i);
Niko Matsakisbc4164d2013-11-16 22:29:39746 d.push_back(2);
747 d.push_back(3);
748 assert_eq!(d.pop_front(), Some(1));
749 d.push_back(4);
750
Jonathan S03609e52014-04-21 04:59:12751 assert_eq!(d.mut_iter().rev().map(|x| *x).collect::<Vec<int>>(),
Huon Wilson4b9a7a22014-04-05 05:45:42752 vec!(4, 3, 2));
Niko Matsakisbc4164d2013-11-16 22:29:39753 }
754
755 #[test]
blake2-ppcf88d5322013-07-06 03:42:45756 fn test_mut_iter() {
blake2-ppc70523712013-07-10 13:27:14757 let mut d = RingBuf::new();
blake2-ppcf88d5322013-07-06 03:42:45758 assert!(d.mut_iter().next().is_none());
759
Daniel Micay100894552013-08-03 16:45:23760 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14761 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45762 }
763
Daniel Micay100894552013-08-03 16:45:23764 for (i, elt) in d.mut_iter().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45765 assert_eq!(*elt, 2 - i);
766 *elt = i;
767 }
768
769 {
770 let mut it = d.mut_iter();
771 assert_eq!(*it.next().unwrap(), 0);
772 assert_eq!(*it.next().unwrap(), 1);
773 assert_eq!(*it.next().unwrap(), 2);
774 assert!(it.next().is_none());
775 }
776 }
777
778 #[test]
779 fn test_mut_rev_iter() {
blake2-ppc70523712013-07-10 13:27:14780 let mut d = RingBuf::new();
Jonathan S03609e52014-04-21 04:59:12781 assert!(d.mut_iter().rev().next().is_none());
blake2-ppcf88d5322013-07-06 03:42:45782
Daniel Micay100894552013-08-03 16:45:23783 for i in range(0u, 3) {
blake2-ppc70523712013-07-10 13:27:14784 d.push_front(i);
blake2-ppcf88d5322013-07-06 03:42:45785 }
786
Jonathan S03609e52014-04-21 04:59:12787 for (i, elt) in d.mut_iter().rev().enumerate() {
blake2-ppcf88d5322013-07-06 03:42:45788 assert_eq!(*elt, i);
789 *elt = i;
790 }
791
792 {
Jonathan S03609e52014-04-21 04:59:12793 let mut it = d.mut_iter().rev();
blake2-ppcf88d5322013-07-06 03:42:45794 assert_eq!(*it.next().unwrap(), 0);
795 assert_eq!(*it.next().unwrap(), 1);
796 assert_eq!(*it.next().unwrap(), 2);
797 assert!(it.next().is_none());
798 }
799 }
800
801 #[test]
Brian Andersonee052192014-03-31 04:45:55802 fn test_from_iter() {
Daniel Micay6919cf52013-09-08 15:01:16803 use std::iter;
Niko Matsakis9e3d0b02014-04-21 21:58:52804 let v = vec!(1i,2,3,4,5,6,7);
Erick Tryzelaar68f40d22013-08-10 03:09:47805 let deq: RingBuf<int> = v.iter().map(|&x| x).collect();
Huon Wilson4b9a7a22014-04-05 05:45:42806 let u: Vec<int> = deq.iter().map(|&x| x).collect();
blake2-ppc08dc72f2013-07-06 03:42:45807 assert_eq!(u, v);
808
Daniel Micay6919cf52013-09-08 15:01:16809 let mut seq = iter::count(0u, 2).take(256);
blake2-ppc70523712013-07-10 13:27:14810 let deq: RingBuf<uint> = seq.collect();
Daniel Micay100894552013-08-03 16:45:23811 for (i, &x) in deq.iter().enumerate() {
blake2-ppc08dc72f2013-07-06 03:42:45812 assert_eq!(2*i, x);
813 }
814 assert_eq!(deq.len(), 256);
815 }
blake2-ppc10c76982013-07-06 13:27:32816
817 #[test]
818 fn test_clone() {
blake2-ppc70523712013-07-10 13:27:14819 let mut d = RingBuf::new();
Niko Matsakis9e3d0b02014-04-21 21:58:52820 d.push_front(17i);
blake2-ppc70523712013-07-10 13:27:14821 d.push_front(42);
822 d.push_back(137);
823 d.push_back(137);
blake2-ppc10c76982013-07-06 13:27:32824 assert_eq!(d.len(), 4u);
825 let mut e = d.clone();
826 assert_eq!(e.len(), 4u);
827 while !d.is_empty() {
828 assert_eq!(d.pop_back(), e.pop_back());
829 }
830 assert_eq!(d.len(), 0u);
831 assert_eq!(e.len(), 0u);
832 }
833
834 #[test]
835 fn test_eq() {
blake2-ppc70523712013-07-10 13:27:14836 let mut d = RingBuf::new();
Alex Crichton02882fb2014-02-28 09:23:06837 assert!(d == RingBuf::with_capacity(0));
Niko Matsakis9e3d0b02014-04-21 21:58:52838 d.push_front(137i);
blake2-ppc70523712013-07-10 13:27:14839 d.push_front(17);
840 d.push_front(42);
841 d.push_back(137);
842 let mut e = RingBuf::with_capacity(0);
843 e.push_back(42);
844 e.push_back(17);
845 e.push_back(137);
846 e.push_back(137);
Alex Crichton02882fb2014-02-28 09:23:06847 assert!(&e == &d);
blake2-ppc10c76982013-07-06 13:27:32848 e.pop_back();
blake2-ppc70523712013-07-10 13:27:14849 e.push_back(0);
blake2-ppc10c76982013-07-06 13:27:32850 assert!(e != d);
851 e.clear();
Alex Crichton02882fb2014-02-28 09:23:06852 assert!(e == RingBuf::new());
blake2-ppc10c76982013-07-06 13:27:32853 }
Adolfo OchagavĂ­a8e4e3ab2014-06-04 14:15:04854
855 #[test]
856 fn test_show() {
Niko Matsakis9e3d0b02014-04-21 21:58:52857 let ringbuf: RingBuf<int> = range(0i, 10).collect();
Adolfo OchagavĂ­a8e4e3ab2014-06-04 14:15:04858 assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
859
860 let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
861 .map(|&s| s)
862 .collect();
863 assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]");
864 }
Michael Sullivanc854d6e2012-07-03 17:52:32865}