From f96d53f48c89c5cc89bb9a3b4cebaee48d1e607a Mon Sep 17 00:00:00 2001 From: jrigada Date: Thu, 2 Feb 2023 13:04:15 +0200 Subject: [PATCH 1/5] add test for display memory --- src/vm/vm_memory/memory.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index ea721ce928..8d046841b3 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -7,6 +7,7 @@ use felt::Felt; use std::{ borrow::Cow, collections::{HashMap, HashSet}, + fmt::{Display, Formatter}, mem::swap, }; @@ -317,6 +318,20 @@ impl Memory { } } +impl Display for Memory { + fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { + writeln!(f, "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, "}}") + } +} + pub(crate) trait RelocateValue<'a, Input: 'a, Output: 'a> { fn relocate_value(&self, value: Input) -> Output; } @@ -991,4 +1006,20 @@ mod memory_tests { ); assert!(memory.temp_data.is_empty()); } + + #[test] + fn test_memory_display() { + let memory = memory![ + ((0, 0), 1), + ((0, 1), 2), + ((0, 2), 3), + ((1, 0), 4), + ((1, 1), 5), + ((1, 2), 6) + ]; + assert_eq!( + format!("{}", memory), + "Memory {\n 0,0 : 1\n 0,1 : 2\n 0,2 : 3\n 1,0 : 4\n 1,1 : 5\n 1,2 : 6\n}\n" + ) + } } From 999f5e73c4ba59dd6a1f9338ae82c946f9d79b24 Mon Sep 17 00:00:00 2001 From: jrigada Date: Thu, 2 Feb 2023 13:16:18 +0200 Subject: [PATCH 2/5] cargo fmt --- src/vm/vm_memory/memory.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index aa4b9c6ca3..0fb0aba05a 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -8,7 +8,7 @@ use std::{ borrow::Cow, collections::{HashMap, HashSet}, fmt::{Display, Formatter}, - mem::swap + mem::swap, }; pub struct ValidationRule( @@ -1256,7 +1256,7 @@ mod memory_tests { "Memory {\n 0,0 : 1\n 0,1 : 2\n 0,2 : 3\n 1,0 : 4\n 1,1 : 5\n 1,2 : 6\n}\n" ); } - + #[test] fn relocate_memory_into_existing_segment_temporary_values_in_temporary_memory() { let mut memory = memory![ From f9d82bce5ef810383c86df282f141ee2471cf296 Mon Sep 17 00:00:00 2001 From: jrigada Date: Fri, 3 Feb 2023 16:38:53 +0200 Subject: [PATCH 3/5] display temporary segments --- src/vm/vm_memory/memory.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index 0fb0aba05a..1786aa445d 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -8,7 +8,6 @@ use std::{ borrow::Cow, collections::{HashMap, HashSet}, fmt::{Display, Formatter}, - mem::swap, }; pub struct ValidationRule( @@ -304,11 +303,20 @@ impl Memory { impl Display for Memory { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - writeln!(f, "Memory {{")?; + 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}")?; + } + } + } + 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, " ({i},{j}) : {cell}")?; } } } @@ -1243,18 +1251,24 @@ mod memory_tests { #[test] fn test_memory_display() { - let memory = memory![ + let mut memory = memory![ ((0, 0), 1), - ((0, 1), 2), + ((0, 1), (-1, 0)), ((0, 2), 3), - ((1, 0), 4), + ((1, 0), (-1, 1)), ((1, 1), 5), - ((1, 2), 6) + ((1, 2), (-1, 2)) ]; + + memory.temp_data = vec![vec![ + mayberelocatable!(-1, 0).into(), + mayberelocatable!(8).into(), + mayberelocatable!(9).into(), + ]]; + assert_eq!( format!("{}", memory), - "Memory {\n 0,0 : 1\n 0,1 : 2\n 0,2 : 3\n 1,0 : 4\n 1,1 : 5\n 1,2 : 6\n}\n" - ); + "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] From 20e7a37bd792a2cd4d0906cc347dfe6aaa5faa1e Mon Sep 17 00:00:00 2001 From: jrigada Date: Mon, 13 Feb 2023 12:42:05 +0100 Subject: [PATCH 4/5] display whole memory without distinction and fix segment indexes --- src/vm/vm_memory/memory.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index 1786aa445d..5370cae012 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -303,16 +303,15 @@ impl Memory { impl Display for Memory { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - writeln!(f, "temp_memory {{")?; + writeln!(f, "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}")?; + let temp_segment = i + 1; + writeln!(f, " (-{temp_segment},{j}) : {cell}")?; } } } - 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 { @@ -1268,7 +1267,7 @@ mod memory_tests { 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"); + "memory {\n (-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] From 9d9aab1cbabba8d44cb092867ce58617fa8196d2 Mon Sep 17 00:00:00 2001 From: jrigada Date: Mon, 13 Feb 2023 18:48:30 +0100 Subject: [PATCH 5/5] remove memory text from display of memory --- src/vm/vm_memory/memory.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index 5370cae012..09b9f7100c 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -303,19 +303,18 @@ impl Memory { impl Display for Memory { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - writeln!(f, "memory {{")?; 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}")?; + 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, "({i},{j}) : {cell}")?; } } } @@ -1267,7 +1266,7 @@ mod memory_tests { assert_eq!( format!("{}", memory), - "memory {\n (-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"); + "(-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]