Skip to content

Commit 4f02f5a

Browse files
authored
Improve memory_segments coverage (#2110)
* Add tests for .gen_arg() and .write_arg() * Add test for .is_valid_memory_value() * Assert contents of memory segments
1 parent 2204910 commit 4f02f5a

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

vm/src/vm/vm_memory/memory_segments.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,22 @@ mod tests {
620620
);
621621
}
622622

623+
#[test]
624+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
625+
fn write_arg_invalid_type() {
626+
let data = vec!["Invalid Type 1", "Invalid Type 2"];
627+
let ptr = Relocatable::from((1, 0));
628+
let mut segments = MemorySegmentManager::new();
629+
for _ in 0..2 {
630+
segments.add();
631+
}
632+
633+
let exec = segments.write_arg(ptr, &data);
634+
635+
assert_eq!(exec, Err(MemoryError::WriteArg));
636+
assert!(segments.memory.data[1].is_empty())
637+
}
638+
623639
#[test]
624640
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
625641
fn segment_default() {
@@ -682,6 +698,18 @@ mod tests {
682698
);
683699
}
684700

701+
#[test]
702+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
703+
fn is_valid_memory_value_felt() {
704+
let mut segment_manager = MemorySegmentManager::new();
705+
706+
segment_manager.segment_used_sizes = Some(vec![10]);
707+
assert_eq!(
708+
segment_manager.is_valid_memory_value(&mayberelocatable!(1)),
709+
Ok(true),
710+
);
711+
}
712+
685713
#[test]
686714
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
687715
fn get_memory_holes_missing_segment_used_sizes() {
@@ -877,11 +905,11 @@ mod tests {
877905
);
878906
}
879907

880-
/// Test that the call to .gen_arg() with a Vec<Relocatable> writes its
881-
/// contents into a new segment and returns a pointer to it.
908+
/// Test that the call to .gen_arg() with a Vec<MaybeRelocatable::Relocatable>
909+
/// writes its contents into a new segment and returns a pointer to it.
882910
#[test]
883911
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
884-
fn gen_arg_vec_relocatable() {
912+
fn gen_arg_vec_mayberelocatables() {
885913
let mut memory_segment_manager = MemorySegmentManager::new();
886914

887915
assert_matches!(
@@ -895,6 +923,44 @@ mod tests {
895923
),
896924
Ok(x) if x == mayberelocatable!(0, 0)
897925
);
926+
assert_eq!(
927+
memory_segment_manager.memory.data[0],
928+
vec![
929+
MemoryCell::new(MaybeRelocatable::from((0, 0))),
930+
MemoryCell::new(MaybeRelocatable::from((0, 1))),
931+
MemoryCell::new(MaybeRelocatable::from((0, 2))),
932+
MemoryCell::new(MaybeRelocatable::from((0, 3))),
933+
],
934+
);
935+
}
936+
937+
/// Test that call to .gen_arg() with a Vec<Relocatable> writes its
938+
/// contents into a new segment and returns a pointer to it.
939+
#[test]
940+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
941+
fn gen_arg_vec_relocatables() {
942+
let mut memory_segment_manager = MemorySegmentManager::new();
943+
944+
assert_matches!(
945+
memory_segment_manager.gen_arg(
946+
&vec![
947+
Relocatable::from((0, 0)),
948+
Relocatable::from((0, 1)),
949+
Relocatable::from((0, 2)),
950+
Relocatable::from((0, 3)),
951+
],
952+
),
953+
Ok(x) if x == mayberelocatable!(0, 0)
954+
);
955+
assert_eq!(
956+
memory_segment_manager.memory.data[0],
957+
vec![
958+
MemoryCell::new(MaybeRelocatable::from((0, 0))),
959+
MemoryCell::new(MaybeRelocatable::from((0, 1))),
960+
MemoryCell::new(MaybeRelocatable::from((0, 2))),
961+
MemoryCell::new(MaybeRelocatable::from((0, 3))),
962+
],
963+
);
898964
}
899965

900966
/// Test that the call to .gen_arg() with any other argument returns a not

0 commit comments

Comments
 (0)