tree_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // tree_tes
  2. package tree
  3. import (
  4. "fmt"
  5. _ "strings"
  6. "testing"
  7. )
  8. type StringTree struct {
  9. Node
  10. Data string
  11. }
  12. func NewStringTree(value string) *StringTree {
  13. res := &StringTree{}
  14. res.Data = value
  15. res.SetParent(nil)
  16. return res
  17. }
  18. func InitStringTree(args ...interface{}) Noder {
  19. str := args[0].(string)
  20. return NewStringTree(str)
  21. }
  22. func TestSibling(test *testing.T) {
  23. n1 := NewStringTree("s1")
  24. n2 := NewStringTree("s2")
  25. n3 := NewStringTree("s3")
  26. AppendSibling(n1, n2)
  27. AppendSibling(n1, n3)
  28. NewSibling(n1, InitStringTree, "s4")
  29. Display(n1)
  30. }
  31. func TestNode(test *testing.T) {
  32. tree := NewStringTree("root")
  33. s1 := "l1 c1"
  34. s2 := "l1 c2"
  35. s3 := "l1 c3"
  36. s4 := "l2 c1"
  37. l1c1 := NewChild(tree, InitStringTree, s1)
  38. if tree.Child() != l1c1 {
  39. test.Errorf("Child() %v: %v<->%v", tree, tree.Child(), l1c1)
  40. }
  41. if LastChild(tree) != l1c1 {
  42. test.Errorf("LastChild() %v: %v<->%v", tree, tree.Child(), l1c1)
  43. }
  44. l1c2 := NewChild(tree, InitStringTree, s2)
  45. if tree.Child() != l1c1 {
  46. test.Errorf("Child() %v: %v<->%v", tree, tree.Child(), l1c1)
  47. }
  48. if LastChild(tree) != l1c2 {
  49. test.Errorf("LastChild() %v: %v<->%v", tree, tree.Child(), l1c2)
  50. }
  51. l1c3 := NewChild(tree, InitStringTree, s3)
  52. if LastChild(tree) != l1c3 {
  53. test.Errorf("LastChild() %v: %v<->%v", tree, tree.Child(), l1c3)
  54. }
  55. l2c1 := NewChild(l1c2, InitStringTree, s4)
  56. if l1c2.Child() != l2c1 {
  57. test.Errorf("Child() %v: %v<->%v", l1c2, l1c2.Child(), l2c1)
  58. }
  59. if LastChild(l1c2) != l2c1 {
  60. test.Errorf("LastChild() %v: %v<->%v", l1c2, l1c2.Child(), l2c1)
  61. }
  62. if l1c1.(*StringTree).Data != s1 {
  63. test.Error("Data ")
  64. }
  65. if l1c2.(*StringTree).Data != s2 {
  66. test.Error("Data ")
  67. }
  68. if l1c3.(*StringTree).Data != s3 {
  69. test.Error("Data ")
  70. }
  71. if l2c1.(*StringTree).Data != s4 {
  72. test.Error("Data ")
  73. }
  74. Display(tree)
  75. if tree.Child() != l1c1 {
  76. test.Errorf("Child() %v: %v<->%v", tree, tree.Child(), l1c1)
  77. }
  78. if l1c1.After() != l1c2 {
  79. test.Errorf("After() %v<->%v", tree.After(), l1c2)
  80. }
  81. if l1c2.After() != l1c3 {
  82. test.Error("After()")
  83. }
  84. if l1c2.Child() != l2c1 {
  85. test.Error("Child()")
  86. }
  87. n := Walk(tree, func(node Noder) Noder {
  88. fmt.Println("%v", node)
  89. if node.(*StringTree).Data == s4 {
  90. return node
  91. }
  92. return nil
  93. })
  94. if n.(*StringTree).Data != s4 {
  95. test.Error("Data ")
  96. }
  97. test.Logf("%v", n.(*StringTree).Data)
  98. test.Log("Hi tree!")
  99. }
  100. type Tn struct {
  101. Node
  102. data string
  103. }
  104. func TestNoder(test *testing.T) {
  105. test.Log("Hi treenoder!")
  106. }
  107. func TestDelete(test *testing.T) {
  108. tree := NewStringTree("root")
  109. s1 := "l1 c1"
  110. s2 := "l1 c2"
  111. s3 := "l1 c3"
  112. s4 := "l2 c1"
  113. _ = NewChild(tree, InitStringTree, s1)
  114. l1c2 := NewChild(tree, InitStringTree, s2)
  115. _ = NewChild(tree, InitStringTree, s3)
  116. _ = NewChild(l1c2, InitStringTree, s4)
  117. Remove(l1c2)
  118. Display(tree)
  119. n := Walk(tree, func(node Noder) Noder {
  120. fmt.Println("%v", node)
  121. if node.(*StringTree).Data == l1c2.(*StringTree).Data {
  122. return node
  123. }
  124. return nil
  125. })
  126. if n != nil {
  127. test.Errorf("Not deleted: %v", n)
  128. }
  129. }