70 lines
1.0 KiB
Go
70 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestMemWrite(t *testing.T) {
|
|
m := Memory{
|
|
intern: make(map[int]int),
|
|
}
|
|
|
|
// write in some memory, not starting at the head
|
|
err := m.Write(1, []int{1, 2, 3})
|
|
if err != nil {
|
|
t.Log(err.Error())
|
|
t.Fail()
|
|
}
|
|
// only three values in memory
|
|
if len(m.intern) != 3 {
|
|
t.Fail()
|
|
}
|
|
if m.NextFreeAddress() != 4 {
|
|
t.Log("expected nextFree to be 4, got", m.nextFree)
|
|
t.Fail()
|
|
}
|
|
|
|
// can't write to negative addresses
|
|
if m.Write(-1, []int{1}) == nil {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestMemRead(t *testing.T) {
|
|
m := Memory{
|
|
intern: map[int]int{0: 1, 1: 2, 2: 3, 100: 101},
|
|
}
|
|
|
|
// Read two known locations
|
|
r := m.Read(0, 2)
|
|
if len(r) != 2 {
|
|
t.Fail()
|
|
}
|
|
if r[0] != 1 || r[1] != 2 {
|
|
t.Fail()
|
|
}
|
|
|
|
// Read past known memory
|
|
r2 := m.Read(100, 2)
|
|
if len(r2) != 2 {
|
|
t.Fail()
|
|
}
|
|
if r2[0] != 101 || r2[1] != 0 {
|
|
t.Fail()
|
|
}
|
|
|
|
// read empty memory
|
|
m2 := Memory{
|
|
intern: map[int]int{},
|
|
}
|
|
r3 := m2.Read(0, 100)
|
|
if len(r3) != 100 {
|
|
t.Fail()
|
|
}
|
|
for i := range r3 {
|
|
if r3[i] != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|