hash_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2010-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/hash.h>
  7. void makeCrcTable(uint32_t _poly)
  8. {
  9. for (uint32_t ii = 0; ii < 256; ++ii)
  10. {
  11. uint32_t crc = ii;
  12. for (uint32_t jj = 0; jj < 8; ++jj)
  13. {
  14. if (1 == (crc & 1) )
  15. {
  16. crc = (crc >> 1) ^ _poly;
  17. }
  18. else
  19. {
  20. crc >>= 1;
  21. }
  22. }
  23. printf("0x%08x,%c", crc, 7 == (ii & 7) ? '\n' : ' ');
  24. }
  25. }
  26. struct HashTest
  27. {
  28. uint32_t crc32;
  29. uint32_t adler32;
  30. uint32_t murmur2a;
  31. const char* input;
  32. };
  33. const HashTest s_hashTest[] =
  34. {
  35. { 0, 1, 0, "" },
  36. { 0xe8b7be43, 0x00620062, 0x803888b, "a" },
  37. { 0x9e83486d, 0x012600c4, 0x618515af, "ab" },
  38. { 0xc340daab, 0x06060205, 0x94e3dc4d, "abvgd" },
  39. { 0x07642fe2, 0x020a00d6, 0xe602fc07, "1389" },
  40. { 0x26d75737, 0x04530139, 0x58d37863, "555333" },
  41. };
  42. TEST_CASE("HashCrc32", "")
  43. {
  44. #if 0
  45. makeCrcTable(0xedb88320);
  46. printf("\n");
  47. makeCrcTable(0x82f63b78);
  48. printf("\n");
  49. makeCrcTable(0xeb31d82e);
  50. #endif // 0
  51. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  52. {
  53. const HashTest& test = s_hashTest[ii];
  54. bx::HashCrc32 hash;
  55. hash.begin();
  56. hash.add(test.input, bx::strLen(test.input) );
  57. REQUIRE(test.crc32 == hash.end() );
  58. }
  59. }
  60. TEST_CASE("HashAdler32", "")
  61. {
  62. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  63. {
  64. const HashTest& test = s_hashTest[ii];
  65. bx::HashAdler32 hash;
  66. hash.begin();
  67. hash.add(test.input, bx::strLen(test.input) );
  68. REQUIRE(test.adler32 == hash.end() );
  69. }
  70. }
  71. /*-----------------------------------------------------------------------------
  72. // MurmurHash2A, by Austin Appleby
  73. //
  74. // This is a variant of MurmurHash2 modified to use the Merkle-Damgard
  75. // construction. Bulk speed should be identical to Murmur2, small-key speed
  76. // will be 10%-20% slower due to the added overhead at the end of the hash.
  77. //
  78. // This variant fixes a minor issue where null keys were more likely to
  79. // collide with each other than expected, and also makes the function
  80. // more amenable to incremental implementations.
  81. */
  82. #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  83. uint32_t MurmurHash2A(const void * key, int len, uint32_t seed = 0)
  84. {
  85. const uint32_t m = 0x5bd1e995;
  86. const int r = 24;
  87. uint32_t l = len;
  88. const unsigned char * data = (const unsigned char *)key;
  89. uint32_t h = seed;
  90. while(len >= 4)
  91. {
  92. uint32_t k = *(uint32_t*)data;
  93. mmix(h,k);
  94. data += 4;
  95. len -= 4;
  96. }
  97. uint32_t t = 0;
  98. switch(len)
  99. {
  100. case 3: t ^= data[2] << 16; BX_FALLTHROUGH;
  101. case 2: t ^= data[1] << 8; BX_FALLTHROUGH;
  102. case 1: t ^= data[0];
  103. };
  104. mmix(h,t);
  105. mmix(h,l);
  106. h ^= h >> 13;
  107. h *= m;
  108. h ^= h >> 15;
  109. return h;
  110. }
  111. TEST_CASE("HashMurmur2A", "")
  112. {
  113. uint32_t seed = 0;
  114. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  115. {
  116. const HashTest& test = s_hashTest[ii];
  117. bx::HashMurmur2A hash;
  118. hash.begin(seed);
  119. hash.add(test.input, bx::strLen(test.input) );
  120. REQUIRE(test.murmur2a == hash.end() );
  121. REQUIRE(test.murmur2a == MurmurHash2A(test.input, bx::strLen(test.input), seed) );
  122. }
  123. }
  124. TEST_CASE("HashMurmur2A-Separate-Add", "")
  125. {
  126. bx::HashMurmur2A hash;
  127. hash.begin();
  128. hash.add("0123456789");
  129. hash.add("abvgd012345");
  130. REQUIRE(MurmurHash2A("0123456789abvgd012345", 21) == hash.end() );
  131. }