Add tests for the Debug and activation Vecs (#11926)

* Add tests for the Debug and activation Vecs

* Rename a bit
This commit is contained in:
Ryo Onodera 2020-09-01 17:48:25 +09:00 committed by GitHub
parent 07ecb56753
commit 11ac4eb21d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 12 deletions

View File

@ -70,7 +70,7 @@ impl std::fmt::Debug for Program {
} }
} }
// given operating_mode and epoch, return the entire set of enabled programs // given operating_mode, return the entire set of enabled programs
fn get_programs(operating_mode: OperatingMode) -> Vec<(Program, Epoch)> { fn get_programs(operating_mode: OperatingMode) -> Vec<(Program, Epoch)> {
match operating_mode { match operating_mode {
OperatingMode::Development => vec![ OperatingMode::Development => vec![
@ -82,13 +82,13 @@ fn get_programs(operating_mode: OperatingMode) -> Vec<(Program, Epoch)> {
Program::Native(solana_exchange_program!()), Program::Native(solana_exchange_program!()),
] ]
.into_iter() .into_iter()
.map(|program| (program, 0)) .map(|program| (program, GENESIS_EPOCH))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
OperatingMode::Preview => vec![ OperatingMode::Preview => vec![
( (
Program::BuiltinLoader(solana_bpf_loader_deprecated_program!()), Program::BuiltinLoader(solana_bpf_loader_deprecated_program!()),
0, GENESIS_EPOCH,
), ),
(Program::BuiltinLoader(solana_bpf_loader_program!()), 89), (Program::BuiltinLoader(solana_bpf_loader_program!()), 89),
], ],
@ -163,18 +163,33 @@ mod tests {
use super::*; use super::*;
use std::collections::HashSet; use std::collections::HashSet;
#[test] fn do_test_uniqueness(programs: Vec<(Program, Epoch)>) {
fn test_id_uniqueness() { let mut unique_ids = HashSet::new();
let mut unique = HashSet::new(); let mut unique_names = HashSet::new();
let programs = get_programs(OperatingMode::Development); let mut prev_start_epoch = GENESIS_EPOCH;
for (program, _start_epoch) in programs { for (program, next_start_epoch) in programs {
assert!(next_start_epoch >= prev_start_epoch);
match program { match program {
Program::Native((name, id)) => assert!(unique.insert((name, id))), Program::Native((name, id)) => {
Program::BuiltinLoader((name, id, _)) => assert!(unique.insert((name, id))), assert!(unique_ids.insert(id));
assert!(unique_names.insert(name));
}
Program::BuiltinLoader((name, id, _)) => {
assert!(unique_ids.insert(id));
assert!(unique_names.insert(name));
}
} }
prev_start_epoch = next_start_epoch;
} }
} }
#[test]
fn test_uniqueness() {
do_test_uniqueness(get_programs(OperatingMode::Development));
do_test_uniqueness(get_programs(OperatingMode::Preview));
do_test_uniqueness(get_programs(OperatingMode::Stable));
}
#[test] #[test]
fn test_development_inflation() { fn test_development_inflation() {
assert_eq!( assert_eq!(
@ -214,4 +229,9 @@ mod tests {
fn test_softlaunch_programs() { fn test_softlaunch_programs() {
assert!(!get_programs(OperatingMode::Stable).is_empty()); assert!(!get_programs(OperatingMode::Stable).is_empty());
} }
#[test]
fn test_debug() {
assert!(!format!("{:?}", get_programs(OperatingMode::Development)).is_empty());
}
} }

View File

@ -8242,7 +8242,7 @@ mod tests {
_ka: &[KeyedAccount], _ka: &[KeyedAccount],
_data: &[u8], _data: &[u8],
) -> std::result::Result<(), InstructionError> { ) -> std::result::Result<(), InstructionError> {
Err(InstructionError::Custom(42)) Ok(())
} }
let slot = 123; let slot = 123;
@ -8281,7 +8281,7 @@ mod tests {
_data: &[u8], _data: &[u8],
_context: &mut dyn solana_sdk::entrypoint_native::InvokeContext, _context: &mut dyn solana_sdk::entrypoint_native::InvokeContext,
) -> std::result::Result<(), InstructionError> { ) -> std::result::Result<(), InstructionError> {
Err(InstructionError::Custom(42)) Ok(())
} }
let slot = 123; let slot = 123;

View File

@ -75,9 +75,29 @@ mod tests {
pubkey::Pubkey, pubkey::Pubkey,
}; };
use std::collections::HashSet;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
fn do_test_uniqueness(builtins: Vec<(Builtin, Epoch)>) {
let mut unique_ids = HashSet::new();
let mut unique_names = HashSet::new();
let mut prev_start_epoch = 0;
for (builtin, next_start_epoch) in builtins {
assert!(next_start_epoch >= prev_start_epoch);
assert!(unique_ids.insert(builtin.name));
assert!(unique_names.insert(builtin.id));
prev_start_epoch = next_start_epoch;
}
}
#[test]
fn test_uniqueness() {
do_test_uniqueness(get_builtins(OperatingMode::Development));
do_test_uniqueness(get_builtins(OperatingMode::Preview));
do_test_uniqueness(get_builtins(OperatingMode::Stable));
}
#[test] #[test]
fn test_get_builtins() { fn test_get_builtins() {
let (mut genesis_config, _mint_keypair) = create_genesis_config(100_000); let (mut genesis_config, _mint_keypair) = create_genesis_config(100_000);

View File

@ -300,6 +300,7 @@ impl std::fmt::Debug for MessageProcessor {
loaders: Vec<String>, loaders: Vec<String>,
native_loader: &'a NativeLoader, native_loader: &'a NativeLoader,
is_cross_program_supported: bool, is_cross_program_supported: bool,
compute_budget: ComputeBudget,
} }
// rustc doesn't compile due to bug without this work around // rustc doesn't compile due to bug without this work around
// https://github.com/rust-lang/rust/issues/50280 // https://github.com/rust-lang/rust/issues/50280
@ -323,6 +324,7 @@ impl std::fmt::Debug for MessageProcessor {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
native_loader: &self.native_loader, native_loader: &self.native_loader,
is_cross_program_supported: self.is_cross_program_supported, is_cross_program_supported: self.is_cross_program_supported,
compute_budget: self.compute_budget,
}; };
write!(f, "{:?}", processor) write!(f, "{:?}", processor)
@ -1554,4 +1556,29 @@ mod tests {
); );
} }
} }
#[test]
fn test_debug() {
let mut message_processor = MessageProcessor::default();
fn mock_process_instruction(
_program_id: &Pubkey,
_keyed_accounts: &[KeyedAccount],
_data: &[u8],
) -> Result<(), InstructionError> {
Ok(())
}
fn mock_ix_processor(
_pubkey: &Pubkey,
_ka: &[KeyedAccount],
_data: &[u8],
_context: &mut dyn solana_sdk::entrypoint_native::InvokeContext,
) -> std::result::Result<(), InstructionError> {
Ok(())
}
let program_id = Pubkey::new_rand();
message_processor.add_program(program_id, mock_process_instruction);
message_processor.add_loader(program_id, mock_ix_processor);
assert!(!format!("{:?}", message_processor).is_empty());
}
} }