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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/*******************************************************************\
Module: Unit tests for dense_integer_map
Author: Diffblue Ltd
\*******************************************************************/
#include <testing-utils/use_catch.h>
#include <util/dense_integer_map.h>
TEST_CASE("no keys", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> empty;
map.setup_for_keys(empty.begin(), empty.end());
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
REQUIRE_THROWS_AS(map.at(0), invariant_failedt);
REQUIRE_THROWS_AS(map[0], invariant_failedt);
REQUIRE_THROWS_AS(map.insert({0, 0}), invariant_failedt);
}
TEST_CASE("one key", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> allowed_keys = {1};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
REQUIRE(map.size() == 0);
REQUIRE_THROWS_AS(map.at(1), invariant_failedt);
REQUIRE(!map.count(1));
REQUIRE(map[1] == 0);
REQUIRE(map.size() == 1);
REQUIRE(map.count(1));
map[1] = 2;
REQUIRE(map.at(1) == 2);
REQUIRE(map[1] == 2);
auto insert_result = map.insert({1, 5});
REQUIRE(!insert_result.second);
REQUIRE(insert_result.first == map.begin());
REQUIRE_THROWS_AS(map.at(0), invariant_failedt);
REQUIRE_THROWS_AS(map[0], invariant_failedt);
REQUIRE_THROWS_AS(map.insert({0, 0}), invariant_failedt);
}
TEST_CASE("insert fresh key", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> allowed_keys = {1};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
auto insert_result = map.insert({1, 5});
REQUIRE(insert_result.second);
REQUIRE(insert_result.first == map.begin());
REQUIRE(map.at(1) == 5);
REQUIRE(map[1] == 5);
}
TEST_CASE("multiple, sparse keys", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> allowed_keys = {1, 10};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
REQUIRE(map.size() == 0);
map.insert({1, 2});
REQUIRE(map.size() == 1);
auto second_insert_result = map.insert({10, 11});
REQUIRE(second_insert_result.second);
REQUIRE(second_insert_result.first == std::next(map.begin()));
REQUIRE_THROWS_AS(map[0], invariant_failedt);
REQUIRE(map[1] == 2);
// Keys in the gap are silently accepted, though this is bad practice:
// REQUIRE_THROWS_AS(map[2], invariant_failedt);
// REQUIRE_THROWS_AS(map[3], invariant_failedt);
// REQUIRE_THROWS_AS(map[4], invariant_failedt);
// REQUIRE_THROWS_AS(map[5], invariant_failedt);
// REQUIRE_THROWS_AS(map[6], invariant_failedt);
// REQUIRE_THROWS_AS(map[7], invariant_failedt);
// REQUIRE_THROWS_AS(map[8], invariant_failedt);
// REQUIRE_THROWS_AS(map[9], invariant_failedt);
REQUIRE(map[10] == 11);
REQUIRE_THROWS_AS(map[11], invariant_failedt);
}
TEST_CASE("iterators", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> allowed_keys = {1, 10};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
map.insert({1, 5});
map.insert({10, 11});
std::vector<std::pair<int, int>> iterator_result{map.begin(), map.end()};
REQUIRE(
iterator_result == std::vector<std::pair<int, int>>{{1, 5}, {10, 11}});
int acc = 0;
for(auto &key_value : map)
key_value.second = ++acc;
iterator_result = std::vector<std::pair<int, int>>{map.begin(), map.end()};
REQUIRE(iterator_result == std::vector<std::pair<int, int>>{{1, 1}, {10, 2}});
REQUIRE(map.begin() != map.end());
REQUIRE(map.begin() != std::next(map.begin()));
REQUIRE(map.begin() == map.begin());
REQUIRE(map.size() == 2);
REQUIRE(std::distance(map.begin(), map.end()) == (ptrdiff_t)map.size());
{
const auto &const_map = map;
iterator_result =
std::vector<std::pair<int, int>>{const_map.begin(), const_map.end()};
REQUIRE(
iterator_result == std::vector<std::pair<int, int>>{{1, 1}, {10, 2}});
auto non_const_iterator = map.begin();
auto converted_non_const_iterator =
(decltype(map)::const_iterator)non_const_iterator;
auto const_iterator = const_map.begin();
auto other_const_iterator = const_map.begin();
REQUIRE(converted_non_const_iterator == const_iterator);
REQUIRE(converted_non_const_iterator == other_const_iterator);
}
}
TEST_CASE("keys must be unique", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int> map;
std::vector<int> allowed_keys = {1, 1};
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
REQUIRE_THROWS_AS(
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end()),
invariant_failedt);
allowed_keys = {1, 2, 1};
REQUIRE_THROWS_AS(
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end()),
invariant_failedt);
allowed_keys = {1, 2};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
}
class reverse_orderingt
{
public:
int operator()(int x)
{
return 10 - x;
}
};
TEST_CASE(
"ordering by custom key-to-integer function",
"[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int, reverse_orderingt> map;
std::vector<int> allowed_keys = {-20, 0, 20};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
map.insert({0, 1});
map.insert({-20, 2});
map.insert({20, 3});
std::vector<std::pair<int, int>> iterator_result{map.begin(), map.end()};
REQUIRE(
iterator_result ==
std::vector<std::pair<int, int>>{{20, 3}, {0, 1}, {-20, 2}});
}
class index_is_mod2t
{
public:
int operator()(int x)
{
return x % 2;
}
};
TEST_CASE("indices must be unique", "[core][util][dense_integer_map]")
{
dense_integer_mapt<int, int, index_is_mod2t> map;
cbmc_invariants_should_throwt invariants_throw_in_this_scope;
// Illegal keys (are equal mod 2)
std::vector<int> allowed_keys = {2, 4};
REQUIRE_THROWS_AS(
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end()),
invariant_failedt);
// Legal keys (not equal mod 2)
allowed_keys = {2, 3};
map.setup_for_keys(allowed_keys.begin(), allowed_keys.end());
}
|