Skip to content

Commit

Permalink
Add tests and fix edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Aug 17, 2023
1 parent 8313dcc commit f59f68a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions radix_tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,18 @@ mod tests {
tree.push(i);
}

tree.truncate(50);
assert_eq!(tree.len(), 100);

tree.truncate(100);
assert_eq!(tree.len(), 100);

tree.truncate(101);
assert_eq!(tree.len(), 100);

tree.truncate(1000);
assert_eq!(tree.len(), 100);

tree.truncate(50);
assert_eq!(tree.len(), 50);
assert_eq!(tree.depth(), 3);

Expand All @@ -125,7 +135,6 @@ mod tests {
}

tree.truncate(1);

assert_eq!(tree.len(), 1);
assert_eq!(tree.depth(), 1);
}
Expand Down
4 changes: 4 additions & 0 deletions radix_tree/src/radix_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ impl<T: Clone, const N: usize> RadixTree<T, N> {
for p in path {
match tree.data_mut() {
RadixTreeData::Meta(meta) => {
if meta.len() <= p {
break;
}

meta.truncate(p + 1);
tree = &mut meta[p];
}
Expand Down

0 comments on commit f59f68a

Please sign in to comment.