Skip to content

Commit 3e33988

Browse files
committed
Refactor serialization tests for geo diff count types
The changes extract a shared test helper for verifying serialization of geo diff counts with different bucket types in MSB sparse bit field representation.
1 parent d90d6f0 commit 3e33988

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

crates/geo_filters/src/diff_count.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,7 @@ impl<C: GeoConfig<Diff>> Count<Diff> for GeoDiffCount<'_, C> {
415415
#[cfg(test)]
416416
mod tests {
417417
use itertools::Itertools;
418-
use rand::{
419-
seq::{IndexedRandom, IteratorRandom},
420-
RngCore, SeedableRng,
421-
};
418+
use rand::{seq::IteratorRandom, RngCore, SeedableRng};
422419

423420
use crate::{
424421
build_hasher::UnstableDefaultBuildHasher,
@@ -653,14 +650,17 @@ mod tests {
653650
assert_eq!(before, after);
654651
}
655652

656-
#[test]
657-
fn test_serialization_round_trip() {
653+
// This helper exists in order to easily test serializing types with different
654+
// bucket types in the MSB sparse bit field representation. See tests below.
655+
fn serialization_round_trip<C: GeoConfig<Diff> + Default>() {
658656
let mut rnd = rand::rngs::StdRng::from_os_rng();
659657

660658
// Run 100 simulations of random values being put into
661-
// a diff counter
659+
// a diff counter. "Serializing" to a vector to emulate
660+
// writing to a disk, and then deserializing and asserting
661+
// the filters are equal.
662662
for _ in 0..100 {
663-
let mut before = GeoDiffCount7::default();
663+
let mut before = GeoDiffCount::<'_, C>::default();
664664

665665
// Select a random number of items to insert
666666
let items = (1..1000).choose(&mut rnd).unwrap();
@@ -670,12 +670,23 @@ mod tests {
670670
}
671671

672672
let mut writer = vec![];
673-
674673
before.write(&mut writer).unwrap();
675674

676-
let after = GeoDiffCount7::from_bytes(before.config.clone(), &writer);
675+
let after = GeoDiffCount::<'_, C>::from_bytes(before.config.clone(), &writer);
677676

678677
assert_eq!(before, after);
679678
}
680679
}
680+
681+
#[test]
682+
fn test_serialization_round_trip_7() {
683+
// Uses a u16 for MSB buckets
684+
serialization_round_trip::<GeoDiffConfig7>();
685+
}
686+
687+
#[test]
688+
fn test_serialization_round_trip_13() {
689+
// Uses a u32 for MSB buckets
690+
serialization_round_trip::<GeoDiffConfig7>();
691+
}
681692
}

0 commit comments

Comments
 (0)