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

trie: add unit tests for nibbles #12758

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions crates/trie/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,23 @@ proptest.workspace = true
proptest-arbitrary-interop.workspace = true
hash-db = "=0.15.2"
plain_hasher = "0.2"
serde_json.workspace = true

[features]
test-utils = [
"dep:plain_hasher",
"dep:hash-db",
"arbitrary",
"reth-primitives-traits/test-utils",
"reth-codecs/test-utils"
"dep:plain_hasher",
"dep:hash-db",
"arbitrary",
"reth-primitives-traits/test-utils",
"reth-codecs/test-utils",
]
arbitrary = [
"alloy-trie/arbitrary",
"dep:arbitrary",
"reth-primitives-traits/arbitrary",
"alloy-consensus/arbitrary",
"alloy-primitives/arbitrary",
"nybbles/arbitrary",
"revm-primitives/arbitrary",
"reth-codecs/arbitrary"
"alloy-trie/arbitrary",
"dep:arbitrary",
"reth-primitives-traits/arbitrary",
"alloy-consensus/arbitrary",
"alloy-primitives/arbitrary",
"nybbles/arbitrary",
"revm-primitives/arbitrary",
"reth-codecs/arbitrary",
]
94 changes: 94 additions & 0 deletions crates/trie/common/src/nibbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,97 @@ impl Compact for StoredNibblesSubKey {
(Self(Nibbles::from_nibbles_unchecked(&buf[..len])), &buf[65..])
}
}

#[cfg(test)]
mod tests {
use super::*;
use bytes::BytesMut;

#[test]
fn test_stored_nibbles_from_nibbles() {
let nibbles = Nibbles::from_nibbles_unchecked(vec![0x12, 0x34, 0x56]);
let stored = StoredNibbles::from(nibbles.clone());
assert_eq!(stored.0, nibbles);
}

#[test]
fn test_stored_nibbles_from_vec() {
let bytes = vec![0x12, 0x34, 0x56];
let stored = StoredNibbles::from(bytes.clone());
assert_eq!(stored.0.as_slice(), bytes.as_slice());
}

#[test]
fn test_stored_nibbles_equality() {
let bytes = vec![0x12, 0x34];
let stored = StoredNibbles::from(bytes.clone());
assert_eq!(stored, *bytes.as_slice());
}

#[test]
fn test_stored_nibbles_partial_cmp() {
let stored = StoredNibbles::from(vec![0x12, 0x34]);
let other = vec![0x12, 0x35];
assert!(stored < *other.as_slice());
}

#[test]
fn test_stored_nibbles_to_compact() {
let stored = StoredNibbles::from(vec![0x12, 0x34]);
let mut buf = BytesMut::with_capacity(10);
let len = stored.to_compact(&mut buf);
assert_eq!(len, 2);
assert_eq!(buf, &vec![0x12, 0x34][..]);
}

#[test]
fn test_stored_nibbles_from_compact() {
let buf = vec![0x12, 0x34, 0x56];
let (stored, remaining) = StoredNibbles::from_compact(&buf, 2);
assert_eq!(stored.0.as_slice(), &[0x12, 0x34]);
assert_eq!(remaining, &[0x56]);
}

#[test]
fn test_stored_nibbles_subkey_from_nibbles() {
let nibbles = Nibbles::from_nibbles_unchecked(vec![0x12, 0x34]);
let subkey = StoredNibblesSubKey::from(nibbles.clone());
assert_eq!(subkey.0, nibbles);
}

#[test]
fn test_stored_nibbles_subkey_to_compact() {
let subkey = StoredNibblesSubKey::from(vec![0x12, 0x34]);
let mut buf = BytesMut::with_capacity(65);
let len = subkey.to_compact(&mut buf);
assert_eq!(len, 65);
assert_eq!(buf[..2], [0x12, 0x34]);
assert_eq!(buf[64], 2); // Length byte
}

#[test]
fn test_stored_nibbles_subkey_from_compact() {
let mut buf = vec![0x12, 0x34];
buf.resize(65, 0);
buf[64] = 2;
let (subkey, remaining) = StoredNibblesSubKey::from_compact(&buf, 65);
assert_eq!(subkey.0.as_slice(), &[0x12, 0x34]);
assert_eq!(remaining, &[] as &[u8]);
}

#[test]
fn test_serialization_stored_nibbles() {
let stored = StoredNibbles::from(vec![0x12, 0x34]);
let serialized = serde_json::to_string(&stored).unwrap();
let deserialized: StoredNibbles = serde_json::from_str(&serialized).unwrap();
assert_eq!(stored, deserialized);
}

#[test]
fn test_serialization_stored_nibbles_subkey() {
let subkey = StoredNibblesSubKey::from(vec![0x12, 0x34]);
let serialized = serde_json::to_string(&subkey).unwrap();
let deserialized: StoredNibblesSubKey = serde_json::from_str(&serialized).unwrap();
assert_eq!(subkey, deserialized);
}
}
Loading