Adding test cover the write_blk encode function
This commit is contained in:
parent
9ded4d79d5
commit
40fd4ae447
|
@ -164,6 +164,7 @@ version = "0.0.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"quickcheck",
|
"quickcheck",
|
||||||
|
"quickcheck_macros",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -484,6 +485,17 @@ dependencies = [
|
||||||
"rand",
|
"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]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
|
|
@ -16,3 +16,4 @@ thiserror = "1.0.31"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
quickcheck = "1"
|
quickcheck = "1"
|
||||||
|
quickcheck_macros = "1"
|
|
@ -1,7 +1,19 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
extern crate quickcheck;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[macro_use(quickcheck)]
|
||||||
|
extern crate quickcheck_macros;
|
||||||
|
|
||||||
use flat_rs::{decode, encode};
|
use flat_rs::{decode, encode};
|
||||||
|
use quickcheck::{Arbitrary, Gen};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use core::num;
|
||||||
|
|
||||||
|
use quickcheck::Arbitrary;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn encode_bool() {
|
fn encode_bool() {
|
||||||
let bytes = crate::encode(&true).unwrap();
|
let bytes = crate::encode(&true).unwrap();
|
||||||
|
@ -31,4 +43,35 @@ mod test {
|
||||||
|
|
||||||
assert_eq!(decoded, 3_u8);
|
assert_eq!(decoded, 3_u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[quickcheck]
|
||||||
|
fn encode_vec_u8(xs: Vec<u8>) -> bool {
|
||||||
|
let bytes = crate::encode(&xs).unwrap();
|
||||||
|
let decoded: Vec<u8> = crate::decode(&bytes).unwrap();
|
||||||
|
decoded == xs
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct BigChunk(Vec<u8>);
|
||||||
|
|
||||||
|
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<u8> = crate::decode(&bytes).unwrap();
|
||||||
|
decoded == xs
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue