-
Notifications
You must be signed in to change notification settings - Fork 2
/
mach_test.go
49 lines (45 loc) · 1.01 KB
/
mach_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
package stackvm
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func buildFilledPages(n int) []*page {
r := make([]*page, n)
v := 0
for i := 0; i < n; i++ {
pg := &page{r: 1}
r[i] = pg
for i := 0; i < len(pg.d); i++ {
pg.d[i] = byte(1 + v%255)
v++
}
}
return r
}
func TestMach_fetchBytes(t *testing.T) {
m := Mach{pages: buildFilledPages(3)}
for stride := 1; stride <= 2*_pageSize; stride++ {
t.Run(fmt.Sprintf("stride %d", stride), func(t *testing.T) {
expected := make([]byte, stride)
v := 0
for i := 0; i < stride; i++ {
expected[i] = byte(1 + v%255)
v++
}
for addr, n := 0, len(m.pages)*_pageSize; addr < n; addr += stride {
buf := make([]byte, stride)
actual := buf[:m.fetchBytes(uint32(addr), buf)]
assert.Equal(t, []byte(expected), actual, "fetchBytes(%04x, %d)", addr, len(buf))
for i := 0; i < len(expected); i++ {
if v < n {
expected[i] = byte(1 + v%255)
v++
} else {
expected[i] = 0
}
}
}
})
}
}