interval_list_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. type IntervalInt int
  8. func (i IntervalInt) SetStartStop(start, stop int64) {
  9. }
  10. func (i IntervalInt) Clone() IntervalValue {
  11. return i
  12. }
  13. func TestIntervalList_Overlay(t *testing.T) {
  14. list := NewIntervalList[IntervalInt]()
  15. list.Overlay(0, 100, 1, 1)
  16. list.Overlay(50, 150, 2, 2)
  17. list.Overlay(200, 250, 3, 3)
  18. list.Overlay(225, 250, 4, 4)
  19. list.Overlay(175, 210, 5, 5)
  20. list.Overlay(0, 25, 6, 6)
  21. for p := list.Front(); p != nil; p = p.Next {
  22. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  23. }
  24. assert.Equal(t, 6, list.Len())
  25. println()
  26. list.Overlay(50, 150, 7, 7)
  27. for p := list.Front(); p != nil; p = p.Next {
  28. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  29. }
  30. assert.Equal(t, 6, list.Len())
  31. }
  32. func TestIntervalList_Overlay2(t *testing.T) {
  33. list := NewIntervalList[IntervalInt]()
  34. list.Overlay(50, 100, 1, 1)
  35. list.Overlay(0, 50, 2, 2)
  36. for p := list.Front(); p != nil; p = p.Next {
  37. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  38. }
  39. }
  40. func TestIntervalList_Overlay3(t *testing.T) {
  41. list := NewIntervalList[IntervalInt]()
  42. list.Overlay(50, 100, 1, 1)
  43. assert.Equal(t, 1, list.Len())
  44. list.Overlay(0, 60, 2, 2)
  45. for p := list.Front(); p != nil; p = p.Next {
  46. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  47. }
  48. assert.Equal(t, 2, list.Len())
  49. }
  50. func TestIntervalList_Overlay4(t *testing.T) {
  51. list := NewIntervalList[IntervalInt]()
  52. list.Overlay(50, 100, 1, 1)
  53. list.Overlay(0, 100, 2, 2)
  54. for p := list.Front(); p != nil; p = p.Next {
  55. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  56. }
  57. assert.Equal(t, 1, list.Len())
  58. }
  59. func TestIntervalList_Overlay5(t *testing.T) {
  60. list := NewIntervalList[IntervalInt]()
  61. list.Overlay(50, 100, 1, 1)
  62. list.Overlay(0, 110, 2, 2)
  63. for p := list.Front(); p != nil; p = p.Next {
  64. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  65. }
  66. assert.Equal(t, 1, list.Len())
  67. }
  68. func TestIntervalList_Overlay6(t *testing.T) {
  69. list := NewIntervalList[IntervalInt]()
  70. list.Overlay(50, 100, 1, 1)
  71. list.Overlay(50, 110, 2, 2)
  72. for p := list.Front(); p != nil; p = p.Next {
  73. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  74. }
  75. assert.Equal(t, 1, list.Len())
  76. }
  77. func TestIntervalList_Overlay7(t *testing.T) {
  78. list := NewIntervalList[IntervalInt]()
  79. list.Overlay(50, 100, 1, 1)
  80. list.Overlay(50, 90, 2, 2)
  81. for p := list.Front(); p != nil; p = p.Next {
  82. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  83. }
  84. assert.Equal(t, 2, list.Len())
  85. }
  86. func TestIntervalList_Overlay8(t *testing.T) {
  87. list := NewIntervalList[IntervalInt]()
  88. list.Overlay(50, 100, 1, 1)
  89. list.Overlay(60, 90, 2, 2)
  90. for p := list.Front(); p != nil; p = p.Next {
  91. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  92. }
  93. assert.Equal(t, 3, list.Len())
  94. }
  95. func TestIntervalList_Overlay9(t *testing.T) {
  96. list := NewIntervalList[IntervalInt]()
  97. list.Overlay(50, 100, 1, 1)
  98. list.Overlay(60, 100, 2, 2)
  99. for p := list.Front(); p != nil; p = p.Next {
  100. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  101. }
  102. assert.Equal(t, 2, list.Len())
  103. }
  104. func TestIntervalList_Overlay10(t *testing.T) {
  105. list := NewIntervalList[IntervalInt]()
  106. list.Overlay(50, 100, 1, 1)
  107. list.Overlay(60, 110, 2, 2)
  108. for p := list.Front(); p != nil; p = p.Next {
  109. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  110. }
  111. assert.Equal(t, 2, list.Len())
  112. }
  113. func TestIntervalList_Overlay11(t *testing.T) {
  114. list := NewIntervalList[IntervalInt]()
  115. list.Overlay(0, 100, 1, 1)
  116. list.Overlay(100, 110, 2, 2)
  117. list.Overlay(0, 90, 3, 3)
  118. list.Overlay(0, 80, 4, 4)
  119. list.Overlay(0, 90, 5, 5)
  120. list.Overlay(90, 90, 6, 6)
  121. for p := list.Front(); p != nil; p = p.Next {
  122. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  123. }
  124. assert.Equal(t, 3, list.Len())
  125. }
  126. func TestIntervalList_insertInterval1(t *testing.T) {
  127. list := NewIntervalList[IntervalInt]()
  128. list.InsertInterval(50, 150, 2, 2)
  129. list.InsertInterval(200, 250, 3, 3)
  130. for p := list.Front(); p != nil; p = p.Next {
  131. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  132. }
  133. assert.Equal(t, 2, list.Len())
  134. }
  135. func TestIntervalList_insertInterval2(t *testing.T) {
  136. list := NewIntervalList[IntervalInt]()
  137. list.InsertInterval(50, 150, 2, 2)
  138. list.InsertInterval(0, 25, 3, 3)
  139. for p := list.Front(); p != nil; p = p.Next {
  140. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  141. }
  142. assert.Equal(t, 2, list.Len())
  143. }
  144. func TestIntervalList_insertInterval3(t *testing.T) {
  145. list := NewIntervalList[IntervalInt]()
  146. list.InsertInterval(50, 150, 2, 2)
  147. list.InsertInterval(200, 250, 4, 4)
  148. list.InsertInterval(0, 75, 3, 3)
  149. for p := list.Front(); p != nil; p = p.Next {
  150. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  151. }
  152. assert.Equal(t, 3, list.Len())
  153. }
  154. func TestIntervalList_insertInterval4(t *testing.T) {
  155. list := NewIntervalList[IntervalInt]()
  156. list.InsertInterval(200, 250, 4, 4)
  157. list.InsertInterval(0, 225, 3, 3)
  158. for p := list.Front(); p != nil; p = p.Next {
  159. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  160. }
  161. assert.Equal(t, 2, list.Len())
  162. }
  163. func TestIntervalList_insertInterval5(t *testing.T) {
  164. list := NewIntervalList[IntervalInt]()
  165. list.InsertInterval(200, 250, 4, 4)
  166. list.InsertInterval(0, 225, 5, 5)
  167. for p := list.Front(); p != nil; p = p.Next {
  168. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  169. }
  170. assert.Equal(t, 2, list.Len())
  171. }
  172. func TestIntervalList_insertInterval6(t *testing.T) {
  173. list := NewIntervalList[IntervalInt]()
  174. list.InsertInterval(50, 150, 2, 2)
  175. list.InsertInterval(200, 250, 4, 4)
  176. list.InsertInterval(0, 275, 1, 1)
  177. for p := list.Front(); p != nil; p = p.Next {
  178. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  179. }
  180. assert.Equal(t, 5, list.Len())
  181. }
  182. func TestIntervalList_insertInterval7(t *testing.T) {
  183. list := NewIntervalList[IntervalInt]()
  184. list.InsertInterval(50, 150, 2, 2)
  185. list.InsertInterval(200, 250, 4, 4)
  186. list.InsertInterval(75, 275, 1, 1)
  187. for p := list.Front(); p != nil; p = p.Next {
  188. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  189. }
  190. assert.Equal(t, 4, list.Len())
  191. }
  192. func TestIntervalList_insertInterval8(t *testing.T) {
  193. list := NewIntervalList[IntervalInt]()
  194. list.InsertInterval(50, 150, 2, 2)
  195. list.InsertInterval(200, 250, 4, 4)
  196. list.InsertInterval(75, 275, 3, 3)
  197. for p := list.Front(); p != nil; p = p.Next {
  198. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  199. }
  200. assert.Equal(t, 4, list.Len())
  201. }
  202. func TestIntervalList_insertInterval9(t *testing.T) {
  203. list := NewIntervalList[IntervalInt]()
  204. list.InsertInterval(50, 150, 2, 2)
  205. list.InsertInterval(200, 250, 4, 4)
  206. list.InsertInterval(50, 150, 3, 3)
  207. for p := list.Front(); p != nil; p = p.Next {
  208. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  209. }
  210. assert.Equal(t, 2, list.Len())
  211. }
  212. func TestIntervalList_insertInterval10(t *testing.T) {
  213. list := NewIntervalList[IntervalInt]()
  214. list.InsertInterval(50, 100, 2, 2)
  215. list.InsertInterval(200, 300, 4, 4)
  216. list.InsertInterval(100, 200, 5, 5)
  217. for p := list.Front(); p != nil; p = p.Next {
  218. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  219. }
  220. assert.Equal(t, 3, list.Len())
  221. }
  222. func TestIntervalList_insertInterval11(t *testing.T) {
  223. list := NewIntervalList[IntervalInt]()
  224. list.InsertInterval(0, 64, 1, 1)
  225. list.InsertInterval(72, 136, 3, 3)
  226. list.InsertInterval(64, 128, 2, 2)
  227. list.InsertInterval(68, 72, 4, 4)
  228. for p := list.Front(); p != nil; p = p.Next {
  229. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  230. }
  231. assert.Equal(t, 4, list.Len())
  232. }
  233. type IntervalStruct struct {
  234. x int
  235. start int64
  236. stop int64
  237. }
  238. func newIntervalStruct(i int) IntervalStruct {
  239. return IntervalStruct{
  240. x: i,
  241. }
  242. }
  243. func (i IntervalStruct) SetStartStop(start, stop int64) {
  244. i.start, i.stop = start, stop
  245. }
  246. func (i IntervalStruct) Clone() IntervalValue {
  247. return &IntervalStruct{
  248. x: i.x,
  249. start: i.start,
  250. stop: i.stop,
  251. }
  252. }
  253. func TestIntervalList_insertIntervalStruct(t *testing.T) {
  254. list := NewIntervalList[IntervalStruct]()
  255. list.InsertInterval(0, 64, 1, newIntervalStruct(1))
  256. list.InsertInterval(64, 72, 2, newIntervalStruct(2))
  257. list.InsertInterval(72, 136, 3, newIntervalStruct(3))
  258. list.InsertInterval(64, 68, 4, newIntervalStruct(4))
  259. for p := list.Front(); p != nil; p = p.Next {
  260. fmt.Printf("[%d,%d) %d %d\n", p.StartOffset, p.StopOffset, p.TsNs, p.Value)
  261. }
  262. assert.Equal(t, 4, list.Len())
  263. }