slice_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package gago
  2. import "testing"
  3. func TestSearch(t *testing.T) {
  4. var s = StringSlice{"イ", "ー", "ス", "タ", "ー"}
  5. if i, err := search("イ", s); i != 0 || err != nil {
  6. t.Error("Problem with search 1")
  7. }
  8. if i, err := search("ー", s); i != 1 || err != nil {
  9. t.Error("Problem with search 2")
  10. }
  11. if i, err := search("ス", s); i != 2 || err != nil {
  12. t.Error("Problem with search 3")
  13. }
  14. if i, err := search("タ", s); i != 3 || err != nil {
  15. t.Error("Problem with search 4")
  16. }
  17. if _, err := search("|", s); err == nil {
  18. t.Error("Problem with search 5")
  19. }
  20. }
  21. func TestNewIndexLookup(t *testing.T) {
  22. var testCases = []struct {
  23. slice Slice
  24. lookup map[interface{}]int
  25. }{
  26. {
  27. slice: IntSlice{1, 2, 3},
  28. lookup: map[interface{}]int{
  29. 1: 0,
  30. 2: 1,
  31. 3: 2,
  32. },
  33. },
  34. }
  35. for _, test := range testCases {
  36. var lookup = newIndexLookup(test.slice)
  37. for k, v := range lookup {
  38. if v != test.lookup[k] {
  39. t.Error("createLookup didn't work as expected")
  40. }
  41. }
  42. }
  43. }
  44. func TestGetCycles(t *testing.T) {
  45. var testCases = []struct {
  46. x []int
  47. y []int
  48. cycles [][]int
  49. }{
  50. {
  51. x: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
  52. y: []int{9, 3, 7, 8, 2, 6, 5, 1, 4},
  53. cycles: [][]int{
  54. []int{0, 8, 3, 7},
  55. []int{1, 2, 6, 4},
  56. []int{5},
  57. },
  58. },
  59. }
  60. for _, test := range testCases {
  61. var cycles = getCycles(IntSlice(test.x), IntSlice(test.y))
  62. for i, cycle := range cycles {
  63. for j, c := range cycle {
  64. if c != test.cycles[i][j] {
  65. t.Error("getCycles didn't work as expected")
  66. }
  67. }
  68. }
  69. }
  70. }
  71. func TestGetNeighbours(t *testing.T) {
  72. var testCases = []struct {
  73. x Slice
  74. neighbours map[interface{}]set
  75. }{
  76. {
  77. x: IntSlice{1, 2, 3, 4, 5, 6, 7, 8, 9},
  78. neighbours: map[interface{}]set{
  79. 1: set{9: true, 2: true},
  80. 2: set{1: true, 3: true},
  81. 3: set{2: true, 4: true},
  82. 4: set{3: true, 5: true},
  83. 5: set{4: true, 6: true},
  84. 6: set{5: true, 7: true},
  85. 7: set{6: true, 8: true},
  86. 8: set{7: true, 9: true},
  87. 9: set{8: true, 1: true},
  88. },
  89. },
  90. }
  91. for _, test := range testCases {
  92. var neighbours = getNeighbours(test.x)
  93. for i, set := range neighbours {
  94. for j := range set {
  95. if !test.neighbours[i][j] {
  96. t.Error("getNeighbours didn't work as expected")
  97. }
  98. }
  99. }
  100. }
  101. }
  102. func TestIntSliceAt(t *testing.T) {
  103. var ints = IntSlice{1, 2, 3}
  104. if ints.At(0) != 1 || ints.At(1) != 2 || ints.At(2) != 3 {
  105. t.Error("IntSlice At method has unexpected behavior")
  106. }
  107. }
  108. func TestIntSliceLen(t *testing.T) {
  109. var ints = IntSlice{1, 2, 3}
  110. if ints.Len() != 3 {
  111. t.Error("IntSlice Len method has unexpected behavior")
  112. }
  113. }
  114. func TestIntSliceSwap(t *testing.T) {
  115. var ints = IntSlice{1, 2, 3}
  116. ints.Swap(0, 2)
  117. if ints.At(0) != 3 || ints.At(2) != 1 {
  118. t.Error("IntSlice Swap method has unexpected behavior")
  119. }
  120. }
  121. func TestIntSliceSlice(t *testing.T) {
  122. var ints = IntSlice{1, 2, 3}.Slice(1, 2)
  123. if ints.Len() != 1 || ints.At(0) != 2 {
  124. t.Error("IntSlice Slice method has unexpected behavior")
  125. }
  126. }
  127. func TestIntSliceSplit(t *testing.T) {
  128. var a, b = IntSlice{1, 2, 3}.Split(1)
  129. if a.Len() != 1 || b.Len() != 2 || a.At(0) != 1 || b.At(0) != 2 || b.At(1) != 3 {
  130. t.Error("IntSlice Split method has unexpected behavior")
  131. }
  132. }
  133. func TestIntSliceAppend(t *testing.T) {
  134. var ints = IntSlice{1}.Append(IntSlice{2})
  135. if ints.Len() != 2 || ints.At(0) != 1 || ints.At(1) != 2 {
  136. t.Error("IntSlice Append method has unexpected behavior")
  137. }
  138. }
  139. func TestIntSliceReplace(t *testing.T) {
  140. var ints = IntSlice{1}
  141. ints.Replace(IntSlice{2})
  142. if ints.Len() != 1 || ints.At(0) != 2 {
  143. t.Error("IntSlice Replace method has unexpected behavior")
  144. }
  145. }
  146. func TestIntSliceCopy(t *testing.T) {
  147. var (
  148. ints = IntSlice{1}
  149. clone = ints.Copy()
  150. )
  151. clone.Replace(IntSlice{2})
  152. if ints.At(0) != 1 {
  153. t.Error("IntSlice Copy method has unexpected behavior")
  154. }
  155. }
  156. func TestFloat64SliceAt(t *testing.T) {
  157. var floats = Float64Slice{1, 2, 3}
  158. if floats.At(0) != 1.0 || floats.At(1) != 2.0 || floats.At(2) != 3.0 {
  159. t.Error("Float64Slice At method has unexpected behavior")
  160. }
  161. }
  162. func TestFloat64SliceLen(t *testing.T) {
  163. var floats = Float64Slice{1, 2, 3}
  164. if floats.Len() != 3 {
  165. t.Error("Float64Slice Len method has unexpected behavior")
  166. }
  167. }
  168. func TestFloat64SliceSwap(t *testing.T) {
  169. var floats = Float64Slice{1, 2, 3}
  170. floats.Swap(0, 2)
  171. if floats.At(0) != 3.0 || floats.At(2) != 1.0 {
  172. t.Error("Float64Slice Swap method has unexpected behavior")
  173. }
  174. }
  175. func TestFloat64SliceSlice(t *testing.T) {
  176. var floats = Float64Slice{1, 2, 3}.Slice(1, 2)
  177. if floats.Len() != 1 || floats.At(0) != 2.0 {
  178. t.Error("Float64Slice Slice method has unexpected behavior")
  179. }
  180. }
  181. func TestFloat64SliceSplit(t *testing.T) {
  182. var a, b = Float64Slice{1, 2, 3}.Split(1)
  183. if a.Len() != 1 || b.Len() != 2 || a.At(0) != 1.0 || b.At(0) != 2.0 || b.At(1) != 3.0 {
  184. t.Error("Float64Slice Split method has unexpected behavior")
  185. }
  186. }
  187. func TestFloat64SliceAppend(t *testing.T) {
  188. var floats = Float64Slice{1}.Append(Float64Slice{2})
  189. if floats.Len() != 2 || floats.At(0) != 1.0 || floats.At(1) != 2.0 {
  190. t.Error("Float64Slice Append method has unexpected behavior")
  191. }
  192. }
  193. func TestFloat64SliceReplace(t *testing.T) {
  194. var floats = Float64Slice{1}
  195. floats.Replace(Float64Slice{2})
  196. if floats.Len() != 1 || floats.At(0) != 2.0 {
  197. t.Error("Float64Slice Replace method has unexpected behavior")
  198. }
  199. }
  200. func TestFloat64SliceCopy(t *testing.T) {
  201. var (
  202. floats = Float64Slice{1}
  203. clone = floats.Copy()
  204. )
  205. clone.Replace(Float64Slice{2})
  206. if floats.At(0) != 1.0 {
  207. t.Error("IntSlice Copy method has unexpected behavior")
  208. }
  209. }
  210. func TestStringSliceAt(t *testing.T) {
  211. var strings = StringSlice{"a", "b", "c"}
  212. if strings.At(0) != "a" || strings.At(1) != "b" || strings.At(2) != "c" {
  213. t.Error("StringSlice At method has unexpected behavior")
  214. }
  215. }
  216. func TestStringSliceLen(t *testing.T) {
  217. var strings = StringSlice{"a", "b", "c"}
  218. if strings.Len() != 3 {
  219. t.Error("StringSlice Len method has unexpected behavior")
  220. }
  221. }
  222. func TestStringSliceSwap(t *testing.T) {
  223. var strings = StringSlice{"a", "b", "c"}
  224. strings.Swap(0, 2)
  225. if strings.At(0) != "c" || strings.At(2) != "a" {
  226. t.Error("StringSlice Swap method has unexpected behavior")
  227. }
  228. }
  229. func TestStringSliceSlice(t *testing.T) {
  230. var strings = StringSlice{"a", "b", "c"}.Slice(1, 2)
  231. if strings.Len() != 1 || strings.At(0) != "b" {
  232. t.Error("StringSlice Slice method has unexpected behavior")
  233. }
  234. }
  235. func TestStringSliceSplit(t *testing.T) {
  236. var a, b = StringSlice{"a", "b", "c"}.Split(1)
  237. if a.Len() != 1 || b.Len() != 2 || a.At(0) != "a" || b.At(0) != "b" || b.At(1) != "c" {
  238. t.Error("StringSlice Split method has unexpected behavior")
  239. }
  240. }
  241. func TestStringSliceAppend(t *testing.T) {
  242. var strings = StringSlice{"a"}.Append(StringSlice{"b"})
  243. if strings.Len() != 2 || strings.At(0) != "a" || strings.At(1) != "b" {
  244. t.Error("StringSlice Append method has unexpected behavior")
  245. }
  246. }
  247. func TestStringSliceReplace(t *testing.T) {
  248. var strings = StringSlice{"a"}
  249. strings.Replace(StringSlice{"b"})
  250. if strings.Len() != 1 || strings.At(0) != "b" {
  251. t.Error("StringSlice Replace method has unexpected behavior")
  252. }
  253. }
  254. func TestStringSliceCopy(t *testing.T) {
  255. var (
  256. strings = StringSlice{"a"}
  257. clone = strings.Copy()
  258. )
  259. clone.Replace(StringSlice{"b"})
  260. if strings.At(0) == "b" {
  261. t.Error("StringSlice Copy method has unexpected behavior")
  262. }
  263. }