col_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. package xlsx
  2. import (
  3. "testing"
  4. qt "github.com/frankban/quicktest"
  5. . "gopkg.in/check.v1"
  6. )
  7. var notNil = qt.Not(qt.IsNil)
  8. func TestNewColForRange(t *testing.T) {
  9. c := qt.New(t)
  10. col := NewColForRange(30, 45)
  11. c.Assert(col, notNil)
  12. c.Assert(col.Min, qt.Equals, 30)
  13. c.Assert(col.Max, qt.Equals, 45)
  14. // Auto fix the min/max
  15. col = NewColForRange(45, 30)
  16. c.Assert(col, notNil)
  17. c.Assert(col.Min, qt.Equals, 30)
  18. c.Assert(col.Max, qt.Equals, 45)
  19. }
  20. func TestCol(t *testing.T) {
  21. c := qt.New(t)
  22. c.Run("SetType", func(c *qt.C) {
  23. expectations := map[CellType]string{
  24. CellTypeString: builtInNumFmt[builtInNumFmtIndex_STRING],
  25. CellTypeNumeric: builtInNumFmt[builtInNumFmtIndex_INT],
  26. CellTypeBool: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  27. CellTypeInline: builtInNumFmt[builtInNumFmtIndex_STRING],
  28. CellTypeError: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  29. CellTypeDate: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  30. CellTypeStringFormula: builtInNumFmt[builtInNumFmtIndex_STRING],
  31. }
  32. assertSetType := func(cellType CellType, expectation string) {
  33. col := &Col{}
  34. col.SetType(cellType)
  35. c.Assert(col.numFmt, qt.Equals, expectation)
  36. }
  37. for k, v := range expectations {
  38. assertSetType(k, v)
  39. }
  40. })
  41. c.Run("SetWidth", func(c *qt.C) {
  42. col := &Col{}
  43. col.SetWidth(20.2)
  44. c.Assert(col.Width, qt.Equals, 20.2)
  45. c.Assert(col.CustomWidth, qt.Equals, true)
  46. })
  47. c.Run("copyToRange", func(c *qt.C) {
  48. nf := &parsedNumberFormat{}
  49. s := &Style{}
  50. c1 := &Col{
  51. Min: 1,
  52. Max: 11,
  53. Hidden: true,
  54. Width: 300.4,
  55. Collapsed: true,
  56. OutlineLevel: 2,
  57. numFmt: "-0.00",
  58. parsedNumFmt: nf,
  59. style: s,
  60. }
  61. c2 := c1.copyToRange(4, 10)
  62. c.Assert(c2.Min, qt.Equals, 4)
  63. c.Assert(c2.Max, qt.Equals, 10)
  64. c.Assert(c2.Hidden, qt.Equals, c1.Hidden)
  65. c.Assert(c2.Width, qt.Equals, c1.Width)
  66. c.Assert(c2.Collapsed, qt.Equals, c1.Collapsed)
  67. c.Assert(c2.OutlineLevel, qt.Equals, c1.OutlineLevel)
  68. c.Assert(c2.numFmt, qt.Equals, c1.numFmt)
  69. c.Assert(c2.parsedNumFmt, qt.Equals, c1.parsedNumFmt)
  70. c.Assert(c2.style, qt.Equals, c1.style)
  71. })
  72. }
  73. type ColStoreSuite struct{}
  74. var _ = Suite(&ColStoreSuite{})
  75. func (css *ColStoreSuite) TestAddRootNode(c *C) {
  76. col := &Col{Min: 1, Max: 1}
  77. cs := ColStore{}
  78. cs.Add(col)
  79. c.Assert(cs.Len, Equals, 1)
  80. c.Assert(cs.Root.Col, Equals, col)
  81. }
  82. func TestMakeWay(t *testing.T) {
  83. c := qt.New(t)
  84. assertWayMade := func(cols []*Col, chainFunc func(*ColStore)) {
  85. cs := &ColStore{}
  86. for _, col := range cols {
  87. _ = cs.Add(col)
  88. }
  89. chainFunc(cs)
  90. }
  91. // Col1: |--|
  92. // Col2: |--|
  93. assertWayMade([]*Col{{Min: 1, Max: 2}, {Min: 3, Max: 4}},
  94. func(cs *ColStore) {
  95. c.Assert(cs.Len, qt.Equals, 2)
  96. root := cs.Root
  97. c.Assert(root.Col.Min, qt.Equals, 1)
  98. c.Assert(root.Col.Max, qt.Equals, 2)
  99. c.Assert(root.Prev, qt.IsNil)
  100. c.Assert(root.Next, notNil)
  101. node2 := root.Next
  102. c.Assert(node2.Prev, qt.Equals, root)
  103. c.Assert(node2.Next, qt.IsNil)
  104. c.Assert(node2.Col.Min, qt.Equals, 3)
  105. c.Assert(node2.Col.Max, qt.Equals, 4)
  106. })
  107. // Col1: |--|
  108. // Col2: |--|
  109. assertWayMade([]*Col{{Min: 3, Max: 4}, {Min: 1, Max: 2}},
  110. func(cs *ColStore) {
  111. root := cs.Root
  112. c.Assert(cs.Len, qt.Equals, 2)
  113. c.Assert(root.Col.Min, qt.Equals, 3)
  114. c.Assert(root.Col.Max, qt.Equals, 4)
  115. c.Assert(root.Prev, notNil)
  116. c.Assert(root.Next, qt.IsNil)
  117. node2 := root.Prev
  118. c.Assert(node2.Next, qt.Equals, root)
  119. c.Assert(node2.Prev, qt.IsNil)
  120. c.Assert(node2.Col.Min, qt.Equals, 1)
  121. c.Assert(node2.Col.Max, qt.Equals, 2)
  122. })
  123. // Col1: |--x|
  124. // Col2: |--|
  125. assertWayMade([]*Col{{Min: 1, Max: 3}, {Min: 3, Max: 4}},
  126. func(cs *ColStore) {
  127. root := cs.Root
  128. c.Assert(cs.Len, qt.Equals, 2)
  129. c.Assert(root.Col.Min, qt.Equals, 1)
  130. c.Assert(root.Col.Max, qt.Equals, 2)
  131. c.Assert(root.Prev, qt.IsNil)
  132. c.Assert(root.Next, notNil)
  133. node2 := root.Next
  134. c.Assert(node2.Prev, qt.Equals, root)
  135. c.Assert(node2.Next, qt.IsNil)
  136. c.Assert(node2.Col.Min, qt.Equals, 3)
  137. c.Assert(node2.Col.Max, qt.Equals, 4)
  138. })
  139. // Col1: |x-|
  140. // Col2: |--|
  141. assertWayMade([]*Col{{Min: 2, Max: 3}, {Min: 1, Max: 2}},
  142. func(cs *ColStore) {
  143. root := cs.Root
  144. c.Assert(cs.Len, qt.Equals, 2)
  145. c.Assert(root.Col.Min, qt.Equals, 3)
  146. c.Assert(root.Col.Max, qt.Equals, 3)
  147. c.Assert(root.Prev, notNil)
  148. c.Assert(root.Next, qt.IsNil)
  149. node2 := root.Prev
  150. c.Assert(node2.Next, qt.Equals, root)
  151. c.Assert(node2.Prev, qt.IsNil)
  152. c.Assert(node2.Col.Min, qt.Equals, 1)
  153. c.Assert(node2.Col.Max, qt.Equals, 2)
  154. })
  155. // Col1: |---xx---|
  156. // Col2: |--|
  157. assertWayMade([]*Col{{Min: 1, Max: 8}, {Min: 4, Max: 5}},
  158. func(cs *ColStore) {
  159. root := cs.Root
  160. c.Assert(cs.Len, qt.Equals, 3)
  161. c.Assert(root.Prev, qt.IsNil)
  162. c.Assert(root.Next, notNil)
  163. c.Assert(root.Col.Min, qt.Equals, 1)
  164. c.Assert(root.Col.Max, qt.Equals, 3)
  165. node2 := root.Next
  166. c.Assert(node2.Prev, qt.Equals, root)
  167. c.Assert(node2.Col.Min, qt.Equals, 4)
  168. c.Assert(node2.Col.Max, qt.Equals, 5)
  169. c.Assert(node2.Next, notNil)
  170. node3 := node2.Next
  171. c.Assert(node3.Prev, qt.Equals, node2)
  172. c.Assert(node3.Next, qt.IsNil)
  173. c.Assert(node3.Col.Min, qt.Equals, 6)
  174. c.Assert(node3.Col.Max, qt.Equals, 8)
  175. })
  176. // Col1: |xx|
  177. // Col2: |--|
  178. assertWayMade([]*Col{{Min: 1, Max: 2, Width: 40.1}, {Min: 1, Max: 2, Width: 10.0}},
  179. func(cs *ColStore) {
  180. root := cs.Root
  181. c.Assert(cs.Len, qt.Equals, 1)
  182. c.Assert(root, notNil)
  183. c.Assert(root.Prev, qt.IsNil)
  184. c.Assert(root.Next, qt.IsNil)
  185. c.Assert(root.Col.Min, qt.Equals, 1)
  186. c.Assert(root.Col.Max, qt.Equals, 2)
  187. // This is how we establish we have the new node, and not the old one
  188. c.Assert(root.Col.Width, qt.Equals, 10.0)
  189. })
  190. // Col1: |xx|
  191. // Col2: |----|
  192. assertWayMade([]*Col{{Min: 2, Max: 3, Width: 40.1}, {Min: 1, Max: 4, Width: 10.0}},
  193. func(cs *ColStore) {
  194. root := cs.Root
  195. c.Assert(cs.Len, qt.Equals, 1)
  196. c.Assert(root.Prev, qt.IsNil)
  197. c.Assert(root.Next, qt.IsNil)
  198. c.Assert(root.Col.Min, qt.Equals, 1)
  199. c.Assert(root.Col.Max, qt.Equals, 4)
  200. // This is how we establish we have the new node, and not the old one
  201. c.Assert(root.Col.Width, qt.Equals, 10.0)
  202. })
  203. // Col1: |--|
  204. // Col2: |--|
  205. // Col3: |--|
  206. assertWayMade([]*Col{{Min: 1, Max: 2}, {Min: 3, Max: 4}, {Min: 5, Max: 6}},
  207. func(cs *ColStore) {
  208. root := cs.Root
  209. c.Assert(cs.Len, qt.Equals, 3)
  210. c.Assert(root.Prev, qt.IsNil)
  211. c.Assert(root.Next, notNil)
  212. c.Assert(root.Col.Min, qt.Equals, 1)
  213. c.Assert(root.Col.Max, qt.Equals, 2)
  214. node2 := root.Next
  215. c.Assert(node2.Prev, qt.Equals, root)
  216. c.Assert(node2.Col.Min, qt.Equals, 3)
  217. c.Assert(node2.Col.Max, qt.Equals, 4)
  218. c.Assert(node2.Next, notNil)
  219. node3 := node2.Next
  220. c.Assert(node3.Prev, qt.Equals, node2)
  221. c.Assert(node3.Next, qt.IsNil)
  222. c.Assert(node3.Col.Min, qt.Equals, 5)
  223. c.Assert(node3.Col.Max, qt.Equals, 6)
  224. })
  225. // Col1: |--|
  226. // Col2: |--|
  227. // Col3: |--|
  228. assertWayMade([]*Col{{Min: 5, Max: 6}, {Min: 3, Max: 4}, {Min: 1, Max: 2}},
  229. func(cs *ColStore) {
  230. root := cs.Root
  231. c.Assert(cs.Len, qt.Equals, 3)
  232. c.Assert(root.Prev, notNil)
  233. c.Assert(root.Next, qt.IsNil)
  234. c.Assert(root.Col.Min, qt.Equals, 5)
  235. c.Assert(root.Col.Max, qt.Equals, 6)
  236. node2 := root.Prev
  237. c.Assert(node2.Next, qt.Equals, root)
  238. c.Assert(node2.Col.Min, qt.Equals, 3)
  239. c.Assert(node2.Col.Max, qt.Equals, 4)
  240. c.Assert(node2.Prev, notNil)
  241. node3 := node2.Prev
  242. c.Assert(node3.Next, qt.Equals, node2)
  243. c.Assert(node3.Prev, qt.IsNil)
  244. c.Assert(node3.Col.Min, qt.Equals, 1)
  245. c.Assert(node3.Col.Max, qt.Equals, 2)
  246. })
  247. // Col1: |--|
  248. // Col2: |--|
  249. // Col3: |--|
  250. assertWayMade([]*Col{{Min: 1, Max: 2}, {Min: 10, Max: 11}, {Min: 5, Max: 6}},
  251. func(cs *ColStore) {
  252. root := cs.Root
  253. c.Assert(cs.Len, qt.Equals, 3)
  254. c.Assert(root.Prev, qt.IsNil)
  255. c.Assert(root.Next, notNil)
  256. c.Assert(root.Col.Min, qt.Equals, 1)
  257. c.Assert(root.Col.Max, qt.Equals, 2)
  258. node2 := root.Next
  259. c.Assert(node2.Prev, qt.Equals, root)
  260. c.Assert(node2.Col.Min, qt.Equals, 5)
  261. c.Assert(node2.Col.Max, qt.Equals, 6)
  262. c.Assert(node2.Next, notNil)
  263. node3 := node2.Next
  264. c.Assert(node3.Prev, qt.Equals, node2)
  265. c.Assert(node3.Next, qt.IsNil)
  266. c.Assert(node3.Col.Min, qt.Equals, 10)
  267. c.Assert(node3.Col.Max, qt.Equals, 11)
  268. })
  269. // Col1: |-x|
  270. // Col2: |x-|
  271. // Col3: |-------|
  272. assertWayMade([]*Col{
  273. {Min: 1, Max: 2}, {Min: 8, Max: 9}, {Min: 2, Max: 8}},
  274. func(cs *ColStore) {
  275. root := cs.Root
  276. c.Assert(cs.Len, qt.Equals, 3)
  277. c.Assert(root.Prev, qt.IsNil)
  278. c.Assert(root.Next, notNil)
  279. c.Assert(root.Col.Min, qt.Equals, 1)
  280. c.Assert(root.Col.Max, qt.Equals, 1)
  281. node2 := root.Next
  282. c.Assert(node2.Prev, qt.Equals, root)
  283. c.Assert(node2.Next, notNil)
  284. c.Assert(node2.Col.Min, qt.Equals, 2)
  285. c.Assert(node2.Col.Max, qt.Equals, 8)
  286. node3 := node2.Next
  287. c.Assert(node3.Prev, qt.Equals, node2)
  288. c.Assert(node3.Next, qt.IsNil)
  289. c.Assert(node3.Col.Min, qt.Equals, 9)
  290. c.Assert(node3.Col.Max, qt.Equals, 9)
  291. })
  292. // Col1: |-x|
  293. // Col2: |--|
  294. // Col3: |-----|
  295. assertWayMade([]*Col{
  296. {Min: 1, Max: 2}, {Min: 8, Max: 9}, {Min: 2, Max: 7}},
  297. func(cs *ColStore) {
  298. root := cs.Root
  299. c.Assert(cs.Len, qt.Equals, 3)
  300. c.Assert(root.Prev, qt.IsNil)
  301. c.Assert(root.Next, notNil)
  302. c.Assert(root.Col.Min, qt.Equals, 1)
  303. c.Assert(root.Col.Max, qt.Equals, 1)
  304. node2 := root.Next
  305. c.Assert(node2.Prev, qt.Equals, root)
  306. c.Assert(node2.Next, notNil)
  307. c.Assert(node2.Col.Min, qt.Equals, 2)
  308. c.Assert(node2.Col.Max, qt.Equals, 7)
  309. node3 := node2.Next
  310. c.Assert(node3.Prev, qt.Equals, node2)
  311. c.Assert(node3.Next, qt.IsNil)
  312. c.Assert(node3.Col.Min, qt.Equals, 8)
  313. c.Assert(node3.Col.Max, qt.Equals, 9)
  314. })
  315. // Col1: |--|
  316. // Col2: |x-|
  317. // Col3: |-----|
  318. assertWayMade([]*Col{
  319. {Min: 1, Max: 2}, {Min: 8, Max: 9}, {Min: 3, Max: 8}},
  320. func(cs *ColStore) {
  321. root := cs.Root
  322. c.Assert(cs.Len, qt.Equals, 3)
  323. c.Assert(root.Prev, qt.IsNil)
  324. c.Assert(root.Next, notNil)
  325. c.Assert(root.Col.Min, qt.Equals, 1)
  326. c.Assert(root.Col.Max, qt.Equals, 2)
  327. node2 := root.Next
  328. c.Assert(node2.Prev, qt.Equals, root)
  329. c.Assert(node2.Next, notNil)
  330. c.Assert(node2.Col.Min, qt.Equals, 3)
  331. c.Assert(node2.Col.Max, qt.Equals, 8)
  332. node3 := node2.Next
  333. c.Assert(node3.Prev, qt.Equals, node2)
  334. c.Assert(node3.Next, qt.IsNil)
  335. c.Assert(node3.Col.Min, qt.Equals, 9)
  336. c.Assert(node3.Col.Max, qt.Equals, 9)
  337. })
  338. // Col1: |--|
  339. // Col2: |xx|
  340. // Col3: |--|
  341. // Col4: |--|
  342. assertWayMade(
  343. []*Col{
  344. {Min: 1, Max: 2},
  345. {Min: 3, Max: 4, Width: 1.0},
  346. {Min: 5, Max: 6},
  347. {Min: 3, Max: 4, Width: 2.0},
  348. },
  349. func(cs *ColStore) {
  350. root := cs.Root
  351. c.Assert(cs.Len, qt.Equals, 3)
  352. c.Assert(root.Prev, qt.IsNil)
  353. c.Assert(root.Next, notNil)
  354. c.Assert(root.Col.Min, qt.Equals, 1)
  355. c.Assert(root.Col.Max, qt.Equals, 2)
  356. node2 := root.Next
  357. c.Assert(node2.Prev, qt.Equals, root)
  358. c.Assert(node2.Next, notNil)
  359. c.Assert(node2.Col.Min, qt.Equals, 3)
  360. c.Assert(node2.Col.Max, qt.Equals, 4)
  361. c.Assert(node2.Col.Width, qt.Equals, 2.0) // We have the later version
  362. node3 := node2.Next
  363. c.Assert(node3.Prev, qt.Equals, node2)
  364. c.Assert(node3.Next, qt.IsNil)
  365. c.Assert(node3.Col.Min, qt.Equals, 5)
  366. c.Assert(node3.Col.Max, qt.Equals, 6)
  367. })
  368. // Col1: |-x|
  369. // Col2: |xx|
  370. // Col3: |x-|
  371. // Col4: |----|
  372. assertWayMade(
  373. []*Col{
  374. {Min: 1, Max: 2, Width: 1.0},
  375. {Min: 3, Max: 4, Width: 2.0},
  376. {Min: 5, Max: 6, Width: 3.0},
  377. {Min: 2, Max: 5, Width: 4.0},
  378. },
  379. func(cs *ColStore) {
  380. root := cs.Root
  381. c.Assert(cs.Len, qt.Equals, 3)
  382. c.Assert(root.Prev, qt.IsNil)
  383. c.Assert(root.Next, notNil)
  384. c.Assert(root.Col.Min, qt.Equals, 1)
  385. c.Assert(root.Col.Max, qt.Equals, 1)
  386. c.Assert(root.Col.Width, qt.Equals, 1.0)
  387. node2 := root.Next
  388. c.Assert(node2.Prev, qt.Equals, root)
  389. c.Assert(node2.Next, notNil)
  390. c.Assert(node2.Col.Min, qt.Equals, 2)
  391. c.Assert(node2.Col.Max, qt.Equals, 5)
  392. c.Assert(node2.Col.Width, qt.Equals, 4.0)
  393. node3 := node2.Next
  394. c.Assert(node3.Prev, qt.Equals, node2)
  395. c.Assert(node3.Next, qt.IsNil)
  396. c.Assert(node3.Col.Min, qt.Equals, 6)
  397. c.Assert(node3.Col.Max, qt.Equals, 6)
  398. c.Assert(node3.Col.Width, qt.Equals, 3.0)
  399. })
  400. }
  401. func (css *ColStoreSuite) TestFindNodeForCol(c *C) {
  402. assertNodeFound := func(cs *ColStore, num int, col *Col) {
  403. node := cs.findNodeForColNum(num)
  404. if col == nil {
  405. c.Assert(node, IsNil)
  406. return
  407. }
  408. c.Assert(node, NotNil)
  409. c.Assert(node.Col, Equals, col)
  410. }
  411. cs := &ColStore{}
  412. col0 := &Col{Min: 1, Max: 1}
  413. cs.Add(col0)
  414. col1 := &Col{Min: 2, Max: 2}
  415. cs.Add(col1)
  416. col2 := &Col{Min: 3, Max: 3}
  417. cs.Add(col2)
  418. col3 := &Col{Min: 4, Max: 4}
  419. cs.Add(col3)
  420. col4 := &Col{Min: 5, Max: 5}
  421. cs.Add(col4)
  422. col5 := &Col{Min: 100, Max: 125}
  423. cs.Add(col5)
  424. assertNodeFound(cs, 0, nil)
  425. assertNodeFound(cs, 1, col0)
  426. assertNodeFound(cs, 2, col1)
  427. assertNodeFound(cs, 3, col2)
  428. assertNodeFound(cs, 4, col3)
  429. assertNodeFound(cs, 5, col4)
  430. assertNodeFound(cs, 6, nil)
  431. assertNodeFound(cs, 99, nil)
  432. assertNodeFound(cs, 100, col5)
  433. assertNodeFound(cs, 110, col5)
  434. assertNodeFound(cs, 125, col5)
  435. assertNodeFound(cs, 126, nil)
  436. }
  437. func (css *ColStoreSuite) TestRemoveNode(c *C) {
  438. assertChain := func(cs *ColStore, chain []*Col) {
  439. node := cs.Root
  440. for _, col := range chain {
  441. c.Assert(node, NotNil)
  442. c.Assert(node.Col.Min, Equals, col.Min)
  443. c.Assert(node.Col.Max, Equals, col.Max)
  444. node = node.Next
  445. }
  446. c.Assert(node, IsNil)
  447. }
  448. cs := &ColStore{}
  449. col0 := &Col{Min: 1, Max: 1}
  450. cs.Add(col0)
  451. col1 := &Col{Min: 2, Max: 2}
  452. cs.Add(col1)
  453. col2 := &Col{Min: 3, Max: 3}
  454. cs.Add(col2)
  455. col3 := &Col{Min: 4, Max: 4}
  456. cs.Add(col3)
  457. col4 := &Col{Min: 5, Max: 5}
  458. cs.Add(col4)
  459. c.Assert(cs.Len, Equals, 5)
  460. cs.removeNode(cs.findNodeForColNum(5))
  461. c.Assert(cs.Len, Equals, 4)
  462. assertChain(cs, []*Col{col0, col1, col2, col3})
  463. cs.removeNode(cs.findNodeForColNum(1))
  464. c.Assert(cs.Len, Equals, 3)
  465. assertChain(cs, []*Col{col1, col2, col3})
  466. }
  467. func (css *ColStoreSuite) TestForEach(c *C) {
  468. cs := &ColStore{}
  469. col0 := &Col{Min: 1, Max: 1, Hidden: true}
  470. cs.Add(col0)
  471. col1 := &Col{Min: 2, Max: 2}
  472. cs.Add(col1)
  473. col2 := &Col{Min: 3, Max: 3}
  474. cs.Add(col2)
  475. col3 := &Col{Min: 4, Max: 4}
  476. cs.Add(col3)
  477. col4 := &Col{Min: 5, Max: 5}
  478. cs.Add(col4)
  479. cs.ForEach(func(index int, col *Col) {
  480. col.Phonetic = true
  481. })
  482. c.Assert(col0.Phonetic, Equals, true)
  483. c.Assert(col1.Phonetic, Equals, true)
  484. c.Assert(col2.Phonetic, Equals, true)
  485. c.Assert(col3.Phonetic, Equals, true)
  486. c.Assert(col4.Phonetic, Equals, true)
  487. }
  488. func (css *ColStoreSuite) TestGetOrMakeColsForRange(c *C) {
  489. assertCols := func(min, max int, initalCols, expectedCols []*Col) {
  490. cs := &ColStore{}
  491. for _, col := range initalCols {
  492. cs.Add(col)
  493. }
  494. result := cs.getOrMakeColsForRange(cs.Root, min, max)
  495. c.Assert(result, HasLen, len(expectedCols))
  496. for i := 0; i < len(expectedCols); i++ {
  497. got := result[i]
  498. expected := expectedCols[i]
  499. c.Assert(got.Min, Equals, expected.Min)
  500. c.Assert(got.Max, Equals, expected.Max)
  501. }
  502. }
  503. // make everything
  504. assertCols(1, 11, nil, []*Col{{Min: 1, Max: 11}})
  505. // get everything, one col
  506. assertCols(1, 11, []*Col{{Min: 1, Max: 11}}, []*Col{{Min: 1, Max: 11}})
  507. // get everything, many cols
  508. assertCols(1, 11,
  509. []*Col{
  510. {Min: 1, Max: 4},
  511. {Min: 5, Max: 8},
  512. {Min: 9, Max: 11},
  513. },
  514. []*Col{
  515. {Min: 1, Max: 4},
  516. {Min: 5, Max: 8},
  517. {Min: 9, Max: 11},
  518. },
  519. )
  520. // make missing col
  521. assertCols(1, 11,
  522. []*Col{
  523. {Min: 1, Max: 4},
  524. {Min: 9, Max: 11},
  525. },
  526. []*Col{
  527. {Min: 1, Max: 4},
  528. {Min: 5, Max: 8},
  529. {Min: 9, Max: 11},
  530. },
  531. )
  532. }