|
|
@@ -12,18 +12,18 @@ func TestStoreGet(t *testing.T) {
|
|
|
|
|
|
ts := &tree{
|
|
|
&treeNode{
|
|
|
- CreateTestNode("/"),
|
|
|
+ NewTestNode("/"),
|
|
|
true,
|
|
|
make(map[string]*treeNode),
|
|
|
},
|
|
|
}
|
|
|
|
|
|
// create key
|
|
|
- ts.set("/foo", CreateTestNode("bar"))
|
|
|
+ ts.set("/foo", NewTestNode("bar"))
|
|
|
// change value
|
|
|
- ts.set("/foo", CreateTestNode("barbar"))
|
|
|
+ ts.set("/foo", NewTestNode("barbar"))
|
|
|
// create key
|
|
|
- ts.set("/hello/foo", CreateTestNode("barbarbar"))
|
|
|
+ ts.set("/hello/foo", NewTestNode("barbarbar"))
|
|
|
treeNode, ok := ts.get("/foo")
|
|
|
|
|
|
if !ok {
|
|
|
@@ -43,7 +43,7 @@ func TestStoreGet(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
// create a key under other key
|
|
|
- ok = ts.set("/foo/foo", CreateTestNode("bar"))
|
|
|
+ ok = ts.set("/foo/foo", NewTestNode("bar"))
|
|
|
if ok {
|
|
|
t.Fatalf("shoud not add key under a exisiting key")
|
|
|
}
|
|
|
@@ -61,14 +61,15 @@ func TestStoreGet(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
// test list
|
|
|
- ts.set("/hello/fooo", CreateTestNode("barbarbar"))
|
|
|
- ts.set("/hello/foooo/foo", CreateTestNode("barbarbar"))
|
|
|
+ ts.set("/hello/fooo", NewTestNode("barbarbar"))
|
|
|
+ ts.set("/hello/foooo/foo", NewTestNode("barbarbar"))
|
|
|
|
|
|
nodes, keys, dirs, ok := ts.list("/hello")
|
|
|
|
|
|
if !ok {
|
|
|
t.Fatalf("cannot list!")
|
|
|
} else {
|
|
|
+ nodes, _ := nodes.([]*Node)
|
|
|
length := len(nodes)
|
|
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
@@ -80,7 +81,7 @@ func TestStoreGet(t *testing.T) {
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
value := strconv.Itoa(rand.Int())
|
|
|
- ts.set(keys[i], CreateTestNode(value))
|
|
|
+ ts.set(keys[i], NewTestNode(value))
|
|
|
treeNode, ok := ts.get(keys[i])
|
|
|
|
|
|
if !ok {
|
|
|
@@ -94,6 +95,48 @@ func TestStoreGet(t *testing.T) {
|
|
|
ts.traverse(f, true)
|
|
|
}
|
|
|
|
|
|
+func TestTreeClone(t *testing.T) {
|
|
|
+ keys := GenKeys(10000, 10)
|
|
|
+
|
|
|
+ ts := &tree{
|
|
|
+ &treeNode{
|
|
|
+ NewTestNode("/"),
|
|
|
+ true,
|
|
|
+ make(map[string]*treeNode),
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ backTs := &tree{
|
|
|
+ &treeNode{
|
|
|
+ NewTestNode("/"),
|
|
|
+ true,
|
|
|
+ make(map[string]*treeNode),
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ // generate the first tree
|
|
|
+ for _, key := range keys {
|
|
|
+ value := strconv.Itoa(rand.Int())
|
|
|
+ ts.set(key, NewTestNode(value))
|
|
|
+ backTs.set(key, NewTestNode(value))
|
|
|
+ }
|
|
|
+
|
|
|
+ copyTs := ts.clone()
|
|
|
+
|
|
|
+ // test if they are identical
|
|
|
+ copyTs.traverse(ts.contain, false)
|
|
|
+
|
|
|
+ // remove all the keys from first tree
|
|
|
+ for _, key := range keys {
|
|
|
+ ts.delete(key)
|
|
|
+ }
|
|
|
+
|
|
|
+ // test if they are identical
|
|
|
+ // make sure changes in the first tree will affect the copy one
|
|
|
+ copyTs.traverse(backTs.contain, false)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
func BenchmarkTreeStoreSet(b *testing.B) {
|
|
|
|
|
|
keys := GenKeys(10000, 10)
|
|
|
@@ -103,7 +146,7 @@ func BenchmarkTreeStoreSet(b *testing.B) {
|
|
|
|
|
|
ts := &tree{
|
|
|
&treeNode{
|
|
|
- CreateTestNode("/"),
|
|
|
+ NewTestNode("/"),
|
|
|
true,
|
|
|
make(map[string]*treeNode),
|
|
|
},
|
|
|
@@ -111,7 +154,7 @@ func BenchmarkTreeStoreSet(b *testing.B) {
|
|
|
|
|
|
for _, key := range keys {
|
|
|
value := strconv.Itoa(rand.Int())
|
|
|
- ts.set(key, CreateTestNode(value))
|
|
|
+ ts.set(key, NewTestNode(value))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -122,7 +165,7 @@ func BenchmarkTreeStoreGet(b *testing.B) {
|
|
|
|
|
|
ts := &tree{
|
|
|
&treeNode{
|
|
|
- CreateTestNode("/"),
|
|
|
+ NewTestNode("/"),
|
|
|
true,
|
|
|
make(map[string]*treeNode),
|
|
|
},
|
|
|
@@ -130,7 +173,7 @@ func BenchmarkTreeStoreGet(b *testing.B) {
|
|
|
|
|
|
for _, key := range keys {
|
|
|
value := strconv.Itoa(rand.Int())
|
|
|
- ts.set(key, CreateTestNode(value))
|
|
|
+ ts.set(key, NewTestNode(value))
|
|
|
}
|
|
|
|
|
|
b.ResetTimer()
|
|
|
@@ -141,13 +184,45 @@ func BenchmarkTreeStoreGet(b *testing.B) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func BenchmarkTreeStoreCopy(b *testing.B) {
|
|
|
+ keys := GenKeys(10000, 10)
|
|
|
+
|
|
|
+ ts := &tree{
|
|
|
+ &treeNode{
|
|
|
+ NewTestNode("/"),
|
|
|
+ true,
|
|
|
+ make(map[string]*treeNode),
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, key := range keys {
|
|
|
+ value := strconv.Itoa(rand.Int())
|
|
|
+ ts.set(key, NewTestNode(value))
|
|
|
+ }
|
|
|
+
|
|
|
+ b.ResetTimer()
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ ts.clone()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func BenchmarkMakeSlice(b *testing.B) {
|
|
|
+
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ for i:=0; i < 100000; i++ {
|
|
|
+ _ = make([]Node, 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func BenchmarkTreeStoreList(b *testing.B) {
|
|
|
|
|
|
keys := GenKeys(10000, 10)
|
|
|
|
|
|
ts := &tree{
|
|
|
&treeNode{
|
|
|
- CreateTestNode("/"),
|
|
|
+ NewTestNode("/"),
|
|
|
true,
|
|
|
make(map[string]*treeNode),
|
|
|
},
|
|
|
@@ -155,7 +230,7 @@ func BenchmarkTreeStoreList(b *testing.B) {
|
|
|
|
|
|
for _, key := range keys {
|
|
|
value := strconv.Itoa(rand.Int())
|
|
|
- ts.set(key, CreateTestNode(value))
|
|
|
+ ts.set(key, NewTestNode(value))
|
|
|
}
|
|
|
|
|
|
b.ResetTimer()
|
|
|
@@ -166,10 +241,17 @@ func BenchmarkTreeStoreList(b *testing.B) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (t *tree) contain(key string, node *Node) {
|
|
|
+ _, ok := t.get(key)
|
|
|
+ if !ok {
|
|
|
+ panic("tree do not contain the given key")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func f(key string, n *Node) {
|
|
|
- fmt.Println(key, "=", n.Value)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
-func CreateTestNode(value string) Node {
|
|
|
+func NewTestNode(value string) Node {
|
|
|
return Node{value, time.Unix(0, 0), nil}
|
|
|
}
|