Skip to content

Commit d8b1c7d

Browse files
authored
Update unit-ordered_map-2.cpp
Added test for iterator consistency Signed-off-by: Andrea Cocito <39852324+puffetto@users.noreply.github.com>
1 parent fb9173f commit d8b1c7d

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tests/src/unit-ordered_map-2.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,40 @@ TEST_CASE("ordered_map with const and iteration consistency")
160160
}
161161
CHECK(keys == std::vector<int>{1,2});
162162
}
163+
164+
TEST_CASE("ordered_map iterator stability")
165+
{
166+
ordered_map<std::string, int> m;
167+
m["a"] = 1;
168+
m["b"] = 2;
169+
m["c"] = 3;
170+
171+
auto ita = m.find("a");
172+
REQUIRE(ita != m.end());
173+
CHECK(ita->second == 1);
174+
175+
SECTION("iterator survives insertion of other keys")
176+
{
177+
m["d"] = 4;
178+
m["e"] = 5;
179+
CHECK(ita->first == "a");
180+
CHECK(ita->second == 1);
181+
}
182+
183+
SECTION("iterator survives erase of different key")
184+
{
185+
CHECK(m.erase("b") == 1);
186+
CHECK(ita->first == "a");
187+
CHECK(ita->second == 1);
188+
CHECK(m.find("b") == m.end());
189+
}
190+
191+
SECTION("iterator invalidated when its own key is erased")
192+
{
193+
// safe pattern: erase returns next iterator
194+
auto next = m.erase(ita);
195+
CHECK(next != m.end());
196+
CHECK(next->first == "b"); // next element after "a"
197+
CHECK(m.find("a") == m.end());
198+
}
199+
}

0 commit comments

Comments
 (0)