1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "gate/tests.hpp"
#include "gate/maps.hpp"
#include "gate/randomgen.hpp"


using namespace gate;

template<class T> bool_t is_map_consistent(Map<T, T, DefaultComparer<T> > const& m)
{
    gate::size_t count = 0;
    typename Map<T, T>::const_iterator iter = m.begin();
    typename Map<T, T>::const_iterator next = iter;
    typename Map<T, T>::const_iterator iterend = m.end();
    while (iter != iterend)
    {
        ++count;
        next = iter;
        ++next;
        if (next != iterend)
        {
            if (iter.key() >= next.key())
            {
                return false;
            }
        }
        iter = next;
    }
    return count == m.count();
}

GATEXX_TEST_UNIT(Map)
{
    RandomGenerator random;
    uint32_t entry;
    Map<uint32_t, uint32_t> m;

    for (gate::size_t n = 0; n < 1000; ++n)
    {
        gate::uint64_t num = 0;
        GATEXX_TEST_CHECK_NOTHROW(random.fillBuffer(&num, sizeof(num)));
        GATEXX_TEST_CHECK_NOTHROW(num = random.getNumber());
        entry = static_cast<uint32_t>(num % 0xffffffff);
        if (n % 2)
        {
            GATEXX_TEST_CHECK_NOTHROW(m.add(entry, entry));
        }
        else
        {
            m.remove(entry);
        }
        GATEXX_TEST_CHECK(is_map_consistent(m));
    }
}