mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-23 10:07:41 +08:00
Fix PR#25973 : 'basic_string::assign(InputIt, InputIt) doesn't provide the strong exception safety guarantee'. This turned out to be a pervasive problem in <string>, which required a fair amount of rework. Add in an optimization for when iterators provide noexcept increment/comparison/assignment/dereference (which covers many of the iterators in libc++). Reviewed as http://reviews.llvm.org/D15862
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@257682 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -24,7 +24,7 @@ int main()
|
||||
s.assign({'a', 'b', 'c'});
|
||||
assert(s == "abc");
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
S s("123");
|
||||
|
@@ -15,7 +15,7 @@
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
#include "../../input_iterator.h"
|
||||
#include "test_iterators.h"
|
||||
#include "min_allocator.h"
|
||||
|
||||
template <class S, class It>
|
||||
@@ -27,6 +27,20 @@ test(S s, It first, It last, S expected)
|
||||
assert(s == expected);
|
||||
}
|
||||
|
||||
template <class S, class It>
|
||||
void
|
||||
test_exceptions(S s, It first, It last)
|
||||
{
|
||||
S aCopy = s;
|
||||
try {
|
||||
s.assign(first, last);
|
||||
assert(false);
|
||||
}
|
||||
catch (...) {}
|
||||
assert(s.__invariants());
|
||||
assert(s == aCopy);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
@@ -147,4 +161,17 @@ int main()
|
||||
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
|
||||
}
|
||||
#endif
|
||||
{ // test iterator operations that throw
|
||||
typedef std::string S;
|
||||
typedef ThrowingIterator<char> TIter;
|
||||
typedef input_iterator<TIter> IIter;
|
||||
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter());
|
||||
test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter());
|
||||
test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter());
|
||||
|
||||
test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
|
||||
test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
|
||||
test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
|
||||
}
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ int main()
|
||||
test(S("12345678901234567890"), "12345678901234567890",
|
||||
S("12345678901234567890"));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), "", S());
|
||||
|
@@ -48,7 +48,7 @@ int main()
|
||||
test(S("12345678901234567890"), "12345678901234567890", 20,
|
||||
S("12345678901234567890"));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), "", 0, S());
|
||||
|
@@ -52,7 +52,7 @@ int main()
|
||||
test(S("12345678901234567890"), S("12345678901234567890"),
|
||||
S("12345678901234567890"));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), S(), S());
|
||||
|
@@ -43,7 +43,7 @@ int main()
|
||||
test(S("12345678901234567890"), 1, 'a', S(1, 'a'));
|
||||
test(S("12345678901234567890"), 10, 'a', S(10, 'a'));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), 0, 'a', S());
|
||||
|
@@ -51,7 +51,7 @@ int main()
|
||||
test(S("12345678901234567890"), S("12345678901234567890"),
|
||||
S("12345678901234567890"));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), S(), S());
|
||||
|
@@ -80,7 +80,7 @@ int main()
|
||||
test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
|
||||
S("6789012345"));
|
||||
}
|
||||
#if __cplusplus >= 201103L
|
||||
#if TEST_STD_VER >= 11
|
||||
{
|
||||
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
|
||||
test(S(), S(), 0, 0, S());
|
||||
|
Reference in New Issue
Block a user