aboutsummaryrefslogtreecommitdiff
path: root/weed/util/bounded_tree/bounded_tree_test.go
diff options
context:
space:
mode:
authorbingoohuang <bingoo.huang@gmail.com>2021-04-26 17:19:35 +0800
committerbingoohuang <bingoo.huang@gmail.com>2021-04-26 17:19:35 +0800
commitd861cbd81b75b6684c971ac00e33685e6575b833 (patch)
tree301805fef4aa5d0096bfb1510536f7a009b661e7 /weed/util/bounded_tree/bounded_tree_test.go
parent70da715d8d917527291b35fb069fac077d17b868 (diff)
parent4ee58922eff61a5a4ca29c0b4829b097a498549e (diff)
downloadseaweedfs-d861cbd81b75b6684c971ac00e33685e6575b833.tar.xz
seaweedfs-d861cbd81b75b6684c971ac00e33685e6575b833.zip
Merge branch 'master' of https://github.com/bingoohuang/seaweedfs
Diffstat (limited to 'weed/util/bounded_tree/bounded_tree_test.go')
-rw-r--r--weed/util/bounded_tree/bounded_tree_test.go126
1 files changed, 126 insertions, 0 deletions
diff --git a/weed/util/bounded_tree/bounded_tree_test.go b/weed/util/bounded_tree/bounded_tree_test.go
new file mode 100644
index 000000000..465f1cc9c
--- /dev/null
+++ b/weed/util/bounded_tree/bounded_tree_test.go
@@ -0,0 +1,126 @@
+package bounded_tree
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "github.com/chrislusf/seaweedfs/weed/util"
+)
+
+var (
+ visitFn = func(path util.FullPath) (childDirectories []string, err error) {
+ fmt.Printf(" visit %v ...\n", path)
+ switch path {
+ case "/":
+ return []string{"a", "g", "h"}, nil
+ case "/a":
+ return []string{"b", "f"}, nil
+ case "/a/b":
+ return []string{"c", "e"}, nil
+ case "/a/b/c":
+ return []string{"d"}, nil
+ case "/a/b/c/d":
+ return []string{"i", "j"}, nil
+ case "/a/b/c/d/i":
+ return []string{}, nil
+ case "/a/b/c/d/j":
+ return []string{}, nil
+ case "/a/b/e":
+ return []string{}, nil
+ case "/a/f":
+ return []string{}, nil
+ }
+ return nil, nil
+ }
+
+ printMap = func(m map[string]*Node) {
+ for k := range m {
+ println(" >", k)
+ }
+ }
+)
+
+func TestBoundedTree(t *testing.T) {
+
+ // a/b/c/d/i
+ // a/b/c/d/j
+ // a/b/c/d
+ // a/b/e
+ // a/f
+ // g
+ // h
+
+ tree := NewBoundedTree(util.FullPath("/"))
+
+ tree.EnsureVisited(util.FullPath("/a/b/c"), visitFn)
+
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b/c")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/c/d")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/f")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/g")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/h")))
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/")))
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/x")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e/x")))
+
+ printMap(tree.root.Children)
+
+ a := tree.root.getChild("a")
+
+ b := a.getChild("b")
+ if !b.isVisited() {
+ t.Errorf("expect visited /a/b")
+ }
+ c := b.getChild("c")
+ if !c.isVisited() {
+ t.Errorf("expect visited /a/b/c")
+ }
+
+ d := c.getChild("d")
+ if d.isVisited() {
+ t.Errorf("expect unvisited /a/b/c/d")
+ }
+
+ tree.EnsureVisited(util.FullPath("/a/b/c/d"), visitFn)
+ tree.EnsureVisited(util.FullPath("/a/b/c/d/i"), visitFn)
+ tree.EnsureVisited(util.FullPath("/a/b/c/d/j"), visitFn)
+ tree.EnsureVisited(util.FullPath("/a/b/e"), visitFn)
+ tree.EnsureVisited(util.FullPath("/a/f"), visitFn)
+
+ printMap(tree.root.Children)
+
+}
+
+func TestEmptyBoundedTree(t *testing.T) {
+
+ // g
+ // h
+
+ tree := NewBoundedTree(util.FullPath("/"))
+
+ visitFn := func(path util.FullPath) (childDirectories []string, err error) {
+ fmt.Printf(" visit %v ...\n", path)
+ switch path {
+ case "/":
+ return []string{"g", "h"}, nil
+ }
+ t.Fatalf("expected visit %s", path)
+ return nil, nil
+ }
+
+ tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
+
+ tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
+
+ printMap(tree.root.Children)
+
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
+ assert.Equal(t, true, tree.HasVisited(util.FullPath("/a")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/g")))
+ assert.Equal(t, false, tree.HasVisited(util.FullPath("/g/x")))
+
+}