Commit 11c75668 authored by Steven Allen's avatar Steven Allen

test: test all varints less than 2**16 againt VarintSize

parent d462e342
package multiaddr package multiaddr
import "testing" import (
"encoding/binary"
"testing"
)
func checkVarint(t *testing.T, x int) {
buf := make([]byte, binary.MaxVarintLen64)
expected := binary.PutUvarint(buf, uint64(x))
func expectVarint(t *testing.T, x, expected int) {
size := VarintSize(x) size := VarintSize(x)
if size != expected { if size != expected {
t.Fatalf("expected varintsize of %d to be %d, got %d", x, expected, size) t.Fatalf("expected varintsize of %d to be %d, got %d", x, expected, size)
...@@ -10,7 +16,8 @@ func expectVarint(t *testing.T, x, expected int) { ...@@ -10,7 +16,8 @@ func expectVarint(t *testing.T, x, expected int) {
} }
func TestVarintSize(t *testing.T) { func TestVarintSize(t *testing.T) {
expectVarint(t, (1<<7)-1, 1) max := 1 << 16
expectVarint(t, 0, 1) for x := 0; x < max; x++ {
expectVarint(t, 1<<7, 2) checkVarint(t, x)
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment