Skip to content

Commit ec44135

Browse files
committed
Add quickcheck tests for from_slices
...and also a property based test for the normal roundtrip, now that we have the dependency anyway.
1 parent 5a91f48 commit ec44135

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ sha1 = "0.5"
2424
sha2 = { version = "0.7", default-features = false }
2525
tiny-keccak = "1.4"
2626
unsigned-varint = "0.3"
27+
28+
[dev-dependencies]
29+
quickcheck = "0.9.2"

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub use hashes::Hash;
2525
use std::fmt;
2626
use storage::Storage;
2727

28+
#[cfg(test)]
29+
#[macro_use]
30+
extern crate quickcheck;
31+
2832
// Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2
2933
macro_rules! encode {
3034
(sha1, Sha1, $input:expr, $output:expr) => {{

src/storage.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl Storage {
6262
#[cfg(test)]
6363
mod tests {
6464
use super::{Storage, MAX_INLINE};
65+
use quickcheck;
6566

6667
#[test]
6768
fn struct_size() {
@@ -78,4 +79,22 @@ mod tests {
7879
assert_eq!(data, storage.bytes());
7980
}
8081
}
82+
83+
quickcheck! {
84+
fn roundtrip_check(data: Vec<u8>) -> bool {
85+
let storage = Storage::from_slice(&data);
86+
storage.bytes() == data.as_slice()
87+
}
88+
89+
fn from_slices_roundtrip_check(data: Vec<Vec<u8>>) -> bool {
90+
let mut slices = Vec::new();
91+
let mut expected = Vec::new();
92+
for v in data.iter() {
93+
slices.push(v.as_slice());
94+
expected.extend_from_slice(&v);
95+
}
96+
let storage = Storage::from_slices(&slices);
97+
storage.bytes() == expected.as_slice()
98+
}
99+
}
81100
}

0 commit comments

Comments
 (0)