43# define NumItems 10000
49template <
template <
typename,
class>
class SetType>
unsigned long
55 unsigned long dup_counter = 0;
57 cout <<
"Testing simple insertions and searches ...." << endl;
59 for (
int i = 0; i < n; i++)
62 if (not table.has(
keys(i)))
63 table.insert(
keys(i));
67 if (table.current_alpha() > 1.1)
69 cout <<
"Resizing table to " << 1.5*table.size() << endl;
70 table.resize(1.5*table.size());
71 cout <<
"done!" << endl;
74 cout <<
"done" << endl;
79template <
template <
typename,
class>
class HashTable>
80HashTable<unsigned long, Aleph::equal_to<unsigned long>>
84 typedef HashTable<unsigned long, Aleph::equal_to<unsigned long>> SetType;
86 for (
auto it = other.get_it(); it.has_curr(); it.next())
87 table.insert(it.get_curr());
92template <
template <
typename,
class>
class HashTable>
95 typedef HashTable<unsigned long, Aleph::equal_to<unsigned long>> SetType;
100 typename SetType::Stats stats = table.stats();
101 table.print_stats(stats);
103 cout << table.size() <<
" items inserted" << endl
104 << dup_counter <<
" duplicated numbers" << endl
106 <<
"testing deletions ...." << endl;
109 const auto ctable = table;
110 assert(table.all([&ctable] (
auto k) { return ctable.find(k) == k; }));
113 unsigned long removed_counter = 0;
114 size_t num_inserted = table.size();
115 for (
size_t i = 0; i < n; i++)
116 if (table.search(
keys(i)) != NULL)
118 table.remove(
keys(i));
122 assert(removed_counter == num_inserted);
123 assert(table.size() == 0);
125 cout << removed_counter <<
" items removed" << endl;
127 cout <<
"testing empty() method ...." << endl;
132 assert(table.size() == 0);
134 unsigned long repeated_counter = 0;
135 cout <<
"Reinserting keys ...." << endl;
136 for (
size_t i = 0; i < n; ++i)
137 if (table.insert(
keys(i)) == NULL)
140 cout << repeated_counter <<
" duplicated numbers" << endl
141 << dup_counter <<
" was the previus value" << endl;
143 assert(dup_counter == repeated_counter);
145 cout <<
"Done!" << endl;
148 cout <<
"Testing iterator and map ...." << endl;
151 ([] (
unsigned long k) ->
unsigned long
157 assert(table.search(it.get_curr()) != NULL);
159 cout <<
"done!" << endl;
163 cout <<
"testing lvalue copy constructor ...." << endl;
166 assert(table.equal_to(tmp));
170 cout <<
"testing lvalue assigment...." << endl;
172 for (
size_t i = 0; i < n/2; ++i)
174 unsigned long key =
r.
get();
182 assert(aux == table);
186 cout <<
"Testing rvalue constructor ...." << endl;
188 assert(tmp == table);
189 cout <<
"done!" << endl
191 <<
"Testing rvalue assign = .... " << endl
194 cout <<
"done!" << endl
199 cout <<
"testing del() of Iterator ...." << endl
200 <<
"Deleting all entries through del() ...." << endl;
202 for (
typename SetType::Iterator it(table); it.has_curr(); )
205 assert(table.is_empty());
209 cout <<
"done" << endl
210 <<
"Reinserting ...." << endl;
211 for (
int i = 0; i < n; ++i)
212 if (table.insert(
keys(i)) == NULL)
215 cout <<
"Searching inserted keys ...." << endl;
216 for (
size_t i = 0; i < n; ++i)
218 unsigned long * ptr = table.search(
keys(i));
223 if (dup_idx < dups.
size()
and dups(dup_idx) == i)
230 cout <<
"Testing keys() in set ...." << endl
233 assert(the_keys.
size() == table.size());
234 assert(
all(the_keys, [&table] (
const size_t & key)
236 return table.has(key);
242 <<
"Testing filter of keys multiples of 13" << endl;
245 filter(table, [] (
const unsigned long & key)
247 return key % 13 == 0;
250 table.
filter( [] (
const unsigned long & key)
252 return key % 13 == 0;
253 }).
for_each( [&v13] (
const unsigned long & key)
263template <
class HashTable>
268 unsigned long dup_counter = 0;
269 cout <<
"Testing simple insertions and searches ...." << endl;
270 for (
long i = 0; i < n; i++)
273 if (not table.has(
keys(i)))
276 [[maybe_unused]]
const auto inserted = table.insert(
keys(i), i);
277 assert(inserted !=
nullptr);
283 cout << n <<
" tries " << endl
284 << dup_counter <<
" duplicated" << endl
285 <<
"size = " << table.size() << endl
287 <<
"Performing map search test" << endl
291 sort(
keys).for_each([] (
auto k) { cout <<
" " <<
k; });
294 sort(table.keys()).for_each([] (
auto k) { cout <<
" " <<
k; });
297 for (
auto i = 0; i < n; i++)
298 assert(table.search(
keys(i)));
300 assert(
keys.all([&table] (
auto k) { return table.search(k) != nullptr; }));
301 cout <<
"Passed" << endl
307template <
template <
typename,
typename,
class>
class HashTable>
310 using MapType = HashTable<unsigned long, long, Aleph::equal_to<unsigned long>>;
315 typename MapType::Stats stats = table.stats();
316 table.print_stats(stats);
318 cout << table.size() <<
" items inserted" << endl
319 << dup_counter <<
" duplicated numbers" << endl
321 <<
"testing deletions ...." << endl;
323 unsigned long removed_counter = 0;
324 size_t num_inserted = table.size();
325 for (
int i = 0; i < n; i++)
326 if (table.search(
keys(i)) !=
nullptr)
328 table.remove(
keys(i));
331 cout << removed_counter <<
" items removed" << endl;
333 assert(removed_counter == num_inserted);
334 assert(table.size() == 0);
337 cout <<
"testing empty() method ...." << endl;
342 assert(table.size() == 0);
344 unsigned long repeated_counter = 0;
345 cout <<
"Reinserting keys ...." << endl;
346 for (
size_t i = 0; i < n; ++i)
347 if (table.insert(
keys(i), i) == NULL)
350 cout << repeated_counter <<
" duplicated numbers" << endl
351 << dup_counter <<
" was the previus value" << endl;
353 assert(dup_counter == repeated_counter);
355 cout <<
"Done!" << endl
357 <<
"Testing for_each and a battery of other tests ...." << endl;
359 assert(table.all( [&table]
360 (
const std::pair<const unsigned long, long> & p)
362 auto ptr = table.search(p.first);
363 assert(ptr != nullptr);
364 assert(table.get_data(p.first) == ptr->second);
365 return table.has(p.first);
369 cout <<
"done!" << endl
371 <<
"testing keys() method and other tests ...." << endl;
373 assert(
all(the_keys, [&table] (
unsigned long k)
374 {
return table.has(
k); }));
376 cout <<
"done!" << endl
384 <<
"Testing items() method and othet stuff ...." << endl;
386 assert(
all(items, [&table]
387 (std::pair<unsigned long, long> p)
388 {
return table.find(p.first) == p.second; } ));
389 cout <<
"done!" << endl
391 <<
"Testing remove by data pointer ...." << endl;
393 for_each(
keys, [&table, &removed_counter] (
const unsigned long &
k)
395 auto ptr = table.search(
k);
399 table.remove_by_data(ptr->second);
402 assert(table.is_empty());
405 <<
"Reinserting keys for doing other tests ...." << endl;
406 for (
int i = 0; i < n; ++i)
407 table.insert(
keys(i), i);
408 assert(table.size() == removed_counter);
409 cout <<
"done!" << endl
414int main(
int argc,
char * argv[])
422 try { n = stoul(argv[1]); }
catch (...) { n =
NumItems; }
427 cerr <<
"n must be positive" << endl;
431 unsigned int t = std::time(NULL);
434 try { t = stoul(argv[2]); }
catch (...) { t = std::time(NULL); }
437 cout << argv[0] <<
" " << n <<
" " << t << endl;
441 test_DynSetLinHash<DynSetLhash>(n);
442 test_DynMapLinHash<DynMapHash>(n);
444 test_DynSetLinHash<DynSetLinHash>(n);
445 test_DynMapLinHash<DynMapLinHash>(n);
447 cout <<
"testing of ODhash based set ...." << endl
449 test_DynSetLinHash<SetODhash>(n);
451 <<
"Done all test of ODhash based set!" << endl
454 <<
"testing of OLhash based set ...." << endl
456 test_DynSetLinHash<SetOLhash>(n);
458 <<
"Done all test of OLhash based set!" << endl
461 <<
"Testing all tests of OLhash based map" << 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.
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
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
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.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
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.
bool check_primes_database()
Verify the order of the prime database.
unsigned long next_prime(unsigned long n)
Find a prime >= n in the database, for hash table sizing.
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().
unsigned long insert_n_random_items_in_set(SetType< unsigned long, Aleph::equal_to< unsigned long > > &table, DynArray< unsigned long > &keys, unsigned long n)
HashTable< unsigned long, Aleph::equal_to< unsigned long > > create_table(const HashTable< unsigned long, Aleph::equal_to< unsigned long > > &other)
void test_DynSetLinHash(size_t n)
void test_DynMapLinHash(size_t n)
unsigned long insert_n_random_items_in_map(HashTable &table, DynArray< unsigned long > &keys, unsigned long n)
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Comprehensive sorting algorithms and search utilities for Aleph-w.