github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/testing/binaryencoding/value_test.go (about)

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