From e462e0aaf10bfd7f61806a1ba865b4196f95b496 Mon Sep 17 00:00:00 2001 From: Awal Garg Date: Sat, 30 Jun 2018 00:52:14 +0530 Subject: [PATCH] add quickcheck test for {un}pack_str --- Cargo.toml | 2 +- src/rust/lib.rs | 4 ++++ src/rust/util.rs | 10 +++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f8191cc4..1050d6c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" publish = false [dependencies] -lazy_static = "1.0" +quickcheck = "0.6.2" [lib] crate-type = ["cdylib"] diff --git a/src/rust/lib.rs b/src/rust/lib.rs index eeaf2d4f..787dcdad 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -1,3 +1,7 @@ +#[cfg(test)] +#[macro_use] +extern crate quickcheck; + #[macro_use] mod dbg; diff --git a/src/rust/util.rs b/src/rust/util.rs index 98adf145..ff7c7694 100644 --- a/src/rust/util.rs +++ b/src/rust/util.rs @@ -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) -> 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)) + } + } + }