diff --git a/include/algorithm b/include/algorithm index 9c051198930d..71e5df37dfeb 100644 --- a/include/algorithm +++ b/include/algorithm @@ -1687,25 +1687,6 @@ search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const } // copy - -template -struct __libcpp_is_trivial_iterator -{ - static const bool value = is_pointer<_Iter>::value; -}; - -template -struct __libcpp_is_trivial_iterator > -{ - static const bool value = is_pointer<_Iter>::value; -}; - -template -struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> > -{ - static const bool value = is_pointer<_Iter>::value; -}; - template inline _LIBCPP_INLINE_VISIBILITY _Iter diff --git a/include/iterator b/include/iterator index 8dd6bd59c1a8..8d9b31101bdf 100644 --- a/include/iterator +++ b/include/iterator @@ -437,6 +437,12 @@ struct __is_bidirectional_iterator : public __has_iterator_category_convertible_ template struct __is_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {}; +template +struct __is_exactly_input_iterator + : public integral_constant::value && + !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {}; + template struct _LIBCPP_TYPE_VIS_ONLY iterator @@ -1404,6 +1410,23 @@ operator+(typename __wrap_iter<_Iter>::difference_type __n, return __x; } +template +struct __libcpp_is_trivial_iterator + : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {}; + +template +struct __libcpp_is_trivial_iterator > + : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {}; + +template +struct __libcpp_is_trivial_iterator > + : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {}; + +template +struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> > + : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {}; + + template inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 _Tp* diff --git a/include/string b/include/string index ee5db1aab63b..91502441e0c0 100644 --- a/include/string +++ b/include/string @@ -1201,6 +1201,30 @@ _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __basic_string_common) #pragma warning( pop ) #endif // _LIBCPP_MSVC +#ifdef _LIBCPP_NO_EXCEPTIONS +template +struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {}; +#elif defined(_LIBCPP_HAS_NO_NOEXCEPT) +template +struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {}; +#else +template ::value> +struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT(( + noexcept(++(declval<_Iter&>())) && + is_nothrow_assignable<_Iter&, _Iter>::value && + noexcept(declval<_Iter>() == declval<_Iter>()) && + noexcept(*declval<_Iter>()) +)) {}; + +template +struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {}; +#endif + + +template +struct __libcpp_string_gets_noexcept_iterator + : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {}; + #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT template @@ -1495,15 +1519,16 @@ public: template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, basic_string& >::type append(_InputIterator __first, _InputIterator __last); template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, basic_string& >::type append(_ForwardIterator __first, _ForwardIterator __last); @@ -1535,15 +1560,16 @@ public: template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, basic_string& >::type assign(_InputIterator __first, _InputIterator __last); template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, basic_string& >::type assign(_ForwardIterator __first, _ForwardIterator __last); @@ -1564,15 +1590,16 @@ public: template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, iterator >::type insert(const_iterator __pos, _InputIterator __first, _InputIterator __last); template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, iterator >::type insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last); @@ -1817,8 +1844,7 @@ private: template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value, void >::type __init(_InputIterator __first, _InputIterator __last); @@ -2195,8 +2221,7 @@ template template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value, void >::type basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) @@ -2494,15 +2519,14 @@ template template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator <_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, basic_string<_CharT, _Traits, _Allocator>& >::type basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last) { - clear(); - for (; __first != __last; ++__first) - push_back(*__first); + basic_string __temp(__first, __last, __alloc()); + assign(__temp.data(), __temp.size()); return *this; } @@ -2510,7 +2534,8 @@ template template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, basic_string<_CharT, _Traits, _Allocator>& >::type basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) @@ -2643,14 +2668,14 @@ template template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, + __is_exactly_input_iterator<_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, basic_string<_CharT, _Traits, _Allocator>& >::type basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last) { - for (; __first != __last; ++__first) - push_back(*__first); + basic_string __temp (__first, __last, __alloc()); + append(__temp.data(), __temp.size()); return *this; } @@ -2658,7 +2683,8 @@ template template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, basic_string<_CharT, _Traits, _Allocator>& >::type basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last) @@ -2774,9 +2800,9 @@ template template typename enable_if < - __is_input_iterator <_InputIterator>::value && - !__is_forward_iterator<_InputIterator>::value, - typename basic_string<_CharT, _Traits, _Allocator>::iterator + __is_exactly_input_iterator<_InputIterator>::value + || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value, + typename basic_string<_CharT, _Traits, _Allocator>::iterator >::type basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) { @@ -2785,24 +2811,16 @@ basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIt "string::insert(iterator, range) called with an iterator not" " referring to this string"); #endif - size_type __old_sz = size(); - difference_type __ip = __pos - begin(); - for (; __first != __last; ++__first) - push_back(*__first); - pointer __p = __get_pointer(); - _VSTD::rotate(__p + __ip, __p + __old_sz, __p + size()); -#if _LIBCPP_DEBUG_LEVEL >= 2 - return iterator(this, __p + __ip); -#else - return iterator(__p + __ip); -#endif + basic_string __temp(__first, __last, __alloc()); + return insert(__pos, __temp.data(), __temp.data() + __temp.size()); } template template typename enable_if < - __is_forward_iterator<_ForwardIterator>::value, + __is_forward_iterator<_ForwardIterator>::value + && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value, typename basic_string<_CharT, _Traits, _Allocator>::iterator >::type basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) @@ -3005,22 +3023,8 @@ typename enable_if basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2) { - for (; true; ++__i1, ++__j1) - { - if (__i1 == __i2) - { - if (__j1 != __j2) - insert(__i1, __j1, __j2); - break; - } - if (__j1 == __j2) - { - erase(__i1, __i2); - break; - } - traits_type::assign(const_cast(*__i1), *__j1); - } - return *this; + basic_string __temp(__j1, __j2, __alloc()); + return this->replace(__i1, __i2, __temp); } template diff --git a/test/libcxx/iterators/trivial_iterators.pass.cpp b/test/libcxx/iterators/trivial_iterators.pass.cpp new file mode 100644 index 000000000000..0b7a47316d65 --- /dev/null +++ b/test/libcxx/iterators/trivial_iterators.pass.cpp @@ -0,0 +1,187 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// + +// + +// __libcpp_is_trivial_iterator + +// __libcpp_is_trivial_iterator determines if an iterator is a "trivial" one, +// that can be used w/o worrying about its operations throwing exceptions. +// Pointers are trivial iterators. Libc++ has three "iterator wrappers": +// reverse_iterator, move_iterator, and __wrap_iter. If the underlying iterator +// is trivial, then those are as well. +// + +#include +#include +#include +#include +#include + +#include "test_iterators.h" + +#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#define DELETE_FUNCTION = delete +#else +#define DELETE_FUNCTION +#endif + +class T; // incomplete + +class my_input_iterator_tag : public std::input_iterator_tag {}; + +template +class my_input_iterator +{ + It it_; + + template friend class input_iterator; +public: + typedef my_input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::reference reference; + + It base() const {return it_;} + + my_input_iterator() : it_() {} + explicit my_input_iterator(It it) : it_(it) {} + template + my_input_iterator(const input_iterator& u) :it_(u.it_) {} + + reference operator*() const {return *it_;} + pointer operator->() const {return it_;} + + my_input_iterator& operator++() {++it_; return *this;} + my_input_iterator operator++(int) + {my_input_iterator tmp(*this); ++(*this); return tmp;} + + friend bool operator==(const my_input_iterator& x, const my_input_iterator& y) + {return x.it_ == y.it_;} + friend bool operator!=(const my_input_iterator& x, const my_input_iterator& y) + {return !(x == y);} + + template + void operator,(T const &) DELETE_FUNCTION; +}; + +template +inline +bool +operator==(const my_input_iterator& x, const my_input_iterator& y) +{ + return x.base() == y.base(); +} + +template +inline +bool +operator!=(const my_input_iterator& x, const my_input_iterator& y) +{ + return !(x == y); +} + + +int main() +{ +// basic tests + static_assert(( std::__libcpp_is_trivial_iterator::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::value), ""); + + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator >::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator >::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator >::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator > ::value), ""); + + static_assert(( std::__libcpp_is_trivial_iterator > > ::value), ""); + +// iterators in the libc++ test suite + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + static_assert((!std::__libcpp_is_trivial_iterator >::value), ""); + + +// Iterator classification + static_assert(( std::__is_input_iterator ::value), "" ); + static_assert(( std::__is_forward_iterator ::value), "" ); + static_assert(( std::__is_bidirectional_iterator::value), "" ); + static_assert(( std::__is_random_access_iterator::value), "" ); + static_assert((!std::__is_exactly_input_iterator::value), "" ); + + static_assert(( std::__is_input_iterator >::value), "" ); + static_assert((!std::__is_forward_iterator >::value), "" ); + static_assert((!std::__is_bidirectional_iterator >::value), "" ); + static_assert((!std::__is_random_access_iterator >::value), "" ); + static_assert(( std::__is_exactly_input_iterator >::value), "" ); + + static_assert(( std::__is_input_iterator >::value), "" ); + static_assert(( std::__is_forward_iterator >::value), "" ); + static_assert((!std::__is_bidirectional_iterator >::value), "" ); + static_assert((!std::__is_random_access_iterator >::value), "" ); + static_assert((!std::__is_exactly_input_iterator >::value), "" ); + + static_assert(( std::__is_input_iterator >::value), "" ); + static_assert(( std::__is_forward_iterator >::value), "" ); + static_assert(( std::__is_bidirectional_iterator >::value), "" ); + static_assert((!std::__is_random_access_iterator >::value), "" ); + static_assert((!std::__is_exactly_input_iterator >::value), "" ); + + static_assert(( std::__is_input_iterator >::value), "" ); + static_assert(( std::__is_forward_iterator >::value), "" ); + static_assert(( std::__is_bidirectional_iterator >::value), "" ); + static_assert(( std::__is_random_access_iterator >::value), "" ); + static_assert((!std::__is_exactly_input_iterator >::value), "" ); + + static_assert(( std::__is_input_iterator >::value), "" ); + static_assert((!std::__is_forward_iterator >::value), "" ); + static_assert((!std::__is_bidirectional_iterator >::value), "" ); + static_assert((!std::__is_random_access_iterator >::value), "" ); + static_assert(( std::__is_exactly_input_iterator >::value), "" ); + +// +// iterators from libc++'s containers +// + +// string + static_assert(( std::__libcpp_is_trivial_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::const_reverse_iterator>::value), ""); + +// vector + static_assert(( std::__libcpp_is_trivial_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::const_reverse_iterator>::value), ""); + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +// Initializer list (which has no reverse iterators) + static_assert(( std::__libcpp_is_trivial_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_is_trivial_iterator::const_iterator> ::value), ""); +#endif + +} diff --git a/test/libcxx/strings/iterators.exceptions.pass.cpp b/test/libcxx/strings/iterators.exceptions.pass.cpp new file mode 100644 index 000000000000..591782b5dd4a --- /dev/null +++ b/test/libcxx/strings/iterators.exceptions.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// + +// + +// __libcpp_is_trivial_iterator + +// __libcpp_string_gets_noexcept_iterator determines if an iterator can be used +// w/o worrying about whether or not certain operations can throw. +// This gives us a "fast path for string operations" +// + +#include +#include +#include +#include +#include + +#include "test_iterators.h" + +int main() +{ +// basic tests + static_assert(( std::__libcpp_string_gets_noexcept_iterator::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > > ::value), ""); + +// iterators in the libc++ test suite + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); + +#if __has_feature(cxx_noexcept) + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); +#else + static_assert((!std::__libcpp_string_gets_noexcept_iterator >::value), ""); +#endif + +// +// iterators from libc++'s containers +// + +// string + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_reverse_iterator>::value), ""); + +// vector + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_reverse_iterator>::value), ""); + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +// Initializer list (which has no reverse iterators) + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); +#endif +} diff --git a/test/libcxx/strings/iterators.noexcept.pass.cpp b/test/libcxx/strings/iterators.noexcept.pass.cpp new file mode 100644 index 000000000000..f39a4deac735 --- /dev/null +++ b/test/libcxx/strings/iterators.noexcept.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// + +// + +// __libcpp_is_trivial_iterator + +// __libcpp_string_gets_noexcept_iterator determines if an iterator can be used +// w/o worrying about whether or not certain operations can throw. +// This gives us a "fast path for string operations". +// +// When exceptions are disabled, all iterators should get this "fast path" +// + +#define _LIBCPP_NO_EXCEPTIONS + +#include +#include +#include +#include +#include + +#include "test_iterators.h" + +int main() +{ +// basic tests + static_assert(( std::__libcpp_string_gets_noexcept_iterator::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator > > ::value), ""); + +// iterators in the libc++ test suite + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + + static_assert(( std::__libcpp_string_gets_noexcept_iterator >::value), ""); + +// +// iterators from libc++'s containers +// + +// string + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_reverse_iterator>::value), ""); + +// vector + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::reverse_iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_reverse_iterator>::value), ""); + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +// Initializer list (which has no reverse iterators) + static_assert(( std::__libcpp_string_gets_noexcept_iterator::iterator> ::value), ""); + static_assert(( std::__libcpp_string_gets_noexcept_iterator::const_iterator> ::value), ""); +#endif +} diff --git a/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp index fb28e498c321..2abfbf0a172b 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp @@ -24,7 +24,7 @@ int main() s.append({'a', 'b', 'c'}); assert(s == "123abc"); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; S s("123"); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp index 3bf2afa25521..82e6fb5895e7 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp @@ -15,7 +15,7 @@ #include #include -#include "../../input_iterator.h" +#include "test_iterators.h" #include "min_allocator.h" template @@ -27,6 +27,20 @@ test(S s, It first, It last, S expected) assert(s == expected); } +template +void +test_exceptions(S s, It first, It last) +{ + S aCopy = s; + try { + s.append(first, last); + assert(false); + } + catch (...) {} + assert(s.__invariants()); + assert(s == aCopy); +} + int main() { { @@ -87,7 +101,7 @@ int main() test(S("12345678901234567890"), input_iterator(s), input_iterator(s+52), S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -147,4 +161,17 @@ int main() S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); } #endif + { // test iterator operations that throw + typedef std::string S; + typedef ThrowingIterator TIter; + typedef input_iterator 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()); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_append/pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/pointer.pass.cpp index 5ae15f0a0a4a..c0413d607e8f 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/pointer.pass.cpp @@ -43,7 +43,7 @@ int main() test(S("12345678901234567890"), "12345678901234567890", S("1234567890123456789012345678901234567890")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), "", S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/pointer_size.pass.cpp index 5a9e8e2b814b..f3ea3b0a89d7 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/pointer_size.pass.cpp @@ -48,7 +48,7 @@ int main() test(S("12345678901234567890"), "12345678901234567890", 20, S("1234567890123456789012345678901234567890")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), "", 0, S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp index b0a2eb5e5cbd..d6116d3f4823 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp @@ -33,7 +33,7 @@ int main() test(S("12345"), 'a', S("12345a")); test(S("12345678901234567890"), 'a', S("12345678901234567890a")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), 'a', S(1, 'a')); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/size_char.pass.cpp index 00e6ae375cce..a049e6112a8a 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/size_char.pass.cpp @@ -43,7 +43,7 @@ int main() test(S("12345678901234567890"), 1, 'a', S("12345678901234567890a")); test(S("12345678901234567890"), 10, 'a', S("12345678901234567890aaaaaaaaaa")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), 0, 'a', S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/string.pass.cpp index 31f174b4f305..e25627fa6a24 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/string.pass.cpp @@ -51,7 +51,7 @@ int main() test(S("12345678901234567890"), S("12345678901234567890"), S("1234567890123456789012345678901234567890")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), S(), S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_append/string_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_append/string_size_size.pass.cpp index 1d409b0932da..d67b0304b41a 100644 --- a/test/std/strings/basic.string/string.modifiers/string_append/string_size_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_append/string_size_size.pass.cpp @@ -80,7 +80,7 @@ int main() test(S("12345678901234567890"), S("12345678901234567890"), 5, 10, S("123456789012345678906789012345")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), S(), 0, 0, S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/initializer_list.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/initializer_list.pass.cpp index 2dae10745965..003af0eef827 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/initializer_list.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/initializer_list.pass.cpp @@ -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, min_allocator> S; S s("123"); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp index 83b5dd14b3f6..690aebd4c2a6 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp @@ -15,7 +15,7 @@ #include #include -#include "../../input_iterator.h" +#include "test_iterators.h" #include "min_allocator.h" template @@ -27,6 +27,20 @@ test(S s, It first, It last, S expected) assert(s == expected); } +template +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 TIter; + typedef input_iterator 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()); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp index adf24ac49877..07dbcea704dc 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp @@ -43,7 +43,7 @@ int main() test(S("12345678901234567890"), "12345678901234567890", S("12345678901234567890")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(), "", S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp index 476fe963de73..a9c71cec882b 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp @@ -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, min_allocator> S; test(S(), "", 0, S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/rv_string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/rv_string.pass.cpp index 4273860f41ad..b3d225a8d04a 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/rv_string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/rv_string.pass.cpp @@ -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, min_allocator> S; test(S(), S(), S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/size_char.pass.cpp index a8f747091c6d..9dd19d877c90 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/size_char.pass.cpp @@ -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, min_allocator> S; test(S(), 0, 'a', S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp index 99b53155d37e..2bc5dd223e62 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp @@ -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, min_allocator> S; test(S(), S(), S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp index db9e9a3271c6..275d249d304c 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp @@ -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, min_allocator> S; test(S(), S(), 0, 0, S()); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp index b9e5fa8bc064..6b0a3aa006ee 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/iter_char.pass.cpp @@ -56,7 +56,7 @@ int main() test(s, s.begin()+5, 'B', S("a567AB1432dcb")); test(s, s.begin()+6, 'C', S("a567ABC1432dcb")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; S s; diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/iter_initializer_list.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/iter_initializer_list.pass.cpp index e091662eae06..2adf846c1e36 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/iter_initializer_list.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/iter_initializer_list.pass.cpp @@ -29,7 +29,7 @@ int main() assert(i - s.begin() == 3); assert(s == "123abc456"); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; S s("123456"); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp index 38353dd2f4e4..4c29ab4ed093 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp @@ -19,7 +19,7 @@ #include #include -#include "../../input_iterator.h" +#include "test_iterators.h" #include "min_allocator.h" template @@ -33,6 +33,21 @@ test(S s, typename S::difference_type pos, It first, It last, S expected) assert(s == expected); } +template +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; + try { + s.insert(p, first, last); + assert(false); + } + catch (...) {} + assert(s.__invariants()); + assert(s == aCopy); +} + int main() { { @@ -80,7 +95,7 @@ int main() test(S("12345678901234567890"), 20, input_iterator(s), input_iterator(s+52), S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -127,6 +142,19 @@ int main() S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); } #endif + { // test iterator operations that throw + typedef std::string S; + typedef ThrowingIterator TIter; + typedef input_iterator IIter; + const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + test_exceptions(S(), 0, IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter()); + test_exceptions(S(), 0, IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter()); + test_exceptions(S(), 0, IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter()); + + test_exceptions(S(), 0, TIter(s, s+10, 4, TIter::TAIncrement), TIter()); + test_exceptions(S(), 0, TIter(s, s+10, 5, TIter::TADereference), TIter()); + test_exceptions(S(), 0, TIter(s, s+10, 6, TIter::TAComparison), TIter()); + } #if _LIBCPP_DEBUG >= 1 { std::string v; diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/iter_size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/iter_size_char.pass.cpp index 739c89404c1d..e6d1db6fc0df 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/iter_size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/iter_size_char.pass.cpp @@ -101,7 +101,7 @@ int main() test(S("abcdefghijklmnopqrst"), 20, 10, '1', S("abcdefghijklmnopqrst1111111111")); test(S("abcdefghijklmnopqrst"), 20, 20, '1', S("abcdefghijklmnopqrst11111111111111111111")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(""), 0, 0, '1', S("")); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp index 8277f88d99cd..247b3308f96e 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp @@ -124,7 +124,7 @@ int main() test(S("abcdefghijklmnopqrst"), 21, "1234567890", S("can't happen")); test(S("abcdefghijklmnopqrst"), 21, "12345678901234567890", S("can't happen")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(""), 0, "", S("")); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp index 4df5a14cbc0e..41b81231075e 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp @@ -365,7 +365,7 @@ int main() test(S("abcdefghijklmnopqrst"), 21, "12345678901234567890", 19, S("can't happen")); test(S("abcdefghijklmnopqrst"), 21, "12345678901234567890", 20, S("can't happen")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(""), 0, "", 0, S("")); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp index 20e030273962..945ec48012ba 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp @@ -125,7 +125,7 @@ int main() test(S("abcdefghijklmnopqrst"), 21, 10, '1', S("can't happen")); test(S("abcdefghijklmnopqrst"), 21, 20, '1', S("can't happen")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(""), 0, 0, '1', S("")); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp index 016dcaf396fe..04b47ec78988 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp @@ -124,7 +124,7 @@ int main() test(S("abcdefghijklmnopqrst"), 21, S("1234567890"), S("can't happen")); test(S("abcdefghijklmnopqrst"), 21, S("12345678901234567890"), S("can't happen")); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test(S(""), 0, S(""), S("")); diff --git a/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp index 3c4c818e0396..c3745c3749db 100644 --- a/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp @@ -1746,7 +1746,7 @@ int main() test29(); test30(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_initializer_list.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_initializer_list.pass.cpp index 20455b258c0a..b60a58c55e0e 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_initializer_list.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_initializer_list.pass.cpp @@ -24,7 +24,7 @@ int main() s.replace(s.cbegin() + 3, s.cbegin() + 6, {'a', 'b', 'c'}); assert(s == "123abc456"); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; S s("123def456"); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_iter_iter.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_iter_iter.pass.cpp index 9295c5bde9fa..057964112aa1 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_iter_iter.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_iter_iter.pass.cpp @@ -18,6 +18,7 @@ #include #include "min_allocator.h" +#include "test_iterators.h" template void @@ -34,6 +35,22 @@ test(S s, typename S::size_type pos1, typename S::size_type n1, It f, It l, S ex assert(s.size() == old_size - xlen + rlen); } +template +void +test_exceptions(S s, typename S::size_type pos1, typename S::size_type n1, It f, It l) +{ + typename S::const_iterator first = s.begin() + pos1; + typename S::const_iterator last = s.begin() + pos1 + n1; + S aCopy = s; + try { + s.replace(first, last, f, l); + assert(false); + } + catch (...) {} + assert(s.__invariants()); + assert(s == aCopy); +} + const char* str = "12345678901234567890"; template @@ -959,7 +976,7 @@ int main() test7(); test8(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); @@ -973,4 +990,17 @@ int main() test8(); } #endif + { // test iterator operations that throw + typedef std::string S; + typedef ThrowingIterator TIter; + typedef input_iterator IIter; + const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter()); + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter()); + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter()); + + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, TIter(s, s+10, 4, TIter::TAIncrement), TIter()); + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, TIter(s, s+10, 5, TIter::TADereference), TIter()); + test_exceptions(S("abcdefghijklmnopqrst"), 10, 5, TIter(s, s+10, 6, TIter::TAComparison), TIter()); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer.pass.cpp index cb4ce61d0942..c7551b928ec1 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer.pass.cpp @@ -274,7 +274,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer_size.pass.cpp index b1d6ab1ba579..087df7824ac2 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_pointer_size.pass.cpp @@ -959,7 +959,7 @@ int main() test7(); test8(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_size_char.pass.cpp index b0e2234a7e53..8be8b5298237 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_size_char.pass.cpp @@ -275,7 +275,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_string.pass.cpp index 0f95492fdb1a..b85a1063ebe1 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/iter_iter_string.pass.cpp @@ -274,7 +274,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer.pass.cpp index 18f32bfe222a..eb0e982ef16e 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer.pass.cpp @@ -365,7 +365,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer_size.pass.cpp index fc7ee98f9e34..898ab2a144c1 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_pointer_size.pass.cpp @@ -1308,7 +1308,7 @@ int main() test10(); test11(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_size_char.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_size_char.pass.cpp index cda4112257b0..e616ecc04261 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_size_char.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_size_char.pass.cpp @@ -364,7 +364,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string.pass.cpp index 59e4d5c28096..ea311dd1564f 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string.pass.cpp @@ -362,7 +362,7 @@ int main() test1(); test2(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string_size_size.pass.cpp index f5ac2e8b6023..01f4a1f6ccef 100644 --- a/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string_size_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_replace/size_size_string_size_size.pass.cpp @@ -5903,7 +5903,7 @@ int main() test54(); test55(); } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef std::basic_string, min_allocator> S; test0(); diff --git a/test/support/test_iterators.h b/test/support/test_iterators.h index e09fd834d215..d814879fa272 100644 --- a/test/support/test_iterators.h +++ b/test/support/test_iterators.h @@ -11,8 +11,11 @@ #define ITERATORS_H #include +#include #include +#include "test_macros.h" + #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS #define DELETE_FUNCTION = delete #else @@ -324,6 +327,202 @@ inline Iter base(random_access_iterator i) { return i.base(); } template // everything else inline Iter base(Iter i) { return i; } +template +struct ThrowingIterator { + typedef std::bidirectional_iterator_tag iterator_category; + typedef ptrdiff_t difference_type; + typedef const T value_type; + typedef const T * pointer; + typedef const T & reference; + + enum ThrowingAction { TAIncrement, TADecrement, TADereference, TAAssignment, TAComparison }; + +// Constructors + ThrowingIterator () + : begin_(nullptr), end_(nullptr), current_(nullptr), action_(TADereference), index_(0) {} + ThrowingIterator (const T *first, const T *last, size_t index = 0, ThrowingAction action = TADereference) + : begin_(first), end_(last), current_(first), action_(action), index_(index) {} + ThrowingIterator (const ThrowingIterator &rhs) + : begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_), action_(rhs.action_), index_(rhs.index_) {} + ThrowingIterator & operator= (const ThrowingIterator &rhs) + { + if (action_ == TAAssignment) + { + if (index_ == 0) + throw std::runtime_error ("throw from iterator assignment"); + else + --index_; + } + begin_ = rhs.begin_; + end_ = rhs.end_; + current_ = rhs.current_; + action_ = rhs.action_; + index_ = rhs.index_; + return *this; + } + +// iterator operations + reference operator*() const + { + if (action_ == TADereference) + { + if (index_ == 0) + throw std::runtime_error ("throw from iterator dereference"); + else + --index_; + } + return *current_; + } + + ThrowingIterator & operator++() + { + if (action_ == TAIncrement) + { + if (index_ == 0) + throw std::runtime_error ("throw from iterator increment"); + else + --index_; + } + ++current_; + return *this; + } + + ThrowingIterator operator++(int) + { + ThrowingIterator temp = *this; + ++(*this); + return temp; + } + + ThrowingIterator & operator--() + { + if (action_ == TADecrement) + { + if (index_ == 0) + throw std::runtime_error ("throw from iterator decrement"); + else + --index_; + } + --current_; + return *this; + } + + ThrowingIterator operator--(int) { + ThrowingIterator temp = *this; + --(*this); + return temp; + } + + bool operator== (const ThrowingIterator &rhs) const + { + if (action_ == TAComparison) + { + if (index_ == 0) + throw std::runtime_error ("throw from iterator comparison"); + else + --index_; + } + bool atEndL = current_ == end_; + bool atEndR = rhs.current_ == rhs.end_; + if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not. + if (atEndL) return true; // both are at the end (or empty) + return current_ == rhs.current_; + } + +private: + const T* begin_; + const T* end_; + const T* current_; + ThrowingAction action_; + mutable size_t index_; +}; + +template +bool operator== (const ThrowingIterator& a, const ThrowingIterator& b) +{ return a.operator==(b); } + +template +bool operator!= (const ThrowingIterator& a, const ThrowingIterator& b) +{ return !a.operator==(b); } + +template +struct NonThrowingIterator { + typedef std::bidirectional_iterator_tag iterator_category; + typedef ptrdiff_t difference_type; + typedef const T value_type; + typedef const T * pointer; + typedef const T & reference; + +// Constructors + NonThrowingIterator () + : begin_(nullptr), end_(nullptr), current_(nullptr) {} + NonThrowingIterator (const T *first, const T* last) + : begin_(first), end_(last), current_(first) {} + NonThrowingIterator (const NonThrowingIterator &rhs) + : begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_) {} + NonThrowingIterator & operator= (const NonThrowingIterator &rhs) TEST_NOEXCEPT + { + begin_ = rhs.begin_; + end_ = rhs.end_; + current_ = rhs.current_; + return *this; + } + +// iterator operations + reference operator*() const TEST_NOEXCEPT + { + return *current_; + } + + NonThrowingIterator & operator++() TEST_NOEXCEPT + { + ++current_; + return *this; + } + + NonThrowingIterator operator++(int) TEST_NOEXCEPT + { + NonThrowingIterator temp = *this; + ++(*this); + return temp; + } + + NonThrowingIterator & operator--() TEST_NOEXCEPT + { + --current_; + return *this; + } + + NonThrowingIterator operator--(int) TEST_NOEXCEPT + { + NonThrowingIterator temp = *this; + --(*this); + return temp; + } + + bool operator== (const NonThrowingIterator &rhs) const TEST_NOEXCEPT + { + bool atEndL = current_ == end_; + bool atEndR = rhs.current_ == rhs.end_; + if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not. + if (atEndL) return true; // both are at the end (or empty) + return current_ == rhs.current_; + } + +private: + const T* begin_; + const T* end_; + const T* current_; +}; + +template +bool operator== (const NonThrowingIterator& a, const NonThrowingIterator& b) TEST_NOEXCEPT +{ return a.operator==(b); } + +template +bool operator!= (const NonThrowingIterator& a, const NonThrowingIterator& b) TEST_NOEXCEPT +{ return !a.operator==(b); } + #undef DELETE_FUNCTION #endif // ITERATORS_H