mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
Use wazerotest.
This commit is contained in:
@@ -3,88 +3,90 @@ package util
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/tetratelabs/wazero/experimental/wazerotest"
|
||||
)
|
||||
|
||||
func TestView_nil(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
View(mock, 0, 8)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestView_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
View(mock, 126, 8)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
View(mock, wazerotest.PageSize-2, 8)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestView_overflow(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
View(mock, 1, math.MaxInt64)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestReadUint32_nil(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
ReadUint32(mock, 0)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestReadUint32_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
ReadUint32(mock, 126)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
ReadUint32(mock, wazerotest.PageSize-2)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestReadUint64_nil(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
ReadUint64(mock, 0)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestReadUint64_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
ReadUint64(mock, 126)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
ReadUint64(mock, wazerotest.PageSize-2)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestWriteUint32_nil(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
WriteUint32(mock, 0, 1)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestWriteUint32_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
WriteUint32(mock, 126, 1)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
WriteUint32(mock, wazerotest.PageSize-2, 1)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestWriteUint64_nil(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
WriteUint64(mock, 0, 1)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestWriteUint64_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
WriteUint64(mock, 126, 1)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
WriteUint64(mock, wazerotest.PageSize-2, 1)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
func TestReadString_range(t *testing.T) {
|
||||
defer func() { _ = recover() }()
|
||||
mock := NewMockModule(128)
|
||||
ReadString(mock, 130, math.MaxUint32)
|
||||
mock := wazerotest.NewModule(wazerotest.NewFixedMemory(wazerotest.PageSize))
|
||||
ReadString(mock, wazerotest.PageSize+2, math.MaxUint32)
|
||||
t.Error("want panic")
|
||||
}
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
|
||||
"github.com/tetratelabs/wazero/api"
|
||||
)
|
||||
|
||||
func NewMockModule(size uint32) api.Module {
|
||||
mem := mockMemory{buf: make([]byte, size)}
|
||||
return mockModule{&mem, nil}
|
||||
}
|
||||
|
||||
type mockModule struct {
|
||||
memory api.Memory
|
||||
api.Module
|
||||
}
|
||||
|
||||
func (m mockModule) Memory() api.Memory { return m.memory }
|
||||
func (m mockModule) String() string { return "mockModule" }
|
||||
func (m mockModule) Name() string { return "mockModule" }
|
||||
|
||||
type mockMemory struct {
|
||||
buf []byte
|
||||
api.Memory
|
||||
}
|
||||
|
||||
func (m mockMemory) Definition() api.MemoryDefinition { return nil }
|
||||
|
||||
func (m mockMemory) Size() uint32 { return uint32(len(m.buf)) }
|
||||
|
||||
func (m mockMemory) ReadByte(offset uint32) (byte, bool) {
|
||||
if offset >= m.Size() {
|
||||
return 0, false
|
||||
}
|
||||
return m.buf[offset], true
|
||||
}
|
||||
|
||||
func (m mockMemory) ReadUint16Le(offset uint32) (uint16, bool) {
|
||||
if !m.hasSize(offset, 2) {
|
||||
return 0, false
|
||||
}
|
||||
return binary.LittleEndian.Uint16(m.buf[offset : offset+2]), true
|
||||
}
|
||||
|
||||
func (m mockMemory) ReadUint32Le(offset uint32) (uint32, bool) {
|
||||
if !m.hasSize(offset, 4) {
|
||||
return 0, false
|
||||
}
|
||||
return binary.LittleEndian.Uint32(m.buf[offset : offset+4]), true
|
||||
}
|
||||
|
||||
func (m mockMemory) ReadFloat32Le(offset uint32) (float32, bool) {
|
||||
v, ok := m.ReadUint32Le(offset)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return math.Float32frombits(v), true
|
||||
}
|
||||
|
||||
func (m mockMemory) ReadUint64Le(offset uint32) (uint64, bool) {
|
||||
if !m.hasSize(offset, 8) {
|
||||
return 0, false
|
||||
}
|
||||
return binary.LittleEndian.Uint64(m.buf[offset : offset+8]), true
|
||||
}
|
||||
|
||||
func (m mockMemory) ReadFloat64Le(offset uint32) (float64, bool) {
|
||||
v, ok := m.ReadUint64Le(offset)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return math.Float64frombits(v), true
|
||||
}
|
||||
|
||||
func (m mockMemory) Read(offset, byteCount uint32) ([]byte, bool) {
|
||||
if !m.hasSize(offset, byteCount) {
|
||||
return nil, false
|
||||
}
|
||||
return m.buf[offset : offset+byteCount : offset+byteCount], true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteByte(offset uint32, v byte) bool {
|
||||
if offset >= m.Size() {
|
||||
return false
|
||||
}
|
||||
m.buf[offset] = v
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteUint16Le(offset uint32, v uint16) bool {
|
||||
if !m.hasSize(offset, 2) {
|
||||
return false
|
||||
}
|
||||
binary.LittleEndian.PutUint16(m.buf[offset:], v)
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteUint32Le(offset, v uint32) bool {
|
||||
if !m.hasSize(offset, 4) {
|
||||
return false
|
||||
}
|
||||
binary.LittleEndian.PutUint32(m.buf[offset:], v)
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteFloat32Le(offset uint32, v float32) bool {
|
||||
return m.WriteUint32Le(offset, math.Float32bits(v))
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteUint64Le(offset uint32, v uint64) bool {
|
||||
if !m.hasSize(offset, 8) {
|
||||
return false
|
||||
}
|
||||
binary.LittleEndian.PutUint64(m.buf[offset:], v)
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteFloat64Le(offset uint32, v float64) bool {
|
||||
return m.WriteUint64Le(offset, math.Float64bits(v))
|
||||
}
|
||||
|
||||
func (m mockMemory) Write(offset uint32, val []byte) bool {
|
||||
if !m.hasSize(offset, uint32(len(val))) {
|
||||
return false
|
||||
}
|
||||
copy(m.buf[offset:], val)
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mockMemory) WriteString(offset uint32, val string) bool {
|
||||
if !m.hasSize(offset, uint32(len(val))) {
|
||||
return false
|
||||
}
|
||||
copy(m.buf[offset:], val)
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *mockMemory) Grow(delta uint32) (result uint32, ok bool) {
|
||||
prev := (len(m.buf) + 65535) / 65536
|
||||
m.buf = append(m.buf, make([]byte, 65536*delta)...)
|
||||
return uint32(prev), true
|
||||
}
|
||||
|
||||
func (m mockMemory) hasSize(offset uint32, byteCount uint32) bool {
|
||||
return uint64(offset)+uint64(byteCount) <= uint64(len(m.buf))
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_mockMemory_byte(t *testing.T) {
|
||||
const want byte = 98
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadByte(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteByte(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteByte(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadByte(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_uint16(t *testing.T) {
|
||||
const want uint16 = 9876
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadUint16Le(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint16Le(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint16Le(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadUint16Le(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_uint32(t *testing.T) {
|
||||
const want uint32 = 987654321
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadUint32Le(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint32Le(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint32Le(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadUint32Le(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_uint64(t *testing.T) {
|
||||
const want uint64 = 9876543210
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadUint64Le(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint64Le(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteUint64Le(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadUint64Le(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_float32(t *testing.T) {
|
||||
const want float32 = math.Pi
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadFloat32Le(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteFloat32Le(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteFloat32Le(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadFloat32Le(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %f, want %f", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_float64(t *testing.T) {
|
||||
const want float64 = math.Pi
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadFloat64Le(128)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteFloat64Le(128, 0)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteFloat64Le(0, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().ReadFloat64Le(0)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("got %f, want %f", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_bytes(t *testing.T) {
|
||||
const want string = "\xca\xfe\xba\xbe"
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().Read(128, uint32(len(want)))
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().Write(128, []byte(want))
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteString(128, want)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
ok = mock.Memory().Write(0, []byte(want))
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().Read(0, uint32(len(want)))
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if string(got) != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
ok = mock.Memory().WriteString(64, want)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
|
||||
got, ok = mock.Memory().Read(64, uint32(len(want)))
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if string(got) != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mockMemory_grow(t *testing.T) {
|
||||
mock := NewMockModule(128)
|
||||
|
||||
_, ok := mock.Memory().ReadByte(65536)
|
||||
if ok {
|
||||
t.Error("want error")
|
||||
}
|
||||
|
||||
got, ok := mock.Memory().Grow(1)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
if got != 1 {
|
||||
t.Errorf("got %d, want 1", got)
|
||||
}
|
||||
|
||||
_, ok = mock.Memory().ReadByte(65536)
|
||||
if !ok {
|
||||
t.Error("want ok")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user