Add tests

This commit is contained in:
Tim Vermeulen 2020-10-02 02:47:01 +02:00 committed by The8472
parent 2b89914962
commit 5e90909f38
2 changed files with 44 additions and 2 deletions

View File

@ -188,7 +188,7 @@ macro_rules! iterator {
#[inline]
fn advance_by(&mut self, n: usize) -> Result<(), usize>{
let advance = cmp::min(n, len!(self));
// SAFETY: `advance` does not exceed `self.len()` by construction
// SAFETY: By construction, `advance` does not exceed `self.len()`.
unsafe { self.post_inc_start(advance as isize) };
if advance == n { Ok(()) } else { Err(advance) }
}
@ -383,7 +383,7 @@ macro_rules! iterator {
#[inline]
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
let advance = cmp::min(n, len!(self));
// SAFETY: `advance` does not exceed `self.len()` by construction
// SAFETY: By construction, `advance` does not exceed `self.len()`.
unsafe { self.pre_dec_end(advance as isize) };
if advance == n { Ok(()) } else { Err(advance) }
}

View File

@ -134,6 +134,48 @@ fn test_partition_point() {
assert_eq!(b.partition_point(|&x| x < 8), 5);
}
#[test]
fn test_iterator_advance_by() {
let v = &[0, 1, 2, 3, 4];
for i in 0..=v.len() {
let mut iter = v.iter();
iter.advance_by(i).unwrap();
assert_eq!(iter.as_slice(), &v[i..]);
}
let mut iter = v.iter();
assert_eq!(iter.advance_by(v.len() + 1), Err(v.len()));
assert_eq!(iter.as_slice(), &[]);
let mut iter = v.iter();
iter.advance_by(3).unwrap();
assert_eq!(iter.as_slice(), &v[3..]);
iter.advance_by(2).unwrap();
assert_eq!(iter.as_slice(), &[]);
}
#[test]
fn test_iterator_advance_back_by() {
let v = &[0, 1, 2, 3, 4];
for i in 0..=v.len() {
let mut iter = v.iter();
iter.advance_back_by(i).unwrap();
assert_eq!(iter.as_slice(), &v[..v.len() - i]);
}
let mut iter = v.iter();
assert_eq!(iter.advance_back_by(v.len() + 1), Err(v.len()));
assert_eq!(iter.as_slice(), &[]);
let mut iter = v.iter();
iter.advance_back_by(3).unwrap();
assert_eq!(iter.as_slice(), &v[..v.len() - 3]);
iter.advance_back_by(2).unwrap();
assert_eq!(iter.as_slice(), &[]);
}
#[test]
fn test_iterator_nth() {
let v: &[_] = &[0, 1, 2, 3, 4];