-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathxpb_test.go
181 lines (156 loc) · 5.18 KB
/
xpb_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2023-2024 Artyom Smirnov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestNewXPBReader(t *testing.T) {
assert.NotNil(t, NewXPBReader(nil))
assert.NotNil(t, NewXPBReader([]byte{1, 2, 3}))
}
func TestXPBReaderEnd(t *testing.T) {
xpb := NewXPBReader(nil)
require.NotNil(t, xpb)
assert.True(t, xpb.End())
xpb = NewXPBReader([]byte{1, 2, 3})
require.NotNil(t, xpb)
assert.False(t, xpb.End())
}
func TestXPBReader_GetInt16(t *testing.T) {
xpb := NewXPBReader([]byte{2, 2})
require.NotNil(t, xpb)
assert.Equal(t, int16(514), xpb.GetInt16())
require.True(t, xpb.End())
}
func TestXPBReader_GetInt32(t *testing.T) {
xpb := NewXPBReader([]byte{1, 2, 3, 4})
require.NotNil(t, xpb)
assert.Equal(t, int32(67305985), xpb.GetInt32())
require.True(t, xpb.End())
}
func TestXPBReader_GetString(t *testing.T) {
xpb := NewXPBReader([]byte{4, 0, 't', 'e', 's', 't'})
require.NotNil(t, xpb)
assert.Equal(t, "test", xpb.GetString())
require.True(t, xpb.End())
}
func TestXPBReader_GetMixed(t *testing.T) {
xpb := NewXPBReader([]byte{4, 0, 't', 'e', 's', 't', 1, 1, 2, 2, 2, 2})
require.NotNil(t, xpb)
assert.Equal(t, "test", xpb.GetString())
assert.Equal(t, int16(257), xpb.GetInt16())
assert.Equal(t, int32(33686018), xpb.GetInt32())
require.True(t, xpb.End())
}
func TestXPBReader_Next(t *testing.T) {
xpb := NewXPBReader([]byte{1, 2, 4, 0, 't', 'e', 's', 't'})
require.NotNil(t, xpb)
have, val := xpb.Next()
assert.True(t, have)
assert.Equal(t, byte(1), val)
have, val = xpb.Next()
assert.True(t, have)
assert.Equal(t, byte(2), val)
assert.Equal(t, "test", xpb.GetString())
have, _ = xpb.Next()
assert.False(t, have)
require.True(t, xpb.End())
}
func TestXPBReader_Get(t *testing.T) {
xpb := NewXPBReader([]byte{3, 4})
require.NotNil(t, xpb)
v1 := xpb.Get()
_, v2 := xpb.Next()
assert.Equal(t, byte(3), v1)
assert.Equal(t, byte(3), v2)
v1 = xpb.Get()
_, v2 = xpb.Next()
assert.Equal(t, byte(4), v1)
assert.Equal(t, byte(4), v2)
}
func TestXPBReader_Reset(t *testing.T) {
xpb := NewXPBReader([]byte{4, 0, 't', 'e', 's', 't'})
require.NotNil(t, xpb)
assert.Equal(t, "test", xpb.GetString())
require.True(t, xpb.End())
xpb.Reset()
require.False(t, xpb.End())
assert.Equal(t, "test", xpb.GetString())
}
func TestNewXPBWriter(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
assert.Equal(t, []byte{}, w.Bytes())
}
func TestNewXPBWriterFromTag(t *testing.T) {
w := NewXPBWriterFromTag(1)
require.NotNil(t, w)
assert.Equal(t, []byte{1}, w.Bytes())
}
func TestNewXPBWriterFromBytes(t *testing.T) {
w := NewXPBWriterFromBytes([]byte{1, 2, 3})
require.NotNil(t, w)
assert.Equal(t, []byte{1, 2, 3}, w.Bytes())
}
func TestXPBWriter_PutTag(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutTag(5)
assert.Equal(t, []byte{5}, w.Bytes())
}
func TestXPBWriter_PutByte(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutByte(5, 6)
assert.Equal(t, []byte{5, 6}, w.Bytes())
}
func TestXPBWriter_PutInt16(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutInt16(7, 20000)
assert.Equal(t, []byte{7, 0x20, 0x4e}, w.Bytes())
}
func TestXPBWriter_PutInt32(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutInt32(7, 200000000)
assert.Equal(t, []byte{7, 0x0, 0xc2, 0xeb, 0xb}, w.Bytes())
}
func TestXPBWriter_PutString(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutString(8, "test")
assert.Equal(t, []byte{8, 4, 0, 't', 'e', 's', 't'}, w.Bytes())
}
func TestXPBWriter_PutMixed(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutTag(1).PutByte(2, 3).PutInt16(4, 5).PutInt32(6, 7).PutString(8, "test").PutTag(9)
assert.Equal(t, []byte{1, 2, 3, 4, 5, 0, 6, 7, 0, 0, 0, 8, 4, 0, 't', 'e', 's', 't', 9}, w.Bytes())
}
func TestXPBWriter_Reset(t *testing.T) {
w := NewXPBWriter()
require.NotNil(t, w)
w.PutString(1, "test")
assert.Equal(t, []byte{1, 4, 0, 't', 'e', 's', 't'}, w.Bytes())
w.Reset()
assert.Equal(t, []byte{}, w.Bytes())
}