Serenity Operating System
at master 55 lines 1.6 kB view raw
1/* 2 * Copyright (c) 2020, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8 9#include <AK/HashFunctions.h> 10#include <AK/Types.h> 11 12TEST_CASE(int_hash) 13{ 14 static_assert(int_hash(42) == 3564735745u); 15 static_assert(int_hash(0) == 1177991625u); 16} 17 18TEST_CASE(pair_int_hash) 19{ 20 static_assert(pair_int_hash(42, 17) == 339337046u); 21 static_assert(pair_int_hash(0, 0) == 954888656u); 22} 23 24TEST_CASE(u64_hash) 25{ 26 static_assert(u64_hash(42) == 2824066580u); 27 static_assert(u64_hash(0) == 954888656u); 28} 29 30TEST_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 38 EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(42)), 2824066580u); 39 EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(0)), 954888656u); 40 } else { 41 EXPECT_EQ(ptr_hash(FlatPtr(42)), 3564735745u); 42 EXPECT_EQ(ptr_hash(FlatPtr(0)), 1177991625u); 43 44 EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(42)), 3564735745u); 45 EXPECT_EQ(ptr_hash(reinterpret_cast<void const*>(0)), 1177991625u); 46 } 47} 48 49TEST_CASE(constexpr_ptr_hash) 50{ 51 // This test does not check the result because the goal is just to 52 // ensure the function can be executed in a constexpr context. The 53 // "ptr_hash" test binds the result. 54 static_assert(ptr_hash(FlatPtr(42))); 55}