-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathencode_test.go
233 lines (192 loc) · 3.93 KB
/
encode_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
package hnsw
import (
"bytes"
"cmp"
"testing"
"github.com/stretchr/testify/require"
)
func Test_binaryVarint(t *testing.T) {
buf := bytes.NewBuffer(nil)
i := 1337
n, err := binaryWrite(buf, i)
require.NoError(t, err)
require.Equal(t, 2, n)
// Ensure that binaryRead doesn't read past the
// varint.
buf.Write([]byte{0, 0, 0, 0})
var j int
_, err = binaryRead(buf, &j)
require.NoError(t, err)
require.Equal(t, 1337, j)
require.Equal(
t,
[]byte{0, 0, 0, 0},
buf.Bytes(),
)
}
func Test_binaryWrite_string(t *testing.T) {
buf := bytes.NewBuffer(nil)
s := "hello"
n, err := binaryWrite(buf, s)
require.NoError(t, err)
// 5 bytes for the string, 1 byte for the length.
require.Equal(t, 5+1, n)
var s2 string
_, err = binaryRead(buf, &s2)
require.NoError(t, err)
require.Equal(t, "hello", s2)
require.Empty(t, buf.Bytes())
}
func verifyGraphNodes[K cmp.Ordered](t *testing.T, g *Graph[K]) {
for _, layer := range g.layers {
for _, node := range layer.nodes {
for neighborKey, neighbor := range node.neighbors {
_, ok := layer.nodes[neighbor.Key]
if !ok {
t.Errorf(
"node %v has neighbor %v, but neighbor does not exist",
node.Key, neighbor.Key,
)
}
if neighborKey != neighbor.Key {
t.Errorf("node %v has neighbor %v, but neighbor key is %v", node.Key,
neighbor.Key,
neighborKey,
)
}
}
}
}
}
// requireGraphApproxEquals checks that two graphs are equal.
func requireGraphApproxEquals[K cmp.Ordered](t *testing.T, g1, g2 *Graph[K]) {
require.Equal(t, g1.Len(), g2.Len())
a1 := Analyzer[K]{g1}
a2 := Analyzer[K]{g2}
require.Equal(
t,
a1.Topography(),
a2.Topography(),
)
require.Equal(
t,
a1.Connectivity(),
a2.Connectivity(),
)
require.NotNil(t, g1.Distance)
require.NotNil(t, g2.Distance)
require.Equal(
t,
g1.Distance([]float32{0.5}, []float32{1}),
g2.Distance([]float32{0.5}, []float32{1}),
)
require.Equal(t,
g1.M,
g2.M,
)
require.Equal(t,
g1.Ml,
g2.Ml,
)
require.Equal(t,
g1.EfSearch,
g2.EfSearch,
)
require.NotNil(t, g1.Rng)
require.NotNil(t, g2.Rng)
}
func TestGraph_ExportImport(t *testing.T) {
g1 := newTestGraph[int]()
for i := 0; i < 128; i++ {
g1.Add(
Node[int]{
i, randFloats(1),
},
)
}
buf := &bytes.Buffer{}
err := g1.Export(buf)
require.NoError(t, err)
// Don't use newTestGraph to ensure parameters
// are imported.
g2 := &Graph[int]{}
err = g2.Import(buf)
require.NoError(t, err)
requireGraphApproxEquals(t, g1, g2)
n1 := g1.Search(
[]float32{0.5},
10,
)
n2 := g2.Search(
[]float32{0.5},
10,
)
require.Equal(t, n1, n2)
verifyGraphNodes(t, g1)
verifyGraphNodes(t, g2)
}
func TestSavedGraph(t *testing.T) {
dir := t.TempDir()
g1, err := LoadSavedGraph[int](dir + "/graph")
require.NoError(t, err)
require.Equal(t, 0, g1.Len())
for i := 0; i < 128; i++ {
g1.Add(
Node[int]{
i, randFloats(1),
},
)
}
err = g1.Save()
require.NoError(t, err)
g2, err := LoadSavedGraph[int](dir + "/graph")
require.NoError(t, err)
requireGraphApproxEquals(t, g1.Graph, g2.Graph)
}
const benchGraphSize = 100
func BenchmarkGraph_Import(b *testing.B) {
b.ReportAllocs()
g := newTestGraph[int]()
for i := 0; i < benchGraphSize; i++ {
g.Add(
Node[int]{
i, randFloats(256),
},
)
}
buf := &bytes.Buffer{}
err := g.Export(buf)
require.NoError(b, err)
b.ResetTimer()
b.SetBytes(int64(buf.Len()))
for i := 0; i < b.N; i++ {
b.StopTimer()
rdr := bytes.NewReader(buf.Bytes())
g := newTestGraph[int]()
b.StartTimer()
err = g.Import(rdr)
require.NoError(b, err)
}
}
func BenchmarkGraph_Export(b *testing.B) {
b.ReportAllocs()
g := newTestGraph[int]()
for i := 0; i < benchGraphSize; i++ {
g.Add(
Node[int]{
i, randFloats(256),
},
)
}
var buf bytes.Buffer
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := g.Export(&buf)
require.NoError(b, err)
if i == 0 {
ln := buf.Len()
b.SetBytes(int64(ln))
}
buf.Reset()
}
}