Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 26, 2024
1 parent 9c9ff09 commit 532d74e
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions crates/core/tests/bin_manager_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use proto_core::layout::BinManager;
use rustc_hash::FxHashMap;
use semver::Version;

mod bin_manager {
use super::*;

#[test]
fn creates_buckets_per_version() {
let v1 = Version::new(1, 2, 3);
let v2 = Version::new(4, 5, 6);

let mut bins = BinManager::default();
bins.add_version(&v1);
bins.add_version(&v2);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v2),
(&"1".to_string(), &v1),
(&"1.2".to_string(), &v1),
(&"4".to_string(), &v2),
(&"4.5".to_string(), &v2),
])
);
}

#[test]
fn highest_replaces() {
let v1 = Version::new(1, 2, 3);
let v2 = Version::new(1, 3, 4);

let mut bins = BinManager::default();
bins.add_version(&v1);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v1),
(&"1".to_string(), &v1),
(&"1.2".to_string(), &v1),
])
);

bins.add_version(&v2);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v2),
(&"1".to_string(), &v2),
(&"1.2".to_string(), &v1),
(&"1.3".to_string(), &v2),
])
);
}

#[test]
fn lowest_doesnt_replace() {
let v1 = Version::new(1, 2, 3);
let v2 = Version::new(1, 1, 4);

let mut bins = BinManager::default();
bins.add_version(&v1);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v1),
(&"1".to_string(), &v1),
(&"1.2".to_string(), &v1),
])
);

bins.add_version(&v2);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v1),
(&"1".to_string(), &v1),
(&"1.1".to_string(), &v2),
(&"1.2".to_string(), &v1),
])
);
}

#[test]
fn removing_rebuilds_buckets() {
let v1 = Version::new(1, 2, 3);
let v2 = Version::new(1, 3, 4);

let mut bins = BinManager::default();
bins.add_version(&v1);
bins.add_version(&v2);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v2),
(&"1".to_string(), &v2),
(&"1.2".to_string(), &v1),
(&"1.3".to_string(), &v2),
])
);

bins.remove_version(&v2);

assert_eq!(
bins.get_buckets(),
FxHashMap::from_iter([
(&"*".to_string(), &v1),
(&"1".to_string(), &v1),
(&"1.2".to_string(), &v1),
])
);
}
}

0 comments on commit 532d74e

Please sign in to comment.