aboutsummaryrefslogtreecommitdiff
path: root/weed/util/bounded_tree/bounded_tree_test.go
blob: 2328f0497ed9272e311c73c2c8e4bfb0d0728fdf (plain)
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
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()

	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()

	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")))

}