Skip to content

Commit

Permalink
Implement vm heap
Browse files Browse the repository at this point in the history
  • Loading branch information
AgwaB committed Mar 3, 2019
1 parent 6b3ebdc commit 2d0238e
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
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 {
datas DataMap
}

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

func (h *Heap) Set(id Hid, value Hvalue) {
h.datas[id] = value
}

func (h *Heap) GetVal(id Hid) Hvalue {
return h.datas[id]
}

func (h *Heap) Size() int {
return len(h.datas)
}
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.GetVal(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.GetVal(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")
}
}

0 comments on commit 2d0238e

Please sign in to comment.