2014-08-06 22:48:25 -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.
|
|
|
|
|
2014-12-30 12:51:18 -06:00
|
|
|
use core::result::Result::{Ok, Err};
|
2014-08-06 22:48:25 -05:00
|
|
|
|
|
|
|
#[test]
|
2016-01-14 14:05:00 -06:00
|
|
|
fn test_binary_search() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 6, 8, 9];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&6)) == Ok(3));
|
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&5)) == Err(3));
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 6, 7, 8, 9];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&6)) == Ok(3));
|
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&5)) == Err(3));
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 6, 8, 9];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&8)) == Ok(4));
|
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&7)) == Err(4));
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 6, 7, 8, 9];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&8)) == Ok(5));
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 5, 6, 8, 9];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&7)) == Err(5));
|
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&0)) == Err(0));
|
2015-01-25 15:05:03 -06:00
|
|
|
let b = [1, 2, 4, 5, 6, 8];
|
2014-12-30 12:51:18 -06:00
|
|
|
assert!(b.binary_search_by(|v| v.cmp(&9)) == Err(6));
|
2014-08-06 22:48:25 -05:00
|
|
|
}
|
2014-11-14 21:44:55 -06:00
|
|
|
|
2015-04-22 15:03:56 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_nth() {
|
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4];
|
|
|
|
for i in 0..v.len() {
|
|
|
|
assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
|
|
|
|
}
|
|
|
|
assert_eq!(v.iter().nth(v.len()), None);
|
|
|
|
|
|
|
|
let mut iter = v.iter();
|
|
|
|
assert_eq!(iter.nth(2).unwrap(), &v[2]);
|
|
|
|
assert_eq!(iter.nth(1).unwrap(), &v[4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_last() {
|
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4];
|
|
|
|
assert_eq!(v.iter().last().unwrap(), &4);
|
|
|
|
assert_eq!(v[..1].iter().last().unwrap(), &0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_count() {
|
|
|
|
let v: &[_] = &[0, 1, 2, 3, 4];
|
|
|
|
assert_eq!(v.iter().count(), 5);
|
|
|
|
|
|
|
|
let mut iter2 = v.iter();
|
|
|
|
iter2.next();
|
|
|
|
iter2.next();
|
|
|
|
assert_eq!(iter2.count(), 3);
|
|
|
|
}
|
2015-08-07 02:10:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_count() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.chunks(3);
|
|
|
|
assert_eq!(c.count(), 2);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.chunks(2);
|
|
|
|
assert_eq!(c2.count(), 3);
|
|
|
|
|
|
|
|
let v3: &[i32] = &[];
|
|
|
|
let c3 = v3.chunks(2);
|
|
|
|
assert_eq!(c3.count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_nth() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let mut c = v.chunks(2);
|
|
|
|
assert_eq!(c.nth(1).unwrap()[1], 3);
|
|
|
|
assert_eq!(c.next().unwrap()[0], 4);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let mut c2 = v2.chunks(3);
|
|
|
|
assert_eq!(c2.nth(1).unwrap()[1], 4);
|
|
|
|
assert_eq!(c2.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_last() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.chunks(2);
|
|
|
|
assert_eq!(c.last().unwrap()[1], 5);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.chunks(2);
|
|
|
|
assert_eq!(c2.last().unwrap()[0], 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_mut_count() {
|
|
|
|
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.chunks_mut(3);
|
|
|
|
assert_eq!(c.count(), 2);
|
|
|
|
|
|
|
|
let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.chunks_mut(2);
|
|
|
|
assert_eq!(c2.count(), 3);
|
|
|
|
|
|
|
|
let mut v3: &mut [i32] = &mut [];
|
|
|
|
let c3 = v3.chunks_mut(2);
|
|
|
|
assert_eq!(c3.count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_mut_nth() {
|
|
|
|
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
|
|
|
let mut c = v.chunks_mut(2);
|
|
|
|
assert_eq!(c.nth(1).unwrap()[1], 3);
|
|
|
|
assert_eq!(c.next().unwrap()[0], 4);
|
|
|
|
|
|
|
|
let mut v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
|
|
|
let mut c2 = v2.chunks_mut(3);
|
|
|
|
assert_eq!(c2.nth(1).unwrap()[1], 4);
|
|
|
|
assert_eq!(c2.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunks_mut_last() {
|
|
|
|
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.chunks_mut(2);
|
|
|
|
assert_eq!(c.last().unwrap()[1], 5);
|
|
|
|
|
|
|
|
let v2: &mut [i32] = &mut [0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.chunks_mut(2);
|
|
|
|
assert_eq!(c2.last().unwrap()[0], 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_windows_count() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.windows(3);
|
|
|
|
assert_eq!(c.count(), 4);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.windows(6);
|
|
|
|
assert_eq!(c2.count(), 0);
|
|
|
|
|
|
|
|
let v3: &[i32] = &[];
|
|
|
|
let c3 = v3.windows(2);
|
|
|
|
assert_eq!(c3.count(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_windows_nth() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let mut c = v.windows(2);
|
|
|
|
assert_eq!(c.nth(2).unwrap()[1], 3);
|
|
|
|
assert_eq!(c.next().unwrap()[0], 3);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let mut c2 = v2.windows(4);
|
|
|
|
assert_eq!(c2.nth(1).unwrap()[1], 2);
|
|
|
|
assert_eq!(c2.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_windows_last() {
|
|
|
|
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
|
|
|
|
let c = v.windows(2);
|
|
|
|
assert_eq!(c.last().unwrap()[1], 5);
|
|
|
|
|
|
|
|
let v2: &[i32] = &[0, 1, 2, 3, 4];
|
|
|
|
let c2 = v2.windows(2);
|
|
|
|
assert_eq!(c2.last().unwrap()[0], 3);
|
|
|
|
}
|