github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/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/tetratelabs/wazero/internal/testing/binaryencoding" 10 "github.com/tetratelabs/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 shared bool 22 expected []byte 23 }{ 24 { 25 name: "min 0", 26 expected: []byte{0x0, 0}, 27 }, 28 { 29 name: "min 0, max 0", 30 max: &zero, 31 expected: []byte{0x1, 0, 0}, 32 }, 33 { 34 name: "min largest", 35 min: largest, 36 expected: []byte{0x0, 0xff, 0xff, 0xff, 0xff, 0xf}, 37 }, 38 { 39 name: "min 0, max largest", 40 max: &largest, 41 expected: []byte{0x1, 0, 0xff, 0xff, 0xff, 0xff, 0xf}, 42 }, 43 { 44 name: "min largest max largest", 45 min: largest, 46 max: &largest, 47 expected: []byte{0x1, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xf}, 48 }, 49 { 50 name: "min 0, shared", 51 shared: true, 52 expected: []byte{0x2, 0}, 53 }, 54 { 55 name: "min 0, max 0, shared", 56 max: &zero, 57 shared: true, 58 expected: []byte{0x3, 0, 0}, 59 }, 60 { 61 name: "min largest, shared", 62 min: largest, 63 shared: true, 64 expected: []byte{0x2, 0xff, 0xff, 0xff, 0xff, 0xf}, 65 }, 66 { 67 name: "min 0, max largest, shared", 68 max: &largest, 69 shared: true, 70 expected: []byte{0x3, 0, 0xff, 0xff, 0xff, 0xff, 0xf}, 71 }, 72 { 73 name: "min largest max largest, shared", 74 min: largest, 75 max: &largest, 76 shared: true, 77 expected: []byte{0x3, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xf}, 78 }, 79 } 80 81 for _, tt := range tests { 82 tc := tt 83 84 b := binaryencoding.EncodeLimitsType(tc.min, tc.max, tc.shared) 85 t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) { 86 require.Equal(t, tc.expected, b) 87 }) 88 89 t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) { 90 min, max, shared, err := decodeLimitsType(bytes.NewReader(b)) 91 require.NoError(t, err) 92 require.Equal(t, min, tc.min) 93 require.Equal(t, max, tc.max) 94 require.Equal(t, shared, tc.shared) 95 }) 96 } 97 }