util_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package gago
  2. import (
  3. "testing"
  4. )
  5. func TestMin(t *testing.T) {
  6. var testCases = [][]int{
  7. []int{1, 2},
  8. []int{2, 2},
  9. []int{2, 3},
  10. }
  11. for _, test := range testCases {
  12. if min(test[0], test[1]) != test[0] {
  13. t.Error("min didn't find the smallest integer")
  14. }
  15. }
  16. }
  17. func TestSumFloat64s(t *testing.T) {
  18. var testCases = []struct {
  19. floats []float64
  20. total float64
  21. }{
  22. {[]float64{1, 2, 3}, 6},
  23. {[]float64{-1, 1}, 0},
  24. {[]float64{1.42, 42.1}, 43.52},
  25. }
  26. for _, test := range testCases {
  27. if sumFloat64s(test.floats) != test.total {
  28. t.Error("sumFloat64s didn't work as expected")
  29. }
  30. }
  31. }
  32. func TestMinFloat64s(t *testing.T) {
  33. var testCases = []struct {
  34. floats []float64
  35. min float64
  36. }{
  37. {[]float64{1.0}, 1.0},
  38. {[]float64{1.0, 2.0}, 1.0},
  39. {[]float64{-1.0, 1.0}, -1.0},
  40. }
  41. for _, test := range testCases {
  42. if minFloat64s(test.floats) != test.min {
  43. t.Error("meanFloat64s didn't work as expected")
  44. }
  45. }
  46. }
  47. func TestMaxFloat64s(t *testing.T) {
  48. var testCases = []struct {
  49. floats []float64
  50. max float64
  51. }{
  52. {[]float64{1.0}, 1.0},
  53. {[]float64{1.0, 2.0}, 2.0},
  54. {[]float64{-1.0, 1.0}, 1.0},
  55. }
  56. for _, test := range testCases {
  57. if maxFloat64s(test.floats) != test.max {
  58. t.Error("maxFloat64s didn't work as expected")
  59. }
  60. }
  61. }
  62. func TestMeanFloat64s(t *testing.T) {
  63. var testCases = []struct {
  64. floats []float64
  65. mean float64
  66. }{
  67. {[]float64{1.0}, 1.0},
  68. {[]float64{1.0, 2.0}, 1.5},
  69. {[]float64{-1.0, 1.0}, 0.0},
  70. }
  71. for _, test := range testCases {
  72. if meanFloat64s(test.floats) != test.mean {
  73. t.Error("meanFloat64s didn't work as expected")
  74. }
  75. }
  76. }
  77. func TestVarianceFloat64s(t *testing.T) {
  78. var testCases = []struct {
  79. floats []float64
  80. variance float64
  81. }{
  82. {[]float64{1.0}, 0.0},
  83. {[]float64{-1.0, 1.0}, 1.0},
  84. {[]float64{-2.0, 2.0}, 4.0},
  85. }
  86. for _, test := range testCases {
  87. if varianceFloat64s(test.floats) != test.variance {
  88. t.Error("varianceFloat64s didn't work as expected")
  89. }
  90. }
  91. }
  92. func TestDivide(t *testing.T) {
  93. var testCases = []struct {
  94. floats []float64
  95. value float64
  96. divided []float64
  97. }{
  98. {[]float64{1, 1}, 1, []float64{1, 1}},
  99. {[]float64{1, 1}, 2, []float64{0.5, 0.5}},
  100. {[]float64{42, -42}, 21, []float64{2, -2}},
  101. }
  102. for _, test := range testCases {
  103. var divided = divide(test.floats, test.value)
  104. for i := range divided {
  105. if divided[i] != test.divided[i] {
  106. t.Error("divided didn't work as expected")
  107. }
  108. }
  109. }
  110. }
  111. func TestCumsum(t *testing.T) {
  112. var testCases = []struct {
  113. floats []float64
  114. summed []float64
  115. }{
  116. {[]float64{1, 2, 3, 4}, []float64{1, 3, 6, 10}},
  117. {[]float64{-1, 0, 1}, []float64{-1, -1, 0}},
  118. }
  119. for _, test := range testCases {
  120. var summed = cumsum(test.floats)
  121. for i := range summed {
  122. if summed[i] != test.summed[i] {
  123. t.Error("cumsum didn't work as expected")
  124. }
  125. }
  126. }
  127. }
  128. func TestUnion(t *testing.T) {
  129. var testCases = []struct {
  130. x set
  131. y set
  132. u set
  133. }{
  134. {
  135. x: set{1: true, 2: true, 3: true},
  136. y: set{4: true, 5: true, 6: true},
  137. u: set{1: true, 2: true, 3: true, 4: true, 5: true, 6: true},
  138. },
  139. {
  140. x: set{1: true, 2: true, 3: true},
  141. y: set{2: true, 3: true, 4: true},
  142. u: set{1: true, 2: true, 3: true, 4: true},
  143. },
  144. }
  145. for _, test := range testCases {
  146. var u = union(test.x, test.y)
  147. for i := range u {
  148. if !test.u[i] {
  149. t.Error("union didn't work as expected")
  150. }
  151. }
  152. }
  153. }