You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB

  1. package filesys
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestContinuousIntervals_AddIntervalAppend(t *testing.T) {
  7. c := &ContinuousIntervals{}
  8. // 25, 25, 25
  9. c.AddInterval(getBytes(25, 3), 0)
  10. // _, _, 23, 23, 23, 23
  11. c.AddInterval(getBytes(23, 4), 2)
  12. expectedData(t, c, 0, 25, 25, 23, 23, 23, 23)
  13. }
  14. func TestContinuousIntervals_AddIntervalInnerOverwrite(t *testing.T) {
  15. c := &ContinuousIntervals{}
  16. // 25, 25, 25, 25, 25
  17. c.AddInterval(getBytes(25, 5), 0)
  18. // _, _, 23, 23
  19. c.AddInterval(getBytes(23, 2), 2)
  20. expectedData(t, c, 0, 25, 25, 23, 23, 25)
  21. }
  22. func TestContinuousIntervals_AddIntervalFullOverwrite(t *testing.T) {
  23. c := &ContinuousIntervals{}
  24. // 25,
  25. c.AddInterval(getBytes(25, 1), 0)
  26. // _, _, _, _, 23, 23
  27. c.AddInterval(getBytes(23, 2), 4)
  28. // _, _, _, 24, 24, 24, 24
  29. c.AddInterval(getBytes(24, 4), 3)
  30. // _, 22, 22
  31. c.AddInterval(getBytes(22, 2), 1)
  32. expectedData(t, c, 0, 25, 22, 22, 24, 24, 24, 24)
  33. }
  34. func expectedData(t *testing.T, c *ContinuousIntervals, offset int, data ...byte) {
  35. start, stop := int64(offset), int64(offset+len(data))
  36. for _, list := range c.lists {
  37. nodeStart, nodeStop := max(start, list.Head.Offset), min(stop, list.Head.Offset+list.Size())
  38. if nodeStart < nodeStop {
  39. buf := make([]byte, nodeStop-nodeStart)
  40. list.ReadData(buf, nodeStart, nodeStop)
  41. if bytes.Compare(buf, data[nodeStart-start:nodeStop-start]) != 0 {
  42. t.Errorf("expected %v actual %v", data[nodeStart-start:nodeStop-start], buf)
  43. }
  44. }
  45. }
  46. }
  47. func getBytes(content byte, length int) []byte {
  48. data := make([]byte, length)
  49. for i := 0; i < length; i++ {
  50. data[i] = content
  51. }
  52. return data
  53. }