blocks_test.go 3.7 KB

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