This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
node_test.go
75 lines (69 loc) · 1.46 KB
/
node_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
package gann
import (
"fmt"
"sync"
"testing"
"github.com/bmizerany/assert"
"github.com/mathetake/gann/metric"
)
func TestNodeBuild(t *testing.T) {
for i, c := range []struct {
vec []float64
items []*item
dim, k int
expLeaf bool
}{
{
vec: []float64{0.0, 1.0},
items: []*item{
{id: 0, vector: []float64{0.0, 1.0}},
{id: 1, vector: []float64{0.0, -1.0}},
},
k: 2,
dim: 2,
expLeaf: true,
},
{
vec: []float64{0.0, 1.0},
items: []*item{
{id: 0, vector: []float64{0.0, 1.0}},
{id: 0, vector: []float64{0.0, 1.1}},
{id: 0, vector: []float64{0.0, 1.2}},
{id: 1, vector: []float64{0.0, -1.0}},
{id: 2, vector: []float64{0.0, -1.1}},
{id: 2, vector: []float64{0.0, -1.2}},
},
k: 2,
dim: 2,
expLeaf: false,
},
} {
c := c
i := i
t.Run(fmt.Sprintf("%d-th case", i), func(t *testing.T) {
m, err := metric.NewCosineMetric(c.dim)
if err != nil {
t.Fatal(err)
}
idxPtr := &index{
k: 1,
mux: &sync.Mutex{},
metric: m,
nodeIDToNode: map[nodeId]*node{},
}
n := &node{
id: nodeId(fmt.Sprintf("%d", i)),
vec: c.vec,
idxPtr: idxPtr,
children: make(map[direction]*node, len(directions)),
}
n.build(c.items)
if c.expLeaf {
assert.Equal(t, true, len(n.leaf) > 0)
} else {
assert.Equal(t, true, len(n.leaf) == 0)
assert.Equal(t, true, len(n.children) > 0)
}
})
}
}