Files
wazero/internal/wasm/binary/code_test.go
Crypt Keeper ce1052a097 Isolates testify to one file, so that it is easier to remove (#460)
This starts the process of removing all dependencies from wazero, by
isolating all assertions we use into a single file. This allows us to
port those assertions as we have time, and when twitchy is gone, the
project literally has no dependencies except go!

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-14 10:05:38 +08:00

97 lines
2.8 KiB
Go

package binary
import (
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
"github.com/tetratelabs/wazero/internal/wasm"
)
var addLocalZeroLocalTwo = []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 2, wasm.OpcodeI32Add, wasm.OpcodeEnd}
func TestEncodeCode(t *testing.T) {
addLocalZeroLocalOne := []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}
tests := []struct {
name string
input *wasm.Code
expected []byte
}{
{
name: "smallest function body",
input: &wasm.Code{ // Ex. (func)
Body: []byte{wasm.OpcodeEnd},
},
expected: []byte{
0x02, // 2 bytes to encode locals and the body
0x00, // no local blocks
wasm.OpcodeEnd, // Body
},
},
{
name: "params and instructions", // local.get index space is params, then locals
input: &wasm.Code{ // Ex. (func (type 3) local.get 0 local.get 1 i32.add)
Body: addLocalZeroLocalOne,
},
expected: append([]byte{
0x07, // 7 bytes to encode locals and the body
0x00, // no local blocks
},
addLocalZeroLocalOne..., // Body
),
},
{
name: "locals and instructions",
input: &wasm.Code{ // Ex. (func (result i32) (local i32, i32) local.get 0 local.get 1 i32.add)
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
Body: addLocalZeroLocalOne,
},
expected: append([]byte{
0x09, // 9 bytes to encode locals and the body
0x01, // 1 local block
0x02, wasm.ValueTypeI32, // local block 1
},
addLocalZeroLocalOne..., // Body
),
},
{
name: "mixed locals and instructions",
input: &wasm.Code{ // Ex. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
Body: addLocalZeroLocalTwo,
},
expected: append([]byte{
0x0d, // 13 bytes to encode locals and the body
0x03, // 3 local blocks
0x01, wasm.ValueTypeI32, // local block 1
0x01, wasm.ValueTypeI64, // local block 2
0x01, wasm.ValueTypeI32, // local block 3
},
addLocalZeroLocalTwo..., // Body
),
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
bytes := encodeCode(tc.input)
require.Equal(t, tc.expected, bytes)
})
}
}
func BenchmarkEncodeCode(b *testing.B) {
input := &wasm.Code{ // Ex. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
Body: addLocalZeroLocalTwo,
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if bytes := encodeCode(input); len(bytes) == 0 {
b.Fatal("didn't encode anything")
}
}
}