Skip to content

Commit 65e6b6a

Browse files
authored
Merge pull request #4547 from danpoe/feature/sharing-map-unit-tests
Add unit tests for sharing map that check that views are not invalidated by modifications
2 parents 620e27d + 2b4f0ae commit 65e6b6a

File tree

1 file changed

+134
-48
lines changed

1 file changed

+134
-48
lines changed

unit/util/sharing_map.cpp

Lines changed: 134 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ Author: Daniel Poetzl
1919
typedef sharing_mapt<irep_idt, std::string, false, irep_id_hash>
2020
sharing_map_standardt;
2121

22-
typedef sharing_mapt<irep_idt, std::string, true, irep_id_hash>
23-
sharing_map_debugt;
24-
2522
class sharing_map_internalt : public sharing_map_standardt
2623
{
2724
friend void sharing_map_internals_test();
@@ -159,6 +156,10 @@ TEST_CASE("Sharing map interface", "[core][util]")
159156
REQUIRE(sm.has_key("i"));
160157
REQUIRE(sm.has_key("j"));
161158
REQUIRE(!sm.has_key("k"));
159+
160+
cbmc_invariants_should_throwt invariants_throw;
161+
162+
REQUIRE_THROWS_AS(sm.insert("i", "4"), invariant_failedt);
162163
}
163164

164165
SECTION("Replace and update elements")
@@ -175,17 +176,38 @@ TEST_CASE("Sharing map interface", "[core][util]")
175176
auto r2 = sm.find("i");
176177
REQUIRE(r2);
177178
REQUIRE(r2->get() == "90");
179+
}
180+
181+
SECTION("Replace and update elements errors")
182+
{
183+
sharing_map_standardt sm;
184+
fill(sm);
185+
186+
sharing_map_error_checkt debug_sm;
187+
fill(debug_sm);
188+
189+
cbmc_invariants_should_throwt invariants_throw;
178190

191+
SECTION("Replace non-existing")
179192
{
180-
cbmc_invariants_should_throwt invariants_throw;
181-
sharing_map_debugt debug_sm;
182-
fill(debug_sm);
183-
REQUIRE_NOTHROW(
184-
debug_sm.update("i", [](std::string &str) { str += "1"; }));
185-
REQUIRE_THROWS(debug_sm.update("i", [](std::string &str) {}));
186-
187-
REQUIRE_NOTHROW(debug_sm.replace("i", "abc"));
188-
REQUIRE_THROWS(debug_sm.replace("i", "abc"));
193+
REQUIRE_THROWS_AS(sm.replace("x", "0"), invariant_failedt);
194+
}
195+
196+
SECTION("Update non-existing")
197+
{
198+
REQUIRE_THROWS_AS(
199+
sm.update("x", [](std::string &str) {}), invariant_failedt);
200+
}
201+
202+
SECTION("Replace with equal")
203+
{
204+
REQUIRE_THROWS_AS(debug_sm.replace("i", "0"), invariant_failedt);
205+
}
206+
207+
SECTION("Update with equal")
208+
{
209+
REQUIRE_THROWS_AS(
210+
debug_sm.update("i", [](std::string &str) {}), invariant_failedt);
189211
}
190212
}
191213

@@ -249,6 +271,10 @@ TEST_CASE("Sharing map interface", "[core][util]")
249271

250272
sm3.erase("i");
251273
REQUIRE(!sm3.has_key("i"));
274+
275+
cbmc_invariants_should_throwt invariants_throw;
276+
277+
REQUIRE_THROWS_AS(sm3.erase("x"), invariant_failedt);
252278
}
253279
}
254280

@@ -309,7 +335,7 @@ TEST_CASE("Sharing map views and iteration", "[core][util]")
309335

310336
SECTION("View")
311337
{
312-
typedef std::pair<dstringt, std::string> pt;
338+
typedef std::pair<std::string, std::string> pt;
313339

314340
sharing_map_standardt sm;
315341
sharing_map_standardt::viewt view;
@@ -319,7 +345,7 @@ TEST_CASE("Sharing map views and iteration", "[core][util]")
319345
pairs.clear();
320346
for(auto &p : view)
321347
{
322-
pairs.push_back({p.first, p.second});
348+
pairs.push_back({id2string(p.first), p.second});
323349
}
324350
std::sort(pairs.begin(), pairs.end());
325351
};
@@ -354,11 +380,11 @@ TEST_CASE("Sharing map views and iteration", "[core][util]")
354380
sharing_map_standardt sm;
355381
fill(sm);
356382

