From 56a26604776467728912a8f7cb8dc214c02e0d07 Mon Sep 17 00:00:00 2001 From: Diego Civini Date: Fri, 30 May 2025 11:13:16 -0300 Subject: [PATCH 1/3] Add tests for .gen_arg() and .write_arg() --- vm/src/vm/vm_memory/memory_segments.rs | 42 ++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index e18e739346..1cfaa28add 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -620,6 +620,22 @@ mod tests { ); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn write_arg_invalid_type() { + let data = vec!["Invalid Type 1", "Invalid Type 2"]; + let ptr = Relocatable::from((1, 0)); + let mut segments = MemorySegmentManager::new(); + for _ in 0..2 { + segments.add(); + } + + let exec = segments.write_arg(ptr, &data); + + assert_eq!(exec, Err(MemoryError::WriteArg)); + assert!(segments.memory.data[1].is_empty()) + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn segment_default() { @@ -877,11 +893,11 @@ mod tests { ); } - /// Test that the call to .gen_arg() with a Vec writes its - /// contents into a new segment and returns a pointer to it. + /// Test that the call to .gen_arg() with a Vec + /// writes its contents into a new segment and returns a pointer to it. #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn gen_arg_vec_relocatable() { + fn gen_arg_vec_mayberelocatables() { let mut memory_segment_manager = MemorySegmentManager::new(); assert_matches!( @@ -897,6 +913,26 @@ mod tests { ); } + /// Test that call to .gen_arg() with a Vec writes its + /// contentsinto a new segment and returns a pointer to it. + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn gen_arg_vec_relocatables() { + let mut memory_segment_manager = MemorySegmentManager::new(); + + assert_matches!( + memory_segment_manager.gen_arg( + &vec![ + Relocatable::from((0, 0)), + Relocatable::from((0, 1)), + Relocatable::from((0, 2)), + Relocatable::from((0, 3)), + ], + ), + Ok(x) if x == mayberelocatable!(0, 0) + ); + } + /// Test that the call to .gen_arg() with any other argument returns a not /// implemented error. #[test] From d2f8b0d733ee1ae96d5d7523b438a3605c63a0cf Mon Sep 17 00:00:00 2001 From: Diego Civini Date: Fri, 30 May 2025 12:38:52 -0300 Subject: [PATCH 2/3] Add test for .is_valid_memory_value() --- vm/src/vm/vm_memory/memory_segments.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 1cfaa28add..48c84e2503 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -698,6 +698,18 @@ mod tests { ); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn is_valid_memory_value_felt() { + let mut segment_manager = MemorySegmentManager::new(); + + segment_manager.segment_used_sizes = Some(vec![10]); + assert_eq!( + segment_manager.is_valid_memory_value(&mayberelocatable!(1)), + Ok(true), + ); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_memory_holes_missing_segment_used_sizes() { From 404c8c9260b42d2bcd4d64150d81718c4e420402 Mon Sep 17 00:00:00 2001 From: Diego Civini Date: Fri, 30 May 2025 17:56:05 -0300 Subject: [PATCH 3/3] Assert contents of memory segments --- vm/src/vm/vm_memory/memory_segments.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 48c84e2503..f21a0fb5fd 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -923,10 +923,19 @@ mod tests { ), Ok(x) if x == mayberelocatable!(0, 0) ); + assert_eq!( + memory_segment_manager.memory.data[0], + vec![ + MemoryCell::new(MaybeRelocatable::from((0, 0))), + MemoryCell::new(MaybeRelocatable::from((0, 1))), + MemoryCell::new(MaybeRelocatable::from((0, 2))), + MemoryCell::new(MaybeRelocatable::from((0, 3))), + ], + ); } /// Test that call to .gen_arg() with a Vec writes its - /// contentsinto a new segment and returns a pointer to it. + /// contents into a new segment and returns a pointer to it. #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn gen_arg_vec_relocatables() { @@ -943,6 +952,15 @@ mod tests { ), Ok(x) if x == mayberelocatable!(0, 0) ); + assert_eq!( + memory_segment_manager.memory.data[0], + vec![ + MemoryCell::new(MaybeRelocatable::from((0, 0))), + MemoryCell::new(MaybeRelocatable::from((0, 1))), + MemoryCell::new(MaybeRelocatable::from((0, 2))), + MemoryCell::new(MaybeRelocatable::from((0, 3))), + ], + ); } /// Test that the call to .gen_arg() with any other argument returns a not