Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoljub-duric committed Mar 5, 2024
1 parent ad838b3 commit eda9ca4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
7 changes: 7 additions & 0 deletions proptest-regressions/storable/tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc a93f6556dd7870c8385191a803f08c9bb2afb28ed66fe2a5266c2f6faa3cf438 # shrinks to v = Some((0, [], [0]))
4 changes: 2 additions & 2 deletions src/storable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ fn get_size_lens(mut sizes_len_byte: u8, number_of_elements: u8) -> Vec<u8> {
panic!("");
}
let mut size_lens = vec![];
for i in 0..number_of_elements - 1 {
for _ in 0..number_of_elements - 1 {
let mask: u8 = 3;
let curr_size: u8 = (sizes_len_byte & mask) + 1;
size_lens.push(curr_size);
Expand All @@ -682,7 +682,7 @@ where
let b_bytes = self.1.to_bytes();
let b_size = b_bytes.len();
let b_size_len = size_len(b_size);
let c_bytes = self.1.to_bytes();
let c_bytes = self.2.to_bytes();
let c_size = c_bytes.len();

let output_size = a_size + b_size + c_size + a_size_len + b_size_len + 1;
Expand Down
60 changes: 60 additions & 0 deletions src/storable/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,59 @@ proptest! {
prop_assert_eq!(tuple, Storable::from_bytes(bytes));
}

#[test]
fn tuple_with_three_elements_roundtrip(x in any::<u64>(), y in uniform20(any::<u8>()), z in uniform20(any::<u8>())) {
let tuple = (x, y, z);
let bytes = tuple.to_bytes();
// 1B sizes len | 1B x size | 8B x bytes | 1B y size | 20B y bytes | 20B z bytes
prop_assert_eq!(bytes.len(), 51);
prop_assert_eq!(tuple, Storable::from_bytes(bytes));
}

#[test]
fn tuple_with_three_unbounded_elements_roundtrip(v1 in pvec(any::<u8>(), 0..4), v2 in pvec(any::<u8>(), 0..8), v3 in pvec(any::<u8>(), 0..12)) {
let tuple = (v1, v2, v3);
assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}

#[test]
fn tuple_with_three_elements_bounded_and_unbounded_roundtrip(v1 in pvec(any::<u8>(), 0..4), x in any::<u64>(), v2 in pvec(any::<u8>(), 0..12)) {
let tuple = (v1, x, v2);
assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}


#[test]
fn tuple_variable_width_u8_roundtrip(x in any::<u64>(), v in pvec(any::<u8>(), 0..40)) {
let bytes = Blob::<48>::try_from(&v[..]).unwrap();
let tuple = (x, bytes);
prop_assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}

#[test]
fn tuple_with_three_elements_variable_width_u8_roundtrip(x in any::<u64>(), v1 in pvec(any::<u8>(), 0..40), v2 in pvec(any::<u8>(), 0..80)) {
let v1_bytes = Blob::<40>::try_from(&v1[..]).unwrap();
let v2_bytes = Blob::<80>::try_from(&v2[..]).unwrap();
let tuple = (x, v1_bytes, v2_bytes);
prop_assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}

#[test]
fn tuple_variable_width_u16_roundtrip(x in any::<u64>(), v in pvec(any::<u8>(), 0..40)) {
let bytes = Blob::<300>::try_from(&v[..]).unwrap();
let tuple = (x, bytes);
prop_assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}

#[test]
fn tuple_with_three_elements_variable_width_u16_roundtrip(x in any::<u64>(), v1 in pvec(any::<u8>(), 0..40), v2 in pvec(any::<u8>(), 0..80)) {
let v1_bytes = Blob::<300>::try_from(&v1[..]).unwrap();
let v2_bytes = Blob::<300>::try_from(&v2[..]).unwrap();

let tuple = (x, v1_bytes, v2_bytes);
prop_assert_eq!(tuple, Storable::from_bytes(tuple.to_bytes()));
}

#[test]
fn f64_roundtrip(v in any::<f64>()) {
prop_assert_eq!(v, Storable::from_bytes(v.to_bytes()));
Expand All @@ -52,12 +91,33 @@ proptest! {
prop_assert_eq!(v, Storable::from_bytes(v.to_bytes()));
}

#[test]
fn optional_tuple_with_three_elements_roundtrip(v in proptest::option::of((any::<u64>(), uniform20(any::<u8>()), uniform20(any::<u8>())))) {
prop_assert_eq!(v, Storable::from_bytes(v.to_bytes()));
}

#[test]
fn optional_tuple_with_three_unbounded_elements_roundtrip(v in proptest::option::of((pvec(any::<u8>(), 0..4), pvec(any::<u8>(), 0..8), pvec(any::<u8>(), 0..12)))) {
prop_assert_eq!(v.clone(), Storable::from_bytes(v.to_bytes()));
}

#[test]
fn optional_tuple_variable_width_u8_roundtrip(v in proptest::option::of((any::<u64>(), pvec(any::<u8>(), 0..40)))) {
let v = v.map(|(n, bytes)| (n, Blob::<48>::try_from(&bytes[..]).unwrap()));
prop_assert_eq!(v, Storable::from_bytes(v.to_bytes()));
}

#[test]
fn optional_tuple_with_three_elements_variable_width_u8_roundtrip(v in proptest::option::of((any::<u64>(), pvec(any::<u8>(), 0..40), pvec(any::<u8>(), 0..80)))) {
let v = v.map(|(n, bytes_1, bytes_2)| (n, Blob::<40>::try_from(&bytes_1[..]).unwrap(), Blob::<80>::try_from(&bytes_2[..]).unwrap()));
prop_assert_eq!(v, Storable::from_bytes(v.to_bytes()));
}

#[test]
fn optional_tuple_with_three_elements_bounded_and_unbounded_roundtrip(v in proptest::option::of((any::<u64>(), pvec(any::<u8>(), 0..40), pvec(any::<u8>(), 0..80)))) {
prop_assert_eq!(v.clone(), Storable::from_bytes(v.to_bytes()));
}

#[test]
fn principal_roundtrip(mut bytes in pvec(any::<u8>(), 0..=28), tag in proptest::prop_oneof![Just(1),Just(2),Just(3),Just(4),Just(7)]) {
bytes.push(tag);
Expand Down

0 comments on commit eda9ca4

Please sign in to comment.