45#include <gtest/gtest.h>
72#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__) \
73 || defined(__SANITIZE_HWADDRESS__)
74# define COUNT_ALLOCATIONS 0
75#elif defined(__has_feature)
76# if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) \
77 || __has_feature(memory_sanitizer) || __has_feature(hwaddress_sanitizer)
78# define COUNT_ALLOCATIONS 0
81#ifndef COUNT_ALLOCATIONS
82# define COUNT_ALLOCATIONS 1
86#define SKIP_UNLESS_COUNTING_ALLOCATIONS() \
87 if (not COUNT_ALLOCATIONS) \
88 GTEST_SKIP() << "a sanitizer provides the allocation functions"
90#if defined(__GNUC__) || defined(__clang__)
91# define ALEPH_TEST_NOINLINE [[gnu::noinline]]
93# define ALEPH_TEST_NOINLINE
103 long allocations = 0;
106 bool out_of_memory =
false;
113 throw std::bad_alloc();
114 if (
void *p = std::malloc(
size == 0 ? 1 :
size))
116 throw std::bad_alloc();
125 template <
class Action>
126 long allocations_of(Action action)
128 const long before = allocations;
130 return allocations - before;
134 template <
class Action>
135 void without_memory(Action action)
139 Guard()
noexcept { out_of_memory =
true; }
140 ~Guard() { out_of_memory =
false; }
150void *
operator new(std::size_t
size,
const std::nothrow_t &)
noexcept
162void *
operator new[](std::size_t
size,
const std::nothrow_t &)
noexcept
174void operator delete(
void *p)
noexcept { release(p); }
175void operator delete[](
void *p)
noexcept { release(p); }
176void operator delete(
void *p, std::size_t)
noexcept { release(p); }
177void operator delete[](
void *p, std::size_t)
noexcept { release(p); }
178void operator delete(
void *p,
const std::nothrow_t &)
noexcept { release(p); }
179void operator delete[](
void *p,
const std::nothrow_t &)
noexcept { release(p); }
182TEST(TreeAllocation, constructing_a_tree_does_not_allocate)
186 EXPECT_EQ(allocations_of([] {
Avl_Tree<int> t; (void) t; }), 0);
188 EXPECT_EQ(allocations_of([] {
Rb_Tree<int> t; (void) t; }), 0);
192 EXPECT_EQ(allocations_of([] {
Treap<int> t; (void) t; }), 0);
203TEST(TreeAllocation, moving_a_tree_does_not_allocate)
208 for (
int i = 0; i < 100; ++i)
210 EXPECT_EQ(allocations_of([&
m]
213 EXPECT_EQ(moved.
size(), 100u);
217 for (
int i = 0; i < 100; ++i)
219 EXPECT_EQ(allocations_of([&s]
222 EXPECT_EQ(moved.
size(), 100u);
225 EXPECT_EQ(allocations_of([] {
Rb_Tree_Rk<int> t, moved(std::move(t)); (void) moved; }), 0);
226 EXPECT_EQ(allocations_of([] {
HtdRbTree<int> t, moved(std::move(t)); (void) moved; }), 0);
227 EXPECT_EQ(allocations_of([] {
HtdRbTreeRk<int> t, moved(std::move(t)); (void) moved; }), 0);
234 void check_insert_and_remove_allocate_only_the_node()
237 for (
int i = 0; i < 100; ++i)
239 EXPECT_EQ(allocations_of([&s] { s.insert(1000); }), 1);
240 EXPECT_EQ(allocations_of([&s] { s.remove(1000); }), 0);
241 EXPECT_EQ(allocations_of([&s] { s.remove(50); }), 0);
242 EXPECT_EQ(allocations_of([&s] { s.insert(50); }), 1);
243 EXPECT_EQ(s.size(), 100u);
247TEST(TreeAllocation, insert_and_remove_allocate_only_the_node)
251 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Avl_Tree>>();
252 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Avl_Tree_Rk>>();
253 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Rb_Tree>>();
254 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Rb_Tree_Rk>>();
255 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Treap>>();
256 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Rand_Tree>>();
257 check_insert_and_remove_allocate_only_the_node<DynSetTree<int, Splay_Tree>>();
261 std::vector<std::unique_ptr<Htd::Node>>
nodes;
262 for (
int i = 0; i < 100; ++i)
263 nodes.emplace_back(
new Htd::Node(i));
265 for (
auto & n :
nodes)
267 EXPECT_EQ(allocations_of([&t] { (void) t.remove(50); }), 0);
268 EXPECT_EQ(allocations_of([&t] { (void) t.remove(0); }), 0);
271TEST(TreeAllocation, iterating_does_not_allocate)
276 for (
int i = 0; i < 100; ++i)
278 EXPECT_EQ(allocations_of([&s]
282 sum += it.get_curr();
283 EXPECT_EQ(
sum, 4950);
287 EXPECT_EQ(
copy.get_curr(), 1);
288 EXPECT_EQ(it.get_curr(), 0);
293 for (
int i = 0; i < 100; ++i)
295 EXPECT_EQ(allocations_of([&splay]
299 sum += it.get_curr();
300 EXPECT_EQ(
sum, 4950);
304 std::vector<std::unique_ptr<Tree::Node>>
nodes;
305 for (
int i = 0; i < 100; ++i)
308 for (
auto & n :
nodes)
310 EXPECT_EQ(allocations_of([&t]
317 EXPECT_EQ(
count, 200);
321 for (
int i = 0; i < 100; ++i)
323 EXPECT_EQ(allocations_of([&heap]
328 EXPECT_EQ(
count, 100);
342 for (
int i = n; i > 0; --i)
348TEST(TreeAllocation, advancing_on_a_deep_tree_allocates)
352 auto tree = deep_tree(1000);
355 EXPECT_GT(allocations_of([&it] { it.next_ne(); }), 0);
362TEST(TreeAllocation, adaptors_propagate_the_bad_alloc_of_a_deep_tree)
366 auto tree = deep_tree(1000);
371 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
375 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
379 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
382 auto it =
zip_it(tree, list);
383 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
386 auto it =
zip_it(list, tree);
387 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
391 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
395 EXPECT_THROW(without_memory([&it] { it.next_ne(); }), std::bad_alloc);
400 for (
auto it =
zip_it(tree, list); it.has_curr(); it.next_ne())
402 EXPECT_EQ(
count, 1001u);
405TEST(TreeAllocation, noexcept_specifications_hold)
407 static_assert(std::is_nothrow_default_constructible_v<Avl_Tree<int>>);
408 static_assert(std::is_nothrow_default_constructible_v<Avl_Tree_Rk<int>>);
409 static_assert(std::is_nothrow_default_constructible_v<Rb_Tree<int>>);
410 static_assert(std::is_nothrow_default_constructible_v<HtdRbTree<int>>);
411 static_assert(std::is_nothrow_default_constructible_v<HtdRbTreeRk<int>>);
412 static_assert(std::is_nothrow_move_constructible_v<Rb_Tree<int>>);
413 static_assert(std::is_nothrow_move_constructible_v<Rb_Tree_Rk<int>>);
414 static_assert(std::is_nothrow_move_constructible_v<HtdRbTree<int>>);
415 static_assert(std::is_nothrow_move_constructible_v<HtdRbTreeRk<int>>);
416 static_assert(std::is_nothrow_move_constructible_v<DynSetTree<int>>);
417 static_assert(std::is_nothrow_move_constructible_v<DynMapTree<int, int>>);
423 static_assert(not std::is_nothrow_constructible_v<BinNodeInfixIterator<Node>,
Node *>);
424 static_assert(std::is_nothrow_constructible_v<BinNodePrefixIterator<Node>,
Node *>);
425 static_assert(not
noexcept(std::declval<BinNodeInfixIterator<Node> &>().
next_ne()));
426 static_assert(not
noexcept(std::declval<BinNodePrefixIterator<Node> &>().
next_ne()));
431 static_assert(
noexcept(std::declval<Pair_Iterator<List_It> &>().
next_ne()));
432 static_assert(not
noexcept(std::declval<Pair_Iterator<Tree_It, List_It> &>().
next_ne()));
433 static_assert(not
noexcept(std::declval<Pair_Iterator<List_It, Tree_It> &>().
next_ne()));
436 static_assert(
noexcept(std::declval<ZipIterator<List, List> &>().
next_ne()));
437 static_assert(not
noexcept(std::declval<ZipIterator<Tree, List> &>().
next_ne()));
438 static_assert(not
noexcept(std::declval<ZipIterator<List, Tree> &>().
next_ne()));
439 static_assert(
noexcept(std::declval<EnumZipIterator<List, List> &>().
next_ne()));
440 static_assert(not
noexcept(std::declval<EnumZipIterator<Tree, List> &>().
next_ne()));
441 static_assert(not
noexcept(std::declval<EnumZipIterator<List, Tree> &>().
next_ne()));
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
WeightedDigraph::Node Node
Inorder iterator on the nodes of a binary tree.
bool has_curr() const noexcept
Return true the iterator has current node.
Preorder iterator on the nodes of a binary tree.
Node belonging to a double circular linked list with header node.
Dynamic heap of elements of type T ordered by a comparison functor.
T & insert(const T &item)
Insert a copy of item into the heap.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
Generic key-value map implemented on top of a binary search tree.
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool has_curr() const noexcept
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Declare AvlNode type with 40-byte pool allocation.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Node for QuadTree spatial data structure.
QuadTree - Hierarchical spatial index for 2D points.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
ZipIterator< Cs... > zip_it(const Cs &...cs)
Alias for get_zip_it.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
EnumZipIterator< Cs... > enum_zip_it(const Cs &...cs)
Alias for get_enum_zip_it.
T * allocate(AhArenaAllocator &arena, Args &&... args)
Allocate and construct an object in an arena.
auto get_pair_it(C1 &c1, C2 &c2)
Create a Pair_Iterator for two containers.
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Ranked AVL tree with nodes without a virtual destructor.
AVL binary search tree with nodes without a virtual destructor.
const Key & get_curr() const
Randomized binary search tree.
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Red-black tree with nodes without virtual destructor.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Dynamic binary heap with node-based storage.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree implementation.
Randomized binary search tree.
Red-Black tree with rank (order statistics).
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree implementation (without rank support).
Treap: randomized BST combining tree and heap properties.
#define ALEPH_TEST_NOINLINE
#define SKIP_UNLESS_COUNTING_ALLOCATIONS()