forked from colinmarc/sequencefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vint_test.go
50 lines (46 loc) · 1.5 KB
/
vint_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
package sequencefile
import (
"bytes"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
// To genenerate these tests in a scala shell:
// scala> def vIntHex(l: Long) = {
// val baos = new java.io.ByteArrayOutputStream()
// val dos = new java.io.DataOutputStream(baos)
// baos.reset()
// org.apache.hadoop.io.WritableUtils.writeVLong(dos, l)
// Hex.encodeHexString(baos.toByteArray)
// }
var vints = []struct {
b []byte
expected int64
}{
{[]byte{0x00}, 0},
{[]byte{0x01}, 1},
{[]byte{0xff}, -1},
{[]byte{0x64}, 100},
{[]byte{0x9c}, -100},
{[]byte{0x8f, 0xc8}, 200},
{[]byte{0x87, 0xc7}, -200},
{[]byte{0x8e, 0x1f, 0xff}, 8191},
{[]byte{0x86, 0x1f, 0xfe}, -8191},
{[]byte{0x8c, 0x7f, 0xff, 0xff, 0xff}, 2147483647},
{[]byte{0x84, 0x7f, 0xff, 0xff, 0xfe}, -2147483647},
{[]byte{0x8c, 0x6d, 0x7f, 0x77, 0x58}, 1837070168},
{[]byte{0x84, 0x6d, 0x7f, 0x77, 0x57}, -1837070168},
{[]byte{0x8c, 0xff, 0xff, 0xff, 0xfe}, 4294967294},
{[]byte{0x84, 0xff, 0xff, 0xff, 0xfd}, -4294967294},
{[]byte{0x88, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 576460752303423488},
{[]byte{0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, -576460752303423488},
}
func TestVInt(t *testing.T) {
for _, spec := range vints {
t.Run(strconv.FormatInt(spec.expected, 10), func(t *testing.T) {
res, err := ReadVInt(bytes.NewBuffer(spec.b))
assert.NoError(t, err, "ReadVInt should return successfully")
assert.Equal(t, spec.expected, res, "ReadVInt should return the correct result")
})
}
}