weakhash_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "io"
  12. "io/ioutil"
  13. "os"
  14. "reflect"
  15. "testing"
  16. )
  17. var payload = []byte("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
  18. func TestFinder(t *testing.T) {
  19. f, err := ioutil.TempFile("", "")
  20. if err != nil {
  21. t.Error(err)
  22. }
  23. defer os.Remove(f.Name())
  24. defer f.Close()
  25. if _, err := f.Write(payload); err != nil {
  26. t.Error(err)
  27. }
  28. if _, err := f.Seek(0, io.SeekStart); err != nil {
  29. t.Error(err)
  30. }
  31. hashes := []uint32{65143183, 65798547}
  32. finder, err := NewFinder(f, 4, hashes)
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. expected := map[uint32][]int64{
  37. 65143183: {1, 27, 53, 79},
  38. 65798547: {2, 28, 54, 80},
  39. }
  40. actual := make(map[uint32][]int64)
  41. b := make([]byte, Size)
  42. for _, hash := range hashes {
  43. _, err := finder.Iterate(hash, b[:4], func(offset int64) bool {
  44. if !bytes.Equal(b, payload[offset:offset+4]) {
  45. t.Errorf("Not equal at %d: %s != %s", offset, string(b), string(payload[offset:offset+4]))
  46. }
  47. actual[hash] = append(actual[hash], offset)
  48. return true
  49. })
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. }
  54. if !reflect.DeepEqual(actual, expected) {
  55. t.Errorf("Not equal: %#v != %#v", actual, expected)
  56. }
  57. }