Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in split_before and split_after for first and last node edge cases #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,15 @@ impl<'a, T> CursorMut<'a, T> {

// What the output will become
let output_len = old_len - new_len;
let output_front = self.list.front;
// We might be at the first node, in which case we don't want to set a new list.front but return an empty list.
let mut output_front = None;
let output_back = prev;

// Break the links between cur and prev
if let Some(prev) = prev {
(*cur.as_ptr()).front = None;
(*prev.as_ptr()).back = None;
output_front = self.list.front
}

// Produce the result:
Expand Down Expand Up @@ -598,12 +600,14 @@ impl<'a, T> CursorMut<'a, T> {
// What the output will become
let output_len = old_len - new_len;
let output_front = next;
let output_back = self.list.back;
// We might be at the last node, in which case we don't want to set a new list.back but return an empty list.
let mut output_back = None;

// Break the links between cur and next
if let Some(next) = next {
(*cur.as_ptr()).back = None;
(*next.as_ptr()).front = None;
output_back = self.list.back;
}

// Produce the result:
Expand Down Expand Up @@ -1197,4 +1201,40 @@ mod test {

assert_eq!(from_front, re_reved);
}

#[test]
fn marker_split_before_first() {
let mut m: LinkedList<u32> = LinkedList::new();
m.extend([1, 2, 3, 4, 5, 6]);
let mut cursor = m.cursor_mut();
cursor.move_next();
assert_eq!(cursor.current(), Some(&mut 1));

let left = cursor.split_before();
assert!(left.is_empty());
assert!(left.front.is_none() && left.back.is_none());

assert_eq!(cursor.current(), Some(&mut 1));
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[1, 2, 3, 4, 5, 6]);
assert_eq!(m.len(), 6);
}

#[test]
fn split_after_last() {
let mut m: LinkedList<u32> = LinkedList::new();
m.extend([1, 2, 3, 4, 5, 6]);
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[1, 2, 3, 4, 5, 6]);
let mut cursor = m.cursor_mut();

cursor.move_prev();
assert_eq!(cursor.current(), Some(&mut 6));

let right = cursor.split_after();
assert!(right.is_empty());
assert!(right.front.is_none() && right.back.is_none());

assert_eq!(cursor.current(), Some(&mut 6));
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[1, 2, 3, 4, 5, 6]);
assert_eq!(m.len(), 6);
}
}