package moneroutil

import (
	"bytes"
	"testing"
)

func TestVarInt(t *testing.T) {
	tests := []struct {
		name   string
		varInt []byte
		want   uint64
	}{
		{
			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,
		},
	}
	var got uint64
	var err error
	var gotVarInt []byte
	buf := new(bytes.Buffer)
	for _, test := range tests {
		gotVarInt = Uint64ToBytes(test.want)
		if bytes.Compare(gotVarInt, test.varInt) != 0 {
			t.Errorf("%s: varint want %x, got %x", test.name, test.varInt, gotVarInt)
			continue
		}
		buf.Reset()
		buf.Write(test.varInt)
		got, err = ReadVarInt(buf)
		if err != nil {
			t.Errorf("%s: %s", test.name, err)
			continue
		}
		if test.want != got {
			t.Errorf("%s: want %d, got %d", test.name, test.want, got)
			continue
		}
	}
}
