hash_test.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  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. const char* input;
  31. };
  32. const HashTest s_hashTest[] =
  33. {
  34. { 0, 1, "" },
  35. { 0xe8b7be43, 0x00620062, "a" },
  36. { 0x9e83486d, 0x012600c4, "ab" },
  37. { 0xc340daab, 0x06060205, "abvgd" },
  38. { 0x07642fe2, 0x020a00d6, "1389" },
  39. { 0x26d75737, 0x04530139, "555333" },
  40. };
  41. TEST_CASE("HashCrc32", "")
  42. {
  43. #if 0
  44. makeCrcTable(0xedb88320);
  45. printf("\n");
  46. makeCrcTable(0x82f63b78);
  47. printf("\n");
  48. makeCrcTable(0xeb31d82e);
  49. #endif // 0
  50. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  51. {
  52. const HashTest& test = s_hashTest[ii];
  53. bx::HashCrc32 hash;
  54. hash.begin();
  55. hash.add(test.input, bx::strLen(test.input) );
  56. REQUIRE(test.crc32 == hash.end() );
  57. }
  58. }
  59. TEST_CASE("HashAdler32", "")
  60. {
  61. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  62. {
  63. const HashTest& test = s_hashTest[ii];
  64. bx::HashAdler32 hash;
  65. hash.begin();
  66. hash.add(test.input, bx::strLen(test.input) );
  67. REQUIRE(test.adler32 == hash.end() );
  68. }
  69. }