From 40fd4ae4479d362de89e41d74f6d0e4290371d69 Mon Sep 17 00:00:00 2001 From: zypeh Date: Thu, 25 Aug 2022 18:26:07 +0800 Subject: [PATCH] Adding test cover the write_blk encode function --- Cargo.lock | 12 ++++++++++ crates/flat/Cargo.toml | 3 ++- crates/flat/tests/flat_test.rs | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index dc4771ea..7c83cbf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,6 +164,7 @@ version = "0.0.7" dependencies = [ "anyhow", "quickcheck", + "quickcheck_macros", "thiserror", ] @@ -484,6 +485,17 @@ dependencies = [ "rand", ] +[[package]] +name = "quickcheck_macros" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b22a693222d716a9587786f37ac3f6b4faedb5b80c23914e7303ff5a1d8016e9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "quote" version = "1.0.18" diff --git a/crates/flat/Cargo.toml b/crates/flat/Cargo.toml index e554971b..c5671fe7 100644 --- a/crates/flat/Cargo.toml +++ b/crates/flat/Cargo.toml @@ -15,4 +15,5 @@ anyhow = "1.0.57" thiserror = "1.0.31" [dev-dependencies] -quickcheck = "1" \ No newline at end of file +quickcheck = "1" +quickcheck_macros = "1" \ No newline at end of file diff --git a/crates/flat/tests/flat_test.rs b/crates/flat/tests/flat_test.rs index db12d545..3ced1ed9 100644 --- a/crates/flat/tests/flat_test.rs +++ b/crates/flat/tests/flat_test.rs @@ -1,7 +1,19 @@ +#[cfg(test)] +extern crate quickcheck; + +#[cfg(test)] +#[macro_use(quickcheck)] +extern crate quickcheck_macros; + use flat_rs::{decode, encode}; +use quickcheck::{Arbitrary, Gen}; #[cfg(test)] mod test { + use core::num; + + use quickcheck::Arbitrary; + #[test] fn encode_bool() { let bytes = crate::encode(&true).unwrap(); @@ -31,4 +43,35 @@ mod test { assert_eq!(decoded, 3_u8); } + + #[quickcheck] + fn encode_vec_u8(xs: Vec) -> bool { + let bytes = crate::encode(&xs).unwrap(); + let decoded: Vec = crate::decode(&bytes).unwrap(); + decoded == xs + } + + #[derive(Clone, Debug)] + struct BigChunk(Vec); + + impl Arbitrary for BigChunk { + fn arbitrary(g: &mut quickcheck::Gen) -> Self { + let num_of_element = g.choose(&[255, 256, 244, 100]).unwrap(); + + let mut vec = Vec::with_capacity(*num_of_element); + for _ in 1..*num_of_element { + vec.push(u8::arbitrary(g)); + } + + BigChunk(vec) + } + } + + #[quickcheck] + fn encode_write_blk(xs: BigChunk) -> bool { + let xs = xs.0; + let bytes = crate::encode(&xs).unwrap(); + let decoded: Vec = crate::decode(&bytes).unwrap(); + decoded == xs + } }