diff --git a/pkg/vm/memory/memory_test.go b/pkg/vm/memory/memory_test.go index 0614f08a..15a94edd 100644 --- a/pkg/vm/memory/memory_test.go +++ b/pkg/vm/memory/memory_test.go @@ -123,3 +123,95 @@ func TestMemoryInsertUnallocatedSegment(t *testing.T) { t.Errorf("Insertion on unallocated segment should fail") } } + +func TestMemorySegmentsLoadDataUnallocatedSegment(t *testing.T) { + mem_manager := memory.NewMemorySegmentManager() + + ptr := memory.NewRelocatable(1, 0) + data := []memory.MaybeRelocatable{*memory.NewMaybeRelocatableInt(5)} + + // Load Data + _, err := mem_manager.LoadData(ptr, &data) + if err == nil { + t.Errorf("Insertion on unallocated segment should fail") + } +} + +func TestMemorySegmentsLoadDataOneElement(t *testing.T) { + mem_manager := memory.NewMemorySegmentManager() + mem_manager.AddSegment() + + ptr := memory.NewRelocatable(0, 0) + val := memory.NewMaybeRelocatableInt(5) + data := []memory.MaybeRelocatable{*val} + + // Load Data + end_ptr, err := mem_manager.LoadData(ptr, &data) + if err != nil { + t.Errorf("LoadData error in test: %s", err) + } + + // Check returned ptr + expected_end_ptr := memory.NewRelocatable(0, 1) + if !reflect.DeepEqual(end_ptr, expected_end_ptr) { + t.Errorf("LoadData returned wrong ptr") + } + + // Check inserted value + res_val, err := mem_manager.Memory.Get(ptr) + if err != nil { + t.Errorf("Get error in test: %s", err) + } + + // Check that the original and the retrieved values are the same + if !reflect.DeepEqual(res_val, val) { + t.Errorf("Inserted value and original value are not the same") + } +} + +func TestMemorySegmentsLoadDataTwoElements(t *testing.T) { + mem_manager := memory.NewMemorySegmentManager() + mem_manager.AddSegment() + + ptr := memory.NewRelocatable(0, 0) + val := memory.NewMaybeRelocatableInt(5) + val2 := memory.NewMaybeRelocatableInt(5) + data := []memory.MaybeRelocatable{*val, *val2} + + // Load Data + end_ptr, err := mem_manager.LoadData(ptr, &data) + if err != nil { + t.Errorf("LoadData error in test: %s", err) + } + + // Check returned ptr + expected_end_ptr := memory.NewRelocatable(0, 2) + if !reflect.DeepEqual(end_ptr, expected_end_ptr) { + t.Errorf("LoadData returned wrong ptr") + } + + // Check inserted values + + // val + res_val, err := mem_manager.Memory.Get(ptr) + if err != nil { + t.Errorf("Get error in test: %s", err) + } + + // Check that the original and the retrieved values are the same + if !reflect.DeepEqual(res_val, val) { + t.Errorf("Inserted value and original value are not the same") + } + + //val2 + ptr2 := memory.NewRelocatable(0, 1) + res_val2, err := mem_manager.Memory.Get(ptr2) + if err != nil { + t.Errorf("Get error in test: %s", err) + } + + // Check that the original and the retrieved values are the same + if !reflect.DeepEqual(res_val2, val2) { + t.Errorf("Inserted value and original value are not the same") + } +} diff --git a/pkg/vm/memory/segments.go b/pkg/vm/memory/segments.go index e0839c2b..d9b4f4d6 100644 --- a/pkg/vm/memory/segments.go +++ b/pkg/vm/memory/segments.go @@ -19,3 +19,16 @@ func (m *MemorySegmentManager) AddSegment() Relocatable { m.Memory.num_segments += 1 return ptr } + +// Writes data into the memory from address ptr and returns the first address after the data. +// If any insertion fails, returns (0,0) and the memory insertion error +func (m *MemorySegmentManager) LoadData(ptr Relocatable, data *[]MaybeRelocatable) (Relocatable, error) { + for _, val := range *data { + err := m.Memory.Insert(ptr, &val) + if err != nil { + return Relocatable{0, 0}, err + } + ptr.offset += 1 + } + return ptr, nil +}