Skip to content

Commit

Permalink
fix: fix bug in the Program deser, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Oct 22, 2024
1 parent b2294a1 commit e548f80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Deserializable for Program {
let kernel = source.read()?;
let entrypoint = MastNodeId::from_u32_safe(source.read_u32()?, &mast_forest)?;

if mast_forest.is_procedure_root(entrypoint) {
if !mast_forest.is_procedure_root(entrypoint) {
return Err(DeserializationError::InvalidValue(format!(
"entrypoint {entrypoint} is not a procedure"
)));
Expand Down
1 change: 1 addition & 0 deletions miden/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod cli;
mod exec_iters;
mod flow_control;
mod operations;
mod program;

// TESTS
// ================================================================================================
Expand Down
61 changes: 61 additions & 0 deletions miden/tests/integration/program.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use alloc::vec::Vec;

use assembly::Assembler;
use miden_vm::Program;
use test_utils::serde::{Deserializable, Serializable};

// PROGRAM SERIALIZATION AND DESERIALIZATION TESTS
// =================================================================

#[test]
fn test_program_serde_simple() {
let source = "
begin
push.1.2
add
drop
end
";

let assembler = Assembler::default();
let original_program = assembler.assemble_program(source).unwrap();

let mut target = Vec::new();
original_program.write_into(&mut target);
let deserialized_program = Program::read_from_bytes(&target).unwrap();

assert_eq!(original_program, deserialized_program);
}

#[test]
fn test_program_serde_with_decorators() {
let source = "
const.DEFAULT_CONST=100
proc.foo
push.1.2 add
debug.stack.8
end
begin
emit.DEFAULT_CONST
exec.foo
debug.stack.4
drop
trace.DEFAULT_CONST
end
";

let assembler = Assembler::default().with_debug_mode(true);
let original_program = assembler.assemble_program(source).unwrap();

let mut target = Vec::new();
original_program.write_into(&mut target);
let deserialized_program = Program::read_from_bytes(&target).unwrap();

assert_eq!(original_program, deserialized_program);
}

0 comments on commit e548f80

Please sign in to comment.