-
Notifications
You must be signed in to change notification settings - Fork 14
Memory relocation #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
69c93b1
Add segments relocation
gabrielbosio 4ec1c0c
Add segment relocation tests
gabrielbosio 8661157
Normalize name convention
gabrielbosio 1f4bcb0
Add and fix segments test
gabrielbosio 13f806f
Add missing memory methods
gabrielbosio 119d47d
Add memory relocation
gabrielbosio 8b446b0
Merge branch 'main' into memory_relocation
gabrielbosio 0a90b93
Improve get methods
gabrielbosio 551fd09
Move cairo runner to runners package
gabrielbosio 4a0f451
Remove unnecessary getters
gabrielbosio 7e79671
Merge branch 'main' into memory_relocation
gabrielbosio a5a571f
Remove check for existing segment size
gabrielbosio f17c2b4
Avoid passing relocation table as a copy
gabrielbosio 3ad79d4
Fix memory relocation fn usage
gabrielbosio e3565a7
Move memory relocation fn
gabrielbosio 3de475e
Recover memory relocation test
gabrielbosio 391aa3e
Make relocated memory a map
gabrielbosio 69ccfa9
Merge branch 'main' into memory_relocation
gabrielbosio 5143818
Remove sorted keys generation
gabrielbosio d51c4f4
Remove zero fill in size computation
gabrielbosio a36e574
Document value relocation fn
gabrielbosio 86e1ed2
Remove unused index var
gabrielbosio a35342b
Remove zero initialization of vm fields
gabrielbosio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
package memory_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/lambdaclass/cairo-vm.go/pkg/vm" | ||
"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" | ||
) | ||
|
||
func TestComputeEffectiveSizeOneSegment(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 0), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
expectedSizes := map[uint]uint{0: 3} | ||
if !reflect.DeepEqual(expectedSizes, segments.SegmentSizes) { | ||
t.Errorf("Segment sizes are not the same") | ||
} | ||
} | ||
|
||
func TestComputeEffectiveSizeOneSegmentWithOneGap(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 6), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
expectedSizes := map[uint]uint{0: 7} | ||
if !reflect.DeepEqual(expectedSizes, segments.SegmentSizes) { | ||
t.Errorf("Segment sizes are not the same") | ||
} | ||
} | ||
|
||
func TestComputeEffectiveSizeOneSegmentWithMultipleGaps(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 3), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 4), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 9), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
expectedSizes := map[uint]uint{0: 10} | ||
if !reflect.DeepEqual(expectedSizes, segments.SegmentSizes) { | ||
t.Errorf("Segment sizes are not the same") | ||
} | ||
} | ||
|
||
func TestComputeEffectiveSizeThreeSegments(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 0), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 0), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 0), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 2), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
expectedSizes := map[uint]uint{0: 3, 1: 3, 2: 3} | ||
if !reflect.DeepEqual(expectedSizes, segments.SegmentSizes) { | ||
t.Errorf("Segment sizes are not the same") | ||
} | ||
} | ||
|
||
func TestComputeEffectiveSizeThreeSegmentsWithGaps(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 5), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 4), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 7), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
expectedSizes := map[uint]uint{0: 8, 1: 2, 2: 8} | ||
if !reflect.DeepEqual(expectedSizes, segments.SegmentSizes) { | ||
t.Errorf("Segment sizes are not the same") | ||
} | ||
} | ||
|
||
func TestGetSegmentUsedSizeAfterComputingUsed(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 5), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 7), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 1), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 2), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 4), memory.NewMaybeRelocatableInt(1)) | ||
segments.Memory.Insert(memory.NewRelocatable(2, 7), memory.NewMaybeRelocatableInt(1)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
segmentSize, ok := segments.SegmentSizes[2] | ||
expectedSize := 8 | ||
if !ok || segmentSize != uint(expectedSize) { | ||
t.Errorf("Segment size should be %d but it's %d", expectedSize, segmentSize) | ||
} | ||
} | ||
|
||
func TestGetSegmentUsedSizeBeforeComputingUsed(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
|
||
_, ok := segments.SegmentSizes[2] | ||
if ok { | ||
t.Errorf("Expected no segment sizes loaded") | ||
} | ||
} | ||
|
||
func TestRelocateOneSegment(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.SegmentSizes = map[uint]uint{0: 3} | ||
relocationTable, ok := segments.RelocateSegments() | ||
|
||
if !ok { | ||
t.Errorf("Memory segment manager doesn't have segment sizes initialized") | ||
} | ||
|
||
expectedTable := []uint{1} | ||
if !reflect.DeepEqual(expectedTable, relocationTable) { | ||
t.Errorf("Relocation tables are not the same") | ||
} | ||
} | ||
|
||
func TestRelocateFiveSegments(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.SegmentSizes = map[uint]uint{0: 3, 1: 3, 2: 56, 3: 78, 4: 8} | ||
relocationTable, ok := segments.RelocateSegments() | ||
|
||
if !ok { | ||
t.Errorf("Memory segment manager doesn't have segment sizes initialized") | ||
} | ||
|
||
expectedTable := []uint{1, 4, 7, 63, 141} | ||
if !reflect.DeepEqual(expectedTable, relocationTable) { | ||
t.Errorf("Relocation tables are not the same") | ||
} | ||
} | ||
|
||
func TestRelocateSegmentsWithHoles(t *testing.T) { | ||
segments := memory.NewMemorySegmentManager() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.AddSegment() | ||
segments.SegmentSizes = map[uint]uint{0: 3, 2: 3} | ||
relocationTable, ok := segments.RelocateSegments() | ||
|
||
if !ok { | ||
t.Errorf("Memory segment manager doesn't have segment sizes initialized") | ||
} | ||
|
||
expectedTable := []uint{1, 4, 4} | ||
if !reflect.DeepEqual(expectedTable, relocationTable) { | ||
t.Errorf("Relocation tables are not the same") | ||
} | ||
} | ||
|
||
func TestRelocateMemory(t *testing.T) { | ||
virtualMachine := vm.NewVirtualMachine() | ||
segments := virtualMachine.Segments | ||
for i := 0; i < 4; i++ { | ||
segments.AddSegment() | ||
} | ||
segments.Memory.Insert(memory.NewRelocatable(0, 0), memory.NewMaybeRelocatableInt(4613515612218425347)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 1), memory.NewMaybeRelocatableInt(5)) | ||
segments.Memory.Insert(memory.NewRelocatable(0, 2), memory.NewMaybeRelocatableInt(2345108766317314046)) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 0), memory.NewMaybeRelocatableRelocatable(memory.NewRelocatable(2, 0))) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 1), memory.NewMaybeRelocatableRelocatable(memory.NewRelocatable(3, 0))) | ||
segments.Memory.Insert(memory.NewRelocatable(1, 5), memory.NewMaybeRelocatableInt(5)) | ||
|
||
segments.ComputeEffectiveSizes() | ||
|
||
relocationTable, ok := segments.RelocateSegments() | ||
if !ok { | ||
t.Errorf("Could not create relocation table") | ||
} | ||
|
||
relocatedMemory, err := segments.RelocateMemory(&relocationTable) | ||
if err != nil { | ||
t.Errorf("Test failed with error: %s", err) | ||
} | ||
|
||
expectedMemory := map[uint]uint{1: 4613515612218425347, 2: 5, 3: 2345108766317314046, 4: 10, 5: 10, 9: 5} | ||
for i, v := range expectedMemory { | ||
actual := relocatedMemory[i] | ||
if actual != v { | ||
t.Errorf("Expected relocated memory at index %d to be %d but it's %d", i, v, actual) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.