BLI_hash_mm2a_test.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. extern "C" {
  4. #include "BLI_hash_mm2a.h"
  5. }
  6. /* Note: Reference results are taken from reference implementation
  7. * (cpp code, CMurmurHash2A variant):
  8. * https://smhasher.googlecode.com/svn-history/r130/trunk/MurmurHash2.cpp
  9. */
  10. TEST(hash_mm2a, MM2ABasic)
  11. {
  12. BLI_HashMurmur2A mm2;
  13. const char *data = "Blender";
  14. BLI_hash_mm2a_init(&mm2, 0);
  15. BLI_hash_mm2a_add(&mm2, (const unsigned char *)data, strlen(data));
  16. #ifdef __LITTLE_ENDIAN__
  17. EXPECT_EQ(BLI_hash_mm2a_end(&mm2), 1633988145);
  18. #else
  19. EXPECT_EQ(BLI_hash_mm2a_end(&mm2), 959283772);
  20. #endif
  21. }
  22. TEST(hash_mm2a, MM2AConcatenateStrings)
  23. {
  24. BLI_HashMurmur2A mm2;
  25. uint32_t hash;
  26. const char *data1 = "Blender";
  27. const char *data2 = " is ";
  28. const char *data3 = "FaNtAsTiC";
  29. const char *data123 = "Blender is FaNtAsTiC";
  30. BLI_hash_mm2a_init(&mm2, 0);
  31. BLI_hash_mm2a_add(&mm2, (const unsigned char *)data1, strlen(data1));
  32. BLI_hash_mm2a_add(&mm2, (const unsigned char *)data2, strlen(data2));
  33. BLI_hash_mm2a_add(&mm2, (const unsigned char *)data3, strlen(data3));
  34. hash = BLI_hash_mm2a_end(&mm2);
  35. BLI_hash_mm2a_init(&mm2, 0);
  36. BLI_hash_mm2a_add(&mm2, (const unsigned char *)data123, strlen(data123));
  37. #ifdef __LITTLE_ENDIAN__
  38. EXPECT_EQ(hash, 1545105348);
  39. #else
  40. EXPECT_EQ(hash, 2604964730);
  41. #endif
  42. EXPECT_EQ(BLI_hash_mm2a_end(&mm2), hash);
  43. }
  44. TEST(hash_mm2a, MM2AIntegers)
  45. {
  46. BLI_HashMurmur2A mm2;
  47. uint32_t hash;
  48. const int ints[4] = {1, 2, 3, 4};
  49. BLI_hash_mm2a_init(&mm2, 0);
  50. BLI_hash_mm2a_add_int(&mm2, ints[0]);
  51. BLI_hash_mm2a_add_int(&mm2, ints[1]);
  52. BLI_hash_mm2a_add_int(&mm2, ints[2]);
  53. BLI_hash_mm2a_add_int(&mm2, ints[3]);
  54. hash = BLI_hash_mm2a_end(&mm2);
  55. BLI_hash_mm2a_init(&mm2, 0);
  56. BLI_hash_mm2a_add(&mm2, (const unsigned char *)ints, sizeof(ints));
  57. /* Yes, same hash here on little and big endian. */
  58. #ifdef __LITTLE_ENDIAN__
  59. EXPECT_EQ(hash, 405493096);
  60. #else
  61. EXPECT_EQ(hash, 405493096);
  62. #endif
  63. EXPECT_EQ(BLI_hash_mm2a_end(&mm2), hash);
  64. }