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

Add Display trait to Memory struct #812

Merged
merged 6 commits into from
Feb 13, 2023
Merged
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
44 changes: 44 additions & 0 deletions src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use felt::Felt;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
fmt::{Display, Formatter},
};

pub struct ValidationRule(
Expand Down Expand Up @@ -300,6 +301,27 @@ impl Memory {
}
}

impl Display for Memory {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
for (i, segment) in self.temp_data.iter().enumerate() {
for (j, cell) in segment.iter().enumerate() {
if let Some(cell) = cell {
let temp_segment = i + 1;
writeln!(f, "(-{temp_segment},{j}) : {cell}")?;
}
}
}
for (i, segment) in self.data.iter().enumerate() {
for (j, cell) in segment.iter().enumerate() {
if let Some(cell) = cell {
writeln!(f, "({i},{j}) : {cell}")?;
}
}
}
writeln!(f, "}}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These symbols make it look like a Debug rather than a Display, we could avoid them

}
}

pub(crate) trait RelocateValue<'a, Input: 'a, Output: 'a> {
fn relocate_value(&self, value: Input) -> Output;
}
Expand Down Expand Up @@ -1225,6 +1247,28 @@ mod memory_tests {
assert!(memory.temp_data.is_empty());
}

#[test]
fn test_memory_display() {
let mut memory = memory![
((0, 0), 1),
((0, 1), (-1, 0)),
((0, 2), 3),
((1, 0), (-1, 1)),
((1, 1), 5),
((1, 2), (-1, 2))
];

memory.temp_data = vec![vec![
mayberelocatable!(-1, 0).into(),
mayberelocatable!(8).into(),
mayberelocatable!(9).into(),
]];

assert_eq!(
format!("{}", memory),
"(-1,0) : -1:0\n(-1,1) : 8\n(-1,2) : 9\n(0,0) : 1\n(0,1) : -1:0\n(0,2) : 3\n(1,0) : -1:1\n(1,1) : 5\n(1,2) : -1:2\n}\n");
}

#[test]
fn relocate_memory_into_existing_segment_temporary_values_in_temporary_memory() {
let mut memory = memory![
Expand Down