weakhash_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // The existence of this file means we get 0% test coverage rather than no
  7. // test coverage at all. Remove when implementing an actual test.
  8. package weakhash
  9. import (
  10. "bytes"
  11. "context"
  12. "io"
  13. "io/ioutil"
  14. "os"
  15. "reflect"
  16. "testing"
  17. )
  18. var payload = []byte("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
  19. func TestFinder(t *testing.T) {
  20. f, err := ioutil.TempFile("", "")
  21. if err != nil {
  22. t.Error(err)
  23. }
  24. defer os.Remove(f.Name())
  25. defer f.Close()
  26. if _, err := f.Write(payload); err != nil {
  27. t.Error(err)
  28. }
  29. if _, err := f.Seek(0, io.SeekStart); err != nil {
  30. t.Error(err)
  31. }
  32. hashes := []uint32{65143183, 65798547}
  33. finder, err := NewFinder(context.Background(), f, 4, hashes)
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. expected := map[uint32][]int64{
  38. 65143183: {1, 27, 53, 79},
  39. 65798547: {2, 28, 54, 80},
  40. }
  41. actual := make(map[uint32][]int64)
  42. b := make([]byte, Size)
  43. for _, hash := range hashes {
  44. _, err := finder.Iterate(hash, b[:4], func(offset int64) bool {
  45. if !bytes.Equal(b, payload[offset:offset+4]) {
  46. t.Errorf("Not equal at %d: %s != %s", offset, string(b), string(payload[offset:offset+4]))
  47. }
  48. actual[hash] = append(actual[hash], offset)
  49. return true
  50. })
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. }
  55. if !reflect.DeepEqual(actual, expected) {
  56. t.Errorf("Not equal: %#v != %#v", actual, expected)
  57. }
  58. }