2
0

hash_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 2010-2023 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[bx::HashCrc32::Count];
  29. uint32_t adler32;
  30. uint32_t murmur2a;
  31. const char* input;
  32. };
  33. const HashTest s_hashTest[] =
  34. {
  35. // Crc32 | Adler32 | Murmur2A | Input
  36. // Ieee Castagnoli Koopman | | |
  37. { { 0, 0, 0 }, 1, 0, "" },
  38. { { 0xe8b7be43, 0xc1d04330, 0x0da2aa8a }, 0x00620062, 0x0803888b, "a" },
  39. { { 0x9e83486d, 0xe2a22936, 0x31ec935a }, 0x012600c4, 0x618515af, "ab" },
  40. { { 0xc340daab, 0x49e1b6e3, 0x945a1e78 }, 0x06060205, 0x94e3dc4d, "abvgd" },
  41. { { 0x07642fe2, 0x45a04162, 0x3d4bf72d }, 0x020a00d6, 0xe602fc07, "1389" },
  42. { { 0x26d75737, 0xb73d7b80, 0xd524eb40 }, 0x04530139, 0x58d37863, "555333" },
  43. };
  44. TEST_CASE("HashCrc32", "")
  45. {
  46. #if 0
  47. makeCrcTable(0xedb88320);
  48. printf("\n");
  49. makeCrcTable(0x82f63b78);
  50. printf("\n");
  51. makeCrcTable(0xeb31d82e);
  52. #endif // 0
  53. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  54. {
  55. const HashTest& test = s_hashTest[ii];
  56. for (uint32_t jj = 0; jj < bx::HashCrc32::Count; ++jj)
  57. {
  58. bx::HashCrc32 hash;
  59. hash.begin(bx::HashCrc32::Enum(jj) );
  60. hash.add(test.input, bx::strLen(test.input) );
  61. REQUIRE(test.crc32[jj] == hash.end() );
  62. }
  63. }
  64. }
  65. TEST_CASE("HashAdler32", "")
  66. {
  67. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  68. {
  69. const HashTest& test = s_hashTest[ii];
  70. bx::HashAdler32 hash;
  71. hash.begin();
  72. hash.add(test.input, bx::strLen(test.input) );
  73. REQUIRE(test.adler32 == hash.end() );
  74. }
  75. }
  76. /*-----------------------------------------------------------------------------
  77. // MurmurHash2A, by Austin Appleby
  78. //
  79. // This is a variant of MurmurHash2 modified to use the Merkle-Damgard
  80. // construction. Bulk speed should be identical to Murmur2, small-key speed
  81. // will be 10%-20% slower due to the added overhead at the end of the hash.
  82. //
  83. // This variant fixes a minor issue where null keys were more likely to
  84. // collide with each other than expected, and also makes the function
  85. // more amenable to incremental implementations.
  86. */
  87. #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  88. uint32_t MurmurHash2A(const void * key, int len, uint32_t seed = 0)
  89. {
  90. const uint32_t m = 0x5bd1e995;
  91. const int r = 24;
  92. uint32_t l = len;
  93. const unsigned char * data = (const unsigned char *)key;
  94. uint32_t h = seed;
  95. while(len >= 4)
  96. {
  97. uint32_t k = *(uint32_t*)data;
  98. mmix(h,k);
  99. data += 4;
  100. len -= 4;
  101. }
  102. uint32_t t = 0;
  103. switch(len)
  104. {
  105. case 3: t ^= data[2] << 16; BX_FALLTHROUGH;
  106. case 2: t ^= data[1] << 8; BX_FALLTHROUGH;
  107. case 1: t ^= data[0];
  108. };
  109. mmix(h,t);
  110. mmix(h,l);
  111. h ^= h >> 13;
  112. h *= m;
  113. h ^= h >> 15;
  114. return h;
  115. }
  116. TEST_CASE("HashMurmur2A", "")
  117. {
  118. uint32_t seed = 0;
  119. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  120. {
  121. const HashTest& test = s_hashTest[ii];
  122. bx::HashMurmur2A hash;
  123. hash.begin(seed);
  124. hash.add(test.input, bx::strLen(test.input) );
  125. REQUIRE(test.murmur2a == hash.end() );
  126. REQUIRE(test.murmur2a == MurmurHash2A(test.input, bx::strLen(test.input), seed) );
  127. }
  128. }
  129. TEST_CASE("HashMurmur2A-Separate-Add", "")
  130. {
  131. bx::HashMurmur2A hash;
  132. hash.begin();
  133. hash.add("0123456789");
  134. hash.add("abvgd012345");
  135. REQUIRE(MurmurHash2A("0123456789abvgd012345", 21) == hash.end() );
  136. }