Skip to content

Commit

Permalink
Add some tests for remove_prefix (#2714)
Browse files Browse the repository at this point in the history
Adds some tests regarding the code added in #2663

I wanted to maybe figure out why #2666 was failing, but didn't manage to find a failing case.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tomaka and mergify[bot] authored Sep 5, 2022
1 parent 6a20354 commit 52300a1
Showing 1 changed file with 281 additions and 0 deletions.
281 changes: 281 additions & 0 deletions src/trie/trie_structure/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,287 @@ fn insert_branch() {
.has_storage_value());
}

#[test]
fn remove_prefix_basic() {
let mut trie = TrieStructure::new();

trie.node([Nibble::try_from(1).unwrap()].iter().cloned())
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(4).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(4).unwrap(),
Nibble::try_from(5).unwrap(),
Nibble::try_from(6).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.remove_prefix(
[Nibble::try_from(1).unwrap(), Nibble::try_from(2).unwrap()]
.iter()
.cloned(),
);

assert_eq!(trie.len(), 1);
assert!(trie
.node([Nibble::try_from(1).unwrap(),].iter().cloned(),)
.into_occupied()
.unwrap()
.has_storage_value());
}

#[test]
fn remove_prefix_clear_all() {
let mut trie = TrieStructure::new();

trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(4).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(4).unwrap(),
Nibble::try_from(5).unwrap(),
Nibble::try_from(6).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.remove_prefix(
[Nibble::try_from(1).unwrap(), Nibble::try_from(2).unwrap()]
.iter()
.cloned(),
);

assert!(trie.is_empty());
}

#[test]
fn remove_prefix_exact() {
let mut trie = TrieStructure::new();

trie.node([Nibble::try_from(1).unwrap()].iter().cloned())
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
Nibble::try_from(4).unwrap(),
Nibble::try_from(5).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());
trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
Nibble::try_from(4).unwrap(),
Nibble::try_from(6).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.remove_prefix(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
);

assert_eq!(trie.len(), 1);
assert!(trie
.node([Nibble::try_from(1).unwrap(),].iter().cloned(),)
.into_occupied()
.unwrap()
.has_storage_value());
}

#[test]
fn remove_prefix_doesnt_match_anything() {
let mut trie = TrieStructure::new();

trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.remove_prefix(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(5).unwrap(),
]
.iter()
.cloned(),
);

assert_eq!(trie.len(), 1);
assert!(trie
.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_occupied()
.unwrap()
.has_storage_value());
}

#[test]
fn remove_prefix_nothing_to_remove() {
let mut trie = TrieStructure::new();

trie.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_vacant()
.unwrap()
.insert_storage_value()
.insert((), ());

trie.remove_prefix(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
Nibble::try_from(4).unwrap(),
]
.iter()
.cloned(),
);

assert_eq!(trie.len(), 1);
assert!(trie
.node(
[
Nibble::try_from(1).unwrap(),
Nibble::try_from(2).unwrap(),
Nibble::try_from(3).unwrap(),
]
.iter()
.cloned(),
)
.into_occupied()
.unwrap()
.has_storage_value());
}

#[test]
fn fuzzing() {
fn uniform_sample(min: u8, max: u8) -> u8 {
Expand Down

0 comments on commit 52300a1

Please sign in to comment.