bar_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package door
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func pctUpdate(pct *int64) func() int64 {
  7. return func() int64 {
  8. return *pct
  9. }
  10. }
  11. func TestBarsMatch(t *testing.T) {
  12. // verify that the BARS_CP437 matches BARS_UNICODE
  13. // When translated via CP437_to_Unicode().
  14. cp437 := BARS_CP437.Solid
  15. convert := CP437_to_Unicode(cp437)
  16. unicode := BARS_UNICODE.Solid
  17. if convert != unicode {
  18. t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
  19. }
  20. for half := 0; half < 2; half++ {
  21. cp437 := BARS_CP437.Half[half]
  22. convert := CP437_to_Unicode(cp437)
  23. unicode := BARS_UNICODE.Half[half]
  24. if convert != unicode {
  25. t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
  26. }
  27. }
  28. for grad := 0; grad < 4; grad++ {
  29. cp437 := BARS_CP437.Gradient[grad]
  30. convert := CP437_to_Unicode(cp437)
  31. unicode := BARS_UNICODE.Gradient[grad]
  32. if convert != unicode {
  33. t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
  34. }
  35. }
  36. }
  37. func TestBarsUnicode(t *testing.T) {
  38. // Init as if we're in Unicode mode
  39. Unicode = true
  40. CP437 = false
  41. BARS = &BARS_UNICODE
  42. testBars(t)
  43. }
  44. func TestBarsCP437(t *testing.T) {
  45. // Init as if we're in CP437 mode
  46. Unicode = false
  47. CP437 = true
  48. BARS = &BARS_CP437
  49. testBars(t)
  50. }
  51. func testBars(t *testing.T) {
  52. // Generic function that tests output by building results
  53. // from BARS structure. (Can test Unicode and CP437.)
  54. BarColor := ColorText("BLUE")
  55. Bar25 := ColorText("RED")
  56. Bar50 := ColorText("BROWN")
  57. Bar75 := ColorText("BOLD YEL")
  58. Bar95 := ColorText("GREEN")
  59. Bar100 := ColorText("BRI GREEN")
  60. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  61. bar.ColorRange = []BarRange{
  62. {2500, Bar25},
  63. {5000, Bar50},
  64. {7500, Bar75},
  65. {9500, Bar95},
  66. {10100, Bar100}}
  67. BarSolid := map[int]string{0: string(Bar25) + " ",
  68. 5: string(Bar25) + " ",
  69. 10: string(Bar25) + BARS.Solid + " ",
  70. 20: string(Bar25) + strings.Repeat(BARS.Solid, 2) + " ",
  71. 25: string(Bar25) + strings.Repeat(BARS.Solid, 2) + " ",
  72. 30: string(Bar50) + strings.Repeat(BARS.Solid, 3) + " ",
  73. 50: string(Bar50) + strings.Repeat(BARS.Solid, 5) + " ",
  74. 75: string(Bar75) + strings.Repeat(BARS.Solid, 7) + " ",
  75. 90: string(Bar95) + strings.Repeat(BARS.Solid, 9) + " ",
  76. 95: string(Bar95) + strings.Repeat(BARS.Solid, 9) + " ",
  77. 100: string(Bar100) + strings.Repeat(BARS.Solid, 10) + "",
  78. }
  79. for pct, text := range BarSolid {
  80. bar.Percent = int64(pct * 100)
  81. got := string(bar.Output())
  82. if got != text {
  83. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  84. }
  85. }
  86. BarColor = ColorText("BLA ON WHI")
  87. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  88. BarHalf := map[int]string{0: string(BarColor) + " 0% ",
  89. 5: string(BarColor) + BARS.Half[1] + " 5% ",
  90. 6: string(BarColor) + BARS.Half[1] + " 6% ",
  91. 10: string(BarColor) + BARS.Half[0] + " 10% ",
  92. 20: string(BarColor) + BARS.Half[0] + BARS.Half[0] + " 20% ",
  93. 25: string(BarColor) + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 25% ",
  94. 26: string(BarColor) + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 26% ",
  95. 50: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 50% ",
  96. 75: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 75% ",
  97. 90: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 90% " + BARS.Half[0] + " ",
  98. 100: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 100 " + strings.Repeat(BARS.Half[0], 2),
  99. }
  100. for pct, text := range BarHalf {
  101. bar.Percent = int64(pct * 100)
  102. got := string(bar.Output())
  103. if got != text {
  104. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  105. }
  106. }
  107. BarColor = ColorText("RED")
  108. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  109. BarGrad := map[int]string{0: string(BarColor) + " ",
  110. 3: string(BarColor) + BARS.Gradient[1] + " ",
  111. 5: string(BarColor) + BARS.Gradient[2] + " ",
  112. 8: string(BarColor) + BARS.Gradient[3] + " ",
  113. 10: string(BarColor) + BARS.Gradient[0] + " ",
  114. 20: string(BarColor) + BARS.Gradient[0] + BARS.Gradient[0] + " ",
  115. 25: string(BarColor) + BARS.Gradient[0] + BARS.Gradient[0] + BARS.Gradient[2] + " ",
  116. 50: string(BarColor) + strings.Repeat(BARS.Gradient[0], 5) + " ",
  117. 75: string(BarColor) + strings.Repeat(BARS.Gradient[0], 7) + BARS.Gradient[2] + " ",
  118. 100: string(BarColor) + strings.Repeat(BARS.Gradient[0], 10),
  119. }
  120. var percent int64
  121. bar.UpdateP = pctUpdate(&percent)
  122. for pct, text := range BarGrad {
  123. percent = int64(pct * 100)
  124. got := string(bar.Output())
  125. if got != text {
  126. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  127. }
  128. }
  129. }
  130. func BenchmarkBar(b *testing.B) {
  131. Unicode = false
  132. BarColor := ColorText("BLUE")
  133. Bar25 := ColorText("RED")
  134. Bar50 := ColorText("BROWN")
  135. Bar75 := ColorText("BOLD YEL")
  136. Bar95 := ColorText("GREEN")
  137. Bar100 := ColorText("BRI GREEN")
  138. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  139. bar.ColorRange = []BarRange{
  140. {2500, Bar25},
  141. {5000, Bar50},
  142. {7500, Bar75},
  143. {9500, Bar95},
  144. {10100, Bar100}}
  145. BarColor2 := ColorText("BLA ON WHI")
  146. bar2 := BarLine{Line: Line{DefaultColor: BarColor2}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  147. BarColor3 := ColorText("RED")
  148. bar3 := BarLine{Line: Line{DefaultColor: BarColor3},
  149. Width: 10, Style: GRADIENT}
  150. var percent int64
  151. var upFunc = func() int64 {
  152. return percent
  153. }
  154. bar.UpdateP = upFunc
  155. bar2.UpdateP = upFunc
  156. bar3.UpdateP = upFunc
  157. for n := 0; n < b.N; n++ {
  158. percent = (int64(n) * 100) / int64(b.N)
  159. bar.Output()
  160. bar2.Output()
  161. bar3.Output()
  162. }
  163. }