Skip to content

Commit

Permalink
Fix & include tests for empty maps
Browse files Browse the repository at this point in the history
  • Loading branch information
cetra3 committed Mar 18, 2019
1 parent 53000d3 commit 83fd51c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions phf/src/map.rs
Expand Up @@ -80,6 +80,7 @@ impl<K, V> Map<K, V> {
where T: Eq + PhfHash,
K: Borrow<T>
{
if self.disps.len() == 0 { return None; } //Prevent panic on empty map
let hash = phf_shared::hash(key, self.key);
let index = phf_shared::get_index(hash, &*self.disps, self.entries.len());
let entry = &self.entries[index as usize];
Expand Down
1 change: 1 addition & 0 deletions phf/src/ordered_map.rs
Expand Up @@ -108,6 +108,7 @@ impl<K, V> OrderedMap<K, V> {
where T: Eq + PhfHash,
K: Borrow<T>
{
if self.disps.len() == 0 { return None; } //Prevent panic on empty map
let hash = phf_shared::hash(key, self.key);
let idx_index = phf_shared::get_index(hash, &*self.disps, self.idxs.len());
let idx = self.idxs[idx_index as usize];
Expand Down
9 changes: 9 additions & 0 deletions phf_codegen/test/build.rs
Expand Up @@ -65,4 +65,13 @@ fn main() {
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();

//u32 is used here purely for a type that impls `Hash+PhfHash+Eq+fmt::Debug`, but is not required for the empty test itself
write!(&mut file, "static EMPTY: ::phf::Map<u32, u32> = ").unwrap();
phf_codegen::Map::<u32>::new().build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();

write!(&mut file, "static EMPTY_ORDERED: ::phf::OrderedMap<u32, u32> = ").unwrap();
phf_codegen::OrderedMap::<u32>::new().build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
}
11 changes: 11 additions & 0 deletions phf_codegen/test/src/lib.rs
Expand Up @@ -55,4 +55,15 @@ mod test {
assert_eq!("b", UNICASE_MAP[&UniCase("DEf")]);
assert!(!UNICASE_MAP.contains_key(&UniCase("XyZ")));
}

#[test]
fn empty_map() {
assert_eq!(None, EMPTY.get(&1));
}

#[test]
fn empty_ordered_map() {
assert_eq!(None, EMPTY_ORDERED.get(&1));
}

}

0 comments on commit 83fd51c

Please sign in to comment.