dynamic_tree_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package tree
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. const result = `(0.00 KB) /
  8. ├──(0.00 KB) i1
  9. │ ├──(0.00 KB) i1s1
  10. │ │ ├──(0.00 KB) i1s1b1
  11. │ │ ├──(0.00 KB) i1s1b2
  12. │ │ └──(0.00 KB) i1s1b3
  13. │ ├──(0.00 KB) i1s2
  14. │ │ ├──(0.00 KB) i1s2b1
  15. │ │ └──(0.00 KB) i1s2b2
  16. │ ├──(0.00 KB) i1s3
  17. │ │ ├──(0.00 KB) i1s3s1
  18. │ │ │ ├──(0.00 KB) i1s3s1b1
  19. │ │ │ ├──(0.00 KB) i1s3s1b2
  20. │ │ │ └──(0.00 KB) i1s3s1b3
  21. │ │ └──(0.00 KB) i1s3b1
  22. │ └──(0.00 KB) i1s4
  23. │ └──(0.00 KB) i1s4b1
  24. ├──(0.00 KB) i2
  25. │ ├──(0.00 KB) i2b1
  26. │ └──(0.00 KB) i2b2
  27. └──(0.00 KB) i3
  28. ├──(0.00 KB) i3s1
  29. │ └──(0.00 KB) i3s1b1
  30. ├──(0.00 KB) i3s2
  31. │ ├──(0.00 KB) i3s2b1
  32. │ └──(0.00 KB) i3s2b2
  33. └──(0.00 KB) i3s3
  34. └──(0.00 KB) i3s3b1
  35. `
  36. func createTree() *Top {
  37. t := NewTop("/")
  38. t.SetUnit("KB")
  39. i1 := t.newIntermediate("i1")
  40. i2 := t.newIntermediate("i2")
  41. i3 := t.newIntermediate("i3")
  42. i1s1 := i1.newIntermediate("i1s1")
  43. i1s2 := i1.newIntermediate("i1s2")
  44. i1s3 := i1.newIntermediate("i1s3")
  45. i1s4 := i1.newIntermediate("i1s4")
  46. i3s1 := i3.newIntermediate("i3s1")
  47. i3s2 := i3.newIntermediate("i3s2")
  48. i3s3 := i3.newIntermediate("i3s3")
  49. _ = i1s1.newBottom("i1s1b1")
  50. _ = i1s1.newBottom("i1s1b2")
  51. _ = i1s1.newBottom("i1s1b3")
  52. _ = i1s2.newBottom("i1s2b1")
  53. _ = i1s2.newBottom("i1s2b2")
  54. i1s3s1 := i1s3.newIntermediate("i1s3s1")
  55. _ = i1s3.newBottom("i1s3b1")
  56. _ = i1s4.newBottom("i1s4b1")
  57. _ = i2.newBottom("i2b1")
  58. _ = i2.newBottom("i2b2")
  59. _ = i3s1.newBottom("i3s1b1")
  60. _ = i3s2.newBottom("i3s2b1")
  61. _ = i3s2.newBottom("i3s2b2")
  62. _ = i3s3.newBottom("i3s3b1")
  63. _ = i1s3s1.newBottom("i1s3s1b1")
  64. _ = i1s3s1.newBottom("i1s3s1b2")
  65. _ = i1s3s1.newBottom("i1s3s1b3")
  66. return t
  67. }
  68. func TestString(t *testing.T) {
  69. tree := createTree()
  70. repr := fmt.Sprintf("%s", tree)
  71. if repr != result {
  72. t.Errorf("repr:\n%s\n\nresult:\n%s", repr, result)
  73. }
  74. }
  75. func TestSize(t *testing.T) {
  76. root := NewTop(".")
  77. i := NewIntermediate("i", root)
  78. b := NewBottom("b", i)
  79. go func() {
  80. ch := b.Collector()
  81. ch <- 1024
  82. }()
  83. go b.Collect()
  84. go i.Collect()
  85. go root.Collect()
  86. time.Sleep(time.Second)
  87. if bsize := b.Size(); bsize != 1024 {
  88. t.Errorf("b -> wrong size: %d\n", bsize)
  89. }
  90. if isize := i.Size(); isize != 1024 {
  91. t.Errorf("i -> wrong size: %d\n", isize)
  92. }
  93. if tsize := root.Size(); tsize != 1024 {
  94. t.Errorf("t -> wrong size: %d\n", tsize)
  95. }
  96. }