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.

122 lines
2.4 KiB

  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()
  48. tree.EnsureVisited(util.FullPath("/a/b/c"), visitFn)
  49. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
  50. printMap(tree.root.Children)
  51. a := tree.root.getChild("a")
  52. b := a.getChild("b")
  53. if !b.isVisited() {
  54. t.Errorf("expect visited /a/b")
  55. }
  56. c := b.getChild("c")
  57. if !c.isVisited() {
  58. t.Errorf("expect visited /a/b/c")
  59. }
  60. d := c.getChild("d")
  61. if d.isVisited() {
  62. t.Errorf("expect unvisited /a/b/c/d")
  63. }
  64. tree.EnsureVisited(util.FullPath("/a/b/c/d"), visitFn)
  65. tree.EnsureVisited(util.FullPath("/a/b/c/d/i"), visitFn)
  66. tree.EnsureVisited(util.FullPath("/a/b/c/d/j"), visitFn)
  67. tree.EnsureVisited(util.FullPath("/a/b/e"), visitFn)
  68. tree.EnsureVisited(util.FullPath("/a/f"), visitFn)
  69. printMap(tree.root.Children)
  70. }
  71. func TestEmptyBoundedTree(t *testing.T) {
  72. // g
  73. // h
  74. tree := NewBoundedTree()
  75. visitFn := func(path util.FullPath) (childDirectories []string, err error) {
  76. fmt.Printf(" visit %v ...\n", path)
  77. switch path {
  78. case "/":
  79. return []string{"g", "h"}, nil
  80. }
  81. t.Fatalf("expected visit %s", path)
  82. return nil, nil
  83. }
  84. tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
  85. tree.EnsureVisited(util.FullPath("/a/b"), visitFn)
  86. printMap(tree.root.Children)
  87. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a/b")))
  88. assert.Equal(t, true, tree.HasVisited(util.FullPath("/a")))
  89. assert.Equal(t, false, tree.HasVisited(util.FullPath("/g")))
  90. assert.Equal(t, false, tree.HasVisited(util.FullPath("/g/x")))
  91. }