From 8509e0b6c85d3fcb58105b4c4b7883ab2d0f473b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 31 Mar 2023 13:42:31 +0100 Subject: [PATCH] Test coverage. --- internal/util/mock.go | 4 - internal/util/mock_test.go | 242 +++++++++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 internal/util/mock_test.go diff --git a/internal/util/mock.go b/internal/util/mock.go index ed1604b..32250d9 100644 --- a/internal/util/mock.go +++ b/internal/util/mock.go @@ -148,10 +148,6 @@ func (m *mockMemory) Grow(delta uint32) (result uint32, ok bool) { return uint32(prev), true } -func (m mockMemory) PageSize() (result uint32) { - return uint32(len(m) / 65536) -} - func (m mockMemory) hasSize(offset uint32, byteCount uint32) bool { return uint64(offset)+uint64(byteCount) <= uint64(len(m)) } diff --git a/internal/util/mock_test.go b/internal/util/mock_test.go new file mode 100644 index 0000000..f5e0967 --- /dev/null +++ b/internal/util/mock_test.go @@ -0,0 +1,242 @@ +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") + } +}