Skip to content

Commit

Permalink
storage: add unit tests for StorageRevertsIter
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored and onbjerg committed Oct 23, 2024
1 parent cf4e774 commit b08b81d
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions crates/storage/provider/src/bundle_state/state_reverts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,105 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_storage_reverts_iter_empty() {
// Create empty sample data for reverts and wiped entries.
let reverts: Vec<(B256, RevertToSlot)> = vec![];
let wiped: Vec<(B256, U256)> = vec![];

// Create the iterator with the empty data.
let iter = StorageRevertsIter::new(reverts, wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify that the results are empty.
assert_eq!(results, vec![]);
}

#[test]
fn test_storage_reverts_iter_reverts_only() {
// Create sample data for only reverts.
let reverts = vec![
(B256::from_slice(&[4; 32]), RevertToSlot::Destroyed),
(B256::from_slice(&[5; 32]), RevertToSlot::Some(U256::from(40))),
];

// Create the iterator with only reverts and no wiped entries.
let iter = StorageRevertsIter::new(reverts, vec![]);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[4; 32]), U256::ZERO), // Revert slot previous value
(B256::from_slice(&[5; 32]), U256::from(40)), // Only revert present.
]
);
}

#[test]
fn test_storage_reverts_iter_wiped_only() {
// Create sample data for only wiped entries.
let wiped = vec![
(B256::from_slice(&[6; 32]), U256::from(50)),
(B256::from_slice(&[7; 32]), U256::from(60)),
];

// Create the iterator with only wiped entries and no reverts.
let iter = StorageRevertsIter::new(vec![], wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[6; 32]), U256::from(50)), // Only wiped present.
(B256::from_slice(&[7; 32]), U256::from(60)), // Only wiped present.
]
);
}

#[test]
fn test_storage_reverts_iter_interleaved() {
// Create sample data for interleaved reverts and wiped entries.
let reverts = vec![
(B256::from_slice(&[8; 32]), RevertToSlot::Some(U256::from(70))),
(B256::from_slice(&[9; 32]), RevertToSlot::Some(U256::from(80))),
// Some higher key than wiped
(B256::from_slice(&[15; 32]), RevertToSlot::Some(U256::from(90))),
];

let wiped = vec![
(B256::from_slice(&[8; 32]), U256::from(75)), // Same key as revert
(B256::from_slice(&[10; 32]), U256::from(85)), // Wiped with new key
];

// Create the iterator with the sample data.
let iter = StorageRevertsIter::new(reverts, wiped);

// Iterate and collect results into a vector for verification.
let results: Vec<_> = iter.collect();

// Verify the output order and values.
assert_eq!(
results,
vec![
(B256::from_slice(&[8; 32]), U256::from(70)), // Revert takes priority.
(B256::from_slice(&[9; 32]), U256::from(80)), // Only revert present.
(B256::from_slice(&[10; 32]), U256::from(85)), // Wiped entry.
(B256::from_slice(&[15; 32]), U256::from(90)), // WGreater revert entry
]
);
}
}

0 comments on commit b08b81d

Please sign in to comment.