56#include <gtest/gtest.h>
93 constexpr size_t max_size = std::numeric_limits<size_t>::max();
100 return {max_size, max_size - 1, max_size - 6, max_size - 7, max_size - 8,
101 max_size / item_size + 1, max_size / 2 + 1};
110 for (
const size_t s : {limit + 1, limit * 2, size_t(1) << 61, size_t(1) << 62})
121 return {max_size, max_size - 1, max_size - 6, max_size - 7, max_size - 8, max_size / 2 + 1,
122 limit * 8 + 1, limit * 8 + 8, limit * 16, size_t(1) << 61, size_t(1) << 62};
127 template <
class Request>
128 std::string refusal_of(Request request)
134 catch (
const std::bad_array_new_length &)
138 catch (
const std::bad_alloc &)
140 return "it tried to allocate";
142 catch (
const std::exception &)
146 return "it took the size";
153 void expect_memarray_and_array_refuse(
const T & item)
155 for (
const size_t n : sizes_beyond_bytes(sizeof(
T)))
161 EXPECT_EQ(refusal_of([&] {
m.reserve(n); }),
"");
162 ASSERT_EQ(
m.
size(), 1u);
163 EXPECT_EQ(
m.first(), item);
165 EXPECT_EQ(refusal_of([&] {
MemArray<T> b(n); }),
"");
170 EXPECT_EQ(refusal_of([&] { a.
reserve(n); }),
"");
171 ASSERT_EQ(a.
size(), 1u);
172 EXPECT_EQ(a[0], item);
174 EXPECT_EQ(refusal_of([&] {
Array<T> b(n, item); }),
"");
181 void expect_queues_and_stacks_refuse(
const T & item)
183 for (
const size_t n : sizes_beyond_bytes(sizeof(
T)))
188 EXPECT_EQ(refusal_of([&] { q.
reserve(n); }),
"");
189 ASSERT_EQ(q.
size(), 1u);
190 EXPECT_EQ(q.
front(), item);
201 for (
const size_t bits : bits_beyond_bitarray())
207 EXPECT_EQ(refusal_of([&] { a.
reserve(
bits); }),
"");
208 ASSERT_EQ(a.
size(), 3u);
216 for (
const size_t n : sizes_beyond_dynarray(
sizeof(
int)))
221 EXPECT_EQ(refusal_of([&] { a.
reserve(n); }),
"");
222 EXPECT_EQ(refusal_of([&] { a.
adjust(n); }),
"");
223 EXPECT_EQ(refusal_of([&] { (void) a.
touch(n - 1); }),
"");
224 EXPECT_EQ(refusal_of([&] { a[n - 1] = 6; }),
"");
225 ASSERT_EQ(a.
size(), 1u);
233 for (
const size_t n : sizes_beyond_dynarray(
sizeof(
int)))
238 EXPECT_EQ(refusal_of([&] {
h.reserve(n); }),
"");
239 ASSERT_EQ(
h.size(), 1u);
240 EXPECT_EQ(
h.top(), 5);
249 sizes.
append(max_size /
sizeof(
int));
250 for (
const size_t n : sizes)
256 EXPECT_EQ(refusal_of([&] {
h.allocate_storage(n); }),
"");
257 ASSERT_EQ(
h.size(), 1u);
258 EXPECT_EQ(
h.top(), 5);
262TEST(SizeLimits, MemArrayAndArray)
264 expect_memarray_and_array_refuse(5);
265 expect_memarray_and_array_refuse(std::string(
"five"));
268TEST(SizeLimits, QueuesAndStacks)
270 expect_queues_and_stacks_refuse(5);
271 expect_queues_and_stacks_refuse(std::string(
"five"));
274TEST(SizeLimits, FlatSetAndFlatMap)
276 for (
const size_t n : sizes_beyond_bytes(
sizeof(std::string)))
281 EXPECT_EQ(refusal_of([&] { s.
reserve(n); }),
"");
282 EXPECT_EQ(s.
size(), 1u);
288 EXPECT_EQ(refusal_of([&] {
m.reserve(n); }),
"");
289 EXPECT_EQ(
m.
size(), 1u);
297 for (
const size_t n : sizes_beyond_bytes(
sizeof(std::string)))
302 EXPECT_EQ(refusal_of([&] { v.
reserve(n); }),
"");
303 ASSERT_EQ(v.
size(), 1u);
304 EXPECT_EQ(v[0],
"five");
309TEST(SizeLimits, StaticEventTable)
312 for (
const size_t n : sizes_beyond_bytes(
sizeof(Table::Event_Handler)))
315 EXPECT_EQ(refusal_of([&] {
Table t(n); }),
"");
323 for (
const size_t n : sizes_beyond_bytes(
sizeof(
double)))
330 EXPECT_EQ(refusal_of([&] {
Simplex<double> s(max_size /
sizeof(
double)); }),
"");
333 const double row[] = {1.0, 1.0, 4.0};
345 const auto refuses = [] (
const char * name,
const size_t bucket_size,
const auto & build)
347 for (
const size_t n : sizes_beyond_bytes(bucket_size))
349 SCOPED_TRACE(testing::Message() << name <<
" of " << n <<
" buckets of " << bucket_size <<
" bytes");
350 EXPECT_THROW(build(n), std::overflow_error);
368 ASSERT_NE(d.insert(5),
nullptr);
369 const size_t d_cap = d.capacity();
371 EXPECT_EQ(d.size(), 1u);
372 EXPECT_EQ(d.capacity(), d_cap);
373 EXPECT_TRUE(d.has(5));
376 ASSERT_NE(
l.
insert(5),
nullptr);
377 const size_t l_cap =
l.capacity();
379 EXPECT_EQ(
l.
size(), 1u);
380 EXPECT_EQ(
l.capacity(), l_cap);
381 EXPECT_TRUE(
l.has(5));
385 const size_t lists_cap = lists.capacity();
386 refuses(
"DynSetLhash::resize()",
sizeof(
Dlink), [&lists] (
const size_t n) { lists.resize(n); });
387 EXPECT_EQ(lists.size(), 1u);
388 EXPECT_EQ(lists.capacity(), lists_cap);
389 EXPECT_TRUE(lists.
has(5));
397TEST(SizeLimits, CuckooFiltersAndTheEstimateOfABloomFilter)
399 for (
const size_t n : {max_size, max_size - 1, max_size / 2 + 1, max_size / 2, max_size / 4})
427 const double not_a_number = std::numeric_limits<double>::quiet_NaN();
430 for (
const double p : {0.0, 1.0, -0.5, 2.0, std::numeric_limits<double>::infinity()})
Simplex algorithm for linear programming.
Space-efficient bit array implementation.
Probabilistic set membership with Bloom filters.
Fixed-capacity binary heap backed by a raw array.
Queue implemented with a single dynamic array.
T & front(const size_t i=0)
Return the i-th oldest item of the queue.
T & put(const T &item)
Copy and put an item in the queue.
void reserve(const size_t cap)
Reserve capacity for at least cap items, keeping the queue order.
Stack implemented with simple dynamic array and with bounds verification.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
constexpr size_t capacity() const noexcept
Return the internal capacity.
void reserve(size_t cap)
Reserves cap cells into the array.
Contiguous array of bits.
std::string get_bit_str() const
void set_bit_str(const std::string &str)
void reserve(const size_t dim)
Reserve memory in advance for the bit array dim dimension.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
void set_size(const size_t sz)
Resets the dimension of the array.
Bloom filter implementation.
static std::tuple< size_t, size_t > estimate(const size_t n, const double p)
Estimate the parameters of a Bloom filter.
Memory-optimized Cuckoo Filter with bit-packed fingerprints.
size_t capacity() const noexcept
Return the total number of fingerprint slots (including stash).
Industrial-grade Cuckoo Filter implementation.
size_t capacity() const noexcept
Return the total number of fingerprint slots (including stash).
Doubly linked circular list node.
Dynamic heap (priority queue) backed by DynArray.
T & insert(const T &key)
Insert a copy of key into the heap.
void adjust(const size_t dim)
Set a new dimension.
T & touch(const size_t i)
Touch the entry i.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Key * insert(const Key &key)
Inserts key into the hash set.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
Dynamic hash table mapping keys to records with separate chaining.
T & insert(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Very simple queue implemented with a contiguous array.
Ordered map stored as two parallel sorted contiguous arrays.
Ordered set stored as a sorted contiguous array.
void reserve(size_t cap)
Reserve capacity for at least cap keys.
bool contains(const Key &k) const
Test membership.
size_t size() const noexcept
Return the number of stored keys. O(1).
std::pair< const_iterator, bool > insert(const Key &k)
Insert a copy of k if no equivalent key exists.
Generic linear hash table.
Simple, scalable and fast dynamic array.
size_t size() const noexcept
Return the number of elements.
Open addressing hash table with double hashing collision resolution.
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Open addressing hash table with linear probing collision resolution.
Linear program solver using the Simplex method.
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
Contiguous dynamic array with N elements of inline storage.
T & push_back(const T &item)
STL-style alias of append(const T &).
size_t size() const noexcept
Return the number of stored elements. O(1).
void reserve(const size_t cap)
Reserve capacity for at least cap elements.
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 size_t capacity() const noexcept
Returns the current capacity of the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Fixed-size event table implementation.
Memory-optimized Cuckoo filter with bit-packed fingerprints.
Probabilistic set membership with Cuckoo filters.
Event-driven table abstraction for event-driven simulations.
LinearHashTable< int > Table
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
std::decay_t< typename HeadC::Item_Type > T
Hash-based dynamic set (defined in tpl_dynSetHash.H).
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.
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.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Sorted-array map (Aleph::FlatMap), a cache-friendly ordered map.
Sorted-array set (Aleph::FlatSet), a cache-friendly ordered set.
Hash tables with collision resolution by separate chaining.
Simple, scalable, contiguous dynamic array.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.
Dynamic array with inline storage (Aleph::SmallVector).