From 40b5b2ee3b128a678fe112ebd6b65acb2c487b04 Mon Sep 17 00:00:00 2001 From: KIMSJ Date: Sun, 3 Mar 2019 16:29:02 +0900 Subject: [PATCH] Implement vm heap --- vm/heap.go | 44 ++++++++++++++ vm/heap_test.go | 149 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 vm/heap.go create mode 100644 vm/heap_test.go diff --git a/vm/heap.go b/vm/heap.go new file mode 100644 index 00000000..5d0857a7 --- /dev/null +++ b/vm/heap.go @@ -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) { + h.data[id] = value +} + +func (h *Heap) Value(id Hid) Hvalue { + return h.data[id] +} + +func (h *Heap) Size() int { + return len(h.data) +} diff --git a/vm/heap_test.go b/vm/heap_test.go new file mode 100644 index 00000000..1301d154 --- /dev/null +++ b/vm/heap_test.go @@ -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") + } +}