-
Notifications
You must be signed in to change notification settings - Fork 131
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
Changes from 4 commits
f96d53f
230d632
999f5e7
f9d82bc
20e7a37
9d9aab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ use felt::Felt; | |
use std::{ | ||
borrow::Cow, | ||
collections::{HashMap, HashSet}, | ||
fmt::{Display, Formatter}, | ||
}; | ||
|
||
pub struct ValidationRule( | ||
|
@@ -300,6 +301,29 @@ impl Memory { | |
} | ||
} | ||
|
||
impl Display for Memory { | ||
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { | ||
writeln!(f, "temp_memory {{")?; | ||
for (i, segment) in self.temp_data.iter().enumerate() { | ||
for (j, cell) in segment.iter().enumerate() { | ||
if let Some(cell) = cell { | ||
writeln!(f, " ({i},{j}) : {cell}")?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember that temporary memory segment indexes start from -1, but we store them from 0 (cuz vec). So when printing out the temporary addresses we should be printing |
||
} | ||
} | ||
} | ||
writeln!(f, "}}")?; | ||
writeln!(f, "real_memory {{")?; | ||
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, "}}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -1225,6 +1249,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), | ||
"temp_memory {\n (0,0) : -1:0\n (0,1) : 8\n (0,2) : 9\n}\nreal_memory {\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![ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could avoid making distinctions between real and temporary memory and just pritnt them out as a single memory. Temporary segments can be identified by their negative index