Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Adds test for SortedStorages::new_with_slots() (#31471)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored May 4, 2023
1 parent ed4cc52 commit 22d4c6a
Showing 1 changed file with 77 additions and 1 deletion.
78 changes: 77 additions & 1 deletion runtime/src/sorted_storages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'a> SortedStoragesIter<'a> {
}

#[cfg(test)]
pub mod tests {
mod tests {
use {
super::*,
crate::{
Expand All @@ -201,6 +201,7 @@ pub mod tests {
},
std::sync::Arc,
};

impl<'a> SortedStorages<'a> {
pub fn new_debug(
source: &[(&'a Arc<AccountStorageEntry>, Slot)],
Expand Down Expand Up @@ -327,6 +328,81 @@ pub mod tests {
);
}

#[test]
fn test_sorted_storages_new_with_slots() {
let store = create_sample_store(1);
let start = 33;
let end = 44;

// ┌───────────────────────────────────────┐
// │ ■ storages ■ │
// └──────┃──────────────────────────┃─────┘
// min┣ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┃max
// ■ ■
{
let min = start + 1;
let max = end - 1;
let storages = SortedStorages::new_with_slots(
[(&store, end), (&store, start)].iter().cloned(),
Some(min),
Some(max),
);
assert_eq!(storages.storages.len(), 2);
assert_eq!(storages.range, start..end + 1);
}

// ┌───────────────────────────────────────┐
// │ storages ■ │ ■
// └──────────────────────────────┃────────┘ ┃
// min┣ ─ ─ ─ ─ ─ ─ ┫max
// ■ ■
{
let min = start + 1;
let max = end + 1;
let storages = SortedStorages::new_with_slots(
[(&store, end), (&store, start)].iter().cloned(),
Some(min),
Some(max),
);
assert_eq!(storages.storages.len(), 2);
assert_eq!(storages.range, start..max + 1);
}

// ┌───────────────────────────────────────┐
// ■ │ ■ storages │
// ┃ └─────┃─────────────────────────────────┘
// min┣ ─ ─ ─ ─ ┫max
// ■ ■
{
let min = start - 1;
let max = end - 1;
let storages = SortedStorages::new_with_slots(
[(&store, end), (&store, start)].iter().cloned(),
Some(min),
Some(max),
);
assert_eq!(storages.storages.len(), 2);
assert_eq!(storages.range, min..end + 1);
}

// ┌───────────────────────────────────────┐
// ■ │ storages │ ■
// ┃ └───────────────────────────────────────┘ ┃
// min┣ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┫max
// ■ ■
{
let min = start - 1;
let max = end + 1;
let storages = SortedStorages::new_with_slots(
[(&store, end), (&store, start)].iter().cloned(),
Some(min),
Some(max),
);
assert_eq!(storages.storages.len(), 2);
assert_eq!(storages.range, min..max + 1);
}
}

#[test]
#[should_panic(expected = "slots are not unique")]
fn test_sorted_storages_duplicate_slots() {
Expand Down

0 comments on commit 22d4c6a

Please sign in to comment.