TestHashFunctions.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TestSuite.h>
  27. #include <AK/HashFunctions.h>
  28. #include <AK/Types.h>
  29. TEST_CASE(int_hash)
  30. {
  31. EXPECT_EQ(int_hash(42), 3564735745u);
  32. EXPECT_EQ(int_hash(0), 1177991625u);
  33. }
  34. TEST_CASE(double_hash)
  35. {
  36. EXPECT_EQ(double_hash(42), 524450u);
  37. EXPECT_EQ(double_hash(0), 12384u);
  38. }
  39. TEST_CASE(pair_int_hash)
  40. {
  41. EXPECT_EQ(pair_int_hash(42, 17), 339337046u);
  42. EXPECT_EQ(pair_int_hash(0, 0), 954888656u);
  43. }
  44. TEST_CASE(u64_hash)
  45. {
  46. EXPECT_EQ(u64_hash(42), 2824066580u);
  47. EXPECT_EQ(u64_hash(0), 954888656u);
  48. }
  49. TEST_CASE(ptr_hash)
  50. {
  51. if constexpr (sizeof(FlatPtr) == 8) {
  52. EXPECT_EQ(ptr_hash(FlatPtr(42)), 2824066580u);
  53. EXPECT_EQ(ptr_hash(FlatPtr(0)), 954888656u);
  54. EXPECT_EQ(ptr_hash(reinterpret_cast<const void*>(42)), 2824066580u);
  55. EXPECT_EQ(ptr_hash(reinterpret_cast<const void*>(0)), 954888656u);
  56. } else {
  57. EXPECT_EQ(ptr_hash(FlatPtr(42)), 3564735745u);
  58. EXPECT_EQ(ptr_hash(FlatPtr(0)), 1177991625u);
  59. EXPECT_EQ(ptr_hash(reinterpret_cast<const void*>(42)), 3564735745u);
  60. EXPECT_EQ(ptr_hash(reinterpret_cast<const void*>(0)), 1177991625u);
  61. }
  62. }
  63. TEST_MAIN(HashFunctions)