github.com/bluenviron/mediacommon@v1.9.3/pkg/codecs/av1/leb128_test.go (about) 1 package av1 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 var casesLEB128 = []struct { 10 name string 11 dec uint 12 enc []byte 13 }{ 14 { 15 "a", 16 1234567, 17 []byte{0x87, 0xad, 0x4b}, 18 }, 19 { 20 "b", 21 127, 22 []byte{0x7f}, 23 }, 24 { 25 "c", 26 651321342, 27 []byte{0xfe, 0xbf, 0xc9, 0xb6, 0x2}, 28 }, 29 } 30 31 func TestLEB128Unmarshal(t *testing.T) { 32 for _, ca := range casesLEB128 { 33 t.Run(ca.name, func(t *testing.T) { 34 dec, n, err := LEB128Unmarshal(ca.enc) 35 require.NoError(t, err) 36 require.Equal(t, len(ca.enc), n) 37 require.Equal(t, ca.dec, dec) 38 }) 39 } 40 } 41 42 func TestLEB128Marshal(t *testing.T) { 43 for _, ca := range casesLEB128 { 44 t.Run(ca.name, func(t *testing.T) { 45 enc := make([]byte, LEB128MarshalSize(ca.dec)) 46 n := LEB128MarshalTo(ca.dec, enc) 47 require.Equal(t, ca.enc, enc) 48 require.Equal(t, len(ca.enc), n) 49 }) 50 } 51 } 52 53 func FuzzLEB128Unmarshal(f *testing.F) { 54 f.Fuzz(func(_ *testing.T, b []byte) { 55 LEB128Unmarshal(b) //nolint:errcheck 56 }) 57 }