-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add misc tests #751
Merged
Merged
Add misc tests #751
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
#[cfg(test)] | ||
use alloc::{ | ||
vec, | ||
vec::Vec, | ||
}; | ||
|
||
use super::Interpreter; | ||
use crate::prelude::*; | ||
use fuel_asm::RegId; | ||
|
@@ -63,140 +57,192 @@ where | |
} | ||
} | ||
|
||
#[test] | ||
fn breakpoint_script() { | ||
use fuel_asm::op; | ||
use fuel_tx::ConsensusParameters; | ||
|
||
let mut vm = Interpreter::<_, _, _>::with_memory_storage(); | ||
|
||
let gas_limit = 1_000_000; | ||
let gas_price = 0; | ||
let height = Default::default(); | ||
|
||
let script = [ | ||
op::addi(0x10, RegId::ZERO, 8), | ||
op::addi(0x11, RegId::ZERO, 16), | ||
op::addi(0x12, RegId::ZERO, 32), | ||
op::addi(0x13, RegId::ZERO, 64), | ||
op::addi(0x14, RegId::ZERO, 128), | ||
op::ret(0x10), | ||
] | ||
.into_iter() | ||
.collect(); | ||
|
||
let consensus_params = ConsensusParameters::standard(); | ||
|
||
let tx = TransactionBuilder::script(script, vec![]) | ||
.script_gas_limit(gas_limit) | ||
.add_random_fee_input() | ||
.finalize() | ||
.into_checked(height, &consensus_params) | ||
.expect("failed to generate checked tx") | ||
.into_ready( | ||
gas_price, | ||
consensus_params.gas_costs(), | ||
consensus_params.fee_params(), | ||
) | ||
.unwrap(); | ||
|
||
let suite = vec![ | ||
( | ||
Breakpoint::script(0), | ||
vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(2), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(3), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(5), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)], | ||
), | ||
]; | ||
|
||
suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b)); | ||
|
||
let state = vm | ||
.transact(tx) | ||
.map(ProgramState::from) | ||
.expect("Failed to execute script!"); | ||
|
||
suite | ||
#[cfg(test)] | ||
mod tests { | ||
use alloc::{ | ||
vec, | ||
vec::Vec, | ||
}; | ||
|
||
use super::Interpreter; | ||
use crate::prelude::*; | ||
use fuel_asm::RegId; | ||
|
||
#[test] | ||
fn breakpoint_script() { | ||
use fuel_asm::op; | ||
use fuel_tx::ConsensusParameters; | ||
|
||
let mut vm = Interpreter::<_, _, _>::with_memory_storage(); | ||
|
||
let gas_limit = 1_000_000; | ||
let gas_price = 0; | ||
let height = Default::default(); | ||
|
||
let script = [ | ||
op::addi(0x10, RegId::ZERO, 8), | ||
op::addi(0x11, RegId::ZERO, 16), | ||
op::addi(0x12, RegId::ZERO, 32), | ||
op::addi(0x13, RegId::ZERO, 64), | ||
op::addi(0x14, RegId::ZERO, 128), | ||
op::ret(0x10), | ||
] | ||
.into_iter() | ||
.collect(); | ||
|
||
let consensus_params = ConsensusParameters::standard(); | ||
|
||
let tx = TransactionBuilder::script(script, vec![]) | ||
.script_gas_limit(gas_limit) | ||
.add_random_fee_input() | ||
.finalize() | ||
.into_checked(height, &consensus_params) | ||
.expect("failed to generate checked tx") | ||
.into_ready( | ||
gas_price, | ||
consensus_params.gas_costs(), | ||
consensus_params.fee_params(), | ||
) | ||
.unwrap(); | ||
|
||
let suite = vec![ | ||
( | ||
Breakpoint::script(0), | ||
vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(2), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(3), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)], | ||
), | ||
( | ||
Breakpoint::script(5), | ||
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)], | ||
), | ||
]; | ||
|
||
suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b)); | ||
|
||
let state = vm | ||
.transact(tx) | ||
.map(ProgramState::from) | ||
.expect("Failed to execute script!"); | ||
|
||
suite | ||
.into_iter() | ||
.fold(state, |state, (breakpoint, registers)| { | ||
let debug = state.debug_ref().expect("Expected breakpoint"); | ||
let b = debug | ||
.breakpoint() | ||
.expect("State without expected breakpoint"); | ||
|
||
assert_eq!(&breakpoint, b); | ||
registers.into_iter().for_each(|(r, w)| { | ||
assert_eq!(w, vm.registers()[r]); | ||
}); | ||
|
||
vm.resume().expect("Failed to resume") | ||
}); | ||
} | ||
|
||
#[test] | ||
fn single_stepping() { | ||
use fuel_asm::op; | ||
use fuel_tx::ConsensusParameters; | ||
|
||
let mut vm = Interpreter::<_, _, _>::with_memory_storage(); | ||
|
||
let gas_limit = 1_000_000; | ||
let height = Default::default(); | ||
let gas_price = 0; | ||
|
||
// Repeats the middle two instructions five times | ||
let script = [ | ||
op::addi(0x10, RegId::ZERO, 5), | ||
op::addi(0x11, 0x11, 1), | ||
op::jnei(0x10, 0x11, 1), | ||
op::ret(0x10), | ||
] | ||
.into_iter() | ||
.fold(state, |state, (breakpoint, registers)| { | ||
let debug = state.debug_ref().expect("Expected breakpoint"); | ||
.collect(); | ||
|
||
let consensus_params = ConsensusParameters::standard(); | ||
|
||
let tx = TransactionBuilder::script(script, vec![]) | ||
.script_gas_limit(gas_limit) | ||
.add_random_fee_input() | ||
.finalize() | ||
.into_checked(height, &consensus_params) | ||
.expect("failed to generate checked tx") | ||
.into_ready( | ||
gas_price, | ||
consensus_params.gas_costs(), | ||
consensus_params.fee_params(), | ||
) | ||
.unwrap(); | ||
|
||
Comment on lines
+172
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems we repeat this part several times, maybe makes sense to move it into a function |
||
vm.set_single_stepping(true); | ||
|
||
let mut state = vm | ||
.transact(tx) | ||
.map(ProgramState::from) | ||
.expect("Failed to execute script!"); | ||
|
||
let mut stops = Vec::new(); | ||
|
||
while let Some(debug) = state.debug_ref() { | ||
let b = debug | ||
.breakpoint() | ||
.expect("State without expected breakpoint"); | ||
|
||
assert_eq!(&breakpoint, b); | ||
registers.into_iter().for_each(|(r, w)| { | ||
assert_eq!(w, vm.registers()[r]); | ||
}); | ||
stops.push(b.pc()); | ||
|
||
vm.resume().expect("Failed to resume") | ||
}); | ||
} | ||
state = vm.resume().expect("Failed to resume"); | ||
} | ||
|
||
#[test] | ||
fn single_stepping() { | ||
use fuel_asm::op; | ||
use fuel_tx::ConsensusParameters; | ||
|
||
let mut vm = Interpreter::<_, _, _>::with_memory_storage(); | ||
|
||
let gas_limit = 1_000_000; | ||
let height = Default::default(); | ||
let gas_price = 0; | ||
|
||
// Repeats the middle two instructions five times | ||
let script = [ | ||
op::addi(0x10, RegId::ZERO, 5), | ||
op::addi(0x11, 0x11, 1), | ||
op::jnei(0x10, 0x11, 1), | ||
op::ret(0x10), | ||
] | ||
.into_iter() | ||
.collect(); | ||
|
||
let consensus_params = ConsensusParameters::standard(); | ||
|
||
let tx = TransactionBuilder::script(script, vec![]) | ||
.script_gas_limit(gas_limit) | ||
.add_random_fee_input() | ||
.finalize() | ||
.into_checked(height, &consensus_params) | ||
.expect("failed to generate checked tx") | ||
.into_ready( | ||
gas_price, | ||
consensus_params.gas_costs(), | ||
consensus_params.fee_params(), | ||
) | ||
.unwrap(); | ||
|
||
vm.set_single_stepping(true); | ||
|
||
let mut state = vm | ||
.transact(tx) | ||
.map(ProgramState::from) | ||
.expect("Failed to execute script!"); | ||
|
||
let mut stops = Vec::new(); | ||
|
||
while let Some(debug) = state.debug_ref() { | ||
let b = debug | ||
.breakpoint() | ||
.expect("State without expected breakpoint"); | ||
|
||
stops.push(b.pc()); | ||
|
||
state = vm.resume().expect("Failed to resume"); | ||
assert_eq!(stops, vec![0, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 12]); | ||
} | ||
|
||
assert_eq!(stops, vec![0, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 12]); | ||
#[test] | ||
fn resume_without_debug() { | ||
use fuel_asm::op; | ||
use fuel_tx::ConsensusParameters; | ||
|
||
let mut vm = Interpreter::<_, _, _>::with_memory_storage(); | ||
vm.resume() | ||
.expect_err("Expected error when resuming without debug"); | ||
|
||
let gas_limit = 1_000_000; | ||
let height = Default::default(); | ||
let gas_price = 0; | ||
|
||
// Just returns | ||
let script: Vec<u8> = [op::ret(0x10)].into_iter().collect(); | ||
|
||
let consensus_params = ConsensusParameters::standard(); | ||
|
||
let tx = TransactionBuilder::script(script, vec![]) | ||
.script_gas_limit(gas_limit) | ||
.add_random_fee_input() | ||
.finalize() | ||
.into_checked(height, &consensus_params) | ||
.expect("failed to generate checked tx") | ||
.into_ready( | ||
gas_price, | ||
consensus_params.gas_costs(), | ||
consensus_params.fee_params(), | ||
) | ||
.unwrap(); | ||
|
||
let _ = vm | ||
.transact(tx) | ||
.map(ProgramState::from) | ||
.expect("Failed to execute script!"); | ||
|
||
vm.resume() | ||
.expect_err("Expected error when resuming without debug"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to use some constant names for registers. It will connect them here with the values in
suite