chunk_interval_list_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package page_writer
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func Test_PageChunkWrittenIntervalList(t *testing.T) {
  7. list := newChunkWrittenIntervalList()
  8. assert.Equal(t, 0, list.size(), "empty list")
  9. list.MarkWritten(0, 5, 1)
  10. assert.Equal(t, 1, list.size(), "one interval")
  11. list.MarkWritten(0, 5, 2)
  12. assert.Equal(t, 1, list.size(), "duplicated interval2")
  13. list.MarkWritten(95, 100, 3)
  14. assert.Equal(t, 2, list.size(), "two intervals")
  15. list.MarkWritten(50, 60, 4)
  16. assert.Equal(t, 3, list.size(), "three intervals")
  17. list.MarkWritten(50, 55, 5)
  18. assert.Equal(t, 4, list.size(), "three intervals merge")
  19. list.MarkWritten(40, 50, 6)
  20. assert.Equal(t, 5, list.size(), "three intervals grow forward")
  21. list.MarkWritten(50, 65, 7)
  22. assert.Equal(t, 4, list.size(), "three intervals grow backward")
  23. list.MarkWritten(70, 80, 8)
  24. assert.Equal(t, 5, list.size(), "four intervals")
  25. list.MarkWritten(60, 70, 9)
  26. assert.Equal(t, 6, list.size(), "three intervals merged")
  27. list.MarkWritten(59, 71, 10)
  28. assert.Equal(t, 6, list.size(), "covered three intervals")
  29. list.MarkWritten(5, 59, 11)
  30. assert.Equal(t, 5, list.size(), "covered two intervals")
  31. list.MarkWritten(70, 99, 12)
  32. assert.Equal(t, 5, list.size(), "covered one intervals")
  33. }
  34. type interval struct {
  35. start int64
  36. stop int64
  37. expected bool
  38. }
  39. func Test_PageChunkWrittenIntervalList1(t *testing.T) {
  40. list := newChunkWrittenIntervalList()
  41. inputs := []interval{
  42. {1, 5, true},
  43. {2, 3, true},
  44. }
  45. for i, input := range inputs {
  46. list.MarkWritten(input.start, input.stop, int64(i)+1)
  47. actual := hasData(list, 0, 4)
  48. if actual != input.expected {
  49. t.Errorf("input [%d,%d) expected %v actual %v", input.start, input.stop, input.expected, actual)
  50. }
  51. }
  52. }
  53. func hasData(usage *ChunkWrittenIntervalList, chunkStartOffset, x int64) bool {
  54. for t := usage.head.next; t != usage.tail; t = t.next {
  55. logicStart := chunkStartOffset + t.StartOffset
  56. logicStop := chunkStartOffset + t.stopOffset
  57. if logicStart <= x && x < logicStop {
  58. return true
  59. }
  60. }
  61. return false
  62. }