Skip to content

Commit

Permalink
add tests for array and byte-slice keys
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Jul 6, 2019
1 parent 40c1476 commit b04764a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions phf_codegen/test/build.rs
Expand Up @@ -74,4 +74,25 @@ fn main() {
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();

write!(&mut file, "static ARRAY_KEYS: ::phf::Map<[u8; 3], u32> = ");
phf_codegen::Map::<[u8; 3]>::new()
.entry(*b"foo", "0")
.entry(*b"bar", "1")
.entry(*b"baz", "2")
.build(&mut file)
.unwrap();

writeln!(&mut file, ";");

write!(&mut file, "static BYTE_STR_KEYS: ::phf::Map<&[u8], u32> = ");
// key type required here as it will infer `&'static [u8; 3]` instead
phf_codegen::Map::<&[u8]>::new()
.entry(b"foo", "0")
.entry(b"bar", "1")
.entry(b"baz", "2")
.entry(b"quux", "3")
.build(&mut file)
.unwrap();
writeln!(&mut file, ";");
}
17 changes: 17 additions & 0 deletions phf_codegen/test/src/lib.rs
Expand Up @@ -53,6 +53,22 @@ mod test {
assert!(!UNICASE_MAP.contains_key(&UniCase::new("XyZ")));
}

#[test]
fn array_keys() {
assert_eq!(0, ARRAY_KEYS[b"foo"]);
assert_eq!(1, ARRAY_KEYS[b"bar"]);
assert_eq!(2, ARRAY_KEYS[b"baz"]);
}

#[test]
fn byte_str_keys() {
// slicing is required unless the key type is fixed-size
assert_eq!(0, BYTE_STR_KEYS[&b"foo"[..]]);
assert_eq!(1, BYTE_STR_KEYS[&b"bar"[..]]);
assert_eq!(2, BYTE_STR_KEYS[&b"baz"[..]]);
assert_eq!(3, BYTE_STR_KEYS[&b"quux"[..]]);
}

#[test]
fn empty_map() {
assert_eq!(None, EMPTY.get(&1));
Expand All @@ -63,4 +79,5 @@ mod test {
assert_eq!(None, EMPTY_ORDERED.get(&1));
}


}

0 comments on commit b04764a

Please sign in to comment.