forked from linuxdeepin/go-x11-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
88 lines (71 loc) · 2.04 KB
/
reader_test.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package x
import (
"testing"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct {
r *Reader
}
var _ = Suite(&MySuite{})
func (s *MySuite) SetUpTest(c *C) {
buf := []byte{1, 2, 3, 4, 5, 6, 7, 8}
s.r = NewReaderFromData(buf)
}
func (s *MySuite) TestReaderReadBytes(c *C) {
v, err := s.r.ReadBytes(4)
c.Assert(v, DeepEquals, []byte{1, 2, 3, 4})
c.Assert(err, IsNil)
v, err = s.r.ReadBytes(3)
c.Assert(v, DeepEquals, []byte{5, 6, 7})
c.Assert(err, IsNil)
}
func (s *MySuite) TestReaderReadBytesWithPad(c *C) {
v, err := s.r.ReadBytesWithPad(3)
c.Assert(v, DeepEquals, []byte{1, 2, 3})
c.Assert(s.r.Pos(), Equals, 4)
c.Assert(err, IsNil)
}
func (s *MySuite) TestReaderRead1b(c *C) {
for i := 1; i <= 8; i++ {
c.Assert(s.r.Read1b(), Equals, uint8(i))
}
}
func (s *MySuite) TestReaderRead2b(c *C) {
results := []uint16{0x0201, 0x0403, 0x0605, 0x0807}
for _, result := range results {
c.Assert(s.r.Read2b(), Equals, result)
}
}
func (s *MySuite) TestReaderRead4b(c *C) {
results := []uint32{0x04030201, 0x08070605}
for _, result := range results {
c.Assert(s.r.Read4b(), Equals, result)
}
}
func (s *MySuite) TestReaderReadNulTermStr(c *C) {
data := []byte{'h', 'e', 'l', 'l', 'o'}
r := NewReaderFromData(data)
str := r.ReadNulTermStr()
c.Assert(str, Equals, "hello")
c.Assert(r.Pos(), Equals, 5)
data = []byte{'h', 'e', 'l', 'l', 'o', 0, 'w', 'o', 'r', 'l', 'd'}
r = NewReaderFromData(data)
str = r.ReadNulTermStr()
c.Assert(str, Equals, "hello")
c.Assert(r.Pos(), Equals, 5)
}
func (s *MySuite) TestReaderRemainAtLeast(c *C) {
s.r.ReadPad(3)
c.Assert(s.r.RemainAtLeast(5), Equals, true)
c.Assert(s.r.RemainAtLeast(6), Equals, false)
s.r.Reset()
c.Assert(s.r.RemainAtLeast4b(0), Equals, true)
c.Assert(s.r.RemainAtLeast4b(1), Equals, true)
c.Assert(s.r.RemainAtLeast4b(2), Equals, true)
c.Assert(s.r.RemainAtLeast4b(3), Equals, false)
}