Avoid allocating memory in Type() method.

This commit is contained in:
Kevin Atkinson
2018-08-25 15:27:05 -04:00
parent 426ebe9e55
commit 667c6a9418
3 changed files with 62 additions and 7 deletions

22
varint_test.go Normal file
View File

@@ -0,0 +1,22 @@
package cid
import (
"encoding/binary"
"testing"
)
func TestUvarintRoundTrip(t *testing.T) {
testCases := []uint64{0, 1, 2, 127, 128, 129, 255, 256, 257, 1<<63 - 1}
for _, tc := range testCases {
buf := make([]byte, 16)
binary.PutUvarint(buf, tc)
v, l1 := uvarint(string(buf))
_, l2 := binary.Uvarint(buf)
if tc != v {
t.Errorf("roundtrip failed expected %d but got %d", tc, v)
}
if l1 != l2 {
t.Errorf("length incorrect expected %d but got %d", l2, l1)
}
}
}