2014-06-28 15:57:36 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use core::iter::*;
|
|
|
|
use core::iter::order::*;
|
2014-12-31 21:10:43 -06:00
|
|
|
use core::iter::MinMaxResult::*;
|
2014-11-12 07:02:42 -06:00
|
|
|
use core::num::SignedInt;
|
2015-02-26 18:43:55 -06:00
|
|
|
use core::usize;
|
2014-06-28 15:57:36 -05:00
|
|
|
use core::cmp;
|
|
|
|
|
2014-08-06 05:20:37 -05:00
|
|
|
use test::Bencher;
|
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
#[test]
|
|
|
|
fn test_lt() {
|
2014-12-30 02:19:41 -06:00
|
|
|
let empty: [int; 0] = [];
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1,2,3];
|
|
|
|
let ys = [1,2,0];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert!(!lt(xs.iter(), ys.iter()));
|
|
|
|
assert!(!le(xs.iter(), ys.iter()));
|
|
|
|
assert!( gt(xs.iter(), ys.iter()));
|
|
|
|
assert!( ge(xs.iter(), ys.iter()));
|
|
|
|
|
|
|
|
assert!( lt(ys.iter(), xs.iter()));
|
|
|
|
assert!( le(ys.iter(), xs.iter()));
|
|
|
|
assert!(!gt(ys.iter(), xs.iter()));
|
|
|
|
assert!(!ge(ys.iter(), xs.iter()));
|
|
|
|
|
|
|
|
assert!( lt(empty.iter(), xs.iter()));
|
|
|
|
assert!( le(empty.iter(), xs.iter()));
|
|
|
|
assert!(!gt(empty.iter(), xs.iter()));
|
|
|
|
assert!(!ge(empty.iter(), xs.iter()));
|
|
|
|
|
|
|
|
// Sequence with NaN
|
|
|
|
let u = [1.0f64, 2.0];
|
|
|
|
let v = [0.0f64/0.0, 3.0];
|
|
|
|
|
|
|
|
assert!(!lt(u.iter(), v.iter()));
|
|
|
|
assert!(!le(u.iter(), v.iter()));
|
|
|
|
assert!(!gt(u.iter(), v.iter()));
|
|
|
|
assert!(!ge(u.iter(), v.iter()));
|
|
|
|
|
|
|
|
let a = [0.0f64/0.0];
|
|
|
|
let b = [1.0f64];
|
|
|
|
let c = [2.0f64];
|
|
|
|
|
|
|
|
assert!(lt(a.iter(), b.iter()) == (a[0] < b[0]));
|
|
|
|
assert!(le(a.iter(), b.iter()) == (a[0] <= b[0]));
|
|
|
|
assert!(gt(a.iter(), b.iter()) == (a[0] > b[0]));
|
|
|
|
assert!(ge(a.iter(), b.iter()) == (a[0] >= b[0]));
|
|
|
|
|
|
|
|
assert!(lt(c.iter(), b.iter()) == (c[0] < b[0]));
|
|
|
|
assert!(le(c.iter(), b.iter()) == (c[0] <= b[0]));
|
|
|
|
assert!(gt(c.iter(), b.iter()) == (c[0] > b[0]));
|
|
|
|
assert!(ge(c.iter(), b.iter()) == (c[0] >= b[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multi_iter() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1,2,3,4];
|
|
|
|
let ys = [4,3,2,1];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(eq(xs.iter(), ys.iter().rev()));
|
|
|
|
assert!(lt(xs.iter(), xs.iter().skip(2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_counter_from_iter() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let it = count(0, 5).take(10);
|
2014-06-28 15:57:36 -05:00
|
|
|
let xs: Vec<int> = FromIterator::from_iter(it);
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(xs, [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_chain() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 4, 5];
|
|
|
|
let ys = [30, 40, 50, 60];
|
2014-06-28 15:57:36 -05:00
|
|
|
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().chain(ys.iter());
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for &x in it {
|
|
|
|
assert_eq!(x, expected[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
|
2015-02-03 07:50:52 -06:00
|
|
|
let ys = count(30, 10).take(4);
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().cloned().chain(ys);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for x in it {
|
|
|
|
assert_eq!(x, expected[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter_map() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let it = count(0, 1).take(10)
|
2014-06-28 15:57:36 -05:00
|
|
|
.filter_map(|x| if x % 2 == 0 { Some(x*x) } else { None });
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(it.collect::<Vec<uint>>(), [0*0, 2*2, 4*4, 6*6, 8*8]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_enumerate() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 4, 5];
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().enumerate();
|
2014-06-28 15:57:36 -05:00
|
|
|
for (i, &x) in it {
|
|
|
|
assert_eq!(i, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_peekable() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = vec![0, 1, 2, 3, 4, 5];
|
2015-02-14 23:19:50 -06:00
|
|
|
let mut it = xs.iter().cloned().peekable();
|
2015-01-21 15:38:50 -06:00
|
|
|
|
|
|
|
assert_eq!(it.len(), 6);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.peek().unwrap(), &0);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 6);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 0);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 5);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 1);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 4);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 2);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 3);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.peek().unwrap(), &3);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 3);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.peek().unwrap(), &3);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 3);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 3);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 2);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 4);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.peek().unwrap(), &5);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next().unwrap(), 5);
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(it.peek().is_none());
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(it.next().is_none());
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_take_while() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
|
|
|
let ys = [0, 1, 2, 3, 5, 13];
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().take_while(|&x| *x < 15);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
2014-07-21 22:54:28 -05:00
|
|
|
for x in it {
|
|
|
|
assert_eq!(*x, ys[i]);
|
2014-06-28 15:57:36 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_skip_while() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
2014-06-28 15:57:36 -05:00
|
|
|
let ys = [15, 16, 17, 19];
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().skip_while(|&x| *x < 15);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
2014-07-21 22:54:28 -05:00
|
|
|
for x in it {
|
|
|
|
assert_eq!(*x, ys[i]);
|
2014-06-28 15:57:36 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_skip() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
|
2014-06-28 15:57:36 -05:00
|
|
|
let ys = [13, 15, 16, 17, 19, 20, 30];
|
|
|
|
let mut it = xs.iter().skip(5);
|
|
|
|
let mut i = 0;
|
2015-01-21 15:38:50 -06:00
|
|
|
while let Some(&x) = it.next() {
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), xs.len()-5-i);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_take() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
|
|
|
let ys = [0, 1, 2, 3, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().take(5);
|
|
|
|
let mut i = 0;
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 5);
|
|
|
|
while let Some(&x) = it.next() {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
assert_eq!(it.len(), 5-i);
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_take_short() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3];
|
|
|
|
let ys = [0, 1, 2, 3];
|
2015-01-21 15:38:50 -06:00
|
|
|
let mut it = xs.iter().take(5);
|
|
|
|
let mut i = 0;
|
|
|
|
assert_eq!(it.len(), 4);
|
|
|
|
while let Some(&x) = it.next() {
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 4-i);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_scan() {
|
|
|
|
// test the type inference
|
|
|
|
fn add(old: &mut int, new: &uint) -> Option<f64> {
|
|
|
|
*old += *new as int;
|
|
|
|
Some(*old as f64)
|
|
|
|
}
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 1, 2, 3, 4];
|
2014-06-28 15:57:36 -05:00
|
|
|
let ys = [0f64, 1.0, 3.0, 6.0, 10.0];
|
|
|
|
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().scan(0, add);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for x in it {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_flat_map() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [0, 3, 6];
|
|
|
|
let ys = [0, 1, 2, 3, 4, 5, 6, 7, 8];
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = xs.iter().flat_map(|&x| count(x, 1).take(3));
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for x in it {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_inspect() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let xs = [1, 2, 3, 4];
|
|
|
|
let mut n = 0;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let ys = xs.iter()
|
2015-02-14 23:19:50 -06:00
|
|
|
.cloned()
|
2014-06-28 15:57:36 -05:00
|
|
|
.inspect(|_| n += 1)
|
|
|
|
.collect::<Vec<uint>>();
|
|
|
|
|
|
|
|
assert_eq!(n, xs.len());
|
2015-02-18 13:48:57 -06:00
|
|
|
assert_eq!(&xs[..], &ys[..]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unfoldr() {
|
|
|
|
fn count(st: &mut uint) -> Option<uint> {
|
|
|
|
if *st < 10 {
|
|
|
|
let ret = Some(*st);
|
|
|
|
*st += 1;
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 17:11:05 -06:00
|
|
|
let it = Unfold::new(0, count);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for counted in it {
|
|
|
|
assert_eq!(counted, i);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cycle() {
|
|
|
|
let cycle_len = 3;
|
2015-02-03 07:50:52 -06:00
|
|
|
let it = count(0, 1).take(cycle_len).cycle();
|
2015-02-26 18:43:55 -06:00
|
|
|
assert_eq!(it.size_hint(), (usize::MAX, None));
|
2014-06-28 15:57:36 -05:00
|
|
|
for (i, x) in it.take(100).enumerate() {
|
|
|
|
assert_eq!(i % cycle_len, x);
|
|
|
|
}
|
|
|
|
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut it = count(0, 1).take(0).cycle();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_nth() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4];
|
2015-02-03 07:50:52 -06:00
|
|
|
for i in 0..v.len() {
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
|
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
assert_eq!(v.iter().nth(v.len()), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_last() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(v.iter().last().unwrap(), &4);
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(v[..1].iter().last().unwrap(), &0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_len() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(v[..4].iter().count(), 4);
|
|
|
|
assert_eq!(v[..10].iter().count(), 10);
|
2015-01-12 15:59:18 -06:00
|
|
|
assert_eq!(v[..0].iter().count(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_sum() {
|
2015-01-30 14:26:44 -06:00
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-14 23:19:50 -06:00
|
|
|
assert_eq!(v[..4].iter().cloned().sum(), 6);
|
|
|
|
assert_eq!(v.iter().cloned().sum(), 55);
|
|
|
|
assert_eq!(v[..0].iter().cloned().sum(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_product() {
|
2015-01-30 14:26:44 -06:00
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-14 23:19:50 -06:00
|
|
|
assert_eq!(v[..4].iter().cloned().product(), 0);
|
|
|
|
assert_eq!(v[1..5].iter().cloned().product(), 24);
|
|
|
|
assert_eq!(v[..0].iter().cloned().product(), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_max() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-14 23:19:50 -06:00
|
|
|
assert_eq!(v[..4].iter().cloned().max(), Some(3));
|
|
|
|
assert_eq!(v.iter().cloned().max(), Some(10));
|
|
|
|
assert_eq!(v[..0].iter().cloned().max(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_min() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-14 23:19:50 -06:00
|
|
|
assert_eq!(v[..4].iter().cloned().min(), Some(0));
|
|
|
|
assert_eq!(v.iter().cloned().min(), Some(0));
|
|
|
|
assert_eq!(v[..0].iter().cloned().min(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_size_hint() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let c = count(0, 1);
|
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
|
|
let v2 = &[10, 11, 12];
|
2014-06-28 15:57:36 -05:00
|
|
|
let vi = v.iter();
|
|
|
|
|
2015-02-26 18:43:55 -06:00
|
|
|
assert_eq!(c.size_hint(), (usize::MAX, None));
|
2015-01-23 09:54:32 -06:00
|
|
|
assert_eq!(vi.clone().size_hint(), (10, Some(10)));
|
|
|
|
|
|
|
|
assert_eq!(c.clone().take(5).size_hint(), (5, Some(5)));
|
|
|
|
assert_eq!(c.clone().skip(5).size_hint().1, None);
|
|
|
|
assert_eq!(c.clone().take_while(|_| false).size_hint(), (0, None));
|
|
|
|
assert_eq!(c.clone().skip_while(|_| false).size_hint(), (0, None));
|
2015-02-26 18:43:55 -06:00
|
|
|
assert_eq!(c.clone().enumerate().size_hint(), (usize::MAX, None));
|
|
|
|
assert_eq!(c.clone().chain(vi.clone().cloned()).size_hint(), (usize::MAX, None));
|
2015-01-23 09:54:32 -06:00
|
|
|
assert_eq!(c.clone().zip(vi.clone()).size_hint(), (10, Some(10)));
|
2015-01-31 10:23:42 -06:00
|
|
|
assert_eq!(c.clone().scan(0, |_,_| Some(0)).size_hint(), (0, None));
|
2015-01-23 09:54:32 -06:00
|
|
|
assert_eq!(c.clone().filter(|_| false).size_hint(), (0, None));
|
2015-02-26 18:43:55 -06:00
|
|
|
assert_eq!(c.clone().map(|_| 0).size_hint(), (usize::MAX, None));
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(c.filter_map(|_| Some(0)).size_hint(), (0, None));
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-01-23 09:54:32 -06:00
|
|
|
assert_eq!(vi.clone().take(5).size_hint(), (5, Some(5)));
|
|
|
|
assert_eq!(vi.clone().take(12).size_hint(), (10, Some(10)));
|
|
|
|
assert_eq!(vi.clone().skip(3).size_hint(), (7, Some(7)));
|
|
|
|
assert_eq!(vi.clone().skip(12).size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(vi.clone().take_while(|_| false).size_hint(), (0, Some(10)));
|
|
|
|
assert_eq!(vi.clone().skip_while(|_| false).size_hint(), (0, Some(10)));
|
|
|
|
assert_eq!(vi.clone().enumerate().size_hint(), (10, Some(10)));
|
|
|
|
assert_eq!(vi.clone().chain(v2.iter()).size_hint(), (13, Some(13)));
|
|
|
|
assert_eq!(vi.clone().zip(v2.iter()).size_hint(), (3, Some(3)));
|
2015-01-31 10:23:42 -06:00
|
|
|
assert_eq!(vi.clone().scan(0, |_,_| Some(0)).size_hint(), (0, Some(10)));
|
2015-01-23 09:54:32 -06:00
|
|
|
assert_eq!(vi.clone().filter(|_| false).size_hint(), (0, Some(10)));
|
|
|
|
assert_eq!(vi.clone().map(|&i| i+1).size_hint(), (10, Some(10)));
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(vi.filter_map(|_| Some(0)).size_hint(), (0, Some(10)));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_collect() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let a = vec![1, 2, 3, 4, 5];
|
2015-02-14 23:19:50 -06:00
|
|
|
let b: Vec<int> = a.iter().cloned().collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_all() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: Box<[int]> = box [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().all(|&x| x < 10));
|
|
|
|
assert!(!v.iter().all(|&x| x % 2 == 0));
|
|
|
|
assert!(!v.iter().all(|&x| x > 100));
|
2015-01-12 15:59:18 -06:00
|
|
|
assert!(v[..0].iter().all(|_| panic!()));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_any() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: Box<[int]> = box [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().any(|&x| x < 10));
|
|
|
|
assert!(v.iter().any(|&x| x % 2 == 0));
|
|
|
|
assert!(!v.iter().any(|&x| x > 100));
|
2015-01-12 15:59:18 -06:00
|
|
|
assert!(!v[..0].iter().any(|_| panic!()));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v: &[int] = &[1, 3, 9, 27, 103, 14, 11];
|
2014-10-31 04:40:15 -05:00
|
|
|
assert_eq!(*v.iter().find(|&&x| x & 1 == 0).unwrap(), 14);
|
|
|
|
assert_eq!(*v.iter().find(|&&x| x % 3 == 0).unwrap(), 3);
|
|
|
|
assert!(v.iter().find(|&&x| x % 12 == 0).is_none());
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_position() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = &[1, 3, 9, 27, 103, 14, 11];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(v.iter().position(|x| *x & 1 == 0).unwrap(), 5);
|
|
|
|
assert_eq!(v.iter().position(|x| *x % 3 == 0).unwrap(), 1);
|
|
|
|
assert!(v.iter().position(|x| *x % 12 == 0).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_count() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = &[1, 2, 2, 1, 5, 9, 0, 2];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(xs.iter().filter(|x| **x == 2).count(), 3);
|
|
|
|
assert_eq!(xs.iter().filter(|x| **x == 5).count(), 1);
|
|
|
|
assert_eq!(xs.iter().filter(|x| **x == 95).count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_by() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs: &[int] = &[-3, 0, 1, 5, -10];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_min_by() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs: &[int] = &[-3, 0, 1, 5, -10];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_by_ref() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut xs = 0..10;
|
2014-06-28 15:57:36 -05:00
|
|
|
// sum the first five values
|
|
|
|
let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
|
|
|
|
assert_eq!(partial_sum, 10);
|
|
|
|
assert_eq!(xs.next(), Some(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rev() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [2, 4, 6, 8, 10, 12, 14, 16];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter();
|
|
|
|
it.next();
|
|
|
|
it.next();
|
2015-02-14 23:19:50 -06:00
|
|
|
assert!(it.rev().cloned().collect::<Vec<int>>() ==
|
2014-06-28 15:57:36 -05:00
|
|
|
vec![16, 14, 12, 10, 8, 6]);
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:00:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_cloned() {
|
|
|
|
let xs = [2u8, 4, 6, 8];
|
|
|
|
|
|
|
|
let mut it = xs.iter().cloned();
|
|
|
|
assert_eq!(it.len(), 4);
|
|
|
|
assert_eq!(it.next(), Some(2));
|
|
|
|
assert_eq!(it.len(), 3);
|
|
|
|
assert_eq!(it.next(), Some(4));
|
|
|
|
assert_eq!(it.len(), 2);
|
|
|
|
assert_eq!(it.next_back(), Some(8));
|
|
|
|
assert_eq!(it.len(), 1);
|
|
|
|
assert_eq!(it.next_back(), Some(6));
|
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
}
|
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
#[test]
|
|
|
|
fn test_double_ended_map() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5, 6];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().map(|&x| x * -1);
|
|
|
|
assert_eq!(it.next(), Some(-1));
|
|
|
|
assert_eq!(it.next(), Some(-2));
|
|
|
|
assert_eq!(it.next_back(), Some(-6));
|
|
|
|
assert_eq!(it.next_back(), Some(-5));
|
|
|
|
assert_eq!(it.next(), Some(-3));
|
|
|
|
assert_eq!(it.next_back(), Some(-4));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_enumerate() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5, 6];
|
2015-02-14 23:19:50 -06:00
|
|
|
let mut it = xs.iter().cloned().enumerate();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next(), Some((0, 1)));
|
|
|
|
assert_eq!(it.next(), Some((1, 2)));
|
|
|
|
assert_eq!(it.next_back(), Some((5, 6)));
|
|
|
|
assert_eq!(it.next_back(), Some((4, 5)));
|
|
|
|
assert_eq!(it.next_back(), Some((3, 4)));
|
|
|
|
assert_eq!(it.next_back(), Some((2, 3)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_zip() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5, 6];
|
|
|
|
let ys = [1, 2, 3, 7];
|
2015-02-14 23:19:50 -06:00
|
|
|
let a = xs.iter().cloned();
|
|
|
|
let b = ys.iter().cloned();
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = a.zip(b);
|
|
|
|
assert_eq!(it.next(), Some((1, 1)));
|
|
|
|
assert_eq!(it.next(), Some((2, 2)));
|
|
|
|
assert_eq!(it.next_back(), Some((4, 7)));
|
|
|
|
assert_eq!(it.next_back(), Some((3, 3)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_filter() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5, 6];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().filter(|&x| *x & 1 == 0);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &6);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &4);
|
|
|
|
assert_eq!(it.next().unwrap(), &2);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_filter_map() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5, 6];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().filter_map(|&x| if x & 1 == 0 { Some(x * 2) } else { None });
|
|
|
|
assert_eq!(it.next_back().unwrap(), 12);
|
|
|
|
assert_eq!(it.next_back().unwrap(), 8);
|
|
|
|
assert_eq!(it.next().unwrap(), 4);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_chain() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
|
|
|
let ys = [7, 9, 11];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().chain(ys.iter()).rev();
|
2014-11-14 11:18:10 -06:00
|
|
|
assert_eq!(it.next().unwrap(), &11);
|
|
|
|
assert_eq!(it.next().unwrap(), &9);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &1);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &2);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &3);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &4);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &5);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &7);
|
|
|
|
assert_eq!(it.next_back(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rposition() {
|
|
|
|
fn f(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'b' }
|
|
|
|
fn g(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'd' }
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(v.iter().rposition(f), Some(3));
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().rposition(g).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn test_rposition_panic() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [(box 0, box 0), (box 0, box 0),
|
|
|
|
(box 0, box 0), (box 0, box 0)];
|
|
|
|
let mut i = 0;
|
2014-06-28 15:57:36 -05:00
|
|
|
v.iter().rposition(|_elt| {
|
|
|
|
if i == 2 {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!()
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-01-02 13:34:34 -06:00
|
|
|
fn check_randacc_iter<A, T>(a: T, len: uint) where
|
|
|
|
A: PartialEq,
|
|
|
|
T: Clone + RandomAccessIterator + Iterator<Item=A>,
|
2014-06-28 15:57:36 -05:00
|
|
|
{
|
|
|
|
let mut b = a.clone();
|
|
|
|
assert_eq!(len, b.indexable());
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut n = 0;
|
2014-06-28 15:57:36 -05:00
|
|
|
for (i, elt) in a.enumerate() {
|
|
|
|
assert!(Some(elt) == b.idx(i));
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(n, len);
|
|
|
|
assert!(None == b.idx(n));
|
|
|
|
// call recursively to check after picking off an element
|
|
|
|
if len > 0 {
|
|
|
|
b.next();
|
|
|
|
check_randacc_iter(b, len-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_flat_map() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let u = [0,1];
|
|
|
|
let v = [5,6,7,8];
|
2015-01-19 10:07:13 -06:00
|
|
|
let mut it = u.iter().flat_map(|x| v[*x..v.len()].iter());
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(it.next_back().unwrap(), &8);
|
|
|
|
assert_eq!(it.next().unwrap(), &5);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &7);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &6);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &8);
|
|
|
|
assert_eq!(it.next().unwrap(), &6);
|
|
|
|
assert_eq!(it.next_back().unwrap(), &7);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_chain() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
|
|
|
let ys = [7, 9, 11];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = xs.iter().chain(ys.iter());
|
|
|
|
assert_eq!(it.idx(0).unwrap(), &1);
|
|
|
|
assert_eq!(it.idx(5).unwrap(), &7);
|
|
|
|
assert_eq!(it.idx(7).unwrap(), &11);
|
|
|
|
assert!(it.idx(8).is_none());
|
|
|
|
|
|
|
|
it.next();
|
|
|
|
it.next();
|
|
|
|
it.next_back();
|
|
|
|
|
|
|
|
assert_eq!(it.idx(0).unwrap(), &3);
|
|
|
|
assert_eq!(it.idx(4).unwrap(), &9);
|
|
|
|
assert!(it.idx(6).is_none());
|
|
|
|
|
|
|
|
check_randacc_iter(it, xs.len() + ys.len() - 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_enumerate() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().enumerate(), xs.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_rev() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().rev(), xs.len());
|
|
|
|
let mut it = xs.iter().rev();
|
|
|
|
it.next();
|
|
|
|
it.next_back();
|
|
|
|
it.next();
|
|
|
|
check_randacc_iter(it, xs.len() - 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_zip() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
|
|
|
let ys = [7, 9, 11];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().zip(ys.iter()), cmp::min(xs.len(), ys.len()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_take() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-11-17 02:39:01 -06:00
|
|
|
let empty: &[int] = &[];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().take(3), 3);
|
|
|
|
check_randacc_iter(xs.iter().take(20), xs.len());
|
|
|
|
check_randacc_iter(xs.iter().take(0), 0);
|
|
|
|
check_randacc_iter(empty.iter().take(2), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_skip() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-11-17 02:39:01 -06:00
|
|
|
let empty: &[int] = &[];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().skip(2), xs.len() - 2);
|
|
|
|
check_randacc_iter(empty.iter().skip(2), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_inspect() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
// test .map and .inspect that don't implement Clone
|
|
|
|
let mut it = xs.iter().inspect(|_| {});
|
|
|
|
assert_eq!(xs.len(), it.indexable());
|
|
|
|
for (i, elt) in xs.iter().enumerate() {
|
|
|
|
assert_eq!(Some(elt), it.idx(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_map() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-02-13 01:33:44 -06:00
|
|
|
let mut it = xs.iter().cloned();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(xs.len(), it.indexable());
|
|
|
|
for (i, elt) in xs.iter().enumerate() {
|
|
|
|
assert_eq!(Some(*elt), it.idx(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_random_access_cycle() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let xs = [1, 2, 3, 4, 5];
|
2014-11-17 02:39:01 -06:00
|
|
|
let empty: &[int] = &[];
|
2014-06-28 15:57:36 -05:00
|
|
|
check_randacc_iter(xs.iter().cycle().take(27), 27);
|
|
|
|
check_randacc_iter(empty.iter().cycle(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_ended_range() {
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!((11..14).rev().collect::<Vec<_>>(), [13, 12, 11]);
|
2015-01-25 15:05:03 -06:00
|
|
|
for _ in (10..0).rev() {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("unreachable");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!((11..14).rev().collect::<Vec<_>>(), [13, 12, 11]);
|
2015-01-30 14:26:44 -06:00
|
|
|
for _ in (10..0).rev() {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("unreachable");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range() {
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!((0..5).collect::<Vec<_>>(), [0, 1, 2, 3, 4]);
|
|
|
|
assert_eq!((-10..-1).collect::<Vec<_>>(), [-10, -9, -8, -7, -6, -5, -4, -3, -2]);
|
|
|
|
assert_eq!((0..5).rev().collect::<Vec<_>>(), [4, 3, 2, 1, 0]);
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!((200..-5).count(), 0);
|
|
|
|
assert_eq!((200..-5).rev().count(), 0);
|
|
|
|
assert_eq!((200..200).count(), 0);
|
|
|
|
assert_eq!((200..200).rev().count(), 0);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!((0..100).size_hint(), (100, Some(100)));
|
2014-06-28 15:57:36 -05:00
|
|
|
// this test is only meaningful when sizeof uint < sizeof u64
|
2015-02-26 18:43:55 -06:00
|
|
|
assert_eq!((usize::MAX - 1..usize::MAX).size_hint(), (1, Some(1)));
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!((-10..-1).size_hint(), (9, Some(9)));
|
2015-02-07 17:17:04 -06:00
|
|
|
assert_eq!((-1..-10).size_hint(), (0, Some(0)));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_inclusive() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert!(range_inclusive(0, 5).collect::<Vec<int>>() ==
|
|
|
|
vec![0, 1, 2, 3, 4, 5]);
|
|
|
|
assert!(range_inclusive(0, 5).rev().collect::<Vec<int>>() ==
|
|
|
|
vec![5, 4, 3, 2, 1, 0]);
|
|
|
|
assert_eq!(range_inclusive(200, -5).count(), 0);
|
|
|
|
assert_eq!(range_inclusive(200, -5).rev().count(), 0);
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(range_inclusive(200, 200).collect::<Vec<int>>(), [200]);
|
|
|
|
assert_eq!(range_inclusive(200, 200).rev().collect::<Vec<int>>(), [200]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_step() {
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(range_step(0, 20, 5).collect::<Vec<int>>(), [0, 5, 10, 15]);
|
|
|
|
assert_eq!(range_step(20, 0, -5).collect::<Vec<int>>(), [20, 15, 10, 5]);
|
|
|
|
assert_eq!(range_step(20, 0, -6).collect::<Vec<int>>(), [20, 14, 8, 2]);
|
|
|
|
assert_eq!(range_step(200u8, 255, 50).collect::<Vec<u8>>(), [200u8, 250]);
|
|
|
|
assert_eq!(range_step(200i, -5, 1).collect::<Vec<int>>(), []);
|
|
|
|
assert_eq!(range_step(200i, 200, 1).collect::<Vec<int>>(), []);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_range_step_inclusive() {
|
2015-02-24 12:15:45 -06:00
|
|
|
assert_eq!(range_step_inclusive(0, 20, 5).collect::<Vec<int>>(), [0, 5, 10, 15, 20]);
|
|
|
|
assert_eq!(range_step_inclusive(20, 0, -5).collect::<Vec<int>>(), [20, 15, 10, 5, 0]);
|
|
|
|
assert_eq!(range_step_inclusive(20, 0, -6).collect::<Vec<int>>(), [20, 14, 8, 2]);
|
|
|
|
assert_eq!(range_step_inclusive(200u8, 255, 50).collect::<Vec<u8>>(), [200u8, 250]);
|
|
|
|
assert_eq!(range_step_inclusive(200, -5, 1).collect::<Vec<int>>(), []);
|
|
|
|
assert_eq!(range_step_inclusive(200, 200, 1).collect::<Vec<int>>(), [200]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reverse() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut ys = [1, 2, 3, 4, 5];
|
2015-01-05 21:13:38 -06:00
|
|
|
ys.iter_mut().reverse_in_place();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(ys == [5, 4, 3, 2, 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_peekable_is_empty() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let a = [1];
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut it = a.iter().peekable();
|
|
|
|
assert!( !it.is_empty() );
|
|
|
|
it.next();
|
|
|
|
assert!( it.is_empty() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_min_max() {
|
2014-12-30 02:19:41 -06:00
|
|
|
let v: [int; 0] = [];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(v.iter().min_max(), NoElements);
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [1];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().min_max() == OneElement(&1));
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [1, 2, 3, 4, 5];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().min_max() == MinMax(&1, &5));
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [1, 2, 3, 4, 5, 6];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().min_max() == MinMax(&1, &6));
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let v = [1, 1, 1, 1];
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v.iter().min_max() == MinMax(&1, &1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_min_max_result() {
|
|
|
|
let r: MinMaxResult<int> = NoElements;
|
2014-11-14 11:18:10 -06:00
|
|
|
assert_eq!(r.into_option(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let r = OneElement(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(r.into_option(), Some((1,1)));
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let r = MinMax(1,2);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(r.into_option(), Some((1,2)));
|
|
|
|
}
|
2014-07-06 17:50:53 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterate() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut it = iterate(1, |x| x * 2);
|
|
|
|
assert_eq!(it.next(), Some(1));
|
|
|
|
assert_eq!(it.next(), Some(2));
|
|
|
|
assert_eq!(it.next(), Some(4));
|
|
|
|
assert_eq!(it.next(), Some(8));
|
2014-07-06 17:50:53 -05:00
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
|
2014-10-30 17:44:42 -05:00
|
|
|
#[test]
|
|
|
|
fn test_repeat() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut it = repeat(42);
|
|
|
|
assert_eq!(it.next(), Some(42));
|
|
|
|
assert_eq!(it.next(), Some(42));
|
|
|
|
assert_eq!(it.next(), Some(42));
|
2014-10-30 17:44:42 -05:00
|
|
|
}
|
|
|
|
|
2015-01-21 15:38:50 -06:00
|
|
|
#[test]
|
|
|
|
fn test_fuse() {
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut it = 0..3;
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 3);
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(it.next(), Some(0));
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 2);
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(it.next(), Some(1));
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 1);
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(it.next(), Some(2));
|
2015-01-21 15:38:50 -06:00
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
assert_eq!(it.len(), 0);
|
|
|
|
}
|
|
|
|
|
2014-08-06 05:20:37 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_rposition(b: &mut Bencher) {
|
2015-02-03 07:50:52 -06:00
|
|
|
let it: Vec<uint> = (0..300).collect();
|
2014-08-06 05:20:37 -05:00
|
|
|
b.iter(|| {
|
|
|
|
it.iter().rposition(|&x| x <= 150);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_skip_while(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2015-02-03 07:50:52 -06:00
|
|
|
let it = 0..100;
|
2014-08-06 05:20:37 -05:00
|
|
|
let mut sum = 0;
|
|
|
|
it.skip_while(|&x| { sum += x; sum < 4000 }).all(|_| true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_multiple_take(b: &mut Bencher) {
|
2015-02-03 07:50:52 -06:00
|
|
|
let mut it = (0..42).cycle();
|
2014-08-06 05:20:37 -05:00
|
|
|
b.iter(|| {
|
|
|
|
let n = it.next().unwrap();
|
2015-02-03 07:50:52 -06:00
|
|
|
for _ in 0..n {
|
2015-01-23 09:54:32 -06:00
|
|
|
it.clone().take(it.next().unwrap()).all(|_| true);
|
2014-08-06 05:20:37 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|