#include "exparray_map.h"
#include
#include
#include
using std::cout;
using std::endl;
/**
* Function: populateMap
* ---------------------
* Clears the specified (non-const) map and then inserts all of the
* integers in the range [0, residue), using the specified n value
* to generate the numbers in an interesting (not sorted) order.
* It's the client's responsibility the ensure that n is less than
* residue (this is checked) and that n and residue are relatively
* prime to one another. The details of the integer generation
* aren't all that fascinating, but those interested should just
* search Google for primes and residue theory and you'll get
* lots of information.
*
* The bottom line is that every integer in [0, residue) is inserted
* into the map after it's cleared, and each integer is mapped to
* itself. The function will terminate the program if it notices
* that the insertion fails or that the same value has somehow been
* entered twice. Notice that the final line of the for loop fails
* and kills the program if the generated integer was seen before or
* if the insertion returns the wrong iterator.
*
* This is a good test for the insert function.
*/
static void populateMap(exparray_map& m, int n, int residue)
{
assert(n < residue);
m.clear(); // just in case
for (int i = 0; i < residue; i++) {
assert(m.size() == i);
int value = static_cast((static_cast(n) * i) % residue);
pair::iterator, bool> result = m.insert(make_pair(value, value));
assert(result.second && result.first->first == value && result.first->second == value);
result = m.insert(make_pair(value, value + 1));
assert(!result.second && result.first->first == result.first->second);
}
}
static void confirmEverythingIsThere(const exparray_map& m, int n)
{
cout << "Checking to see that all numbers between 0 and " << (n - 1)
<< ", inclusive, are present." << endl;
for (int i = 0; i < n; i++) {
exparray_map::const_iterator found = m.find(i);
assert(found != m.end() && (found->first == i));
}
}
static const unsigned int kLargePrime = 1398269;
static const unsigned int kEvenLargerPrime = 3021377;
int main(int argc, char *argv[])
{
exparray_map numbers;
populateMap(numbers, kLargePrime, kEvenLargerPrime);
confirmEverythingIsThere(numbers, kLargePrime);
return 0;
}