소스 검색

add quickcheck test for {un}pack_str

Awal Garg 6 년 전
부모
커밋
e462e0aaf1
3개의 변경된 파일14개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 1
      Cargo.toml
  2. 4 0
      src/rust/lib.rs
  3. 9 1
      src/rust/util.rs

+ 1 - 1
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"]

+ 4 - 0
src/rust/lib.rs

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

+ 9 - 1
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<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))
+        }
+    }
+
 }