Skip to content
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

Implement vm heap #361

Open
wants to merge 1 commit into
base: 0.2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions vm/heap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 De-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vm

type Hid int
type Hvalue []byte

type DataMap map[Hid]Hvalue

type Heap struct {
data DataMap
}

func NewHeap() *Heap {
return &Heap{
data: make(map[Hid]Hvalue),
}
}

func (h *Heap) Set(id Hid, value Hvalue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Store, not Set?

h.data[id] = value
}

func (h *Heap) Value(id Hid) Hvalue {
return h.data[id]
}

func (h *Heap) Size() int {
return len(h.data)
}
149 changes: 149 additions & 0 deletions vm/heap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2018 De-labtory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vm_test

import (
"bytes"
"testing"

"github.com/DE-labtory/koa/vm"
)

func TestHeap_Set(t *testing.T) {
heap := vm.NewHeap()

tests := []struct {
id vm.Hid
value vm.Hvalue
}{
{
id: 0,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
},
{
id: 1,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a},
},
{
id: 2,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
},
{
id: 3,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15},
},
}

testsExpected := []struct {
id vm.Hid
value vm.Hvalue
}{
{
id: 0,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
},
{
id: 1,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a},
},
{
id: 2,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
},
{
id: 3,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15},
},
}

for _, test := range tests {
heap.Set(test.id, test.value)
}

for _, expected := range testsExpected {
value := heap.Value(expected.id)
if !bytes.Equal(expected.value, value) {
t.Errorf("Invalid heap value")
}
}
}

func TestHeap_GetVal(t *testing.T) {
heap := vm.NewHeap()

test := struct {
id vm.Hid
value vm.Hvalue
}{
id: 0,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
}

testExpected := struct {
id vm.Hid
value vm.Hvalue
}{
id: 0,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
}

heap.Set(test.id, test.value)

value := heap.Value(testExpected.id)

if !bytes.Equal(testExpected.value, value) {
t.Error("Invalid heap memory")
}
}

func TestHeap_Size(t *testing.T) {
heap := vm.NewHeap()

tests := []struct {
id vm.Hid
value vm.Hvalue
}{
{
id: 0,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
},
{
id: 1,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a},
},
{
id: 2,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
},
{
id: 3,
value: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15},
},
{
id: 3,
value: []byte{0x00},
},
}

for _, test := range tests {
heap.Set(test.id, test.value)
}

if heap.Size() != 4 {
t.Error("Invalid heap size")
}
}