github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/limits_test.go (about)

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