Skip to content

Commit

Permalink
Fix size bug and add test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
dspyz-matician committed Jul 9, 2024
1 parent 89eab3a commit a95df8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,11 +954,10 @@ impl<T> IndexList<T> {
}
#[inline]
fn remove_elem_at_index(&mut self, this: ListIndex) -> Option<T> {
this.get()
.and_then(|at| {
self.size -= 1;
self.elems[at].take()
})
let at = this.get()?;
let removed = self.elems[at].take()?;
self.size -= 1;
Some(removed)
}
fn new_node(&mut self, elem: Option<T>) -> ListIndex {
let reuse = self.free.head;
Expand Down
10 changes: 10 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ fn test_single_element() {
assert_eq!(list.len(), 0);
}
#[test]
fn test_remove_element_twice() {
let mut list = IndexList::<u64>::new();
let index = list.insert_first(0);
let removed1 = list.remove(index);
assert_eq!(removed1, Some(0));
let removed2 = list.remove(index);
assert_eq!(removed2, None);
assert_eq!(list.len(), 0);
}
#[test]
fn insert_remove_variants() {
let count = 256;
let mut rng = rand::thread_rng();
Expand Down

0 comments on commit a95df8b

Please sign in to comment.