-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathns_test.go
147 lines (115 loc) · 2.88 KB
/
ns_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
// Copyright 2018 Ara Israelyan. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package nestedset
import (
"fmt"
"testing"
)
var (
ns *NestedSet
nodes []NodeInterface
)
func createTestNestedSet(t *testing.T) {
ns = NewNestedSet()
nodes = make([]NodeInterface, 0)
nodes = append(nodes, ns.rootNode)
for i := 1; i <= 6; i++ {
n := NewNode()
n.NodeName = fmt.Sprintf("node %d", i)
nodes = append(nodes, n)
}
err := ns.Add(nodes[1], nil)
if err != nil {
t.Fatal(err)
}
err = ns.Add(nodes[2], nodes[1])
if err != nil {
t.Fatal(err)
}
err = ns.Add(nodes[3], nodes[0])
if err != nil {
t.Fatal(err)
}
err = ns.Add(nodes[4], nodes[3])
if err != nil {
t.Fatal(err)
}
err = ns.Add(nodes[5], nodes[1])
if err != nil {
t.Fatal(err)
}
err = ns.Add(nodes[6], nodes[4])
if err != nil {
t.Fatal(err)
}
}
func checkNode(t *testing.T, node NodeInterface, level, left, right int64) {
if node.Level() != level {
t.Errorf("Invalid level for node '%s', expected %d, get %d", node.Name(), right, node.Level())
}
if node.Left() != left {
t.Errorf("Invalid left for node '%s', expected %d, get %d", node.Name(), left, node.Left())
}
if node.Right() != right {
t.Errorf("Invalid right for node '%s', expected %d, get %d", node.Name(), right, node.Right())
}
}
func TestNestedSet_Add(t *testing.T) {
createTestNestedSet(t)
checkNode(t, nodes[0], 0, 0, 13)
checkNode(t, nodes[1], 1, 1, 6)
checkNode(t, nodes[2], 2, 2, 3)
checkNode(t, nodes[3], 1, 7, 12)
checkNode(t, nodes[4], 2, 8, 11)
checkNode(t, nodes[5], 2, 4, 5)
checkNode(t, nodes[6], 3, 9, 10)
}
func TestNestedSet_Delete(t *testing.T) {
createTestNestedSet(t)
err := ns.Delete(nodes[1])
if err != nil {
t.Fatal(err)
}
if ns.exists(nodes[1]) {
t.Fatalf("Error deleting node '%s'", nodes[0].Name())
}
if ns.exists(nodes[2]) {
t.Fatalf("Error deleting node '%s'", nodes[2].Name())
}
if ns.exists(nodes[5]) {
t.Fatalf("Error deleting node '%s'", nodes[3].Name())
}
checkNode(t, nodes[0], 0, 0, 7)
checkNode(t, nodes[3], 1, 1, 6)
checkNode(t, nodes[4], 2, 2, 5)
checkNode(t, nodes[6], 3, 3, 4)
}
func TestNestedSet_Move(t *testing.T) {
createTestNestedSet(t)
ns.Move(nodes[4], nodes[2])
checkNode(t, nodes[0], 0, 0, 13)
checkNode(t, nodes[1], 1, 1, 10)
checkNode(t, nodes[2], 2, 2, 7)
checkNode(t, nodes[3], 1, 11, 12)
checkNode(t, nodes[4], 3, 3, 6)
checkNode(t, nodes[5], 2, 8, 9)
checkNode(t, nodes[6], 4, 4, 5)
}
func TestNestedSet_Branch(t *testing.T) {
branch := ns.Branch(nodes[1])
if branch == nil {
t.Error("Returned nil branch for node 1")
return
}
printBranch(branch)
}
func printBranch(branch []NodeInterface) {
for _, n := range branch {
var i int64
for i = 0; i < n.Level(); i++ {
fmt.Print("..")
}
fmt.Printf("%s lvl:%d, left:%d, right:%d\n", n.Name(), n.Level(), n.Left(), n.Right())
}
}