38#include <gtest/gtest.h>
63 bool operator()(
int a,
int b)
const noexcept {
return a > b; }
71 EXPECT_EQ(
m.
size(), 0u);
82 EXPECT_EQ(
m.
size(), 3u);
83 EXPECT_TRUE(
m.
has(1));
84 EXPECT_TRUE(
m.
has(2));
85 EXPECT_TRUE(
m.
has(3));
97 ASSERT_NE(p1,
nullptr);
99 EXPECT_EQ(p2,
nullptr);
100 EXPECT_EQ(
m.
size(), 1u);
101 EXPECT_EQ(
m.
find(1), 10);
107 EXPECT_NE(
m.
append(1, 10),
nullptr);
108 EXPECT_EQ(
m.put(2, 20)->second, 20);
109 EXPECT_EQ(
m.
size(), 2u);
114 auto m = make_sample();
117 ASSERT_NE(p,
nullptr);
118 EXPECT_EQ(p->second,
"two");
120 EXPECT_TRUE(
m.
has(1));
122 EXPECT_FALSE(
m.
has(99));
128 EXPECT_THROW((
void)
m.
find(1), std::domain_error);
137 EXPECT_EQ(
m.
find(42), 7);
138 EXPECT_EQ(
m.
size(), 1u);
145 EXPECT_THROW((
void)cm[1], std::domain_error);
150 auto m = make_sample();
151 EXPECT_EQ(
m.
size(), 3u);
153 const auto removed =
m.
remove(2);
154 EXPECT_EQ(removed,
"two");
155 EXPECT_FALSE(
m.
has(2));
156 EXPECT_EQ(
m.
size(), 2u);
162 EXPECT_THROW(
m.remove_key(1), std::domain_error);
167 auto m = make_sample();
170 auto values =
m.values();
172 EXPECT_EQ(
keys.size(), 3u);
173 EXPECT_EQ(values.size(), 3u);
178 auto m = make_sample();
180 auto vp =
m.values_ptr();
181 auto ip =
m.items_ptr();
183 EXPECT_EQ(vp.size(), 3u);
184 EXPECT_EQ(ip.size(), 3u);
187 *vp.get_first() =
"ONE";
188 EXPECT_EQ(
m.
find(1),
"ONE");
193 auto m = make_sample();
195 ASSERT_NE(p,
nullptr);
199 EXPECT_EQ(data,
"three");
201 EXPECT_EQ(
m.
find(3),
"THREE");
218 ASSERT_TRUE(it.has_curr());
219 EXPECT_EQ(it.get_curr().first, 3);
224 static_assert(std::is_same_v<DynMapTree<int, int>::Key_Type,
int>);
225 static_assert(std::is_same_v<DynMapTree<int, int>::Value_Type,
int>);
231template <
template <
typename,
class>
class T>
234 template <
class K,
class D>
241std::string string_key(
const int i,
const size_t len,
const bool prefix)
243 const std::string digits = std::to_string(1000 + i);
244 return prefix ? std::string(len - 4,
'k') + digits : digits + std::string(len - 4,
'x');
248class DynMapTreeSearchByKey :
public ::testing::Test {};
251 ::testing::Types<Tree_Of<Avl_Tree>, Tree_Of<Avl_Tree_Rk>, Tree_Of<Rb_Tree>, Tree_Of<TdRbTree>,
252 Tree_Of<Treap>, Tree_Of<BinTree>, Tree_Of<Rand_Tree>, Tree_Of<Splay_Tree>>;
259TYPED_TEST(DynMapTreeSearchByKey, FindsEveryStringKeyAndNoOther)
261 using Map =
typename TypeParam::template Map<std::string, int>;
262 constexpr int N = 300;
263 for (
const size_t len : {size_t(7), size_t(16), size_t(60)})
264 for (
const bool prefix : {
false,
true})
266 SCOPED_TRACE(
"length " + std::to_string(len) + (
prefix ?
", with a prefix" :
""));
268 for (
int i = 0; i <
N; ++i)
269 ASSERT_NE(map.insert(string_key(i * 37 %
N, len,
prefix), i * 37 %
N),
nullptr);
271 for (
int i = 0; i <
N; ++i)
273 const std::string key = string_key(i, len,
prefix);
274 const auto * p = map.search(key);
275 ASSERT_NE(p,
nullptr) << key;
276 EXPECT_EQ(p->first, key);
277 EXPECT_EQ(p->second, i);
280 for (
const auto & key : absent)
281 EXPECT_EQ(map.search(key),
nullptr) << key;
282 EXPECT_EQ(map.size(),
static_cast<size_t>(
N));
283 EXPECT_TRUE(map.verify());
287int main(
int argc,
char ** argv)
289 ::testing::InitGoogleTest(&argc, argv);
290 return RUN_ALL_TESTS();
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic key-value map implemented on top of a binary search tree.
static const Key & get_key(Data *data_ptr) noexcept
typename Base::Iterator Iterator
static Data & get_data(Key &key) noexcept
Number Theoretic Transform over Z / MOD Z.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
void remove(const Key &key)
Remove a key from the hash table.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Key * append(const Key &key)
Alias for insert() (copy version).
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
DynList< Key > keys() const
Returns a list containing all keys in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
constexpr bool has(const Key &key) const noexcept
Checks if a key exists in the table.
Key & find(const Key &key)
Finds a key and returns a reference to it.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
static void prefix(Node *root, DynList< Node * > &acc)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic key-value map based on balanced binary search trees.
TYPED_TEST(DynMapTreeSearchByKey, FindsEveryStringKeyAndNoOther)