Skip to content

Add optional arguments budget and env to run_program test helper #294

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

Merged
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
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/c_rust_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn do_test(data: &[u8]) {
};

let (program, witness) = data.split_at(prog_len);
let c_result = run_program(program, witness, TestUpTo::CheckOneOne);
let c_result = run_program(program, witness, TestUpTo::CheckOneOne, None, None);

let prog_iter = BitIter::from(program);
let wit_iter = BitIter::from(witness);
Expand Down
15 changes: 11 additions & 4 deletions simplicity-sys/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::tests::ffi::{
type_inference::simplicity_mallocTypeInference,
SimplicityErr,
};
use crate::CElementsTxEnv;

/// The result of parsing, typechecking, and running a Simplicity program
/// through the C FFI
Expand Down Expand Up @@ -72,7 +73,8 @@ pub fn run_test(
target_ihr: &[u32; 8],
cost_bound: ubounded,
) {
let result = run_program(program, witness, TestUpTo::Everything).expect("running program");
let result =
run_program(program, witness, TestUpTo::Everything, None, None).expect("running program");
assert_eq!(result.amr, CSha256Midstate { s: *target_amr });
assert_eq!(result.cmr, CSha256Midstate { s: *target_cmr });
assert_eq!(result.ihr, CSha256Midstate { s: *target_ihr });
Expand All @@ -90,7 +92,8 @@ pub fn run_test_fail(
target_ihr: &[u32; 8],
cost_bound: ubounded,
) {
let result = run_program(program, witness, TestUpTo::Everything).expect("running program");
let result =
run_program(program, witness, TestUpTo::Everything, None, None).expect("running program");
assert_eq!(result.amr, CSha256Midstate { s: *target_amr });
assert_eq!(result.cmr, CSha256Midstate { s: *target_cmr });
assert_eq!(result.ihr, CSha256Midstate { s: *target_ihr });
Expand All @@ -110,10 +113,13 @@ impl Drop for FreeOnDrop {
/// Run a program and return data about it
///
/// This is mostly a direct port of `run_program` in C `tests.c`.
/// WARNING: empty environment could lead to a crash (depending on which jets are used).
pub fn run_program(
program: &[u8],
witness: &[u8],
test_up_to: TestUpTo,
budget: Option<u32>,
env: Option<&CElementsTxEnv>,
) -> Result<TestOutput, SimplicityErr> {
let mut result = TestOutput {
amr: CSha256Midstate::default(),
Expand Down Expand Up @@ -266,8 +272,9 @@ pub fn run_program(
}

// 9. Run the program
result.eval_result =
simplicity_evalTCOProgram(dag, type_dag, len, ptr::null(), ptr::null());
let budget_ptr = budget.map(|b| b as *const _).unwrap_or(ptr::null());
let env_ptr = env.map(|e| e as *const _).unwrap_or(ptr::null());
result.eval_result = simplicity_evalTCOProgram(dag, type_dag, len, budget_ptr, env_ptr);
}

Ok(result)
Expand Down
10 changes: 8 additions & 2 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,14 @@ mod tests {
fn check_merkle_roots(test: &TestData) {
let prog = BitIter::from(test.prog.as_slice());
let witness = BitIter::from(test.witness.as_slice());
ffi::tests::run_program(&test.prog, &test.witness, ffi::tests::TestUpTo::CheckOneOne)
.unwrap();
ffi::tests::run_program(
&test.prog,
&test.witness,
ffi::tests::TestUpTo::CheckOneOne,
None,
None,
)
.unwrap();
let prog = RedeemNode::<Elements>::decode(prog, witness).unwrap();
assert_eq!(prog.cmr().to_byte_array(), test.cmr);
assert_eq!(prog.amr().to_byte_array(), test.amr);
Expand Down
Loading