Skip to content

Commit

Permalink
draft: adding unit tests for the algorithm formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Apr 10, 2024
1 parent 101450a commit d7ce052
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions sn_networking/src/sybil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,41 @@ fn common_prefix_length(lhs: &[u8], rhs: &[u8]) -> usize {
}
common_prefix_length
}

#[cfg(test)]
mod tests {
use super::common_prefix_length;

use xor_name::XorName;

#[test]
fn test_common_prefix_length() {
// we use XorName utilities as it's easier to build test data with them
let mut rng = rand::thread_rng();
let mut lhs = XorName::random(&mut rng);
assert_eq!(common_prefix_length(&lhs, &lhs), 256);

let mut rhs = !lhs;
// let's first make sure lhs != rhs in every bit
assert_eq!(common_prefix_length(&lhs, &rhs), 0);

for i in 0..=255 {
lhs = lhs.with_bit(i, true);
rhs = rhs.with_bit(i, true);
assert_eq!(
i as usize + 1,
common_prefix_length(&lhs, &rhs),
"unexpected result from common_prefix_length fn"
);
}
}

#[test]
fn test_net_size_estimate() {
// Build a map with 256 random keys, one for each Kbucket
// with their corresponding K-closest peers to a random CID;
// e.g. in Kbucket #2 get the 20 closest peers to a random CID that shares 2 bits as a prefix.

todo!()
}
}

0 comments on commit d7ce052

Please sign in to comment.