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.

89 lines
1.9 KiB

5 years ago
  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. // 1,
  25. c.AddInterval(getBytes(1, 1), 0)
  26. // _, 2,
  27. c.AddInterval(getBytes(2, 1), 1)
  28. // _, _, 3, 3, 3
  29. c.AddInterval(getBytes(3, 3), 2)
  30. // _, _, _, 4, 4, 4
  31. c.AddInterval(getBytes(4, 3), 3)
  32. expectedData(t, c, 0, 1, 2, 3, 4, 4, 4)
  33. }
  34. func TestContinuousIntervals_RealCase1(t *testing.T) {
  35. c := &ContinuousIntervals{}
  36. // 25,
  37. c.AddInterval(getBytes(25, 1), 0)
  38. // _, _, _, _, 23, 23
  39. c.AddInterval(getBytes(23, 2), 4)
  40. // _, _, _, 24, 24, 24, 24
  41. c.AddInterval(getBytes(24, 4), 3)
  42. // _, 22, 22
  43. c.AddInterval(getBytes(22, 2), 1)
  44. expectedData(t, c, 0, 25, 22, 22, 24, 24, 24, 24)
  45. }
  46. func expectedData(t *testing.T, c *ContinuousIntervals, offset int, data ...byte) {
  47. start, stop := int64(offset), int64(offset+len(data))
  48. for _, list := range c.lists {
  49. nodeStart, nodeStop := max(start, list.Head.Offset), min(stop, list.Head.Offset+list.Size())
  50. if nodeStart < nodeStop {
  51. buf := make([]byte, nodeStop-nodeStart)
  52. list.ReadData(buf, nodeStart, nodeStop)
  53. if bytes.Compare(buf, data[nodeStart-start:nodeStop-start]) != 0 {
  54. t.Errorf("expected %v actual %v", data[nodeStart-start:nodeStop-start], buf)
  55. }
  56. }
  57. }
  58. }
  59. func getBytes(content byte, length int) []byte {
  60. data := make([]byte, length)
  61. for i := 0; i < length; i++ {
  62. data[i] = content
  63. }
  64. return data
  65. }