-
Notifications
You must be signed in to change notification settings - Fork 230
/
byte_input_test.go
69 lines (53 loc) · 1.48 KB
/
byte_input_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
package roaring
import (
"bytes"
"testing"
"github.com/RoaringBitmap/roaring/v2/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestByteInputFlow(t *testing.T) {
t.Run("Test should be an error on empty data", func(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
instances := []internal.ByteInput{
internal.NewByteInput(buf.Bytes()),
internal.NewByteInputFromReader(buf),
}
for _, input := range instances {
n, err := input.ReadUInt16()
assert.EqualValues(t, 0, n)
assert.Error(t, err)
p, err := input.ReadUInt32()
assert.EqualValues(t, 0, p)
assert.Error(t, err)
b, err := input.Next(10)
assert.Nil(t, b)
assert.Error(t, err)
err = input.SkipBytes(10)
assert.Error(t, err)
}
})
t.Run("Test on nonempty data", func(t *testing.T) {
buf := bytes.NewBuffer(uint16SliceAsByteSlice([]uint16{1, 10, 32, 66, 23}))
instances := []internal.ByteInput{
internal.NewByteInput(buf.Bytes()),
internal.NewByteInputFromReader(buf),
}
for _, input := range instances {
n, err := input.ReadUInt16()
assert.EqualValues(t, 1, n)
require.NoError(t, err)
p, err := input.ReadUInt32()
assert.EqualValues(t, 2097162, p) // 32 << 16 | 10
require.NoError(t, err)
b, err := input.Next(2)
assert.EqualValues(t, []byte{66, 0}, b)
require.NoError(t, err)
err = input.SkipBytes(2)
require.NoError(t, err)
b, err = input.Next(1)
assert.Nil(t, b)
assert.Error(t, err)
}
})
}