Skip to content

Commit

Permalink
Fix CairoRunner::write_output so that it prints missing and relocat…
Browse files Browse the repository at this point in the history
…able values (#853)

* Print relocatables & missing members in write_output

* Add test
  • Loading branch information
fmoletta authored Feb 17, 2023
1 parent 2016148 commit eaa7ab1
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,11 @@ impl CairoRunner {
vm: &mut VirtualMachine,
stdout: &mut dyn io::Write,
) -> Result<(), RunnerError> {
let builtin = vm.builtin_runners.iter().find_map(|(k, v)| match k {
&OUTPUT_BUILTIN_NAME => Some(v),
_ => None,
});
let builtin = match builtin {
let (_, builtin) = match vm
.builtin_runners
.iter()
.find(|(k, _)| k == &OUTPUT_BUILTIN_NAME)
{
Some(x) => x,
_ => return Ok(()),
};
Expand All @@ -895,13 +895,18 @@ impl CairoRunner {
let segment_index = builtin.base();
#[allow(deprecated)]
for i in 0..segment_used_sizes[segment_index] {
let value = vm
let formatted_value = match vm
.segments
.memory
.get_integer((segment_index as isize, i).into())
.map_err(|_| RunnerError::MemoryGet((segment_index as isize, i).into()))?
.to_bigint();
writeln!(stdout, "{value}").map_err(|_| RunnerError::WriteFail)?;
.get(&Relocatable::from((segment_index as isize, i)))
{
Some(val) => match val.as_ref() {
MaybeRelocatable::Int(num) => format!("{}", num.to_bigint()),
MaybeRelocatable::RelocatableValue(rel) => format!("{}", rel),
},
_ => "<missing>".to_string(),
};
writeln!(stdout, "{formatted_value}").map_err(|_| RunnerError::WriteFail)?;
}

Ok(())
Expand Down Expand Up @@ -2816,6 +2821,49 @@ mod tests {
assert_eq!(String::from_utf8(stdout), Ok(String::from("1\n17\n")));
}

#[test]
/*Program used:
%builtins output
func main{output_ptr: felt*}() {
//Memory Gap + Relocatable value
assert [output_ptr + 1] = cast(output_ptr, felt);
let output_ptr = output_ptr + 2;
return ();
}*/
fn write_output_from_program_gap_relocatable_output() {
//Initialization Phase
let program = program!(
builtins = vec![OUTPUT_BUILTIN_NAME],
data = vec_data!(
(4612671187288162301),
(5198983563776458752),
(2),
(2345108766317314046)
),
main = Some(0),
);
let mut cairo_runner = cairo_runner!(program);
let mut vm = vm!();
cairo_runner.initialize_builtins(&mut vm).unwrap();
cairo_runner.initialize_segments(&mut vm, None);
let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap();
cairo_runner.initialize_vm(&mut vm).unwrap();
//Execution Phase
let mut hint_processor = BuiltinHintProcessor::new_empty();
assert_matches!(
cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor),
Ok(())
);

let mut stdout = Vec::<u8>::new();
cairo_runner.write_output(&mut vm, &mut stdout).unwrap();
assert_eq!(
String::from_utf8(stdout),
Ok(String::from("<missing>\n2:0\n"))
);
}

#[test]
fn write_output_from_preset_memory_neg_output() {
let program = program![OUTPUT_BUILTIN_NAME];
Expand Down

0 comments on commit eaa7ab1

Please sign in to comment.