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.

40 lines
994 B

  1. package filesys
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestContinuousIntervals_AddInterval(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 expectedData(t *testing.T, c *ContinuousIntervals, offset int, data ...byte) {
  15. start, stop := int64(offset), int64(offset+len(data))
  16. for _, list := range c.lists {
  17. nodeStart, nodeStop := max(start, list.Head.Offset), min(stop, list.Head.Offset+list.Size())
  18. if nodeStart < nodeStop {
  19. buf := make([]byte, nodeStop-nodeStart)
  20. list.ReadData(buf, nodeStart, nodeStop)
  21. if bytes.Compare(buf, data[nodeStart-start:nodeStop-start]) != 0 {
  22. t.Errorf("expected %v actual %v", data[nodeStart-start:nodeStop-start], buf)
  23. }
  24. }
  25. }
  26. }
  27. func getBytes(content byte, length int) []byte {
  28. data := make([]byte, length)
  29. for i := 0; i < length; i++ {
  30. data[i] = content
  31. }
  32. return data
  33. }