-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
61 lines (54 loc) · 1.21 KB
/
codec_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
package scale
import (
"bytes"
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestCompact32(t *testing.T) {
buf := bytes.NewBuffer(nil)
enc := NewEncoder(buf)
const expected = 321
_, err := EncodeCompact32(enc, expected)
require.NoError(t, err)
dec := NewDecoder(buf)
rst, _, err := DecodeCompact32(dec)
require.NoError(t, err)
require.EqualValues(t, expected, rst)
}
func TestCompactBigInt(t *testing.T) {
const encoded uint64 = math.MaxUint64
buf := bytes.NewBuffer(nil)
enc := NewEncoder(buf)
_, err := EncodeCompact64(enc, encoded)
require.NoError(t, err)
dec := NewDecoder(buf)
rst, _, err := DecodeCompact64(dec)
require.NoError(t, err)
require.EqualValues(t, encoded, rst)
}
func TestLenSize(t *testing.T) {
for _, l := range []uint32{
0,
1,
16,
64,
65,
1<<14 - 1,
1 << 14,
1 << 20,
1 << 30,
1 << 31,
1<<32 - 1,
} {
var b bytes.Buffer
n, err := EncodeLen(NewEncoder(&b), l, 1<<32-1)
require.NoError(t, err)
require.Equal(t, uint32(b.Len()), LenSize(l))
require.Equal(t, uint32(n), LenSize(l))
decoded, n, err := DecodeLen(NewDecoder(&b), 1<<32-1)
require.NoError(t, err)
require.Equal(t, l, decoded)
require.Equal(t, uint32(n), LenSize(l))
}
}