aboutsummaryrefslogtreecommitdiffstats
path: root/utils/binary/writer_test.go
blob: 1380280c0d598e6f64db7b8dba108ec70dd5330c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package binary

import (
	"bytes"
	"encoding/binary"

	. "gopkg.in/check.v1"
)

func (s *BinarySuite) TestWrite(c *C) {
	expected := bytes.NewBuffer(nil)
	err := binary.Write(expected, binary.BigEndian, int64(42))
	c.Assert(err, IsNil)
	err = binary.Write(expected, binary.BigEndian, int32(42))
	c.Assert(err, IsNil)

	buf := bytes.NewBuffer(nil)
	err = Write(buf, int64(42), int32(42))
	c.Assert(err, IsNil)
	c.Assert(buf, DeepEquals, expected)
}

func (s *BinarySuite) TestWriteUint32(c *C) {
	expected := bytes.NewBuffer(nil)
	err := binary.Write(expected, binary.BigEndian, int32(42))
	c.Assert(err, IsNil)

	buf := bytes.NewBuffer(nil)
	err = WriteUint32(buf, 42)
	c.Assert(err, IsNil)
	c.Assert(buf, DeepEquals, expected)
}

func (s *BinarySuite) TestWriteUint16(c *C) {
	expected := bytes.NewBuffer(nil)
	err := binary.Write(expected, binary.BigEndian, int16(42))
	c.Assert(err, IsNil)

	buf := bytes.NewBuffer(nil)
	err = WriteUint16(buf, 42)
	c.Assert(err, IsNil)
	c.Assert(buf, DeepEquals, expected)
}

func (s *BinarySuite) TestWriteVariableWidthInt(c *C) {
	buf := bytes.NewBuffer(nil)

	err := WriteVariableWidthInt(buf, 366)
	c.Assert(err, IsNil)
	c.Assert(buf.Bytes(), DeepEquals, []byte{129, 110})
}

func (s *BinarySuite) TestWriteVariableWidthIntShort(c *C) {
	buf := bytes.NewBuffer(nil)

	err := WriteVariableWidthInt(buf, 19)
	c.Assert(err, IsNil)
	c.Assert(buf.Bytes(), DeepEquals, []byte{19})
}