diff options
| author | Robert Griesemer <gri@golang.org> | 2011-09-28 22:36:52 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2011-09-28 22:36:52 -0700 |
| commit | f30719dc89c2a41502fa584b790943170ad2d1ce (patch) | |
| tree | dfe3b4fdcbe4ebe1c234d3abfa5b050ee39a6093 /src/pkg/encoding/binary/varint_test.go | |
| parent | b74136984dc04d3f0025f3eaf55c0ecdf3c38857 (diff) | |
| download | go-f30719dc89c2a41502fa584b790943170ad2d1ce.tar.xz | |
encoding/binary: support for varint encoding
R=rsc, r, nigeltao, r, dsymonds
CC=golang-dev
https://golang.org/cl/5146048
Diffstat (limited to 'src/pkg/encoding/binary/varint_test.go')
| -rw-r--r-- | src/pkg/encoding/binary/varint_test.go | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/pkg/encoding/binary/varint_test.go b/src/pkg/encoding/binary/varint_test.go new file mode 100644 index 0000000000..a85aceeeac --- /dev/null +++ b/src/pkg/encoding/binary/varint_test.go @@ -0,0 +1,175 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package binary + +import ( + "bytes" + "os" + "testing" +) + +func testConstant(t *testing.T, w uint, max int) { + n := -PutUvarint(nil, 1<<w-1) + if n != max { + t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n) + } +} + +func TestConstants(t *testing.T) { + testConstant(t, 16, MaxVarintLen16) + testConstant(t, 32, MaxVarintLen32) + testConstant(t, 64, MaxVarintLen64) +} + +func testVarint(t *testing.T, x int64) { + var buf1 [10]byte + n := PutVarint(buf1[:], x) + y, m := Varint(buf1[0:n]) + if x != y { + t.Errorf("Varint(%d): got %d", x, y) + } + if n != m { + t.Errorf("Varint(%d): got n = %d; want %d", x, m, n) + } + + var buf2 bytes.Buffer + err := WriteVarint(&buf2, x) + if err != nil { + t.Errorf("WriteVarint(%d): %s", x, err) + } + if n != buf2.Len() { + t.Errorf("WriteVarint(%d): got n = %d; want %d", x, buf2.Len(), n) + } + y, err = ReadVarint(&buf2) + if err != nil { + t.Errorf("ReadVarint(%d): %s", x, err) + } + if x != y { + t.Errorf("ReadVarint(%d): got %d", x, y) + } +} + +func testUvarint(t *testing.T, x uint64) { + var buf1 [10]byte + n := PutUvarint(buf1[:], x) + y, m := Uvarint(buf1[0:n]) + if x != y { + t.Errorf("Uvarint(%d): got %d", x, y) + } + if n != m { + t.Errorf("Uvarint(%d): got n = %d; want %d", x, m, n) + } + + var buf2 bytes.Buffer + err := WriteUvarint(&buf2, x) + if err != nil { + t.Errorf("WriteUvarint(%d): %s", x, err) + } + if n != buf2.Len() { + t.Errorf("WriteUvarint(%d): got n = %d; want %d", x, buf2.Len(), n) + } + y, err = ReadUvarint(&buf2) + if err != nil { + t.Errorf("ReadUvarint(%d): %s", x, err) + } + if x != y { + t.Errorf("ReadUvarint(%d): got %d", x, y) + } +} + +var tests = []int64{ + -1 << 63, + -1<<63 + 1, + -1, + 0, + 1, + 2, + 10, + 20, + 63, + 64, + 65, + 127, + 128, + 129, + 255, + 256, + 257, + 1<<63 - 1, +} + +func TestVarint(t *testing.T) { + for _, x := range tests { + testVarint(t, x) + testVarint(t, -x) + } + for x := int64(0x7); x != 0; x <<= 1 { + testVarint(t, x) + testVarint(t, -x) + } +} + +func TestUvarint(t *testing.T) { + for _, x := range tests { + testUvarint(t, uint64(x)) + } + for x := uint64(0x7); x != 0; x <<= 1 { + testUvarint(t, x) + } +} + +func TestBufferTooSmall(t *testing.T) { + for i := 0; i < 10; i++ { + buf := make([]byte, i) + x := uint64(1) << (uint(i) * 7) + n0 := -i + if i == 0 { + n0 = -1 // encoding 0 takes one byte + } + if n := PutUvarint(buf, x); n != n0 { + t.Errorf("PutUvarint([%d]byte, %d): got n = %d; want %d", len(buf), x, n, n0) + } + } + + buf := []byte{0x80, 0x80, 0x80, 0x80} + for i := 0; i <= len(buf); i++ { + buf := buf[0:i] + x, n := Uvarint(buf) + if x != 0 || n != 0 { + t.Errorf("Uvarint(%v): got x = %d, n = %d", buf, x, n) + } + + x, err := ReadUvarint(bytes.NewBuffer(buf)) + if x != 0 || err != os.EOF { + t.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf, x, err) + } + } +} + +func testOverflow(t *testing.T, buf []byte, n0 int, err0 os.Error) { + x, n := Uvarint(buf) + if x != 0 || n != n0 { + t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, %d", buf, x, n, n0) + } + + x, err := ReadUvarint(bytes.NewBuffer(buf)) + if x != 0 || err != err0 { + t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want 0, %s", buf, x, err, err0) + } +} + +func TestOverflow(t *testing.T) { + testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, -10, overflow) + testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, -13, overflow) +} + +func TestNonCanonicalZero(t *testing.T) { + buf := []byte{0x80, 0x80, 0x80, 0} + x, n := Uvarint(buf) + if x != 0 || n != 4 { + t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, 4", buf, x, n) + + } +} |
