-
Notifications
You must be signed in to change notification settings - Fork 0
/
compound_test.go
104 lines (94 loc) · 2.08 KB
/
compound_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
package compound_test
import (
"testing"
"github.com/tv42/compound"
)
func TestKeyString(t *testing.T) {
type T struct {
S string
}
val := T{S: "foo"}
key := compound.Key(val)
if g, e := string(key), "\x02foo\x00"; g != e {
t.Errorf("bad Key: %#v -> %q != %q", val, g, e)
}
}
func TestKeyStringEmpty(t *testing.T) {
type T struct {
S string
}
val := T{S: ""}
key := compound.Key(val)
if g, e := string(key), "\x02\x00"; g != e {
t.Errorf("bad Key: %#v -> %q != %q", val, g, e)
}
}
func TestKeyStringNul(t *testing.T) {
type T struct {
S string
}
val := T{S: "foo\x00bar"}
key := compound.Key(val)
if g, e := string(key), "\x02foo\x00\xFFbar\x00"; g != e {
t.Errorf("bad Key: %#v -> %q != %q", val, g, e)
}
}
func TestKeyBytes(t *testing.T) {
type T struct {
S []byte
}
val := T{S: []byte("foo")}
key := compound.Key(val)
if g, e := string(key), "\x01foo\x00"; g != e {
t.Errorf("bad Key: %#v -> %q != %q", val, g, e)
}
}
func TestKeyUint64(t *testing.T) {
type T struct {
N uint64
}
val := T{N: 532}
key := compound.Key(val)
if g, e := string(key), "\x03\x00\x00\x00\x00\x00\x00\x02\x14"; g != e {
t.Errorf("bad Key: %#v -> %q != %q", val, g, e)
}
}
func TestDecodeString(t *testing.T) {
type T struct {
S string
}
key := []byte("\x02foo\x00")
var val T
if err := compound.Decode(key, &val); err != nil {
t.Fatal(err)
}
if g, e := val.S, "foo"; g != e {
t.Errorf("bad Decode: %#v -> %q != %q", key, g, e)
}
}
func TestPrefixN(t *testing.T) {
type K struct {
A uint64
B string
C uint64
}
val := K{A: 42, B: "noise"}
fields := 1
key := compound.PrefixN(val, fields)
if g, e := string(key), "\x03\x00\x00\x00\x00\x00\x00\x00*"; g != e {
t.Errorf("bad PrefixN: %#v %d -> %q != %q", val, fields, g, e)
}
}
func TestPrefixNPartial(t *testing.T) {
type K struct {
A uint64
B string
C uint64
}
val := K{A: 42, B: "foo"}
fields := 2
key := compound.PrefixNPartial(val, fields)
if g, e := string(key), "\x03\x00\x00\x00\x00\x00\x00\x00*\x02foo"; g != e {
t.Errorf("bad PrefixNPartial: %#v %d -> %q != %q", val, fields, g, e)
}
}