TestHashFunctions.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/HashFunctions.h>
  8. #include <AK/Types.h>
  9. TEST_CASE(int_hash)
  10. {
  11. static_assert(int_hash(42) == 3564735745u);
  12. static_assert(int_hash(0) == 1177991625u);
  13. }
  14. TEST_CASE(rehash_for_collision)
  15. {
  16. static_assert(rehash_for_collision(666) == 171644115u);
  17. static_assert(rehash_for_collision(0) == 1189591134u);
  18. static_assert(rehash_for_collision(0xBA5EDB01) == 0u);
  19. }
  20. TEST_CASE(pair_int_hash)
  21. {
  22. static_assert(pair_int_hash(42, 17) == 339337046u);
  23. static_assert(pair_int_hash(0, 0) == 954888656u);
  24. }
  25. TEST_CASE(u64_hash)
  26. {
  27. static_assert(u64_hash(42) == 2824066580u);
  28. static_assert(u64_hash(0) == 954888656u);
  29. }
  30. TEST_CASE(ptr_hash)
  31. {
  32. // These tests are not static_asserts because the values are
  33. // different and the goal is to bind the behavior.
  34. if constexpr (sizeof(FlatPtr) == 8) {
  35. EXPECT_EQ(ptr_hash(FlatPtr(42)), 2824066580u);
  36. EXPECT_EQ(ptr_hash(FlatPtr(0)), 954888656u);
  37. EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(42)), 2824066580u);
  38. EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(0)), 954888656u);
  39. } else {
  40. EXPECT_EQ(ptr_hash(FlatPtr(42)), 3564735745u);
  41. EXPECT_EQ(ptr_hash(FlatPtr(0)), 1177991625u);
  42. EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(42)), 3564735745u);
  43. EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(0)), 1177991625u);
  44. }
  45. }
  46. TEST_CASE(constexpr_ptr_hash)
  47. {
  48. // This test does not check the result because the goal is just to
  49. // ensure the function can be executed in a constexpr context. The
  50. // "ptr_hash" test binds the result.
  51. static_assert(ptr_hash(FlatPtr(42)));
  52. }