Skip to content

Commit

Permalink
test(cordyceps): use new impls in List tests (#232)
Browse files Browse the repository at this point in the history
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Jun 21, 2022
1 parent d9bec37 commit d14d4cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
26 changes: 12 additions & 14 deletions cordyceps/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ fn push_all<'a>(
list: &mut List<Entry<'a>>,
entries: impl IntoIterator<Item = &'a Pin<Box<Entry<'a>>>>,
) {
for entry in entries.into_iter() {
list.push_back(entry.as_ref());
}
list.extend(entries.into_iter().map(Pin::as_ref))
}

fn list_from_iter<'a>(
entries: impl IntoIterator<Item = &'a Pin<Box<Entry<'a>>>>,
) -> List<Entry<'a>> {
let mut list = List::new();
push_all(&mut list, entries);
list
}

macro_rules! assert_clean {
Expand Down Expand Up @@ -230,10 +236,7 @@ fn push_pop_push_pop() {
#[test]
fn double_ended_iter() {
let entries = [entry(1), entry(2), entry(3)];

let mut list = List::new();

push_all(&mut list, &entries);
let list = list_from_iter(&entries);

let head_to_tail = list.iter().map(|entry| entry.val).collect::<Vec<_>>();
assert_eq!(&head_to_tail, &[1, 2, 3]);
Expand All @@ -250,9 +253,7 @@ fn double_ended_iter() {
fn double_ended_iter_empties() {
let entries = [entry(1), entry(2), entry(3), entry(4)];

let mut list = List::new();

push_all(&mut list, &entries);
let list = list_from_iter(&entries);

let mut iter = list.iter();

Expand All @@ -269,10 +270,7 @@ fn double_ended_iter_empties() {
#[test]
fn drain_filter() {
let entries = [entry(1), entry(2), entry(3), entry(4)];

let mut list = List::new();

push_all(&mut list, &entries);
let mut list = list_from_iter(&entries);

{
// Create a scope so that the mutable borrow on the list is released
Expand Down
6 changes: 2 additions & 4 deletions cordyceps/src/list/tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ fn move_peek() {
let _ = super::trace_init();

let entries = [entry(1), entry(2), entry(3), entry(4), entry(5), entry(6)];
let mut list = list_from_iter(&entries);

let mut list = List::new();
push_all(&mut list, &entries);
let mut cursor = list.cursor_front_mut();
assert_eq!(val(cursor.current()), Some(1));
assert_eq!(val(cursor.peek_next()), Some(2));
Expand Down Expand Up @@ -111,8 +110,7 @@ fn cursor_mut_insert() {
let nine = entry(9);
let ten = entry(10);

let mut list = List::<Entry<'_>>::new();
push_all(&mut list, &entries);
let mut list = list_from_iter(&entries);

let mut cursor = list.cursor_front_mut();
cursor.insert_before(seven.as_ref());
Expand Down
35 changes: 15 additions & 20 deletions cordyceps/src/list/tests/remove_by_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ fn first() {

unsafe {
// Remove first
let mut list = List::new();
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

push_all(&mut list, vec![&a, &b, &c]);
assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
list.assert_valid();
Expand Down Expand Up @@ -41,9 +42,7 @@ fn first() {

unsafe {
// Remove first of two
let mut list = List::new();

push_all(&mut list, vec![&a, &b]);
let mut list = [a.as_ref(), b.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand Down Expand Up @@ -73,9 +72,9 @@ fn middle() {
let c = entry(31);

unsafe {
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand All @@ -91,9 +90,9 @@ fn middle() {
}

unsafe {
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&b)).is_some());
assert_clean!(b);
Expand All @@ -118,9 +117,9 @@ fn last_middle() {
unsafe {
// Remove last
// Remove middle
let mut list = List::new();

push_all(&mut list, vec![&a, &b, &c]);
let mut list = [a.as_ref(), b.as_ref(), c.as_ref()]
.into_iter()
.collect::<List<_>>();

assert!(list.remove(ptr(&c)).is_some());
assert_clean!(c);
Expand All @@ -143,9 +142,7 @@ fn last() {

unsafe {
// Remove last item
let mut list = List::new();

push_all(&mut list, Some(&a));
let mut list = [a.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&a)).is_some());
assert_clean!(a);
Expand All @@ -159,9 +156,7 @@ fn last() {

unsafe {
// Remove last of two
let mut list = List::new();

push_all(&mut list, vec![&a, &b]);
let mut list = [a.as_ref(), b.as_ref()].into_iter().collect::<List<_>>();

assert!(list.remove(ptr(&b)).is_some());
assert_clean!(b);
Expand Down

0 comments on commit d14d4cb

Please sign in to comment.