...
1 package binaryencoding
2
3 import (
4 "testing"
5
6 "github.com/tetratelabs/wazero/internal/leb128"
7 "github.com/tetratelabs/wazero/internal/testing/require"
8 "github.com/tetratelabs/wazero/internal/wasm"
9 )
10
11 func TestEncodeGlobal(t *testing.T) {
12 tests := []struct {
13 name string
14 input wasm.Global
15 expected []byte
16 }{
17 {
18 name: "const",
19 input: wasm.Global{
20 Type: wasm.GlobalType{ValType: wasm.ValueTypeI32},
21 Init: wasm.ConstantExpression{Opcode: wasm.OpcodeI32Const, Data: leb128.EncodeInt32(1)},
22 },
23 expected: []byte{
24 wasm.ValueTypeI32, 0x00,
25 wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
26 },
27 },
28 {
29 name: "var",
30 input: wasm.Global{
31 Type: wasm.GlobalType{ValType: wasm.ValueTypeI32, Mutable: true},
32 Init: wasm.ConstantExpression{Opcode: wasm.OpcodeI32Const, Data: leb128.EncodeInt32(1)},
33 },
34 expected: []byte{
35 wasm.ValueTypeI32, 0x01,
36 wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
37 },
38 },
39 }
40
41 for _, tt := range tests {
42 tc := tt
43
44 t.Run(tc.name, func(t *testing.T) {
45 bytes := encodeGlobal(tc.input)
46 require.Equal(t, tc.expected, bytes)
47 })
48 }
49 }
50
View as plain text