murmurhash3_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package hash
  10. import (
  11. "testing"
  12. )
  13. var testData = []byte("Now is the time for all good men to come to the aid of their country")
  14. var resultArray1 = []uint32{
  15. 0x249cb285, 0xcae32c45, 0x49cc6fdd, 0x3c89b814, 0xdc9778bb, 0x6db6607a,
  16. 0x736df8ad, 0xd367e257, 0x59b32232, 0x2496a9b4, 0x01d69f33, 0x08454378,
  17. 0x4ad4f630, 0x0ae1ca05, 0x042bdb5b, 0xbf3592e8, 0x0ed8b048, 0xb86958db,
  18. 0xa74ca5b6, 0xb7982271, 0x10a77c40, 0x8caba8ef, 0xe5085ab6, 0x8ee964b8,
  19. 0x170f0222, 0x42dec76d, 0xc4ebe4e5, 0x3d246566, 0x64f1133e, 0x8a0597dd,
  20. 0x5b13cdb8, 0x1c723636, 0xc8b60a2f, 0xb572fe46, 0xb801f177, 0x71d44c64,
  21. 0x755aeff1, 0x66ba2eeb, 0x5cfec249, 0x5b9d603f, 0x4e916049, 0x07622306,
  22. 0x57d4271f, 0x3fa8e56a, 0x4b4fe703, 0x995e958d, 0xdaf48fbb, 0xbe381e68,
  23. 0xd4af5452, 0x6b8e4cdc, 0x3c7bbc57, 0xd834a3e0, 0x78665c77, 0x5ab0d747,
  24. 0x4b34afb7, 0xbce90104, 0x25a31264, 0xa348c314, 0xab9fb213, 0x48f40ea9,
  25. 0xa232f18e, 0xda12f11a, 0x7dcdfcfb, 0x24381ba8, 0x1a15737d, 0x32b1ea01,
  26. 0x7ed7f6c6, 0xd16ab3ed}
  27. func TestMurMurHashData(t *testing.T) {
  28. data := []byte{0xf6, 0x02, 0x03, 0x04}
  29. // Test invalid data boundaries
  30. _, err := MurMurHashData(data, 1, -3, 6)
  31. if err == nil {
  32. t.Error("Invalid boundaries should cause an error")
  33. } else if err.Error() != "Invalid data boundaries; offset: 1; size: -3" {
  34. t.Errorf("Unexpected error: %v", err)
  35. }
  36. _, err = MurMurHashData(data, 1, 5, 6)
  37. if err == nil {
  38. t.Error("Invalid boundaries should cause an error")
  39. } else if err.Error() != "Data out of bounds; set boundary: 4; data length: 4" {
  40. t.Errorf("Unexpected error: %v", err)
  41. }
  42. // Test against data
  43. // Go source code is always UTF-8, so the string literal is UTF-8 text.
  44. data = []byte("Now is the time for all good men to come to the aid of their country")
  45. doTest := func(offset, size int) uint32 {
  46. res, err := MurMurHashData(data, offset, size, 4)
  47. if err != nil {
  48. t.Errorf("Unexpected error: %v", err)
  49. }
  50. return res
  51. }
  52. for i := 0; i < len(resultArray1); i++ {
  53. res := doTest(0, i)
  54. if res != resultArray1[i] {
  55. t.Errorf("Unexpected result; Expected: 0x%x; Got: 0x%x", resultArray1[i], res)
  56. }
  57. }
  58. }