murmur_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package murmur
  2. import (
  3. "strconv"
  4. "testing"
  5. )
  6. // Test the implementation of murmur3
  7. func TestMurmur3H1(t *testing.T) {
  8. // these examples are based on adding a index number to a sample string in
  9. // a loop. The expected values were generated by the java datastax murmur3
  10. // implementation. The number of examples here of increasing lengths ensure
  11. // test coverage of all tail-length branches in the murmur3 algorithm
  12. seriesExpected := [...]uint64{
  13. 0x0000000000000000, // ""
  14. 0x2ac9debed546a380, // "0"
  15. 0x649e4eaa7fc1708e, // "01"
  16. 0xce68f60d7c353bdb, // "012"
  17. 0x0f95757ce7f38254, // "0123"
  18. 0x0f04e459497f3fc1, // "01234"
  19. 0x88c0a92586be0a27, // "012345"
  20. 0x13eb9fb82606f7a6, // "0123456"
  21. 0x8236039b7387354d, // "01234567"
  22. 0x4c1e87519fe738ba, // "012345678"
  23. 0x3f9652ac3effeb24, // "0123456789"
  24. 0x3f33760ded9006c6, // "01234567890"
  25. 0xaed70a6631854cb1, // "012345678901"
  26. 0x8a299a8f8e0e2da7, // "0123456789012"
  27. 0x624b675c779249a6, // "01234567890123"
  28. 0xa4b203bb1d90b9a3, // "012345678901234"
  29. 0xa3293ad698ecb99a, // "0123456789012345"
  30. 0xbc740023dbd50048, // "01234567890123456"
  31. 0x3fe5ab9837d25cdd, // "012345678901234567"
  32. 0x2d0338c1ca87d132, // "0123456789012345678"
  33. }
  34. sample := ""
  35. for i, expected := range seriesExpected {
  36. assertMurmur3H1(t, []byte(sample), expected)
  37. sample = sample + strconv.Itoa(i%10)
  38. }
  39. // Here are some test examples from other driver implementations
  40. assertMurmur3H1(t, []byte("hello"), 0xcbd8a7b341bd9b02)
  41. assertMurmur3H1(t, []byte("hello, world"), 0x342fac623a5ebc8e)
  42. assertMurmur3H1(t, []byte("19 Jan 2038 at 3:14:07 AM"), 0xb89e5988b737affc)
  43. assertMurmur3H1(t, []byte("The quick brown fox jumps over the lazy dog."), 0xcd99481f9ee902c9)
  44. }
  45. // helper function for testing the murmur3 implementation
  46. func assertMurmur3H1(t *testing.T, data []byte, expected uint64) {
  47. actual := Murmur3H1(data)
  48. if actual != expected {
  49. t.Errorf("Expected h1 = %x for data = %x, but was %x", expected, data, actual)
  50. }
  51. }
  52. // Benchmark of the performance of the murmur3 implementation
  53. func BenchmarkMurmur3H1(b *testing.B) {
  54. data := make([]byte, 1024)
  55. for i := 0; i < 1024; i++ {
  56. data[i] = byte(i)
  57. }
  58. b.ResetTimer()
  59. b.RunParallel(func(pb *testing.PB) {
  60. for pb.Next() {
  61. h1 := Murmur3H1(data)
  62. if h1 != 7627370222079200297 {
  63. b.Fatalf("expected %d got %d", 7627370222079200297, h1)
  64. }
  65. }
  66. })
  67. }