This repository was archived by the owner on Dec 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_test.go
More file actions
63 lines (53 loc) · 1.45 KB
/
codec_test.go
File metadata and controls
63 lines (53 loc) · 1.45 KB
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
package db_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/toba/pbdb"
"github.com/toba/pbdb/schema"
)
var (
signed = map[int][]byte{
-123456: []byte{255, 255, 255, 255, 255, 254, 29, 192},
0: []byte{0, 0, 0, 0, 0, 0, 0, 0},
56: []byte{0, 0, 0, 0, 0, 0, 0, 56},
123456789: []byte{0, 0, 0, 0, 7, 91, 205, 21},
123456789123456789: []byte{1, 182, 155, 75, 172, 208, 95, 21},
}
unsigned = map[uint][]byte{
1984: []byte{0, 0, 0, 0, 0, 0, 7, 192},
2017: []byte{0, 0, 0, 0, 0, 0, 7, 225},
}
)
func TestEncodeDecode(t *testing.T) {
buf, err := db.Encode(employee.Value)
assert.NoError(t, err)
assert.NotNil(t, buf)
out := &schema.Employee{}
err = db.Decode(buf, out)
assert.NoError(t, err)
assert.NotNil(t, out)
assert.Equal(t, employeeNumber, out.Number)
assert.Equal(t, firstName, out.FirstName)
}
func TestNumberToBytes(t *testing.T) {
for num, expect := range signed {
buf, err := db.NumberToBytes(num)
assert.NoError(t, err)
assert.Equal(t, buf, expect)
}
for num, expect := range unsigned {
buf, err := db.NumberToBytes(num)
assert.NoError(t, err)
assert.Equal(t, buf, expect)
}
buf, err := db.NumberToBytes("notanumber")
assert.Error(t, err)
assert.Nil(t, buf)
}
func TestNumberFromBytes(t *testing.T) {
for expect, buf := range signed {
num, err := db.NumberFromBytes(buf)
assert.NoError(t, err)
assert.Equal(t, num, int64(expect))
}
}