Add a bytes round-trip test for compactsize encoding.

This commit is contained in:
Henry de Valence 2019-11-19 17:08:24 -08:00 committed by Deirdre Connolly
parent 986b5ee46b
commit 68a6837cc1
2 changed files with 17 additions and 2 deletions

View File

@ -5,3 +5,4 @@
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc ddefa5145f08994fb873af028344fc03d656dcd1f9287fac71dfa98cbd6a5090 # shrinks to s = 65536
cc c04b21f87727bdd1fd9eb7a1616104fd65fa617f507bb6e8163be6542ff2ed68 # shrinks to bytes = [253, 0, 0, 0, 0, 0, 0, 0, 0]

View File

@ -282,16 +282,30 @@ mod tests {
use std::io::Cursor;
proptest! {
// The test below is cheap so we can run it a lot.
// The tests below are cheap so we can run them a lot.
#![proptest_config(ProptestConfig::with_cases(100_000))]
#[test]
fn compactsize_round_trip(s in 0u64..0x2_0000u64) {
fn compactsize_write_then_read_round_trip(s in 0u64..0x2_0000u64) {
// Maximum encoding size of a compactsize is 9 bytes.
let mut buf = [0u8; 8+1];
Cursor::new(&mut buf[..]).write_compactsize(s).unwrap();
let expect_s = Cursor::new(&buf[..]).read_compactsize().unwrap();
prop_assert_eq!(s, expect_s);
}
#[test]
fn compactsize_read_then_write_round_trip(bytes in prop::array::uniform9(0u8..)) {
let s = Cursor::new(&bytes[..]).read_compactsize().unwrap();
// The compactsize encoding is variable-length, so we may not even
// read all of the input bytes, and therefore we can't expect that
// the encoding will reproduce bytes that were never read. Instead,
// copy the input bytes, and overwrite them with the encoding of s,
// so that if the encoding is different, we'll catch it on the part
// that's written.
let mut expect_bytes = bytes.clone();
Cursor::new(&mut expect_bytes[..]).write_compactsize(s).unwrap();
prop_assert_eq!(bytes, expect_bytes);
}
}
}