Skip to content
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

test: unit aot #584

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ criterion = { version = "0.5.1", features = ["html_reports"] }
lambdaworks-math = "0.7"
pretty_assertions_sorted = "1.2.3"
proptest = "1.4"
rstest = "0.19.0"
test-case = "3.3"
walkdir = "2"
serde_json = { version = "1.0" }
Expand Down
28 changes: 28 additions & 0 deletions src/cache/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,31 @@ where
f.write_str("AotProgramCache")
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{utils::test::load_cairo, values::JitValue};
use starknet_types_core::felt::Felt;

#[test]
fn test_aot_compile_and_insert() {
let native_context = NativeContext::new();
let mut cache = AotProgramCache::new(&native_context);

let (_, program) = load_cairo! {
fn run_test() -> felt252 {
42
}
};

let function_id = &program.funcs.first().expect("should have a function").id;
let executor = cache.compile_and_insert((), &program, OptLevel::default());
let res = executor
.invoke_dynamic(function_id, &[], Some(u128::MAX))
.expect("should run");

// After compiling and inserting the program, we should be able to run it.
assert_eq!(res.return_value, JitValue::Felt252(Felt::from(42)));
}
}
131 changes: 131 additions & 0 deletions src/executor/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,134 @@ impl AotNativeExecutor {
&self.registry.get_function(function_id).unwrap().signature
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
context::NativeContext,
utils::test::{load_cairo, load_starknet, TestSyscallHandler},
};
use cairo_lang_sierra::program::Program;
use rstest::*;

#[fixture]
fn program() -> Program {
let (_, program) = load_cairo! {
use core::starknet::{SyscallResultTrait, get_block_hash_syscall};

fn run_test() -> felt252 {
42
}

fn get_block_hash() -> felt252 {
get_block_hash_syscall(1).unwrap_syscall()
}
};
program
}

#[fixture]
fn starknet_program() -> Program {
let (_, program) = load_starknet! {
#[starknet::interface]
trait ISimpleStorage<TContractState> {
fn get(self: @TContractState) -> u128;
}

#[starknet::contract]
mod contract {
#[storage]
struct Storage {}

#[abi(embed_v0)]
impl ISimpleStorageImpl of super::ISimpleStorage<ContractState> {
fn get(self: @ContractState) -> u128 {
42
}
}
}
};
program
}

#[rstest]
fn test_invoke_dynamic(program: Program) {
let native_context = NativeContext::new();
let module = native_context
.compile(&program, None)
.expect("failed to compile context");
let executor = AotNativeExecutor::from_native_module(module, OptLevel::default());

// The first function in the program is `run_test`.
let entrypoint_function_id = &program.funcs.first().expect("should have a function").id;

let result = executor
.invoke_dynamic(entrypoint_function_id, &[], Some(u128::MAX))
.unwrap();

assert_eq!(result.return_value, JitValue::Felt252(Felt::from(42)));
}

#[rstest]
fn test_invoke_dynamic_with_syscall_handler(program: Program) {
let native_context = NativeContext::new();
let module = native_context
.compile(&program, None)
.expect("failed to compile context");
let executor = AotNativeExecutor::from_native_module(module, OptLevel::default());

// The second function in the program is `get_block_hash`.
let entrypoint_function_id = &program.funcs.get(1).expect("should have a function").id;

let mut syscall_handler = TestSyscallHandler;
let result = executor
.invoke_dynamic_with_syscall_handler(
entrypoint_function_id,
&[],
Some(u128::MAX),
syscall_handler.clone(),
)
.unwrap();

let expected_value = JitValue::Enum {
tag: 0,
value: JitValue::Struct {
fields: vec![JitValue::Felt252(
syscall_handler.get_block_hash(0, &mut 0).unwrap(),
)],
debug_name: Some("Tuple<felt252>".into()),
}
.into(),
debug_name: Some("core::panics::PanicResult::<(core::felt252,)>".into()),
};
assert_eq!(result.return_value, expected_value);
}

#[rstest]
fn test_invoke_contract_dynamic(starknet_program: Program) {
let native_context = NativeContext::new();
let module = native_context
.compile(&starknet_program, None)
.expect("failed to compile context");
let executor = AotNativeExecutor::from_native_module(module, OptLevel::default());

// The last function in the program is the `get` wrapper function.
let entrypoint_function_id = &starknet_program
.funcs
.last()
.expect("should have a function")
.id;

let result = executor
.invoke_contract_dynamic(
entrypoint_function_id,
&[],
Some(u128::MAX),
TestSyscallHandler,
)
.unwrap();

assert_eq!(result.return_values, vec![Felt::from(42)]);
}
}
Loading
Loading