-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
list_test.go
156 lines (145 loc) · 3.66 KB
/
list_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package capnp
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToListDefault(t *testing.T) {
msg := &Message{Arena: SingleSegment([]byte{
0, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
})}
seg, err := msg.Segment(0)
if err != nil {
t.Fatal(err)
}
tests := []struct {
ptr Ptr
def []byte
list List
}{
{Ptr{}, nil, List{}},
{Struct{}.ToPtr(), nil, List{}},
{Struct{seg: seg, off: 0, depthLimit: maxDepth}.ToPtr(), nil, List{}},
{List{}.ToPtr(), nil, List{}},
{
ptr: List{
seg: seg,
off: 8,
length: 1,
size: ObjectSize{DataSize: 8},
depthLimit: maxDepth,
}.ToPtr(),
list: List{
seg: seg,
off: 8,
length: 1,
size: ObjectSize{DataSize: 8},
depthLimit: maxDepth,
},
},
}
for _, test := range tests {
list, err := test.ptr.ListDefault(test.def)
if err != nil {
t.Errorf("%#v.ListDefault(% 02x) error: %v", test.ptr, test.def, err)
continue
}
if !deepPointerEqual(list.ToPtr(), test.list.ToPtr()) {
t.Errorf("%#v.ListDefault(% 02x) = %#v; want %#v", test.ptr, test.def, list, test.list)
}
}
}
func TestTextListBytesAt(t *testing.T) {
msg := &Message{Arena: SingleSegment([]byte{
0, 0, 0, 0, 0, 0, 0, 0,
0x01, 0, 0, 0, 0x22, 0, 0, 0,
'f', 'o', 'o', 0, 0, 0, 0, 0,
})}
seg, err := msg.Segment(0)
if err != nil {
t.Fatal(err)
}
list := TextList{
seg: seg,
off: 8,
length: 1,
size: ObjectSize{PointerCount: 1},
depthLimit: maxDepth,
}
b, err := list.BytesAt(0)
if err != nil {
t.Errorf("list.BytesAt(0) error: %v", err)
}
if !bytes.Equal(b, []byte("foo")) {
t.Errorf("list.BytesAt(0) = %q; want \"foo\"", b)
}
}
func TestListRaw(t *testing.T) {
_, seg, err := NewMessage(SingleSegment(nil))
if err != nil {
t.Fatal(err)
}
tests := []struct {
list List
raw rawPointer
}{
{list: List{}, raw: 0},
{
list: List{seg: seg, length: 3, size: ObjectSize{}},
raw: 0x0000001800000001,
},
{
list: List{seg: seg, off: 24, length: 15, flags: isBitList},
raw: 0x0000007900000001,
},
{
list: List{seg: seg, off: 40, length: 15, size: ObjectSize{DataSize: 1}},
raw: 0x0000007a00000001,
},
{
list: List{seg: seg, off: 40, length: 15, size: ObjectSize{DataSize: 2}},
raw: 0x0000007b00000001,
},
{
list: List{seg: seg, off: 40, length: 15, size: ObjectSize{DataSize: 4}},
raw: 0x0000007c00000001,
},
{
list: List{seg: seg, off: 40, length: 15, size: ObjectSize{DataSize: 8}},
raw: 0x0000007d00000001,
},
{
list: List{seg: seg, off: 40, length: 15, size: ObjectSize{PointerCount: 1}},
raw: 0x0000007e00000001,
},
{
list: List{seg: seg, off: 40, length: 7, size: ObjectSize{DataSize: 16, PointerCount: 1}, flags: isCompositeList},
raw: 0x000000af00000001,
},
}
for _, test := range tests {
if raw := test.list.raw(); raw != test.raw {
t.Errorf("%+v.raw() = %#v; want %#v", test.list, raw, test.raw)
}
}
}
// TestListCastRegression is a regression test for a bug where, if a struct
// list whose elements have a non-empty data section was cast to a pointer
// list, the pointer would be read out of the data section of the relevant
// element, rather than the pointer section.
func TestListCastRegression(t *testing.T) {
_, seg, err := NewMessage(SingleSegment(nil))
assert.Nil(t, err)
txt, err := NewText(seg, "Text")
assert.Nil(t, err)
i := 3
l, err := NewCompositeList(seg, ObjectSize{DataSize: 16, PointerCount: 1}, 6)
assert.Nil(t, err)
strct := l.Struct(i)
strct.SetPtr(0, txt.ToPtr())
ptrList := PointerList(l)
ptr, err := ptrList.At(i)
assert.Nil(t, err)
assert.Equal(t, ptr.Text(), "Text")
}