github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/wasm/binary/value_test.go (about) 1 package binary 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/wasilibs/wazerox/internal/testing/binaryencoding" 8 "github.com/wasilibs/wazerox/internal/testing/require" 9 "github.com/wasilibs/wazerox/internal/wasm" 10 ) 11 12 func TestEncodeValTypes(t *testing.T) { 13 i32, i64, f32, f64, ext, fref := wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeF32, wasm.ValueTypeF64, wasm.ValueTypeExternref, wasm.ValueTypeFuncref 14 tests := []struct { 15 name string 16 input []wasm.ValueType 17 expected []byte 18 }{ 19 { 20 name: "empty", 21 input: []wasm.ValueType{}, 22 expected: []byte{0}, 23 }, 24 { 25 name: "undefined", // ensure future spec changes don't panic 26 input: []wasm.ValueType{0x6f}, 27 expected: []byte{1, 0x6f}, 28 }, 29 { 30 name: "funcref", 31 input: []wasm.ValueType{fref}, 32 expected: []byte{1, fref}, 33 }, 34 { 35 name: "externref", 36 input: []wasm.ValueType{ext}, 37 expected: []byte{1, ext}, 38 }, 39 { 40 name: "i32", 41 input: []wasm.ValueType{i32}, 42 expected: []byte{1, i32}, 43 }, 44 { 45 name: "i64", 46 input: []wasm.ValueType{i64}, 47 expected: []byte{1, i64}, 48 }, 49 { 50 name: "f32", 51 input: []wasm.ValueType{f32}, 52 expected: []byte{1, f32}, 53 }, 54 { 55 name: "f64", 56 input: []wasm.ValueType{f64}, 57 expected: []byte{1, f64}, 58 }, 59 { 60 name: "i32i64", 61 input: []wasm.ValueType{i32, i64}, 62 expected: []byte{2, i32, i64}, 63 }, 64 { 65 name: "i32i64f32", 66 input: []wasm.ValueType{i32, i64, f32}, 67 expected: []byte{3, i32, i64, f32}, 68 }, 69 { 70 name: "i32i64f32f64", 71 input: []wasm.ValueType{i32, i64, f32, f64}, 72 expected: []byte{4, i32, i64, f32, f64}, 73 }, 74 { 75 name: "i32i64f32f64i32", 76 input: []wasm.ValueType{i32, i64, f32, f64, i32}, 77 expected: []byte{5, i32, i64, f32, f64, i32}, 78 }, 79 { 80 name: "i32i64f32f64i32i64", 81 input: []wasm.ValueType{i32, i64, f32, f64, i32, i64}, 82 expected: []byte{6, i32, i64, f32, f64, i32, i64}, 83 }, 84 { 85 name: "i32i64f32f64i32i64f32", 86 input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32}, 87 expected: []byte{7, i32, i64, f32, f64, i32, i64, f32}, 88 }, 89 { 90 name: "i32i64f32f64i32i64f32f64", 91 input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64}, 92 expected: []byte{8, i32, i64, f32, f64, i32, i64, f32, f64}, 93 }, 94 { 95 name: "i32i64f32f64i32i64f32f64i32", 96 input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32}, 97 expected: []byte{9, i32, i64, f32, f64, i32, i64, f32, f64, i32}, 98 }, 99 { 100 name: "i32i64f32f64i32i64f32f64i32i64", 101 input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, 102 expected: []byte{10, i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, 103 }, 104 } 105 106 for _, tt := range tests { 107 tc := tt 108 109 t.Run(tc.name, func(t *testing.T) { 110 bytes := binaryencoding.EncodeValTypes(tc.input) 111 require.Equal(t, tc.expected, bytes) 112 }) 113 } 114 } 115 116 func Test_decodeUTF8(t *testing.T) { 117 t.Run("empty", func(t *testing.T) { 118 actual, n, err := decodeUTF8(bytes.NewReader([]byte{0, '?', '?'}), "") 119 require.NoError(t, err) 120 require.Equal(t, "", actual) 121 require.Equal(t, uint32(1), n) 122 }) 123 t.Run("non-empty", func(t *testing.T) { 124 actual, n, err := decodeUTF8(bytes.NewReader([]byte{3, 'f', 'o', 'o', '?', '?'}), "") 125 require.NoError(t, err) 126 require.Equal(t, "foo", actual) 127 require.Equal(t, uint32(4), n) 128 }) 129 }