Skip to content

Commit

Permalink
Add a couple basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh committed Oct 31, 2024
1 parent 1def915 commit e87cd13
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,62 @@ mod memory_tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn relocate_address_to_integer() {
let mut memory = Memory::new();
memory
.add_relocation_rule_maybe_relocatable((-1, 0).into(), 0.into())
.unwrap();
memory
.add_relocation_rule_maybe_relocatable((-2, 0).into(), 42.into())
.unwrap();

assert_eq!(
Memory::relocate_address((-1, 0).into(), &memory.relocation_rules).unwrap(),
MaybeRelocatable::Int(0.into()),
);
assert_eq!(
Memory::relocate_address((-2, 0).into(), &memory.relocation_rules).unwrap(),
MaybeRelocatable::Int(42.into()),
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn relocate_address_integer_no_duplicates() {
let mut memory = Memory::new();
memory
.add_relocation_rule_maybe_relocatable((-1, 0).into(), 1.into())
.unwrap();
assert_eq!(
memory.add_relocation_rule_maybe_relocatable((-1, 0).into(), 42.into()),
Err(MemoryError::DuplicatedRelocation(-1))
);
assert_eq!(
memory.add_relocation_rule_maybe_relocatable((-1, 0).into(), (2, 0).into()),
Err(MemoryError::DuplicatedRelocation(-1))
);

assert_eq!(
Memory::relocate_address((-1, 0).into(), &memory.relocation_rules).unwrap(),
MaybeRelocatable::Int(1.into()),
);

memory
.add_relocation_rule_maybe_relocatable((-2, 0).into(), (3, 0).into())
.unwrap();
assert_eq!(
memory.add_relocation_rule_maybe_relocatable((-2, 0).into(), 1.into()),
Err(MemoryError::DuplicatedRelocation(-2))
);

assert_eq!(
Memory::relocate_address((-2, 0).into(), &memory.relocation_rules).unwrap(),
MaybeRelocatable::RelocatableValue((3, 0).into()),
);
}

#[test]
fn mark_address_as_accessed() {
let mut memory = memory![((0, 0), 0)];
Expand Down

0 comments on commit e87cd13

Please sign in to comment.