#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;<--- The scope of the variable 'entry' can be reduced. [+]The scope of the variable 'entry' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
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));
}
}