Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Nov 3, 2024
1 parent cbdeaf1 commit 157f4e1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,41 @@ mod tests {

assert_eq!(total_before - sketch.additions, diff);
}

#[test]
fn test_sketch_heavy_hitters() {
let mut sketch = CountMinSketch::new(512);
let hasher = RandomState::new();

for i in 100..100000 {
let h = hasher.hash_one(format!("k:{}", i));
sketch.add(h);
}

for i in (0..10).step_by(2) {
for _ in 0..i {
let h = hasher.hash_one(format!("k:{}", i));
sketch.add(h);
}
}

// A perfect popularity count yields an array [0, 0, 2, 0, 4, 0, 6, 0, 8, 0]
let mut popularity = vec![0; 10];
for i in 0..10 {
let h = hasher.hash_one(format!("k:{}", i));
popularity[i] = sketch.estimate(h) as i32;
}

for (i, &pop_count) in popularity.iter().enumerate() {
if [0, 1, 3, 5, 7, 9].contains(&i) {
assert!(pop_count <= popularity[2]);
} else if i == 2 {
assert!(popularity[2] <= popularity[4]);
} else if i == 4 {
assert!(popularity[4] <= popularity[6]);
} else if i == 6 {
assert!(popularity[6] <= popularity[8]);
}
}
}
}

0 comments on commit 157f4e1

Please sign in to comment.