| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <string> |
| 10 | // UNSUPPORTED: c++03, c++11, c++14 |
| 11 | |
| 12 | // template<class charT, |
| 13 | // class traits, |
| 14 | // class Allocator = allocator<charT> |
| 15 | // > |
| 16 | // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator()) |
| 17 | // -> basic_string<charT, traits, Allocator>; |
| 18 | // |
| 19 | // The deduction guide shall not participate in overload resolution if Allocator |
| 20 | // is a type that does not qualify as an allocator. |
| 21 | |
| 22 | #include <string> |
| 23 | #include <string_view> |
| 24 | #include <iterator> |
| 25 | #include <memory> |
| 26 | #include <type_traits> |
| 27 | #include <cassert> |
| 28 | #include <cstddef> |
| 29 | |
| 30 | #include "test_macros.h" |
| 31 | #include "test_allocator.h" |
| 32 | #include "min_allocator.h" |
| 33 | |
| 34 | template <class StringView, class Allocator, class = void> |
| 35 | struct CanDeduce : std::false_type {}; |
| 36 | |
| 37 | template <class StringView, class Allocator> |
| 38 | struct CanDeduce<StringView, |
| 39 | Allocator, |
| 40 | decltype((void)std::basic_string{std::declval<StringView>(), std::declval<Allocator>()})> |
| 41 | : std::true_type {}; |
| 42 | |
| 43 | struct NotAnAllocator {}; |
| 44 | static_assert(CanDeduce<std::string_view, std::allocator<char>>::value); |
| 45 | static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value); |
| 46 | |
| 47 | TEST_CONSTEXPR_CXX20 bool test() { |
| 48 | { |
| 49 | std::string_view sv = "12345678901234" ; |
| 50 | std::basic_string s1(sv); |
| 51 | using S = decltype(s1); // what type did we get? |
| 52 | static_assert(std::is_same_v<S::value_type, char>, "" ); |
| 53 | static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "" ); |
| 54 | static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "" ); |
| 55 | assert(s1.size() == sv.size()); |
| 56 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | std::string_view sv = "12345678901234" ; |
| 61 | std::basic_string s1{sv, std::allocator<char>{}}; |
| 62 | using S = decltype(s1); // what type did we get? |
| 63 | static_assert(std::is_same_v<S::value_type, char>, "" ); |
| 64 | static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "" ); |
| 65 | static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "" ); |
| 66 | assert(s1.size() == sv.size()); |
| 67 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 68 | } |
| 69 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 70 | { |
| 71 | std::wstring_view sv = L"12345678901234" ; |
| 72 | std::basic_string s1{sv, test_allocator<wchar_t>{}}; |
| 73 | using S = decltype(s1); // what type did we get? |
| 74 | static_assert(std::is_same_v<S::value_type, wchar_t>, "" ); |
| 75 | static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "" ); |
| 76 | static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "" ); |
| 77 | assert(s1.size() == sv.size()); |
| 78 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 79 | } |
| 80 | #endif |
| 81 | #ifndef TEST_HAS_NO_CHAR8_T |
| 82 | { |
| 83 | std::u8string_view sv = u8"12345678901234" ; |
| 84 | std::basic_string s1{sv, min_allocator<char8_t>{}}; |
| 85 | using S = decltype(s1); // what type did we get? |
| 86 | static_assert(std::is_same_v<S::value_type, char8_t>, "" ); |
| 87 | static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "" ); |
| 88 | static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "" ); |
| 89 | assert(s1.size() == sv.size()); |
| 90 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 91 | } |
| 92 | #endif |
| 93 | { |
| 94 | std::u16string_view sv = u"12345678901234" ; |
| 95 | std::basic_string s1{sv, min_allocator<char16_t>{}}; |
| 96 | using S = decltype(s1); // what type did we get? |
| 97 | static_assert(std::is_same_v<S::value_type, char16_t>, "" ); |
| 98 | static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "" ); |
| 99 | static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "" ); |
| 100 | assert(s1.size() == sv.size()); |
| 101 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 102 | } |
| 103 | { |
| 104 | std::u32string_view sv = U"12345678901234" ; |
| 105 | std::basic_string s1{sv, explicit_allocator<char32_t>{}}; |
| 106 | using S = decltype(s1); // what type did we get? |
| 107 | static_assert(std::is_same_v<S::value_type, char32_t>, "" ); |
| 108 | static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "" ); |
| 109 | static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "" ); |
| 110 | assert(s1.size() == sv.size()); |
| 111 | assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); |
| 112 | } |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | int main(int, char**) { |
| 118 | test(); |
| 119 | #if TEST_STD_VER > 17 |
| 120 | static_assert(test()); |
| 121 | #endif |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |