Add tests to StakeDelegations and StakeHistory to ensure that the outer
types serialize and deserialize correctly to/from the inner types.
(cherry picked from commit da4015a959
)
Co-authored-by: Brooks Prumo <brooks@solana.com>
This commit is contained in:
@ -35,6 +35,7 @@ type StakeDelegationsInner = HashMap<Pubkey, Delegation>;
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Ensure that StakeDelegations is indeed clone-on-write
|
||||
#[test]
|
||||
fn test_stake_delegations_is_cow() {
|
||||
let voter_pubkey = Pubkey::new_unique();
|
||||
@ -83,4 +84,45 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that StakeDelegations serializes and deserializes between the inner and outer types
|
||||
#[test]
|
||||
fn test_stake_delegations_serde() {
|
||||
let voter_pubkey = Pubkey::new_unique();
|
||||
let stake = rand::random();
|
||||
let activation_epoch = rand::random();
|
||||
let warmup_cooldown_rate = rand::random();
|
||||
let delegation =
|
||||
Delegation::new(&voter_pubkey, stake, activation_epoch, warmup_cooldown_rate);
|
||||
|
||||
let pubkey = Pubkey::new_unique();
|
||||
|
||||
let mut stake_delegations_outer = StakeDelegations::default();
|
||||
stake_delegations_outer.insert(pubkey, delegation);
|
||||
|
||||
let mut stake_delegations_inner = StakeDelegationsInner::default();
|
||||
stake_delegations_inner.insert(pubkey, delegation);
|
||||
|
||||
// Test: Assert that serializing the outer and inner types produces the same data
|
||||
assert_eq!(
|
||||
bincode::serialize(&stake_delegations_outer).unwrap(),
|
||||
bincode::serialize(&stake_delegations_inner).unwrap(),
|
||||
);
|
||||
|
||||
// Test: Assert that serializing the outer type then deserializing to the inner type
|
||||
// produces the same values
|
||||
{
|
||||
let data = bincode::serialize(&stake_delegations_outer).unwrap();
|
||||
let deserialized_inner: StakeDelegationsInner = bincode::deserialize(&data).unwrap();
|
||||
assert_eq!(&deserialized_inner, stake_delegations_outer.deref());
|
||||
}
|
||||
|
||||
// Test: Assert that serializing the inner type then deserializing to the outer type
|
||||
// produces the same values
|
||||
{
|
||||
let data = bincode::serialize(&stake_delegations_inner).unwrap();
|
||||
let deserialized_outer: StakeDelegations = bincode::deserialize(&data).unwrap();
|
||||
assert_eq!(deserialized_outer.deref(), &stake_delegations_inner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that StakeHistory is indeed clone-on-write
|
||||
#[test]
|
||||
fn test_stake_history_is_cow() {
|
||||
let mut stake_history = StakeHistory::default();
|
||||
@ -81,4 +82,38 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensure that StakeHistory serializes and deserializes between the inner and outer types
|
||||
#[test]
|
||||
fn test_stake_history_serde() {
|
||||
let mut stake_history_outer = StakeHistory::default();
|
||||
let mut stake_history_inner = StakeHistoryInner::default();
|
||||
(2134..).take(11).for_each(|epoch| {
|
||||
let entry = rand_stake_history_entry();
|
||||
stake_history_outer.add(epoch, entry.clone());
|
||||
stake_history_inner.add(epoch, entry);
|
||||
});
|
||||
|
||||
// Test: Assert that serializing the outer and inner types produces the same data
|
||||
assert_eq!(
|
||||
bincode::serialize(&stake_history_outer).unwrap(),
|
||||
bincode::serialize(&stake_history_inner).unwrap(),
|
||||
);
|
||||
|
||||
// Test: Assert that serializing the outer type then deserializing to the inner type
|
||||
// produces the same values
|
||||
{
|
||||
let data = bincode::serialize(&stake_history_outer).unwrap();
|
||||
let deserialized_inner: StakeHistoryInner = bincode::deserialize(&data).unwrap();
|
||||
assert_eq!(&deserialized_inner, stake_history_outer.deref());
|
||||
}
|
||||
|
||||
// Test: Assert that serializing the inner type then deserializing to the outer type
|
||||
// produces the same values
|
||||
{
|
||||
let data = bincode::serialize(&stake_history_inner).unwrap();
|
||||
let deserialized_outer: StakeHistory = bincode::deserialize(&data).unwrap();
|
||||
assert_eq!(deserialized_outer.deref(), &stake_history_inner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user