Skip to content

Commit

Permalink
fixup! storage/lazy_vec: add state machine test for lazy vec API
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Sep 14, 2022
1 parent ce59201 commit 92d8079
Showing 1 changed file with 56 additions and 13 deletions.
69 changes: 56 additions & 13 deletions shared/src/ledger/storage_api/collections/lazy_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,8 @@ mod test {
prop_oneof![arb_test_vec_item().prop_map(Transition::Push)]
.boxed()
} else {
let indices: Vec<Index> = (0_usize..state.0.len())
.enumerate()
.map(|(_, ix)| ix as Index)
.collect();
let indices: Vec<Index> =
(0_usize..state.0.len()).map(|ix| ix as Index).collect();
let arb_index = proptest::sample::select(indices);
prop_oneof![
Just(Transition::Pop),
Expand Down Expand Up @@ -712,26 +710,59 @@ mod test {
mut state: Self::ConcreteState,
transition: <Self::Abstract as AbstractStateMachine>::Transition,
) -> Self::ConcreteState {
// Apply transitions in the eager vec first for comparison
apply_transition_on_eager_vec(&mut state.eager_vec, &transition);

// Transition application on lazy vec and post-conditions:
match dbg!(transition) {
match dbg!(&transition) {
Transition::Push(value) => {
state.lazy_vec.push(&mut state.storage, value).unwrap()
let old_len = state.lazy_vec.len(&state.storage).unwrap();

state
.lazy_vec
.push(&mut state.storage, value.clone())
.unwrap();

// Post-conditions:
let new_len = state.lazy_vec.len(&state.storage).unwrap();
let stored_value = state
.lazy_vec
.get(&state.storage, new_len - 1)
.unwrap()
.unwrap();
assert_eq!(
&stored_value, value,
"the new item must be added to the back"
);
assert_eq!(old_len + 1, new_len, "length must increment");
}
Transition::Pop => {
let _popped =
state.lazy_vec.pop(&mut state.storage).unwrap();
let old_len = state.lazy_vec.len(&state.storage).unwrap();

let popped = state
.lazy_vec
.pop(&mut state.storage)
.unwrap()
.unwrap();

// Post-conditions:
let new_len = state.lazy_vec.len(&state.storage).unwrap();
assert_eq!(old_len, new_len + 1, "length must decrement");
assert_eq!(
&popped,
state.eager_vec.last().unwrap(),
"popped element matches the last element in eager vec \
before it's updated"
);
}
Transition::Update { index, value } => {
state
.lazy_vec
.update(&mut state.storage, index, value)
.update(&mut state.storage, *index, value.clone())
.unwrap();
}
}

// Apply transition in the eager vec for comparison
apply_transition_on_eager_vec(&mut state.eager_vec, &transition);

// Global post-conditions:

// All items in eager vec must be present in lazy vec
Expand All @@ -741,7 +772,19 @@ mod test {
.get(&state.storage, ix as Index)
.unwrap()
.expect("The expected item must be present in lazy vec");
assert_eq!(expected_item, &got);
assert_eq!(expected_item, &got, "at index {ix}");
}

// All items in lazy vec must be present in eager vec
for (ix, expected_item) in
state.lazy_vec.iter(&state.storage).unwrap().enumerate()
{
let expected_item = expected_item.unwrap();
let got = state
.eager_vec
.get(ix)
.expect("The expected item must be present in eager vec");
assert_eq!(&expected_item, got, "at index {ix}");
}

state
Expand Down

0 comments on commit 92d8079

Please sign in to comment.