64#include <gtest/gtest.h>
120#include <type_traits>
125#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
126# define TEST_UNDER_A_SANITIZER 1
127#elif defined(__has_feature)
128# if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || \
129 __has_feature(memory_sanitizer)
130# define TEST_UNDER_A_SANITIZER 1
133#ifndef TEST_UNDER_A_SANITIZER
134# define TEST_UNDER_A_SANITIZER 0
137#define TEST_SEES_THE_HEAP 0
138#if defined(__GLIBC__) && !TEST_UNDER_A_SANITIZER
139# if __GLIBC_PREREQ(2, 33)
141# undef TEST_SEES_THE_HEAP
142# define TEST_SEES_THE_HEAP 1
146using namespace Aleph;
155 std::unique_ptr<int> number;
157 Allocating() : number(
new int(-1)) {}
159 Allocating(
const int i) : number(
new int(i)) {}
161 Allocating(
const Allocating & o) : number(
new int(o.value())) {}
163 Allocating(Allocating &&)
noexcept =
default;
165 Allocating & operator = (
const Allocating & o)
167 number.reset(
new int(o.value()));
171 Allocating & operator = (Allocating &&)
noexcept =
default;
173 int value()
const noexcept {
return number ==
nullptr ? -2 : *number; }
175 bool operator < (
const Allocating & o)
const noexcept {
return value() < o.value(); }
177 bool operator == (
const Allocating & o)
const noexcept {
return value() == o.value(); }
186 std::unique_ptr<int> number;
188 Without_Move() : number(
new int(-1)) {}
190 Without_Move(
const int i) : number(
new int(i)) {}
192 Without_Move(
const Without_Move & o) : number(
new int(o.value())) {}
194 Without_Move & operator = (
const Without_Move & o)
196 number.reset(
new int(o.value()));
200 int value()
const noexcept {
return *number; }
202 bool operator < (
const Without_Move & o)
const noexcept {
return value() < o.value(); }
204 bool operator == (
const Without_Move & o)
const noexcept {
return value() == o.value(); }
210 return static_cast<size_t>(a.value()) * 40503u;
215 return static_cast<size_t>(a.value()) * 40503u;
219 size_t another_hash(
const int &
k)
noexcept
221 return static_cast<size_t>(
k) * 40503u + 7;
227 bool use_after_move =
false;
230 constexpr Leaving_Out nothing{};
231 constexpr Leaving_Out its_use_after_a_move{
true};
234 constexpr bool nothrow_swap =
235 requires (C & a, C & b) { { a.swap(b) }
noexcept; };
241 template <
class C,
class Fill>
242 void expect_that_what_is_noexcept_does_not_allocate(
const char * name, Fill
fill,
243 const Leaving_Out
out = nothing)
247 if (std::is_nothrow_default_constructible_v<C>)
249 std::optional<C> built;
250 const size_t before = allocations;
252 EXPECT_EQ(allocations - before, 0u) <<
"its default construction";
257 if (std::is_nothrow_move_constructible_v<C>)
259 std::optional<C> target;
260 const size_t before = allocations;
261 target.emplace(std::move(source));
262 EXPECT_EQ(allocations - before, 0u) <<
"its move construction";
263 if (not
out.use_after_move)
270 if (std::is_nothrow_move_assignable_v<C>)
275 const size_t before = allocations;
276 to = std::move(from);
277 EXPECT_EQ(allocations - before, 0u) <<
"its move assignment";
278 if (not
out.use_after_move)
285 if constexpr (nothrow_swap<C>)
289 const size_t before = allocations;
291 EXPECT_EQ(allocations - before, 0u) <<
"its swap";
299 static T item(
const int i) {
return T(i); }
301 static constexpr auto append = [] (
auto & c,
const int from)
303 for (
int i = from; i < from + 3; ++i)
304 (
void) c.append(item(i));
307 static constexpr auto insert = [] (
auto & c,
const int from)
309 for (
int i = from; i < from + 3; ++i)
310 (
void) c.insert(item(i));
313 static constexpr auto push = [] (
auto & c,
const int from)
315 for (
int i = from; i < from + 3; ++i)
316 (
void) c.push(item(i));
319 static constexpr auto put = [] (
auto & c,
const int from)
321 for (
int i = from; i < from + 3; ++i)
322 (
void) c.put(item(i));
325 static constexpr auto push_back = [] (
auto & c,
const int from)
327 for (
int i = from; i < from + 3; ++i)
328 c.push_back(item(i));
332 static constexpr auto insert_value = [] (
auto & c,
const int from)
334 for (
int i = from; i < from + 3; ++i)
335 (
void) c.insert(i, item(i));
339 static constexpr auto insert_key = [] (
auto & c,
const int from)
341 for (
int i = from; i < from + 3; ++i)
342 (
void) c.insert(item(i), i);
345 static constexpr auto insert_node = [] (
auto & g,
const int from)
347 for (
int i = from; i < from + 3; ++i)
348 (
void) g.insert_node(item(i));
351 static constexpr auto write = [] (
auto &
m,
const int from)
353 m.set_dimension(2, 2);
354 m.write(0, 1, item(from));
360 void check_the_sequences(
const std::string & of)
362 auto name = [&of] (
const char * container) {
return container + (
"<" + of +
">"); };
363 const auto w = With<T>();
366# define CHECK(Container, how) \
367 expect_that_what_is_noexcept_does_not_allocate<Container<T>> \
368 (name(#Container).c_str(), w.how)
369# define CHECK_FIXED(Container, how) \
370 expect_that_what_is_noexcept_does_not_allocate<Container<T>> \
371 (name(#Container).c_str(), w.how, its_use_after_a_move)
393 expect_that_what_is_noexcept_does_not_allocate<SmallVector<T, 4>>
394 (name(
"SmallVector").c_str(),
w.push_back);
395 expect_that_what_is_noexcept_does_not_allocate<FlatMap<int, T>>
396 (name(
"FlatMap<int, ").c_str(),
w.insert_value);
407 void check_the_trees(
const std::string & of)
409 auto name = [&of] (
const char * tree) {
return "DynSetTree<" + of +
", " + tree +
">"; };
410 const auto w = With<T>();
411# define CHECK(Tree) \
412 expect_that_what_is_noexcept_does_not_allocate<DynSetTree<T, Tree>> \
413 (name(#Tree).c_str(), w.insert); \
414 expect_that_what_is_noexcept_does_not_allocate<DynMapTree<int, T, Tree>> \
415 (("DynMapTree<int, " + of + ", " #Tree ">").c_str(), w.insert_value)
432 expect_that_what_is_noexcept_does_not_allocate<DynMapTree<T, int>>
433 ((
"DynMapTree<" + of +
", int>").c_str(),
w.insert_key);
434 expect_that_what_is_noexcept_does_not_allocate<AHMapping<int, T>>
435 ((
"AHMapping<int, " + of +
">").c_str(),
w.insert_value);
443 void check_the_hash_tables(
const std::string & of)
445 const auto w = With<T>();
446 const Leaving_Out linear = nothing;
447# define CHECK_SET(Set, out) \
448 expect_that_what_is_noexcept_does_not_allocate<Set<T>> \
449 ((std::string(#Set "<") + of + ">").c_str(), w.insert, out)
450# define CHECK_MAP(Map, out) \
451 expect_that_what_is_noexcept_does_not_allocate<Map<int, T>> \
452 ((std::string(#Map "<int, ") + of + ">").c_str(), w.insert_value, out);\
453 expect_that_what_is_noexcept_does_not_allocate<Map<T, int>> \
454 ((std::string(#Map "<") + of + ", int>").c_str(), w.insert_key, out)
479 GTEST_SKIP() <<
"ThreadSanitizer provides the allocation functions";
485 ASSERT_FALSE(std::is_nothrow_default_constructible_v<Allocating>);
486 ASSERT_TRUE(std::is_nothrow_move_constructible_v<Allocating>);
488 size_t before = allocations;
490 EXPECT_EQ(allocations - before, 1u);
492 before = allocations;
493 const Allocating b(std::move(a));
494 EXPECT_EQ(allocations - before, 0u);
495 EXPECT_EQ(b.value(), -1);
502 ASSERT_FALSE(std::is_nothrow_default_constructible_v<Without_Move>);
503 ASSERT_FALSE(std::is_nothrow_move_constructible_v<Without_Move>);
504 ASSERT_FALSE(std::is_nothrow_swappable_v<Without_Move>);
506 size_t before = allocations;
508 EXPECT_EQ(allocations - before, 1u);
510 before = allocations;
511 const Without_Move b(std::move(a));
512 EXPECT_EQ(allocations - before, 1u);
513 EXPECT_EQ(b.value(), 7);
514 EXPECT_EQ(a.value(), 7);
519 check_the_sequences<int>(
"int");
520 check_the_sequences<Allocating>(
"Allocating");
521 check_the_sequences<Without_Move>(
"Without_Move");
522 expect_that_what_is_noexcept_does_not_allocate<BitArray>
528 check_the_trees<int>(
"int");
529 check_the_trees<Allocating>(
"Allocating");
530 check_the_trees<Without_Move>(
"Without_Move");
534 expect_that_what_is_noexcept_does_not_allocate<MultiPolynomial>
556 EXPECT_TRUE(std::is_nothrow_move_constructible_v<MultiPolynomial>);
567 check_the_hash_tables<int>(
"int");
568 check_the_hash_tables<Allocating>(
"Allocating");
569 check_the_hash_tables<Without_Move>(
"Without_Move");
600 const auto fill = [] (
auto &
m,
const int from)
603 for (
int i = from; i < from + 3; ++i)
606 expect_that_what_is_noexcept_does_not_allocate<DynMapHash<int, int>>
607 (
"DynMapHash<int, int> after set_hash_fct()",
fill);
616 std::array<size_t, 16> salt{};
618 size_t operator () (
const int &
k)
const noexcept
620 return static_cast<size_t>(
k) * 40503u + salt[0];
632 using Fct = std::function<size_t(
const int &)>;
634 const Fct large{Large_Hash()};
635 const size_t before = allocations;
636 const Fct
copy(large);
637 ASSERT_GT(allocations - before, 0u) <<
"the hash function of this test must not fit";
641 from.set_hash_fct(Fct(Large_Hash()));
643 for (
int i = 0; i < 3; ++i)
645 size_t before = allocations;
646 to = std::move(from);
647 EXPECT_EQ(allocations - before, 0u) <<
"the move assignment of LhashTable";
648 EXPECT_NE(to.
search(1),
nullptr);
650 EXPECT_NE(from.search(9),
nullptr);
653 set_from.set_hash_fct(Fct(Large_Hash()));
654 set_to.set_hash_fct(Fct(Large_Hash()));
655 for (
int i = 0; i < 3; ++i)
656 ASSERT_NE(set_from.
insert(i),
nullptr);
657 before = allocations;
658 set_to = std::move(set_from);
659 EXPECT_EQ(allocations - before, 0u) <<
"the move assignment of DynSetLhash";
661 ASSERT_NE(set_from.
insert(9),
nullptr);
669 const auto w = With<Allocating>();
670 expect_that_what_is_noexcept_does_not_allocate<List_Graph<Node, Arc>>(
"List_Graph",
w.insert_node);
671 expect_that_what_is_noexcept_does_not_allocate<List_Digraph<Node, Arc>>(
"List_Digraph",
w.insert_node);
674#if TEST_SEES_THE_HEAP
680 const struct mallinfo2
m = mallinfo2();
681 return m.uordblks +
m.hblkhd;
693 class Empty_Thread_Cache
695 static constexpr size_t Largest = 1040, Step = 16, Of_Each = 8;
697 void *
volatile held[(Largest / Step) * Of_Each];
700 Empty_Thread_Cache() noexcept
704 for (
size_t k = 0;
k < Of_Each; ++
k)
705 held[i++] = std::malloc(
size - 8);
708 ~Empty_Thread_Cache()
710 for (
void * block : held)
714 Empty_Thread_Cache(
const Empty_Thread_Cache &) =
delete;
715 Empty_Thread_Cache & operator = (
const Empty_Thread_Cache &) =
delete;
721 std::pair<long, long> heap_taken_by()
723 const Empty_Thread_Cache no_cache;
724 const size_t before = heap_in_use();
725 const auto since = [before]
727 return static_cast<long>(heap_in_use()) -
static_cast<long>(before);
729 std::optional<C> a, b, c;
731 const long built = since();
732 if constexpr (std::is_move_constructible_v<C>)
734 b.emplace(std::move(*a));
735 c.emplace(std::move(*b));
737 return {built, since()};
741 void expect_no_memory_of_malloc(
const char * name)
743 const auto [built, moved] = heap_taken_by<C>();
744 EXPECT_EQ(built, 0) << name <<
", built by default";
745 EXPECT_EQ(moved, 0) << name <<
", moved twice";
757#if TEST_SEES_THE_HEAP
766 const Empty_Thread_Cache no_cache;
767 const size_t before = heap_in_use();
768 void *
volatile block = std::malloc(512);
769 const bool shows = heap_in_use() > before;
772 GTEST_SKIP() <<
"the heap does not show a block of 512 bytes";
775 expect_no_memory_of_malloc<DynArray<int>>(
"DynArray<int>");
776 expect_no_memory_of_malloc<DynArrayHeap<int>>(
"DynArrayHeap<int>");
777 expect_no_memory_of_malloc<Random_Set<int>>(
"Random_Set<int>");
778 expect_no_memory_of_malloc<BitArray>(
"BitArray");
779 expect_no_memory_of_malloc<LinearHashTable<int>>(
"LinearHashTable<int>");
780 expect_no_memory_of_malloc<DynSetLinHash<int>>(
"DynSetLinHash<int>");
781 expect_no_memory_of_malloc<DynSetLinHash<std::string>>(
"DynSetLinHash<std::string>");
782 expect_no_memory_of_malloc<DynMapLinHash<int, int>>(
"DynMapLinHash<int, int>");
788 const Empty_Thread_Cache no_cache;
789 const size_t before = heap_in_use();
790 (void) array.
exist(0);
791 EXPECT_EQ(heap_in_use(), before);
794 EXPECT_EQ(heap_in_use(), before);
797 EXPECT_GT(heap_in_use(), before);
800 GTEST_SKIP() <<
"the heap of malloc() cannot be looked at here";
Bidirectional mapping between two types.
bool operator<(const Time &l, const Time &r)
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
WeightedDigraph::Node Node
Space-efficient bit array implementation.
size_t size_t int32_t value
size_t size_t int32_t * out
A generic key-value mapping container with inverse operation support.
Fixed-capacity binary heap backed by a raw array.
Queue implemented with a single dynamic array.
Stack implemented with simple dynamic array and with bounds verification.
Simple dynamic array with automatic resizing and functional operations.
Contiguous array of bits.
void push(const unsigned int value)
Inserts the value at the end of the array.
Dynamic heap (priority queue) backed by DynArray.
T & touch(const size_t i)
Touch the entry i.
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
Dynamic heap of elements of type T ordered by a comparison functor.
Dynamic doubly linked list with O(1) size and bidirectional access.
Self-adjusting dynamic hash table.
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
Key * insert(const Key &key)
Inserts key into the hash set.
Dynamic hash table mapping keys to records with separate chaining.
Dynamic queue of elements of generic type T based on single linked list.
Dynamic stack of elements of generic type T based on a singly linked list.
Doubly-linked list (defined in tpl_dynList.H).
Generic key-value map implemented on top of a binary search tree.
Dynamic matrix with sparse storage.
Dynamic set backed by balanced binary search trees with automatic memory management.
Dynamic ordered set implemented with a Skip List.
Very simple queue implemented with a contiguous array.
Ordered set stored as a sorted contiguous array.
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
void set_hash_fct(Hash_Fct fct)
Sets the hash function.
Sparse multivariate polynomial.
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree.
Simple, scalable and fast dynamic array.
Open addressing hash table with double hashing collision resolution.
void set_hash_fct(Hash_Fct fct)
Open addressing hash table with linear probing collision resolution.
Top-down red-black tree with rank (no virtual destructor).
Top-down red-black tree without virtual destructor.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Singly linked list implementations with head-tail access.
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Gen_MultiPolynomial< double, Grevlex_Order > MultiPolynomial
Multivariate polynomial with double coefficients, grevlex.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
std::decay_t< typename HeadC::Item_Type > T
size_t aleph_hash_value(const AdversarialTranspositionKey< StateKey > &key) noexcept
#define CHECK_FIXED(Container, how)
#define CHECK_SET(Set, out)
#define CHECK_MAP(Map, out)
TEST_F(NoexceptAllocation, TheItemAllocatesWhenItIsBuiltByDefaultAndNotWhenItMoves)
#define CHECK(Container, how)
Ranked AVL tree with nodes without a virtual destructor.
AVL binary search tree with nodes without a virtual destructor.
Binary search tree with nodes without virtual destructors,.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
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.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
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)
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Generic unbalanced binary search tree.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic hash table mapping keys to records with separate chaining.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Dynamic matrix with lazy allocation.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Sorted-array map (Aleph::FlatMap), a cache-friendly ordered map.
Sorted-array set (Aleph::FlatSet), a cache-friendly ordered set.
Generic graph and digraph implementations.
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree implementation.
Unified hash table interface.
Simple, scalable, contiguous dynamic array.
Sparse multivariate polynomial over an arbitrary coefficient ring.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.
Randomized binary search tree.
Random access queue (bag) with O(1) random pop.
Red-Black tree with rank (order statistics).
Red-Black tree implementation (bottom-up balancing).
Dynamic array with inline storage (Aleph::SmallVector).
Top-down splay tree with rank support.
Top-down splay tree implementation (without rank support).
Top-down Red-Black tree with rank support.
Top-down Red-Black tree implementation.
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.