-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_test.go
70 lines (60 loc) · 1.24 KB
/
memory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package soda
import (
"testing"
)
func TestMainMemory_Allocate(t *testing.T) {
//Create a MainMemory
mm := make(MainMemory)
//Allocate some memory blocks in various sizes
for i := word(1); i < 257; i++ {
addr, err := mm.Allocate(i)
if err != nil {
t.Error(err)
}
//Make sure blocks are in map
if val, ok := mm[addr]; !ok || val == nil {
t.Fail()
}
}
}
func TestMainMemory_Free(t *testing.T) {
//Create MainMemory
mm := make(MainMemory)
//Create some memoryblocks and but them in the map.
var addrs []address = nil
for i := address(0); i < 257; i++ {
mm[i+32456] = make(MemoryBlock, 1)
addrs = append(addrs, i+32456)
}
defer func() {
if r := recover(); r != nil {
t.Fail()
}
}()
//Call free on all blocks
for _, addr := range addrs {
err := mm.Free(addr)
if err != nil {
t.Error(err)
}
//Make sure blocks are not in map
if _, ok := mm[addr]; ok {
t.Fail()
}
}
}
func TestMainMemory_LoadWord(t *testing.T) {
mm := make(MainMemory)
mm[0] = MemoryBlock{1, 2, 3, 4, 5, 6}
expected := []word{0x04030201, 0x05040302, 0x06050403}
for i := 0; i < len(mm[0])-4; i++ {
t.Log(i)
w, err := mm.LoadWord(0, word(i))
if err != nil {
t.Error(err)
}
if w != expected[i] {
t.Fail()
}
}
}