Skip to content

Commit

Permalink
Add tests of slice indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed May 27, 2021
1 parent a3c2f69 commit 6004ebd
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,119 @@ impl<K, V> IndexMut<(Bound<usize>, Bound<usize>)> for Slice<K, V> {
Slice::from_mut_slice(&mut entries[range])
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;

#[test]
fn slice_index() {
fn check(
vec_slice: &[(i32, i32)],
map_slice: &Slice<i32, i32>,
sub_slice: &Slice<i32, i32>,
) {
assert_eq!(map_slice as *const _, sub_slice as *const _);
itertools::assert_equal(
vec_slice.iter().copied(),
map_slice.iter().map(|(&k, &v)| (k, v)),
);
itertools::assert_equal(vec_slice.iter().map(|(k, _)| k), map_slice.keys());
itertools::assert_equal(vec_slice.iter().map(|(_, v)| v), map_slice.values());
}

let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect();
let map: IndexMap<i32, i32> = vec.iter().cloned().collect();
let slice = map.as_slice();

// RangeFull
check(&vec[..], &map[..], &slice[..]);

for i in 0usize..10 {
// Index
assert_eq!(vec[i].1, map[i]);
assert_eq!(vec[i].1, slice[i]);
assert_eq!(map[&(i as i32)], map[i]);
assert_eq!(map[&(i as i32)], slice[i]);

// RangeFrom
check(&vec[i..], &map[i..], &slice[i..]);

// RangeTo
check(&vec[..i], &map[..i], &slice[..i]);

// RangeToInclusive
check(&vec[..=i], &map[..=i], &slice[..=i]);

// (Bound<usize>, Bound<usize>)
let bounds = (Bound::Excluded(i), Bound::Unbounded);
check(&vec[i + 1..], &map[bounds], &slice[bounds]);

for j in i..=10 {
// Range
check(&vec[i..j], &map[i..j], &slice[i..j]);
}

for j in i..10 {
// RangeInclusive
check(&vec[i..=j], &map[i..=j], &slice[i..=j]);
}
}
}

#[test]
fn slice_index_mut() {
fn check_mut(
vec_slice: &[(i32, i32)],
map_slice: &mut Slice<i32, i32>,
sub_slice: &mut Slice<i32, i32>,
) {
assert_eq!(map_slice, sub_slice);
itertools::assert_equal(
vec_slice.iter().copied(),
map_slice.iter_mut().map(|(&k, &mut v)| (k, v)),
);
itertools::assert_equal(
vec_slice.iter().map(|&(_, v)| v),
map_slice.values_mut().map(|&mut v| v),
);
}

let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect();
let mut map: IndexMap<i32, i32> = vec.iter().cloned().collect();
let mut map2 = map.clone();
let slice = map2.as_mut_slice();

// RangeFull
check_mut(&vec[..], &mut map[..], &mut slice[..]);

for i in 0usize..10 {
// IndexMut
assert_eq!(&mut map[i], &mut slice[i]);

// RangeFrom
check_mut(&vec[i..], &mut map[i..], &mut slice[i..]);

// RangeTo
check_mut(&vec[..i], &mut map[..i], &mut slice[..i]);

// RangeToInclusive
check_mut(&vec[..=i], &mut map[..=i], &mut slice[..=i]);

// (Bound<usize>, Bound<usize>)
let bounds = (Bound::Excluded(i), Bound::Unbounded);
check_mut(&vec[i + 1..], &mut map[bounds], &mut slice[bounds]);

for j in i..=10 {
// Range
check_mut(&vec[i..j], &mut map[i..j], &mut slice[i..j]);
}

for j in i..10 {
// RangeInclusive
check_mut(&vec[i..=j], &mut map[i..=j], &mut slice[i..=j]);
}
}
}
}
50 changes: 50 additions & 0 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,53 @@ impl<T> Index<(Bound<usize>, Bound<usize>)> for Slice<T> {
Slice::from_slice(&entries[range])
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec::Vec;

#[test]
fn slice_index() {
fn check(vec_slice: &[i32], set_slice: &Slice<i32>, sub_slice: &Slice<i32>) {
assert_eq!(set_slice as *const _, sub_slice as *const _);
itertools::assert_equal(vec_slice, set_slice);
}

let vec: Vec<i32> = (0..10).map(|i| i * i).collect();
let set: IndexSet<i32> = vec.iter().cloned().collect();
let slice = set.as_slice();

// RangeFull
check(&vec[..], &set[..], &slice[..]);

for i in 0usize..10 {
// Index
assert_eq!(vec[i], set[i]);
assert_eq!(vec[i], slice[i]);

// RangeFrom
check(&vec[i..], &set[i..], &slice[i..]);

// RangeTo
check(&vec[..i], &set[..i], &slice[..i]);

// RangeToInclusive
check(&vec[..=i], &set[..=i], &slice[..=i]);

// (Bound<usize>, Bound<usize>)
let bounds = (Bound::Excluded(i), Bound::Unbounded);
check(&vec[i + 1..], &set[bounds], &slice[bounds]);

for j in i..=10 {
// Range
check(&vec[i..j], &set[i..j], &slice[i..j]);
}

for j in i..10 {
// RangeInclusive
check(&vec[i..=j], &set[i..=j], &slice[i..=j]);
}
}
}
}

0 comments on commit 6004ebd

Please sign in to comment.