Skip to content

Commit 2d29291

Browse files
committed
Refactor existing tests of error cases to use cbmc_invariants_should_throwt
This adds sharing map unit tests to check that operations fail as expected. For example, calling map.replace(key, value) when the key does not exist in the map should fail.
1 parent 94bc97f commit 2d29291

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

unit/util/sharing_map.cpp

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ TEST_CASE("Sharing map interface", "[core][util]")
156156
REQUIRE(sm.has_key("i"));
157157
REQUIRE(sm.has_key("j"));
158158
REQUIRE(!sm.has_key("k"));
159+
160+
cbmc_invariants_should_throwt invariants_throw;
161+
162+
REQUIRE_THROWS(sm.insert("i", "4"));
159163
}
160164

161165
SECTION("Replace and update elements")
@@ -172,17 +176,36 @@ TEST_CASE("Sharing map interface", "[core][util]")
172176
auto r2 = sm.find("i");
173177
REQUIRE(r2);
174178
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);
175188

189+
cbmc_invariants_should_throwt invariants_throw;
190+
191+
SECTION("Replace non-existing")
176192
{
177-
cbmc_invariants_should_throwt invariants_throw;
178-
sharing_map_error_checkt debug_sm;
179-
fill(debug_sm);
180-
REQUIRE_NOTHROW(
181-
debug_sm.update("i", [](std::string &str) { str += "1"; }));
182-
REQUIRE_THROWS(debug_sm.update("i", [](std::string &str) {}));
193+
REQUIRE_THROWS(sm.replace("x", "0"));
194+
}
183195

184-
REQUIRE_NOTHROW(debug_sm.replace("i", "abc"));
185-
REQUIRE_THROWS(debug_sm.replace("i", "abc"));
196+
SECTION("Update non-existing")
197+
{
198+
REQUIRE_THROWS(sm.update("x", [](std::string &str) {}));
199+
}
200+
201+
SECTION("Replace with equal")
202+
{
203+
REQUIRE_THROWS(debug_sm.replace("i", "0"));
204+
}
205+
206+
SECTION("Update with equal")
207+
{
208+
REQUIRE_THROWS(debug_sm.update("i", [](std::string &str) {}));
186209
}
187210
}
188211

@@ -246,6 +269,10 @@ TEST_CASE("Sharing map interface", "[core][util]")
246269

247270
sm3.erase("i");
248271
REQUIRE(!sm3.has_key("i"));
272+
273+
cbmc_invariants_should_throwt invariants_throw;
274+
275+
REQUIRE_THROWS(sm3.erase("x"));
249276
}
250277
}
251278

@@ -621,35 +648,3 @@ TEST_CASE("Sharing map sharing stats", "[core][util]")
621648
}
622649
#endif
623650
}
624-
625-
TEST_CASE("Sharing map replace non-existing", "[.]")
626-
{
627-
sharing_map_standardt sm;
628-
fill(sm);
629-
630-
sm.replace("x", "0");
631-
}
632-
633-
TEST_CASE("Sharing map replace with equal value", "[.]")
634-
{
635-
sharing_map_error_checkt sm;
636-
637-
sm.insert("i", "0");
638-
sm.replace("i", "0");
639-
}
640-
641-
TEST_CASE("Sharing map insert existing", "[.]")
642-
{
643-
sharing_map_standardt sm;
644-
fill(sm);
645-
646-
sm.insert("i", "4");
647-
}
648-
649-
TEST_CASE("Sharing map erase non-existing", "[.]")
650-
{
651-
sharing_map_standardt sm;
652-
fill(sm);
653-
654-
sm.erase("x");
655-
}

0 commit comments

Comments
 (0)