[libcxx-commits] [libcxx] [libc++] Improve the tests for vector::erase (PR #116265)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 14 10:44:13 PST 2024
================
@@ -200,5 +174,48 @@ int main(int, char**) {
}
#endif
+ // Real world example with std::string, mostly intended to test trivial relocation
+ {
+ std::vector<std::string> v;
+
+ // fill the vector with half short string and half long strings
+ std::string short_string = "short";
+ std::string long_string(256, 'x');
+ for (int i = 0; i != 10; ++i) {
+ v.push_back(i % 2 == 0 ? short_string : long_string);
+ }
+
+ std::vector<std::string> original = v;
+
+ auto it = v.erase(v.cbegin() + 2, v.cbegin() + 8);
+ assert(v.size() == 4);
+ assert(v[0] == original[0]);
+ assert(v[1] == original[1]);
+ assert(v[2] == original[8]);
+ assert(v[3] == original[9]);
+ assert(it == v.begin() + 2);
+ }
+
+ // Make sure we satisfy the complexity requirement in terms of the number of times the assignment
+ // operator is called.
+ {
+ Tracker tracker;
+ std::vector<TrackedAssignment> v;
+
+ // Set up the vector with 5 elements.
+ for (int i = 0; i != 5; ++i) {
+ v.emplace_back(&tracker);
+ }
+ assert(tracker.copy_assignments == 0);
+ assert(tracker.move_assignments == 0);
+
+ // Erase elements [1] and [2] from it. Elements [3] [4] should be shifted, so we should
+ // see 2 move assignments (and nothing else).
+ v.erase(v.begin() + 1, v.begin() + 3);
+ assert(v.size() == 3);
+ assert(tracker.copy_assignments == 0);
+ assert(tracker.move_assignments == 2);
----------------
philnik777 wrote:
I don't think this is valid. We can only check that `move_assignments` is at most two, since we only ever have upper bound guarantees (http://eel.is/c++draft/structure#specifications-7).
https://github.com/llvm/llvm-project/pull/116265
More information about the libcxx-commits
mailing list