Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash_chain_stats_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
43#include <gtest/gtest.h>
44
45#include <cstddef>
46#include <utility>
47
48#include <tpl_dynSetHash.H>
49
50using namespace Aleph;
51
52namespace
53{
55 template <class Stats>
56 size_t num_chains(const Stats & st)
57 {
58 size_t count = 0;
59 for (size_t i = 0; i < st.lens.size(); ++i)
60 count += st.lens(i);
61 return count;
62 }
63
65 template <class Stats>
66 size_t num_entries(const Stats & st)
67 {
68 size_t count = 0;
69 for (size_t i = 0; i < st.lens.size(); ++i)
70 count += i * st.lens(i);
71 return count;
72 }
73
75 size_t constant_hash(const int &) noexcept { return 0; }
76
77 template <class Table>
78 class HashChainStats : public ::testing::Test
79 {
80 };
81
82 using Chained_Tables = ::testing::Types<DynSetHash<int>, DynSetLinHash<int>>;
83 TYPED_TEST_SUITE(HashChainStats, Chained_Tables);
84} // namespace
85
86TYPED_TEST(HashChainStats, EmptyTableHasOnlyEmptyChains)
87{
88 const TypeParam table;
89 const auto st = table.stats();
90 EXPECT_EQ(num_chains(st), table.capacity());
91 EXPECT_EQ(num_entries(st), 0u);
92 EXPECT_EQ(st.avg, 0.0f);
93}
94
95TYPED_TEST(HashChainStats, HistogramAccountsForEveryChainAndEntry)
96{
97 TypeParam table;
98 for (int i = 0; i < 1000; ++i)
99 ASSERT_NE(table.insert(i * 7919), nullptr);
100
101 const auto st = table.stats();
102 EXPECT_EQ(num_chains(st), table.capacity());
103 EXPECT_EQ(num_entries(st), table.size());
104 EXPECT_NEAR(st.avg, static_cast<float>(table.size()) / table.capacity(), 1e-4);
105}
106
107TEST(HashChainStats, ChainLongerThanSeveralHistogramBlocks)
108{
109 // Every key collides, so the histogram only records lengths 0 and n. Its
110 // DynArray must still be readable at every index below lens.size().
111 DynSetHash<int> table(101, constant_hash);
112 constexpr int n = 9000;
113 for (int i = 0; i < n; ++i)
114 ASSERT_NE(table.insert(i), nullptr);
115
116 const auto st = table.stats();
117 ASSERT_EQ(st.lens.size(), static_cast<size_t>(n) + 1);
118 EXPECT_EQ(st.lens(n), 1u);
119 EXPECT_EQ(st.lens(n / 2), 0u);
120 EXPECT_EQ(num_chains(st), table.capacity());
121 EXPECT_EQ(num_entries(st), static_cast<size_t>(n));
122}
123
124// A table of chained lists that a move left without lists has no chain.
125// stats() divided by the number of chains, which gave an average and a
126// variance that are not numbers
127TEST(HashChainStats, ATableWithoutListsHasNoChains)
128{
129 DynSetLhash<int> source;
130 for (int i = 0; i < 100; ++i)
131 ASSERT_NE(source.insert(i), nullptr);
132 const DynSetLhash<int> target(std::move(source));
133 ASSERT_EQ(target.size(), 100u);
134 ASSERT_EQ(source.capacity(), 0u);
135
136 const auto st = source.stats();
137 EXPECT_EQ(st.lens.size(), 0u);
138 EXPECT_EQ(st.avg, 0.0f);
139 EXPECT_EQ(st.var, 0.0f);
140 EXPECT_EQ(source.current_alpha(), 0.0f);
141}
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
Self-adjusting dynamic hash table.
Key * insert(const Key &key)
Inserts key into the hash set.
#define TEST(name)
TYPED_TEST(HashChainStats, EmptyTableHasOnlyEmptyChains)
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Hash-based dynamic set (defined in tpl_dynSetHash.H).
Container for comprehensive statistical results.
Definition stat_utils.H:125
Dynamic set implementations based on hash tables.