wa-lang.org/wazero@v1.0.2/internal/wasm/binary/limits_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math"
     7  	"testing"
     8  
     9  	"wa-lang.org/wazero/internal/testing/require"
    10  )
    11  
    12  func TestLimitsType(t *testing.T) {
    13  	zero := uint32(0)
    14  	largest := uint32(math.MaxUint32)
    15  
    16  	tests := []struct {
    17  		name     string
    18  		min      uint32
    19  		max      *uint32
    20  		expected []byte
    21  	}{
    22  		{
    23  			name:     "min 0",
    24  			expected: []byte{0x0, 0},
    25  		},
    26  		{
    27  			name:     "min 0, max 0",
    28  			max:      &zero,
    29  			expected: []byte{0x1, 0, 0},
    30  		},
    31  		{
    32  			name:     "min largest",
    33  			min:      largest,
    34  			expected: []byte{0x0, 0xff, 0xff, 0xff, 0xff, 0xf},
    35  		},
    36  		{
    37  			name:     "min 0, max largest",
    38  			max:      &largest,
    39  			expected: []byte{0x1, 0, 0xff, 0xff, 0xff, 0xff, 0xf},
    40  		},
    41  		{
    42  			name:     "min largest max largest",
    43  			min:      largest,
    44  			max:      &largest,
    45  			expected: []byte{0x1, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xf},
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		tc := tt
    51  
    52  		b := encodeLimitsType(tc.min, tc.max)
    53  		t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) {
    54  			require.Equal(t, tc.expected, b)
    55  		})
    56  
    57  		t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
    58  			min, max, err := decodeLimitsType(bytes.NewReader(b))
    59  			require.NoError(t, err)
    60  			require.Equal(t, min, tc.min)
    61  			require.Equal(t, max, tc.max)
    62  		})
    63  	}
    64  }