You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.0 KiB

5 years ago
  1. package bounded_tree
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. )
  8. var (
  9. visitFn = func(path util.FullPath) (childDirectories []string, err error) {
  10. fmt.Printf(" visit %v ...\n", path)
  11. switch path {
  12. case "/":
  13. return []string{"a", "g", "h"}, nil
  14. case "/a":
  15. return []string{"b", "f"}, nil
  16. case "/a/b":
  17. return []string{"c", "e"}, nil
  18. case "/a/b/c":
  19. return []string{"d"}, nil
  20. case "/a/b/c/d":
  21. return []string{"i", "j"}, nil
  22. case "/a/b/c/d/i":
  23. return []string{}, nil
  24. case "/a/b/c/d/j":
  25. return []string{}, nil
  26. case "/a/b/e":
  27. return []string{}, nil
  28. case "/a/f":
  29. return []string{}, nil
  30. }
  31. return nil, nil
  32. }
  33. printMap = func(m map[string]*Node) {
  34. for k := range m {
  35. println(" >", k)
  36. }
  37. }
  38. )
  39. func TestBoundedTree(t *testing.T) {
  40. // a/b/c/d/i
  41. // a/b/c/d/j
  42. // a/b/c/d
  43. // a/b/e
  44. // a/f
  45. // g
  46. // h
  47. tree := NewBoundedTree(util.FullPath("/"))
  48. tree.EnsureVisited(util.FullPath("/a/b/c"), visitFn)
  49. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
  50. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b/c")))
  51. assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/c/d")))
  52. assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e")))
  53. assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/f")))
  54. assert.Equal(t, false, tree.HasVisited(util.FullPath("/g")))
  55. assert.Equal(t, false, tree.HasVisited(util.FullPath("/h")))
  56. assert.Equal(t, true, tree.HasVisited(util.FullPath("/")))
  57. assert.Equal(t, true, tree.HasVisited(util.FullPath("/x")))
  58. assert.Equal(t, false, tree.HasVisited(util.FullPath("/a/b/e/x")))
  59. printMap(tree.root.Children)
  60. a := tree.root.getChild("a")
  61. b := a.getChild("b")
  62. if !b.isVisited() {
  63. t.Errorf("expect visited /a/b")
  64. }
  65. c := b.getChild("c")
  66. if !c.isVisited() {
  67. t.Errorf("expect visited /a/b/c")
  68. }
  69. d := c.getChild("d")
  70. if d.isVisited() {
  71. t.Errorf("expect unvisited /a/b/c/d")
  72. }
  73. tree.EnsureVisited(util.FullPath("/a/b/c/d"), visitFn)
  74. tree.EnsureVisited(util.FullPath("/a/b/c/d/i"), visitFn)
  75. tree.EnsureVisited(util.FullPath("/a/b/c/d/j"), visitFn)
  76. tree.EnsureVisited(util.FullPath("/a/b/e"), visitFn)
  77. tree.EnsureVisited(util.FullPath("/a/f"), visitFn)
  78. printMap(tree.root.Children)
  79. }
  80. func TestEmptyBoundedTree(t *testing.T) {
  81. // g
  82. // h
  83. tree := NewBoundedTree(util.FullPath("/"))
  84. visitFn := func(path util.FullPath) (childDirectories []string, err error) {
  85. fmt.Printf(" visit %v ...\n", path)
  86. switch path {
  87. case "/":
  88. return []string{"g", "h"}, nil
  89. }
  90. t.Fatalf("expected visit %s", path)
  91. return nil, nil
  92. }
  93. tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
  94. tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
  95. printMap(tree.root.Children)
  96. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
  97. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a")))
  98. assert.Equal(t, false, tree.HasVisited(util.FullPath("/g")))
  99. assert.Equal(t, false, tree.HasVisited(util.FullPath("/g/x")))
  100. }