-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvalues_test.go
243 lines (225 loc) · 5.35 KB
/
values_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ptrie
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/stretchr/testify/assert"
"hash/fnv"
"io"
"log"
"reflect"
"testing"
"unsafe"
)
type useCase[T any] struct {
description string
values []T
hasError bool
}
func test[T any](t *testing.T, uc useCase[T]) {
values := newValues[T]()
for _, item := range uc.values {
_, err := values.put(item)
assert.Nil(t, err, uc.description)
}
writer := new(bytes.Buffer)
err := values.Encode(writer)
if uc.hasError {
assert.NotNil(t, err, uc.description)
cloned := newValues[T]()
cloned.useType(reflect.TypeOf(uc.values[0]))
err = cloned.Decode(writer)
assert.NotNil(t, err)
return
}
if !assert.Nil(t, err, uc.description) {
log.Print(err)
return
}
cloned := newValues[T]()
cloned.useType(reflect.TypeOf(uc.values[0]))
err = cloned.Decode(writer)
assert.Nil(t, err, uc.description)
assert.EqualValues(t, len(values.data), len(cloned.data))
for i := range values.data {
assert.EqualValues(t, values.data[i], cloned.data[i], fmt.Sprintf("[%d]: %v", i, uc.description))
}
}
func TestIntValues_Decode(t *testing.T) {
u := useCase[int]{
description: "int coding",
values: []int{0, 10, 30, 300, 4},
}
test(t, u)
}
func TestStringValues_Decode(t *testing.T) {
u := useCase[string]{
description: "string coding",
values: []string{"abc", "xyz", "klm", "xyz", "eee"},
}
test(t, u)
}
func TestInt8Values_Decode(t *testing.T) {
u := useCase[int8]{
description: "int8 coding",
values: []int8{int8(3), int8(10), int8(30), int8(121), int8(4)},
}
test(t, u)
}
func TestInt64Values_Decode(t *testing.T) {
u := useCase[int64]{
description: "int64 coding",
values: []int64{int64(3), int64(10), int64(88888888830), int64(121), int64(4)},
}
test(t, u)
}
func TestBoolValues_Decode(t *testing.T) {
u := useCase[bool]{
description: "bool coding",
values: []bool{true, false},
}
test(t, u)
}
func TestByteValues_Decode(t *testing.T) {
u := useCase[[]byte]{
description: "[]byte coding",
values: [][]byte{[]byte("abc"), []byte("xyz")},
}
test(t, u)
}
func TestCustomValues_Decode(t *testing.T) {
u := useCase[*foo]{
description: "custom type coding",
values: []*foo{{ID: 10, Name: "abc"}, {ID: 20, Name: "xyz"}},
}
test(t, u)
}
func TestCustomValuesErr_Decode(t *testing.T) {
u := useCase[*bar]{
description: "custom type error coding",
values: []*bar{{ID: 10, Name: "abc"}, {ID: 20, Name: "xyz"}},
hasError: true,
}
test(t, u)
}
func TestStuct_Decode(t *testing.T) {
u := useCase[*rules]{
description: "scalar struct type coding",
values: []*rules{
{
P: []rule{{Id1: 1, Id2: 2}},
B: []rule{{Id1: 3, Id2: 4}},
K: []rule{{Id1: 5, Id2: 6}},
},
},
}
test(t, u)
}
type foo struct {
ID int
Name string
}
type bar foo
func (c *bar) Key() interface{} {
h := fnv.New32a()
_, _ = h.Write([]byte(c.Name))
return c.ID + 100000*int(h.Sum32())
}
func (c *foo) Key() interface{} {
h := fnv.New32a()
_, _ = h.Write([]byte(c.Name))
return c.ID + 100000*int(h.Sum32())
}
func (c *foo) Decode(reader io.Reader) error {
id := int64(0)
if err := binary.Read(reader, binary.BigEndian, &id); err != nil {
return err
}
c.ID = int(id)
length := uint16(0)
err := binary.Read(reader, binary.BigEndian, &length)
if err == nil {
name := make([]byte, length)
if err = binary.Read(reader, binary.BigEndian, name); err == nil {
c.Name = string(name)
}
}
return err
}
func (c *foo) Encode(writer io.Writer) error {
err := binary.Write(writer, binary.BigEndian, int64(c.ID))
if err != nil {
return err
}
length := uint16(len(c.Name))
if err = binary.Write(writer, binary.BigEndian, length); err == nil {
err = binary.Write(writer, binary.BigEndian, []byte(c.Name))
}
return err
}
type rules struct {
P []rule
B []rule
K []rule
}
type rule struct {
Id1 int32
Id2 int32
}
func (rs *rules) Key() interface{} {
result := 0
if len(rs.P) > 0 {
for i, r := range rs.P {
result += int(r.Id1+r.Id2) * (1 + i + (i * 10))
}
}
if len(rs.B) > 0 {
for i, r := range rs.B {
result += 100000 * int(r.Id2+r.Id1) * (1 + i + (i * 10))
}
}
if len(rs.K) > 0 {
for i, r := range rs.K {
result += 2000000 * int(r.Id2+r.Id1) * (1 + i + (i * 10))
}
}
return result
}
func (r *rules) decode(reader io.Reader, rs *[]rule) error {
size := uint32(0)
err := binary.Read(reader, binary.LittleEndian, &size)
if size == 0 || err != nil {
return err
}
*rs = make([]rule, size)
bytes := unsafe.Slice((*byte)(unsafe.Pointer(&(*rs)[0])), len(*rs)*int(unsafe.Sizeof(&(*rs)[0])))
return binary.Read(reader, binary.LittleEndian, bytes)
}
// Decode decodes data into match rules
func (r *rules) Decode(reader io.Reader) error {
err := r.decode(reader, &r.P)
if err == nil {
if err = r.decode(reader, &r.B); err == nil {
err = r.decode(reader, &r.K)
}
}
return err
}
func (r *rules) encode(rs []rule, writer io.Writer) error {
err := binary.Write(writer, binary.LittleEndian, uint32(len(rs)))
if len(rs) == 0 || err != nil {
return err
}
bytes := unsafe.Slice((*byte)(unsafe.Pointer(&rs[0])), len(rs)*int(unsafe.Sizeof(&rs[0])))
return binary.Write(writer, binary.LittleEndian, &bytes)
}
// Encode encode message rules into writer
func (r *rules) Encode(writer io.Writer) error {
err := r.encode(r.P, writer)
if err == nil {
if err = r.encode(r.B, writer); err == nil {
err = r.encode(r.K, writer)
}
}
return err
}