util: drop io, reuse its tests for binaryRW

This commit is contained in:
Roman Khimov 2019-08-28 19:41:56 +03:00
parent eba83a0e31
commit 15311f202b
2 changed files with 21 additions and 108 deletions

View file

@ -12,9 +12,9 @@ func TestWriteVarUint1(t *testing.T) {
val = uint64(1)
buf = new(bytes.Buffer)
)
if err := WriteVarUint(buf, val); err != nil {
t.Fatal(err)
}
bw := BinWriter{W: buf}
bw.WriteVarUint(val)
assert.Nil(t, bw.Err)
assert.Equal(t, 1, buf.Len())
}
@ -23,13 +23,14 @@ func TestWriteVarUint1000(t *testing.T) {
val = uint64(1000)
buf = new(bytes.Buffer)
)
if err := WriteVarUint(buf, val); err != nil {
t.Fatal(err)
}
bw := BinWriter{W: buf}
bw.WriteVarUint(val)
assert.Nil(t, bw.Err)
assert.Equal(t, 3, buf.Len())
assert.Equal(t, byte(0xfd), buf.Bytes()[0])
res := ReadVarUint(buf)
br := BinReader{R: buf}
res := br.ReadVarUint()
assert.Nil(t, br.Err)
assert.Equal(t, val, res)
}
@ -38,13 +39,14 @@ func TestWriteVarUint100000(t *testing.T) {
val = uint64(100000)
buf = new(bytes.Buffer)
)
if err := WriteVarUint(buf, val); err != nil {
t.Fatal(err)
}
bw := BinWriter{W: buf}
bw.WriteVarUint(val)
assert.Nil(t, bw.Err)
assert.Equal(t, 5, buf.Len())
assert.Equal(t, byte(0xfe), buf.Bytes()[0])
res := ReadVarUint(buf)
br := BinReader{R: buf}
res := br.ReadVarUint()
assert.Nil(t, br.Err)
assert.Equal(t, val, res)
}
@ -53,12 +55,13 @@ func TestWriteVarUint100000000000(t *testing.T) {
val = uint64(1000000000000)
buf = new(bytes.Buffer)
)
if err := WriteVarUint(buf, val); err != nil {
t.Fatal(err)
}
bw := BinWriter{W: buf}
bw.WriteVarUint(val)
assert.Nil(t, bw.Err)
assert.Equal(t, 9, buf.Len())
assert.Equal(t, byte(0xff), buf.Bytes()[0])
res := ReadVarUint(buf)
br := BinReader{R: buf}
res := br.ReadVarUint()
assert.Nil(t, br.Err)
assert.Equal(t, val, res)
}

View file

@ -1,90 +0,0 @@
package util
import (
"encoding/binary"
"io"
)
// Variable length integer, can be encoded to save space according to the value typed.
// len 1 uint8
// len 3 0xfd + uint16
// len 5 0xfe = uint32
// len 9 0xff = uint64
// For more information about this:
// https://github.com/neo-project/neo/wiki/Network-Protocol
// ReadVarUint reads a variable unsigned integer and returns it as a uint64.
func ReadVarUint(r io.Reader) uint64 {
var b uint8
binary.Read(r, binary.LittleEndian, &b)
switch b {
case 0xfd:
var v uint16
binary.Read(r, binary.LittleEndian, &v)
return uint64(v)
case 0xfe:
var v uint32
binary.Read(r, binary.LittleEndian, &v)
return uint64(v)
case 0xff:
var v uint64
binary.Read(r, binary.LittleEndian, &v)
return v
default:
return uint64(b)
}
}
// WriteVarUint writes a variable unsigned integer.
func WriteVarUint(w io.Writer, val uint64) error {
if val < 0xfd {
return binary.Write(w, binary.LittleEndian, uint8(val))
}
if val < 0xFFFF {
if err := binary.Write(w, binary.LittleEndian, byte(0xfd)); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, uint16(val))
}
if val < 0xFFFFFFFF {
if err := binary.Write(w, binary.LittleEndian, byte(0xfe)); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, uint32(val))
}
if err := binary.Write(w, binary.LittleEndian, byte(0xff)); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, val)
}
// ReadVarBytes reads a variable length byte array.
func ReadVarBytes(r io.Reader) ([]byte, error) {
n := ReadVarUint(r)
b := make([]byte, n)
if err := binary.Read(r, binary.LittleEndian, b); err != nil {
return nil, err
}
return b, nil
}
// ReadVarString reads a variable length string.
func ReadVarString(r io.Reader) (string, error) {
b, err := ReadVarBytes(r)
return string(b), err
}
// WriteVarString writes a variable length string.
func WriteVarString(w io.Writer, s string) error {
return WriteVarBytes(w, []byte(s))
}
// WriteVarBytes writes a variable length byte array.
func WriteVarBytes(w io.Writer, b []byte) error {
if err := WriteVarUint(w, uint64(len(b))); err != nil {
return err
}
return binary.Write(w, binary.LittleEndian, b)
}