add quickcheck test for {un}pack_str

This commit is contained in:
Awal Garg 2018-06-30 00:52:14 +05:30 committed by Fabian
parent e33cc8f072
commit e462e0aaf1
3 changed files with 14 additions and 2 deletions

View file

@ -4,7 +4,7 @@ version = "0.1.0"
publish = false
[dependencies]
lazy_static = "1.0"
quickcheck = "0.6.2"
[lib]
crate-type = ["cdylib"]

View file

@ -1,3 +1,7 @@
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
#[macro_use]
mod dbg;

View file

@ -122,7 +122,7 @@ pub type PackedStr = (u64, u64, u64);
#[allow(dead_code)]
pub fn pack_str(s: &str) -> PackedStr {
assert!(s.len() <= 16);
assert!(s.len() <= 24);
let mut a: [u8; 24] = [0; 24];
for (i, ch) in s.char_indices() {
a[i] = ch as u8;
@ -175,4 +175,12 @@ mod tests {
assert_eq!("abcdefghijkl", unpack_str(pstr));
}
quickcheck! {
fn prop(xs: Vec<u8>) -> bool {
if xs.len() > 24 || xs.contains(&0) { return true; }
let xs = String::from_utf8(xs).expect("get string");
xs == unpack_str(pack_str(&xs))
}
}
}