blocks_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package model
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. )
  7. var blocksTestData = []struct {
  8. data []byte
  9. blocksize int
  10. hash []string
  11. }{
  12. {[]byte(""), 1024, []string{}},
  13. {[]byte("contents"), 1024, []string{
  14. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  15. {[]byte("contents"), 9, []string{
  16. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  17. {[]byte("contents"), 8, []string{
  18. "d1b2a59fbea7e20077af9f91b27e95e865061b270be03ff539ab3b73587882e8"}},
  19. {[]byte("contents"), 7, []string{
  20. "ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
  21. "043a718774c572bd8a25adbeb1bfcd5c0256ae11cecf9f9c3f925d0e52beaf89"},
  22. },
  23. {[]byte("contents"), 3, []string{
  24. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  25. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  26. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  27. },
  28. {[]byte("conconts"), 3, []string{
  29. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  30. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  31. "44ad63f60af0f6db6fdde6d5186ef78176367df261fa06be3079b6c80c8adba4"},
  32. },
  33. {[]byte("contenten"), 3, []string{
  34. "1143da2bc54c495c4be31d3868785d39ffdfd56df5668f0645d8f14d47647952",
  35. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3",
  36. "e4432baa90819aaef51d2a7f8e148bf7e679610f3173752fabb4dcb2d0f418d3"},
  37. },
  38. }
  39. func TestBlocks(t *testing.T) {
  40. for _, test := range blocksTestData {
  41. buf := bytes.NewBuffer(test.data)
  42. blocks, err := Blocks(buf, test.blocksize)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if l := len(blocks); l != len(test.hash) {
  47. t.Fatalf("Incorrect number of blocks %d != %d", l, len(test.hash))
  48. } else {
  49. i := 0
  50. for off := int64(0); off < int64(len(test.data)); off += int64(test.blocksize) {
  51. if blocks[i].Offset != off {
  52. t.Errorf("Incorrect offset for block %d: %d != %d", i, blocks[i].Offset, off)
  53. }
  54. bs := test.blocksize
  55. if rem := len(test.data) - int(off); bs > rem {
  56. bs = rem
  57. }
  58. if int(blocks[i].Size) != bs {
  59. t.Errorf("Incorrect length for block %d: %d != %d", i, blocks[i].Size, bs)
  60. }
  61. if h := fmt.Sprintf("%x", blocks[i].Hash); h != test.hash[i] {
  62. t.Errorf("Incorrect block hash %q != %q", h, test.hash[i])
  63. }
  64. i++
  65. }
  66. }
  67. }
  68. }
  69. var diffTestData = []struct {
  70. a string
  71. b string
  72. s int
  73. d []Block
  74. }{
  75. {"contents", "contents", 1024, []Block{}},
  76. {"", "", 1024, []Block{}},
  77. {"contents", "contents", 3, []Block{}},
  78. {"contents", "cantents", 3, []Block{{0, 3, nil}}},
  79. {"contents", "contants", 3, []Block{{3, 3, nil}}},
  80. {"contents", "cantants", 3, []Block{{0, 3, nil}, {3, 3, nil}}},
  81. {"contents", "", 3, nil},
  82. {"", "contents", 3, []Block{{0, 3, nil}, {3, 3, nil}, {6, 2, nil}}},
  83. {"con", "contents", 3, []Block{{3, 3, nil}, {6, 2, nil}}},
  84. {"contents", "con", 3, nil},
  85. {"contents", "cont", 3, []Block{{3, 1, nil}}},
  86. {"cont", "contents", 3, []Block{{3, 3, nil}, {6, 2, nil}}},
  87. }
  88. func TestDiff(t *testing.T) {
  89. for i, test := range diffTestData {
  90. a, _ := Blocks(bytes.NewBufferString(test.a), test.s)
  91. b, _ := Blocks(bytes.NewBufferString(test.b), test.s)
  92. _, d := BlockDiff(a, b)
  93. if len(d) != len(test.d) {
  94. t.Fatalf("Incorrect length for diff %d; %d != %d", i, len(d), len(test.d))
  95. } else {
  96. for j := range test.d {
  97. if d[j].Offset != test.d[j].Offset {
  98. t.Errorf("Incorrect offset for diff %d block %d; %d != %d", i, j, d[j].Offset, test.d[j].Offset)
  99. }
  100. if d[j].Size != test.d[j].Size {
  101. t.Errorf("Incorrect length for diff %d block %d; %d != %d", i, j, d[j].Size, test.d[j].Size)
  102. }
  103. }
  104. }
  105. }
  106. }