File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments