44# define NumItems 10000
54 unsigned long dup_counter = 0;
56 cout <<
"Testing simple insertions and searches ...." << endl;
58 for (
unsigned long i = 0; i < n; i++)
73 other.
for_each([&table] (
unsigned long item)
88 unsigned long removed_counter = 0;
89 size_t num_inserted = table.
size();
90 for (
size_t i = 0; i < n; i++)
97 assert(removed_counter == num_inserted);
98 assert(table.
size() == 0);
100 cout << removed_counter <<
" items removed" << endl;
102 cout <<
"testing empty() method ...." << endl;
107 assert(table.
size() == 0);
109 unsigned long repeated_counter = 0;
110 cout <<
"Reinserting keys ...." << endl;
111 for (
size_t i = 0; i < n; ++i)
115 cout << repeated_counter <<
" duplicated numbers" << endl
116 << dup_counter <<
" was the previous value" << endl;
118 assert(dup_counter == repeated_counter);
120 cout <<
"Done!" << endl;
123 cout <<
"Testing iterator and map ...." << endl;
126 ( [] (
const unsigned long &
k) ->
unsigned long
132 assert(table.
search(it.get_curr()) !=
nullptr);
134 cout <<
"done!" << endl;
138 cout <<
"testing lvalue copy constructor ...." << endl;
145 cout <<
"testing lvalue assigment...." << endl;
147 for (
size_t i = 0; i < n/2; ++i)
149 unsigned long key =
r.
get();
157 assert(aux == table);
161 cout <<
"Testing rvalue constructor ...." << endl;
163 assert(tmp == table);
164 cout <<
"done!" << endl
166 <<
"Testing rvalue assign = .... " << endl
169 cout <<
"done!" << endl
176 cout <<
"done" << endl
177 <<
"Reinserting ...." << endl;
182 for (
size_t i = 0; i < n; ++i)
186 cout <<
"Searching inserted keys ...." << endl;
187 for (
size_t i = 0; i < n; ++i)
190 assert(ptr !=
nullptr);
194 if (dup_idx < dups.
size()
and dups(dup_idx) == i)
201 cout <<
"Testing keys() in set ...." << endl
204 assert(the_keys.
size() == table.
size());
205 assert(
all(the_keys, [&table] (
const unsigned long & key)
207 return table.
has(key);
213 <<
"Testing filter of keys multiples of 13" << endl;
216 filter(table, [](
const unsigned long & key)
218 return key % 13 == 0;
221 table.
filter( [] (
const unsigned long & key)
223 return key % 13 == 0;
224 }).
for_each( [&v13] (
const unsigned long & key)
239 unsigned long dup_counter = 0;
240 cout <<
"Testing simple insertions and searches ...." << endl;
241 for (
unsigned long i = 0; i < n; i++)
247 [[maybe_unused]]
const auto inserted = table.
insert(
keys(i), i);
248 assert(inserted !=
nullptr);
263 unsigned long removed_counter = 0;
264 size_t num_inserted = table.size();
265 for (
size_t i = 0; i < n; i++)
266 if (table.search(
keys(i)) !=
nullptr)
268 table.remove(
keys(i));
272 assert(removed_counter == num_inserted);
273 assert(table.size() == 0);
275 cout << removed_counter <<
" items removed" << endl;
277 cout <<
"testing empty() method ...." << endl;
282 assert(table.size() == 0);
284 unsigned long repeated_counter = 0;
285 cout <<
"Reinserting keys ...." << endl;
286 for (
size_t i = 0; i < n; ++i)
287 if (table.insert(
keys(i), i) ==
nullptr)
290 cout << repeated_counter <<
" duplicated numbers" << endl
291 << dup_counter <<
" was the previous value" << endl;
293 assert(dup_counter == repeated_counter);
295 cout <<
"Done!" << endl
297 <<
"Testing for_each and a battery of other tests ...." << endl;
299 assert(table.all([&table] (
const std::pair<const unsigned long, long> & p)
301 auto * ptr = table.search(p.first);
302 assert(ptr != nullptr);
303 assert(table.get_data(p.first) == ptr->second);
304 return table.has(p.first);
308 cout <<
"done!" << endl
310 <<
"testing keys() method and other tests ...." << endl;
312 assert(
all(the_keys, [&table] (
unsigned long k)
313 {
return table.has(
k); }));
315 cout <<
"done!" << endl
317 <<
"Testing items() method and other stuff ...." << endl;
319 assert(
all(items, [&table]
320 (std::pair<unsigned long, long> p)
321 {
return table.find(p.first) == p.second; } ));
322 cout <<
"done!" << endl
326int main(
int argc,
char *argv[])
331 if (argv[1][0] ==
'-')
333 cerr <<
"Invalid n: " << argv[1] << endl;
336 char * endptr =
nullptr;
338 n = strtoul(argv[1], &endptr, 10);
339 if (errno != 0 or endptr == argv[1] or *endptr !=
'\0')
341 cerr <<
"Invalid n: " << argv[1] << endl;
346 unsigned int t = (
unsigned int) time(
nullptr);
349 if (argv[2][0] ==
'-')
351 cerr <<
"Invalid t: " << argv[2] << endl;
354 char * endptr =
nullptr;
356 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
357 if (errno != 0 or endptr == argv[2] or *endptr !=
'\0' or parsed_t > UINT_MAX)
359 cerr <<
"Invalid t: " << argv[2] << endl;
362 t =
static_cast<unsigned int>(parsed_t);
365 cout << argv[0] <<
" " << n <<
" " << t << endl;
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Iterator on the items of list.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
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(const Key &key) const
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
void empty()
Remove all the elements of the set.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
unsigned long get() noexcept
The next number, as operator()().
bool has_curr() const noexcept
bool equal_to(const Container &r) const
Equality test between this and r.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
and
Check uniqueness with explicit hash + equality functors.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Aleph::DynList< T > keys() const
See items().
DynSetTree< unsigned long > create_table(const DynSetTree< unsigned long > &other)
void test_DynSet(size_t n)
unsigned long insert_n_random_items_in_set(DynSetTree< unsigned long > &table, DynArray< unsigned long > &keys, unsigned long n)
unsigned long insert_n_random_items_in_map(DynMapTree< unsigned long, long > &table, DynArray< unsigned long > &keys, unsigned long n)
void test_DynMap(size_t n)
Dynamic key-value map based on balanced binary search trees.
Comprehensive sorting algorithms and search utilities for Aleph-w.