gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
40using namespace gem5;
41
42TEST(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>);
53 EXPECT_TRUE((stl_helpers::is_hash_enabled<std::pair<vector_t, 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.
63TEST(HashHelpers, hashPair)
64{
65 auto p = std::make_pair(1, std::string("hello"));
66 auto hashVal = stl_helpers::hash_value(p);
68 EXPECT_EQ(hashVal, hashFunc(p));
69}
70
71TEST(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
79TEST(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}
STL pair class.
Definition stl.hh:58
STL vector class.
Definition stl.hh:37
TEST(HashHelpers, isHashEnabled)
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 5 > t
Definition misc_types.hh:71
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0