357-
typedef std::pair<dstringt, std::string> pt;
383+
typedef std::pair<std::string, std::string> pt;
358384
std::vector<pt> pairs;
359385

360386
sm.iterate([&pairs](const irep_idt &key, const std::string &value) {
361-
pairs.push_back({key, value});
387+
pairs.push_back({id2string(key), value});
362388
});
363389

364390
std::sort(pairs.begin(), pairs.end());
@@ -442,6 +468,98 @@ TEST_CASE("Sharing map views and iteration", "[core][util]")
442468
}
443469
}
444470

471+
TEST_CASE("Sharing map view validity", "[core][util]")
472+
{
473+
SECTION("View validity")
474+
{
475+
sharing_map_standardt sm;
476+
sharing_map_standardt::viewt view;
477+
478+
fill(sm);
479+
fill2(sm);
480+
481+
sharing_map_standardt sm2(sm);
482+
sm2.replace("l", "8");
483+
484+
sm.get_view(view);
485+
486+
std::size_t i_idx = 0;
487+
std::size_t k_idx = 0;
488+
489+
for(std::size_t i = 0; i < view.size(); i++)
490+
{
491+
if(view[i].first == "i")
492+
i_idx = i;
493+
494+
if(view[i].first == "k")
495+
k_idx = i;
496+
}
497+
498+
sm.erase("i");
499+
sm.replace("k", "7");
500+
sm.insert("o", "6");
501+
502+
for(std::size_t i = 0; i < view.size(); i++)
503+
{
504+
if(i == i_idx || i == k_idx)
505+
continue;
506+
507+
auto &p = view[i];
508+
509+
REQUIRE(&p.second == &sm.find(p.first)->get());
510+
}
511+
}
512+
513+
SECTION("Delta view validity")
514+
{
515+
sharing_map_standardt sm;
516+
517+
sharing_map_standardt::delta_viewt delta_view;
518+
519+
fill(sm);
520+
fill2(sm);
521+
522+
sharing_map_standardt sm2(sm);
523+
524+
sm2.erase("i");
525+
sm2.erase("j");
526+
sm2.erase("k");
527+
528+
sm2.erase("m");
529+
sm2.erase("n");
530+
531+
sm.get_delta_view(sm2, delta_view, false);
532+
533+
REQUIRE(delta_view.size() == 5);
534+
535+
std::size_t i_idx = 0;
536+
std::size_t k_idx = 0;
537+
538+
for(std::size_t i = 0; i < delta_view.size(); i++)
539+
{
540+
if(delta_view[i].k == "i")
541+
i_idx = i;
542+
543+
if(delta_view[i].k == "k")
544+
k_idx = i;
545+
}
546+
547+
sm.erase("i");
548+
sm.replace("k", "7");
549+
sm.insert("o", "6");
550+
551+
for(std::size_t i = 0; i < delta_view.size(); i++)
552+
{
553+
if(i == i_idx || i == k_idx)
554+
continue;
555+
556+
auto &delta_item = delta_view[i];
557+
558+
REQUIRE(&delta_item.m == &sm.find(delta_item.k)->get());
559+
}
560+
}
561+
}
562+
445563
TEST_CASE("Sharing map sharing stats", "[core][util]")
446564
{
447565
#if !defined(_MSC_VER)
@@ -532,35 +650,3 @@ TEST_CASE("Sharing map sharing stats", "[core][util]")
532650
}
533651
#endif
534652
}
535-
536-
TEST_CASE("Sharing map replace non-existing", "[.]")
537-
{
538-
sharing_map_standardt sm;
539-
fill(sm);
540-
541-
sm.replace("x", "0");
542-
}
543-
544-
TEST_CASE("Sharing map replace with equal value", "[.]")
545-
{
546-
sharing_map_error_checkt sm;
547-
548-
sm.insert("i", "0");
549-
sm.replace("i", "0");
550-
}
551-
552-
TEST_CASE("Sharing map insert existing", "[.]")
553-
{
554-
sharing_map_standardt sm;
555-
fill(sm);
556-
557-
sm.insert("i", "4");
558-
}
559-
560-
TEST_CASE("Sharing map erase non-existing", "[.]")
561-
{
562-
sharing_map_standardt sm;
563-
fill(sm);
564-
565-
sm.erase("x");
566-
}

0 commit comments

Comments
 (0)