Skip to content

Commit

Permalink
Implement MemorySegmentManager.LoadData (#5)
Browse files Browse the repository at this point in the history
* Switch memory structure to map

* Remove uneeded fns

* Fix Memory initialization in tests + remove implementation-specific checks

* Add tests

* Fix test assertion

* Add load_data fn

* Add tests

* Add test

* Rename Add to AddSegment

* Update name
  • Loading branch information
fmoletta committed Jul 31, 2023
1 parent a2f4749 commit fbe6145
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
92 changes: 92 additions & 0 deletions pkg/vm/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
13 changes: 13 additions & 0 deletions pkg/vm/memory/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fbe6145

Please sign in to comment.