[libc++] [test] Improve test_exceptions() in each string.modifiers test.
authorArthur O'Dwyer <arthur.j.odwyer@gmail.com>
Fri, 16 Apr 2021 21:31:33 +0000 (17:31 -0400)
committerArthur O'Dwyer <arthur.j.odwyer@gmail.com>
Mon, 26 Apr 2021 20:22:43 +0000 (16:22 -0400)
When checking the strong exception guarantee, also check that
iterators haven't been invalidated.

Reviewed as part of https://reviews.llvm.org/D98573

libcxx/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp
libcxx/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp
libcxx/test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp
libcxx/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_iter_iter.pass.cpp

index 8c0d3d7..31ba8b0 100644 (file)
@@ -34,14 +34,21 @@ template <class S, class It>
 void
 test_exceptions(S s, It first, It last)
 {
-    S aCopy = s;
+    S original = s;
+    typename S::iterator begin = s.begin();
+    typename S::iterator end = s.end();
+
     try {
         s.append(first, last);
         assert(false);
-        }
-    catch (...) {}
+    } catch (...) {}
+
+    // Part of "no effects" is that iterators and pointers
+    // into the string must not have been invalidated.
     LIBCPP_ASSERT(s.__invariants());
-    assert(s == aCopy);
+    assert(s == original);
+    assert(s.begin() == begin);
+    assert(s.end() == end);
 }
 #endif
 
index a4bad33..60201c0 100644 (file)
@@ -34,14 +34,21 @@ template <class S, class It>
 void
 test_exceptions(S s, It first, It last)
 {
-    S aCopy = s;
+    S original = s;
+    typename S::iterator begin = s.begin();
+    typename S::iterator end = s.end();
+
     try {
         s.assign(first, last);
         assert(false);
-    }
-    catch (...) {}
+    } catch (...) {}
+
+    // Part of "no effects" is that iterators and pointers
+    // into the string must not have been invalidated.
     LIBCPP_ASSERT(s.__invariants());
-    assert(s == aCopy);
+    assert(s == original);
+    assert(s.begin() == begin);
+    assert(s.end() == end);
 }
 #endif
 
index 471e301..d86da1c 100644 (file)
@@ -37,14 +37,22 @@ void
 test_exceptions(S s, typename S::difference_type pos, It first, It last)
 {
     typename S::const_iterator p = s.cbegin() + pos;
-    S aCopy = s;
+
+    S original = s;
+    typename S::iterator begin = s.begin();
+    typename S::iterator end = s.end();
+
     try {
         s.insert(p, first, last);
         assert(false);
-        }
-    catch (...) {}
+    } catch (...) {}
+
+    // Part of "no effects" is that iterators and pointers
+    // into the string must not have been invalidated.
     LIBCPP_ASSERT(s.__invariants());
-    assert(s == aCopy);
+    assert(s == original);
+    assert(s.begin() == begin);
+    assert(s.end() == end);
 }
 #endif
 
index 3e6907a..d5e58db 100644 (file)
@@ -44,14 +44,22 @@ test_exceptions(S s, typename S::size_type pos1, typename S::size_type n1, It f,
 {
     typename S::const_iterator first = s.begin() + pos1;
     typename S::const_iterator last = s.begin() + pos1 + n1;
-    S aCopy = s;
+
+    S original = s;
+    typename S::iterator begin = s.begin();
+    typename S::iterator end = s.end();
+
     try {
         s.replace(first, last, f, l);
         assert(false);
-        }
-    catch (...) {}
+    } catch (...) {}
+
+    // Part of "no effects" is that iterators and pointers
+    // into the string must not have been invalidated.
     LIBCPP_ASSERT(s.__invariants());
-    assert(s == aCopy);
+    assert(s == original);
+    assert(s.begin() == begin);
+    assert(s.end() == end);
 }
 #endif