-
Notifications
You must be signed in to change notification settings - Fork 2
/
writer_test.go
290 lines (264 loc) · 7.43 KB
/
writer_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package iox
import (
"bytes"
"testing"
)
func TestWriter(t *testing.T) {
var tint8 int8 = -100
//int16ToBytes
b := int8ToBytes(tint8)
tint82 := bytesToInt8(b)
if tint8 != tint82 {
t.Fatalf("unexpected value obtained; got %b want %b", tint8, tint82)
}
var tuint8 uint8 = 100
//int16ToBytes
b = uint8ToBytes(tuint8)
tuint82 := bytesToUint8(b)
if tuint82 != tuint82 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint8, tuint82)
}
var tint16 int16 = -1000
//int16ToBytes
b = int16ToBytes(tint16)
tInt162 := bytesToInt16(b)
if tint16 != tInt162 {
t.Fatalf("unexpected value obtained; got %b want %b", tint16, tInt162)
}
//int16ToBytesBigEndian
b = int16ToBytesBigEndian(tint16)
tInt162 = bytesToInt16BigEndian(b)
if tint16 != tInt162 {
t.Fatalf("unexpected value obtained; got %b want %b", tint16, tInt162)
}
var tuint16 uint16 = 1000
//uint16ToBytes
b = uint16ToBytes(tuint16)
tuint162 := bytesToUint16(b)
if tuint16 != tuint162 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint16, tuint162)
}
b = uint16ToBytesBigEndian(tuint16)
tuint162 = bytesToUint16BigEndian(b)
if tuint16 != tuint162 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint16, tuint162)
}
var tInt32 int32 = -1000
//int32ToBytes
b = int32ToBytes(tInt32)
tInt322 := bytesToInt32(b)
if tInt32 != tInt322 {
t.Fatalf("unexpected value obtained; got %b want %b", tInt32, tInt322)
}
//int32ToBytesBigEndian
b = int32ToBytesBigEndian(tInt32)
tInt322 = bytesToInt32BigEndian(b)
if tInt32 != tInt322 {
t.Fatalf("unexpected value obtained; got %b want %b", tInt32, tInt322)
}
//uint32ToBytes
var tuint32 uint32 = 1000
b = uint32ToBytes(tuint32)
tuint322 := bytesToUint32(b)
if tuint32 != tuint322 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint32, tuint322)
}
//uint32ToBytesBigEndian
b = uint32ToBytesBigEndian(tuint32)
tuint322 = bytesToUint32BigEndian(b)
if tuint32 != tuint322 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint32, tuint322)
}
var tInt64 int64 = -1000
//int64ToBytes
b = int64ToBytes(tInt64)
tInt642 := bytesToInt64(b)
if tInt64 != tInt642 {
t.Fatalf("unexpected value obtained; got %b want %b", tInt64, tInt642)
}
//int64ToBytesBigEndian
b = int64ToBytesBigEndian(tInt64)
tInt642 = bytesToInt64BigEndian(b)
if tInt64 != tInt642 {
t.Fatalf("unexpected value obtained; got %b want %b", tInt64, tInt642)
}
var tuint64 uint64 = 1000
//uint64ToBytes
b = uint64ToBytes(tuint64)
tuint62 := bytesToUint64(b)
if tuint64 != tuint62 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint64, tuint62)
}
//uint64ToBytesBigEndian
b = uint64ToBytesBigEndian(tuint64)
tuint62 = bytesToUint64BigEndian(b)
if tuint64 != tuint62 {
t.Fatalf("unexpected value obtained; got %b want %b", tuint64, tuint62)
}
var buf bytes.Buffer
bb := NewBytesBuffer(buf)
//WriteInt8 Readint8
bb.Reset()
bb.WriteInt8(-100)
rd := NewReadSeekerFromBytes(bb.Bytes())
yint8, _ := rd.ReadInt8()
if yint8 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint8, -100)
}
//WriteUint8 ReadUint8
bb.Reset()
bb.WriteUint8(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint8, _ := rd.ReadUint8()
if yuint8 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint8, 100)
}
//WriteInt16 ReadInt16
bb.Reset()
bb.WriteInt16(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint16, _ := rd.ReadInt16()
if yint16 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint16, -100)
}
//WriteInt16BigEndian ReadInt16BigEndian
bb.Reset()
bb.WriteInt16BigEndian(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint16, _ = rd.ReadInt16BigEndian()
if yint16 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint16, -100)
}
//WriteUint16 ReadUint16
bb.Reset()
bb.WriteUint16(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint16, _ := rd.ReadUint16()
if yuint16 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint16, 100)
}
//WriteUint16BigEndian ReadUint16
bb.Reset()
bb.WriteUint16BigEndian(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint16, _ = rd.ReadUint16BigEndian()
if yuint16 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint16, 100)
}
//WriteInt32 ReadInt32
bb.Reset()
bb.WriteInt32(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint32, _ := rd.ReadInt32()
if yint32 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint32, -100)
}
//WriteInt32BigEndian ReadInt32BigEndian
bb.Reset()
bb.WriteInt32BigEndian(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint32, _ = rd.ReadInt32BigEndian()
if yint32 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint32, -100)
}
//WriteInt32 ReadInt32
bb.Reset()
bb.WriteUint32(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint32, _ := rd.ReadUint32()
if yint32 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint32, 100)
}
//WriteInt32 ReadInt32
bb.Reset()
bb.WriteUint32BigEndian(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint32, _ = rd.ReadUint32BigEndian()
if yint32 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint32, 100)
}
//WriteInt64 ReadInt64
bb.Reset()
bb.WriteInt64(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint64, _ := rd.ReadInt64()
if yint64 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint64, -100)
}
//WriteInt64 ReadInt64
bb.Reset()
bb.WriteInt64BigEndian(-100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yint64, _ = rd.ReadInt64BigEndian()
if yint64 != -100 {
t.Fatalf("unexpected value obtained; got %v want %v", yint64, -100)
}
//WriteUint64 ReadUint64
bb.Reset()
bb.WriteUint64(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint64, _ := rd.ReadUint64()
if yuint64 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint64, 100)
}
//WriteUint64 ReadUint64
bb.Reset()
bb.WriteUint64BigEndian(100)
rd = NewReadSeekerFromBytes(bb.Bytes())
yuint64, _ = rd.ReadUint64BigEndian()
if yuint64 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yuint64, 100)
}
//float32
bb.Reset()
bb.WriteFloat32(float32(100.10))
rd = NewReadSeekerFromBytes(bb.Bytes())
yfloat32, _ := rd.ReadFloat32()
if yuint64 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yfloat32, float32(100.10))
}
//float64
bb.Reset()
bb.WriteFloat32(float32(100.1010))
rd = NewReadSeekerFromBytes(bb.Bytes())
yfloat64, _ := rd.ReadFloat64()
if yuint64 != 100 {
t.Fatalf("unexpected value obtained; got %v want %v", yfloat64, float64(100.1010))
}
var s string
for i := 0; i < 100; i++ {
s = s + "s"
}
//WriteStringUint8 ReadStringUint8
bb.Reset()
bb.WriteStringUint8(s)
rd = NewReadSeekerFromBytes(bb.Bytes())
ss, _ := rd.ReadStringUint8()
if ss != s {
t.Fatalf("unexpected value obtained; got %v want %v", ss, s)
}
//WriteStringUint16 ReadStringUint16
bb.Reset()
bb.WriteStringUint16(s)
rd = NewReadSeekerFromBytes(bb.Bytes())
ss, _ = rd.ReadStringUint16()
if ss != s {
t.Fatalf("unexpected value obtained; got %v want %v", ss, s)
}
//WriteStringUint32 ReadStringUint32
bb.Reset()
bb.WriteStringUint32(s)
rd = NewReadSeekerFromBytes(bb.Bytes())
ss, _ = rd.ReadStringUint32()
if ss != s {
t.Fatalf("unexpected value obtained; got %v want %v", ss, s)
}
//WriteStringUint64 ReadStringUint64
bb.Reset()
bb.WriteStringUint64(s)
rd = NewReadSeekerFromBytes(bb.Bytes())
ss, _ = rd.ReadStringUint64()
if ss != s {
t.Fatalf("unexpected value obtained; got %v want %v", ss, s)
}
}