blob: d025a67538ccdb66e42834d9540437fbb87caa14 [file] [log] [blame]
 // 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" "io" "math" "testing" ) func testConstant(t *testing.T, w uint, max int) { buf := make([]byte, MaxVarintLen64) n := PutUvarint(buf, 1< MaxVarintLen64 { t.Errorf("ReadUvarint(%v): read more than MaxVarintLen64 bytes, got %d", buf, read) } } func TestOverflow(t *testing.T) { testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, 0, -10, overflow) testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, 0, -11, overflow) testOverflow(t, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 1<<64-1, -11, overflow) // 11 bytes, should 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) } } func BenchmarkPutUvarint32(b *testing.B) { buf := make([]byte, MaxVarintLen32) b.SetBytes(4) for i := 0; i < b.N; i++ { for j := uint(0); j < MaxVarintLen32; j++ { PutUvarint(buf, 1<<(j*7)) } } } func BenchmarkPutUvarint64(b *testing.B) { buf := make([]byte, MaxVarintLen64) b.SetBytes(8) for i := 0; i < b.N; i++ { for j := uint(0); j < MaxVarintLen64; j++ { PutUvarint(buf, 1<<(j*7)) } } }