This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
moneroutil/varint_test.go

58 lines
1 KiB
Go
Raw Permalink Normal View History

2017-04-25 00:19:04 +00:00
package moneroutil
import (
"bytes"
"testing"
)
func TestVarInt(t *testing.T) {
tests := []struct {
name string
varInt []byte
2017-04-27 23:21:44 +00:00
want uint64
2017-04-25 00:19:04 +00:00
}{
{
name: "1 byte",
varInt: []byte{0x01},
want: 1,
},
{
name: "3 bytes",
varInt: []byte{0x8f, 0xd6, 0x17},
want: 387855,
},
{
name: "4 bytes",
varInt: []byte{0x80, 0x92, 0xf4, 0x01},
want: 4000000,
},
{
name: "7 bytes",
varInt: []byte{0x80, 0xc0, 0xca, 0xf3, 0x84, 0xa3, 0x02},
want: 10000000000000,
},
}
2017-04-27 23:21:44 +00:00
var got uint64
var err error
2017-04-25 00:19:04 +00:00
var gotVarInt []byte
2017-04-27 23:21:44 +00:00
buf := new(bytes.Buffer)
2017-04-25 00:19:04 +00:00
for _, test := range tests {
2017-04-27 23:21:44 +00:00
gotVarInt = Uint64ToBytes(test.want)
2017-04-25 00:19:04 +00:00
if bytes.Compare(gotVarInt, test.varInt) != 0 {
t.Errorf("%s: varint want %x, got %x", test.name, test.varInt, gotVarInt)
continue
}
2017-04-27 23:21:44 +00:00
buf.Reset()
buf.Write(test.varInt)
got, err = ReadVarInt(buf)
if err != nil {
t.Errorf("%s: %s", test.name, err)
continue
}
2017-04-25 00:19:04 +00:00
if test.want != got {
t.Errorf("%s: want %d, got %d", test.name, test.want, got)
continue
}
}
}