diff options
Diffstat (limited to 'test/std/strings/basic.string/string.cons')
8 files changed, 384 insertions, 14 deletions
diff --git a/test/std/strings/basic.string/string.cons/copy_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/copy_alloc.pass.cpp index b3447b94b..982bb4328 100644 --- a/test/std/strings/basic.string/string.cons/copy_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/copy_alloc.pass.cpp @@ -18,6 +18,65 @@ #include "test_allocator.h" #include "min_allocator.h" +#ifndef TEST_HAS_NO_EXCEPTIONS +template <class T> +struct alloc_imp { + bool active; + + alloc_imp() : active(true) {} + + T* allocate(std::size_t n) + { + if (active) + return static_cast<T*>(std::malloc(n * sizeof(T))); + else + throw std::bad_alloc(); + } + + void deallocate(T* p, std::size_t) { std::free(p); } + void activate () { active = true; } + void deactivate() { active = false; } +}; + +template <class T> +struct poca_alloc { + typedef T value_type; + typedef std::true_type propagate_on_container_copy_assignment; + + alloc_imp<T> *imp; + + poca_alloc(alloc_imp<T> *imp_) : imp (imp_) {} + + template <class U> + poca_alloc(const poca_alloc<U>& other) : imp(other.imp) {} + + T* allocate (std::size_t n) { return imp->allocate(n);} + void deallocate(T* p, std::size_t n) { imp->deallocate(p, n); } +}; + +template <typename T, typename U> +bool operator==(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs) +{ + return lhs.imp == rhs.imp; +} + +template <typename T, typename U> +bool operator!=(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs) +{ + return lhs.imp != rhs.imp; +} + +template <class S> +void test_assign(S &s1, const S& s2) +{ + try { s1 = s2; } + catch ( std::bad_alloc &) { return; } + assert(false); +} +#endif + + + template <class S> void test(S s1, const typename S::allocator_type& a) @@ -46,5 +105,27 @@ int main() test(S("1"), A()); test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A()); } + +#ifndef TEST_HAS_NO_EXCEPTIONS + { + typedef poca_alloc<char> A; + typedef std::basic_string<char, std::char_traits<char>, A> S; + const char * p1 = "This is my first string"; + const char * p2 = "This is my second string"; + + alloc_imp<char> imp1; + alloc_imp<char> imp2; + S s1(p1, A(&imp1)); + S s2(p2, A(&imp2)); + + assert(s1 == p1); + assert(s2 == p2); + + imp2.deactivate(); + test_assign(s1, s2); + assert(s1 == p1); + assert(s2 == p2); + } +#endif #endif } diff --git a/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp b/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp index 0c6362d96..f4ff0645a 100644 --- a/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp +++ b/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp @@ -42,8 +42,10 @@ int main() typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C; static_assert(std::is_nothrow_destructible<C>::value, ""); } +#if defined(_LIBCPP_VERSION) { typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C; - LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<C>::value, ""); + static_assert(!std::is_nothrow_destructible<C>::value, ""); } +#endif // _LIBCPP_VERSION } diff --git a/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp b/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp new file mode 100644 index 000000000..0fbd663db --- /dev/null +++ b/test/std/strings/basic.string/string.cons/implicit_deduction_guides.pass.cpp @@ -0,0 +1,300 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// UNSUPPORTED: libcpp-no-deduction-guides + +// <string> + +// Test that the constructors offered by std::basic_string are formulated +// so they're compatible with implicit deduction guides. + +#include <string> +#include <string_view> +#include <cassert> + +#include "test_macros.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "constexpr_char_traits.hpp" + +template <class T, class Alloc = std::allocator<T>> +using BStr = std::basic_string<T, std::char_traits<T>, Alloc>; + +// Overloads +// using A = Allocator; +// using BS = basic_string +// using BSV = basic_string_view +// --------------- +// (1) basic_string() - NOT TESTED +// (2) basic_string(A const&) - BROKEN +// (3) basic_string(size_type, CharT, const A& = A()) +// (4) basic_string(BS const&, size_type, A const& = A()) +// (5) basic_string(BS const&, size_type, size_type, A const& = A()) - PARTIALLY BROKEN +// (6) basic_string(const CharT*, size_type, A const& = A()) +// (7) basic_string(const CharT*, A const& = A()) +// (8) basic_string(InputIt, InputIt, A const& = A()) - BROKEN +// (9) basic_string(BS const&) +// (10) basic_string(BS const&, A const&) +// (11) basic_string(BS&&) +// (12) basic_string(BS&&, A const&) +// (13) basic_string(initializer_list<CharT>, A const& = A()) +// (14) basic_string(BSV, A const& = A()) +// (15) basic_string(const T&, size_type, size_type, A const& = A()) - BROKEN +int main() +{ + using TestSizeT = test_allocator<char>::size_type; + { // Testing (1) + // Nothing TODO. Cannot deduce without any arguments. + } + { // Testing (2) + // This overload isn't compatible with implicit deduction guides as + // specified in the standard. + // const test_allocator<char> alloc{}; + // std::basic_string s(alloc); + } + { // Testing (3) w/o allocator + std::basic_string s(6ull, 'a'); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "aaaaaa"); + + std::basic_string w(2ull, L'b'); + ASSERT_SAME_TYPE(decltype(w), std::wstring); + assert(w == L"bb"); + } + { // Testing (3) w/ allocator + std::basic_string s(6ull, 'a', test_allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>); + assert(s == "aaaaaa"); + + std::basic_string w(2ull, L'b', test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); + assert(w == L"bb"); + } + { // Testing (4) w/o allocator + const std::string sin("abc"); + std::basic_string s(sin, (size_t)1); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "bc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win, (TestSizeT)3); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"def"); + } + { // Testing (4) w/ allocator + const std::string sin("abc"); + std::basic_string s(sin, (size_t)1, std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "bc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"def"); + } + { // Testing (5) w/o allocator +#if 0 // FIXME: This doesn't work + const std::string sin("abc"); + std::basic_string s(sin, (size_t)1, (size_t)3); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "bc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win, (TestSizeT)2, (TestSizeT)3); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"cde"); +#endif + } + { // Testing (5) w/ allocator + const std::string sin("abc"); + std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "bc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"cde"); + } + { // Testing (6) w/o allocator + std::basic_string s("abc", (size_t)2); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "ab"); + + std::basic_string w(L"abcdef", (size_t)3); + ASSERT_SAME_TYPE(decltype(w), std::wstring); + assert(w == L"abc"); + } + { // Testing (6) w/ allocator + std::basic_string s("abc", (size_t)2, std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "ab"); + + using WStr = std::basic_string<wchar_t, + std::char_traits<wchar_t>, + test_allocator<wchar_t>>; + std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abc"); + } + { // Testing (7) w/o allocator + std::basic_string s("abc"); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + std::basic_string w(L"abcdef"); + ASSERT_SAME_TYPE(decltype(w), std::wstring); + assert(w == L"abcdef"); + } + { // Testing (7) w/ allocator + std::basic_string s("abc", std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using WStr = std::basic_string<wchar_t, + std::char_traits<wchar_t>, + test_allocator<wchar_t>>; + std::basic_string w(L"abcdef", test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abcdef"); + } + { // (8) w/o allocator + // This overload isn't compatible with implicit deduction guides as + // specified in the standard. + // FIXME: Propose adding an explicit guide to the standard? + } + { // (8) w/ allocator + // This overload isn't compatible with implicit deduction guides as + // specified in the standard. + // FIXME: Propose adding an explicit guide to the standard? +#if 0 + using It = input_iterator<const char*>; + const char* input = "abcdef"; + std::basic_string s(It(input), It(input + 3), std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); +#endif + } + { // Testing (9) + const std::string sin("abc"); + std::basic_string s(sin); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abcdef"); + } + { // Testing (10) + const std::string sin("abc"); + std::basic_string s(sin, std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + const WStr win(L"abcdef"); + std::basic_string w(win, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abcdef"); + } + { // Testing (11) + std::string sin("abc"); + std::basic_string s(std::move(sin)); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + WStr win(L"abcdef"); + std::basic_string w(std::move(win)); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abcdef"); + } + { // Testing (12) + std::string sin("abc"); + std::basic_string s(std::move(sin), std::allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using WStr = std::basic_string<wchar_t, + constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + WStr win(L"abcdef"); + std::basic_string w(std::move(win), test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), WStr); + assert(w == L"abcdef"); + } + { // Testing (13) w/o allocator + std::basic_string s({'a', 'b', 'c'}); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + std::basic_string w({L'a', L'b', L'c'}); + ASSERT_SAME_TYPE(decltype(w), std::wstring); + assert(w == L"abc"); + } + { // Testing (13) w/ allocator + std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>); + assert(s == "abc"); + + std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>); + assert(w == L"abc"); + } + { // Testing (14) w/o allocator + std::string_view sv("abc"); + std::basic_string s(sv); + ASSERT_SAME_TYPE(decltype(s), std::string); + assert(s == "abc"); + + using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>; + std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); + std::basic_string w(BSV); + ASSERT_SAME_TYPE(decltype(w), Expect); + assert(w == L"abcdef"); + } + { // Testing (14) w/ allocator + using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>; + std::string_view sv("abc"); + std::basic_string s(sv, test_allocator<char>{}); + ASSERT_SAME_TYPE(decltype(s), ExpectS); + assert(s == "abc"); + + using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>, + test_allocator<wchar_t>>; + std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef"); + std::basic_string w(BSV, test_allocator<wchar_t>{}); + ASSERT_SAME_TYPE(decltype(w), ExpectW); + assert(w == L"abcdef"); + } + { // Testing (15) + // This overload isn't compatible with implicit deduction guides as + // specified in the standard. + } +} diff --git a/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp index 1b10224cd..1f8369689 100644 --- a/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp @@ -29,7 +29,6 @@ test(It first, It last) { typedef typename std::iterator_traits<It>::value_type charT; typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; - typedef typename S::traits_type T; typedef typename S::allocator_type A; S s2(first, last); LIBCPP_ASSERT(s2.__invariants()); @@ -47,7 +46,6 @@ test(It first, It last, const A& a) { typedef typename std::iterator_traits<It>::value_type charT; typedef std::basic_string<charT, std::char_traits<charT>, A> S; - typedef typename S::traits_type T; S s2(first, last, a); LIBCPP_ASSERT(s2.__invariants()); assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); diff --git a/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp index f56780095..d9d451dc7 100644 --- a/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp @@ -56,7 +56,6 @@ int main() { { typedef test_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test(""); test("", A(2)); @@ -73,7 +72,6 @@ int main() #if TEST_STD_VER >= 11 { typedef min_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test(""); test("", A()); diff --git a/test/std/strings/basic.string/string.cons/pointer_size_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/pointer_size_alloc.pass.cpp index bcab9eb67..3c75a700e 100644 --- a/test/std/strings/basic.string/string.cons/pointer_size_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/pointer_size_alloc.pass.cpp @@ -53,7 +53,6 @@ int main() { { typedef test_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test("", 0); test("", 0, A(2)); @@ -70,7 +69,6 @@ int main() #if TEST_STD_VER >= 11 { typedef min_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test("", 0); test("", 0, A()); diff --git a/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp index 60443e9f3..2adf0049a 100644 --- a/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp @@ -26,7 +26,6 @@ void test(unsigned n, charT c) { typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; - typedef typename S::traits_type T; typedef typename S::allocator_type A; S s2(n, c); LIBCPP_ASSERT(s2.__invariants()); @@ -42,7 +41,6 @@ void test(unsigned n, charT c, const A& a) { typedef std::basic_string<charT, std::char_traits<charT>, A> S; - typedef typename S::traits_type T; S s2(n, c, a); LIBCPP_ASSERT(s2.__invariants()); assert(s2.size() == n); @@ -58,7 +56,6 @@ test(Tp n, Tp c) { typedef char charT; typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S; - typedef typename S::traits_type T; typedef typename S::allocator_type A; S s2(n, c); LIBCPP_ASSERT(s2.__invariants()); @@ -75,7 +72,6 @@ test(Tp n, Tp c, const A& a) { typedef char charT; typedef std::basic_string<charT, std::char_traits<charT>, A> S; - typedef typename S::traits_type T; S s2(n, c, a); LIBCPP_ASSERT(s2.__invariants()); assert(s2.size() == static_cast<std::size_t>(n)); @@ -89,7 +85,6 @@ int main() { { typedef test_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test(0, 'a'); test(0, 'a', A(2)); @@ -109,7 +104,6 @@ int main() #if TEST_STD_VER >= 11 { typedef min_allocator<char> A; - typedef std::basic_string<char, std::char_traits<char>, A> S; test(0, 'a'); test(0, 'a', A()); diff --git a/test/std/strings/basic.string/string.cons/substr.pass.cpp b/test/std/strings/basic.string/string.cons/substr.pass.cpp index 4fd974273..13a9a4b96 100644 --- a/test/std/strings/basic.string/string.cons/substr.pass.cpp +++ b/test/std/strings/basic.string/string.cons/substr.pass.cpp @@ -98,7 +98,6 @@ void test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) { typedef typename S::traits_type T; - typedef typename S::allocator_type A; if (pos <= str.size()) { |