[libc++] [test] Improve test_exceptions() in each string.modifiers test.

When checking the strong exception guarantee, also check that
iterators haven't been invalidated.

Reviewed as part of https://reviews.llvm.org/D98573
This commit is contained in:
Arthur O'Dwyer 2021-04-16 17:31:33 -04:00
parent e87479b00f
commit 036b80fcbb
4 changed files with 46 additions and 16 deletions

View 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

View 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

View 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

View 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