diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-31 14:56:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-31 14:56:38 +0000 |
commit | 659386309f36c482ddc0bb9e854ebda3da216491 (patch) | |
tree | eb156bfbfad599f3dd88e184a7f690f67c64337d /utils/binary/read_test.go | |
parent | 5944d8a185ff1f91e0e108dab1d756fd88692c3b (diff) | |
download | go-git-659386309f36c482ddc0bb9e854ebda3da216491.tar.gz |
utils: binary, new package that collect all the spare helper functions about binary operations (#102)
Diffstat (limited to 'utils/binary/read_test.go')
-rw-r--r-- | utils/binary/read_test.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/utils/binary/read_test.go b/utils/binary/read_test.go new file mode 100644 index 0000000..6579ffb --- /dev/null +++ b/utils/binary/read_test.go @@ -0,0 +1,87 @@ +package binary + +import ( + "bytes" + "encoding/binary" + "testing" + + . "gopkg.in/check.v1" + "gopkg.in/src-d/go-git.v3/core" +) + +func Test(t *testing.T) { TestingT(t) } + +type BinarySuite struct{} + +var _ = Suite(&BinarySuite{}) + +func (s *BinarySuite) TestRead(c *C) { + buf := bytes.NewBuffer(nil) + err := binary.Write(buf, binary.BigEndian, int64(42)) + c.Assert(err, IsNil) + err = binary.Write(buf, binary.BigEndian, int32(42)) + c.Assert(err, IsNil) + + var i64 int64 + var i32 int32 + err = Read(buf, &i64, &i32) + c.Assert(err, IsNil) + c.Assert(i64, Equals, int64(42)) + c.Assert(i32, Equals, int32(42)) +} + +func (s *BinarySuite) TestReadUntil(c *C) { + buf := bytes.NewBuffer([]byte("foo bar")) + + b, err := ReadUntil(buf, ' ') + c.Assert(err, IsNil) + c.Assert(b, HasLen, 3) + c.Assert(string(b), Equals, "foo") +} + +func (s *BinarySuite) TestReadVariableWidthInt(c *C) { + buf := bytes.NewBuffer([]byte{129, 110}) + + i, err := ReadVariableWidthInt(buf) + c.Assert(err, IsNil) + c.Assert(i, Equals, int64(366)) +} + +func (s *BinarySuite) TestReadVariableWidthIntShort(c *C) { + buf := bytes.NewBuffer([]byte{19}) + + i, err := ReadVariableWidthInt(buf) + c.Assert(err, IsNil) + c.Assert(i, Equals, int64(19)) +} + +func (s *BinarySuite) TestReadUint32(c *C) { + buf := bytes.NewBuffer(nil) + err := binary.Write(buf, binary.BigEndian, uint32(42)) + c.Assert(err, IsNil) + + i32, err := ReadUint32(buf) + c.Assert(err, IsNil) + c.Assert(i32, Equals, uint32(42)) +} + +func (s *BinarySuite) TestReadUint16(c *C) { + buf := bytes.NewBuffer(nil) + err := binary.Write(buf, binary.BigEndian, uint16(42)) + c.Assert(err, IsNil) + + i32, err := ReadUint16(buf) + c.Assert(err, IsNil) + c.Assert(i32, Equals, uint16(42)) +} + +func (s *BinarySuite) TestReadHash(c *C) { + expected := core.NewHash("43aec75c611f22c73b27ece2841e6ccca592f285") + buf := bytes.NewBuffer(nil) + err := binary.Write(buf, binary.BigEndian, expected) + c.Assert(err, IsNil) + + hash, err := ReadHash(buf) + c.Assert(err, IsNil) + c.Assert(hash.String(), Equals, expected.String()) +} |