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 103b6f7 commit 72f85ed
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions sn_networking/src/sybil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,40 @@ 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 72f85ed

Please sign in to comment.