| 1 |
package binary |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
|
| 7 |
. "gopkg.in/check.v1" |
| 8 |
) |
| 9 |
|
| 10 |
func (s *BinarySuite) TestWrite(c *C) { |
| 11 |
expected := bytes.NewBuffer(nil) |
| 12 |
err := binary.Write(expected, binary.BigEndian, int64(42)) |
| 13 |
c.Assert(err, IsNil) |
| 14 |
err = binary.Write(expected, binary.BigEndian, int32(42)) |
| 15 |
c.Assert(err, IsNil) |
| 16 |
|
| 17 |
buf := bytes.NewBuffer(nil) |
| 18 |
err = Write(buf, int64(42), int32(42)) |
| 19 |
c.Assert(err, IsNil) |
| 20 |
c.Assert(buf, DeepEquals, expected) |
| 21 |
} |
| 22 |
|
| 23 |
func (s *BinarySuite) TestWriteUint32(c *C) { |
| 24 |
expected := bytes.NewBuffer(nil) |
| 25 |
err := binary.Write(expected, binary.BigEndian, int32(42)) |
| 26 |
c.Assert(err, IsNil) |
| 27 |
|
| 28 |
buf := bytes.NewBuffer(nil) |
| 29 |
err = WriteUint32(buf, 42) |
| 30 |
c.Assert(err, IsNil) |
| 31 |
c.Assert(buf, DeepEquals, expected) |
| 32 |
} |
| 33 |
|
| 34 |
func (s *BinarySuite) TestWriteUint16(c *C) { |
| 35 |
expected := bytes.NewBuffer(nil) |
| 36 |
err := binary.Write(expected, binary.BigEndian, int16(42)) |
| 37 |
c.Assert(err, IsNil) |
| 38 |
|
| 39 |
buf := bytes.NewBuffer(nil) |
| 40 |
err = WriteUint16(buf, 42) |
| 41 |
c.Assert(err, IsNil) |
| 42 |
c.Assert(buf, DeepEquals, expected) |
| 43 |
} |
| 44 |
|
| 45 |
func (s *BinarySuite) TestWriteVariableWidthInt(c *C) { |
| 46 |
buf := bytes.NewBuffer(nil) |
| 47 |
|
| 48 |
err := WriteVariableWidthInt(buf, 366) |
| 49 |
c.Assert(err, IsNil) |
| 50 |
c.Assert(buf.Bytes(), DeepEquals, []byte{129, 110}) |
| 51 |
} |
| 52 |
|
| 53 |
func (s *BinarySuite) TestWriteVariableWidthIntShort(c *C) { |
| 54 |
buf := bytes.NewBuffer(nil) |
| 55 |
|
| 56 |
err := WriteVariableWidthInt(buf, 19) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
c.Assert(buf.Bytes(), DeepEquals, []byte{19}) |
| 59 |
} |