gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hash_helpers.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Arteris, Inc. and its applicable licensors and
3  * affiliates.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <gtest/gtest.h>
31 
32 #include <string>
33 #include <tuple>
34 #include <unordered_map>
35 #include <utility>
36 #include <vector>
37 
39 
40 using namespace gem5;
41 
42 TEST(HashHelpers, isHashEnabled)
43 {
44  EXPECT_TRUE(stl_helpers::is_hash_enabled<int>);
45  EXPECT_TRUE(stl_helpers::is_hash_enabled<long>);
46  EXPECT_TRUE(stl_helpers::is_hash_enabled<double>);
47  EXPECT_TRUE(stl_helpers::is_hash_enabled<std::string>);
48  EXPECT_TRUE(stl_helpers::is_hash_enabled<void*>);
49  using vector_t = std::vector<int>;
50  EXPECT_TRUE(stl_helpers::is_hash_enabled<vector_t>);
51  using tuple_t = std::tuple<int, bool, int**, std::string(*)(float)>;
52  EXPECT_TRUE(stl_helpers::is_hash_enabled<tuple_t>);
54  EXPECT_TRUE((stl_helpers::is_hash_enabled<
55  std::unordered_map<tuple_t, vector_t>>));
56 }
57 
58 // The following tests do not test the hash value as it is considered an
59 // implementation detail and there is no contract on the way that value is
60 // computed. Testing for hash quality is extremelly computationnaly intensive
61 // and is not suitable for unit tests. Consider these tests to be more of a
62 // "does it compile?" check as well as a small set of examples for the user.
63 TEST(HashHelpers, hashPair)
64 {
65  auto p = std::make_pair(1, std::string("hello"));
66  auto hashVal = stl_helpers::hash_value(p);
67  auto hashFunc = stl_helpers::hash<std::pair<int, std::string>>{};
68  EXPECT_EQ(hashVal, hashFunc(p));
69 }
70 
71 TEST(HashHelpers, hashTuple)
72 {
73  auto t = std::make_tuple(1, "hello", 4.2, std::make_pair(true, 0.f));
74  auto hashVal = stl_helpers::hash_value(t);
75  auto hashFunc = stl_helpers::hash<decltype(t)>{};
76  EXPECT_EQ(hashVal, hashFunc(t));
77 }
78 
79 TEST(HashHelpers, hashVector)
80 {
81  auto v = std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9};
82  auto hashVal = stl_helpers::hash_value(v);
83  auto hashFunc = stl_helpers::hash<decltype(v)>{};
84  EXPECT_EQ(hashVal, hashFunc(v));
85 }
gem5::VegaISA::f
Bitfield< 56 > f
Definition: pagetable.hh:53
hash_helpers.hh
std::vector< int >
gem5::stl_helpers::hash_impl::is_hash_enabled
constexpr bool is_hash_enabled
Definition: hash_helpers.hh:159
TEST
TEST(HashHelpers, isHashEnabled)
Definition: hash_helpers.test.cc:42
gem5::VegaISA::t
Bitfield< 51 > t
Definition: pagetable.hh:56
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
std::pair
STL pair class.
Definition: stl.hh:58
gem5::stl_helpers::hash_impl::hash_value
constexpr auto hash_value(const T &v)
Definition: hash_helpers.hh:111
gem5::VegaISA::v
Bitfield< 0 > v
Definition: pagetable.hh:65
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17