1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func TestEncodeUint(t *testing.T) {
13 testCases := []struct {
22 {1 << 14, "\x80\x80\x01"},
23 {0xffffffff, "\xff\xff\xff\xff\x0f"},
24 {0xffffffffffffffff, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"},
26 for _, tc := range testCases {
27 buf := [maxVarintBytes]byte{}
28 got := string(buf[:encodeUint(buf[:], tc.x)])
30 t.Errorf("EncodeUint(%#x) = %q; want %q", tc.x, got, tc.enc)
35 func TestDecodeUint(t *testing.T) {
36 testCases := []struct {
45 err: errIllegalVarint,
50 err: errIllegalVarint,
55 err: errIllegalVarint,
83 enc: "\xff\xff\xff\xff\x0f",
85 x: 0xffffffffffffffff,
87 enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01",
89 x: 0xffffffffffffffff,
91 enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01\x00",
95 enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01",
96 err: errVarintTooLarge,
100 decode func(s string) (x uint64, size int, err error)
102 {"decode", func(s string) (x uint64, size int, err error) {
103 return decodeUint([]byte(s))
105 {"decodeString", decodeUintString},
107 for _, f := range forms {
108 for _, tc := range testCases {
109 t.Run(fmt.Sprintf("%s:%q", f.name, tc.enc), func(t *testing.T) {
110 x, size, err := f.decode(tc.enc)
112 t.Errorf("err = %q; want %q", err, tc.err)
115 t.Errorf("size = %d; want %d", size, tc.size)
118 t.Errorf("decode = %#x; want %#x", x, tc.x)