176b26852SMarshall Clow //===----------------------------------------------------------------------===//
276b26852SMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
676b26852SMarshall Clow //
776b26852SMarshall Clow //===----------------------------------------------------------------------===//
876b26852SMarshall Clow
976b26852SMarshall Clow // <string>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
1176b26852SMarshall Clow
1276b26852SMarshall Clow // template<class charT,
1376b26852SMarshall Clow // class traits,
1476b26852SMarshall Clow // class Allocator = allocator<charT>
1576b26852SMarshall Clow // >
1676b26852SMarshall Clow // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
1776b26852SMarshall Clow // -> basic_string<charT, traits, Allocator>;
1876b26852SMarshall Clow //
1976b26852SMarshall Clow // The deduction guide shall not participate in overload resolution if Allocator
2076b26852SMarshall Clow // is a type that does not qualify as an allocator.
2176b26852SMarshall Clow
2276b26852SMarshall Clow #include <string>
2376b26852SMarshall Clow #include <string_view>
2476b26852SMarshall Clow #include <iterator>
2576b26852SMarshall Clow #include <memory>
2676b26852SMarshall Clow #include <type_traits>
2776b26852SMarshall Clow #include <cassert>
2876b26852SMarshall Clow #include <cstddef>
2976b26852SMarshall Clow
3076b26852SMarshall Clow #include "test_macros.h"
3176b26852SMarshall Clow #include "test_allocator.h"
3276b26852SMarshall Clow #include "min_allocator.h"
3376b26852SMarshall Clow
348ec49997SLouis Dionne template <class StringView, class Allocator, class = void>
358ec49997SLouis Dionne struct CanDeduce : std::false_type { };
368ec49997SLouis Dionne
378ec49997SLouis Dionne template <class StringView, class Allocator>
388ec49997SLouis Dionne struct CanDeduce<StringView, Allocator, decltype((void)
398ec49997SLouis Dionne std::basic_string{std::declval<StringView>(), std::declval<Allocator>()}
408ec49997SLouis Dionne )> : std::true_type { };
418ec49997SLouis Dionne
428ec49997SLouis Dionne struct NotAnAllocator { };
438ec49997SLouis Dionne static_assert( CanDeduce<std::string_view, std::allocator<char>>::value);
448ec49997SLouis Dionne static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value);
458ec49997SLouis Dionne
test()46*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
4776b26852SMarshall Clow {
4876b26852SMarshall Clow std::string_view sv = "12345678901234";
4976b26852SMarshall Clow std::basic_string s1(sv);
5076b26852SMarshall Clow using S = decltype(s1); // what type did we get?
5176b26852SMarshall Clow static_assert(std::is_same_v<S::value_type, char>, "");
5276b26852SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
5376b26852SMarshall Clow static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
5476b26852SMarshall Clow assert(s1.size() == sv.size());
5576b26852SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
5676b26852SMarshall Clow }
5776b26852SMarshall Clow
5876b26852SMarshall Clow {
5976b26852SMarshall Clow std::string_view sv = "12345678901234";
6076b26852SMarshall Clow std::basic_string s1{sv, std::allocator<char>{}};
6176b26852SMarshall Clow using S = decltype(s1); // what type did we get?
6276b26852SMarshall Clow static_assert(std::is_same_v<S::value_type, char>, "");
6376b26852SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
6476b26852SMarshall Clow static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
6576b26852SMarshall Clow assert(s1.size() == sv.size());
6676b26852SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
6776b26852SMarshall Clow }
68f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
6976b26852SMarshall Clow {
7076b26852SMarshall Clow std::wstring_view sv = L"12345678901234";
7176b26852SMarshall Clow std::basic_string s1{sv, test_allocator<wchar_t>{}};
7276b26852SMarshall Clow using S = decltype(s1); // what type did we get?
7376b26852SMarshall Clow static_assert(std::is_same_v<S::value_type, wchar_t>, "");
7476b26852SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
7576b26852SMarshall Clow static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
7676b26852SMarshall Clow assert(s1.size() == sv.size());
7776b26852SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
7876b26852SMarshall Clow }
79f4c1258dSLouis Dionne #endif
80*425620ccSNikolas Klauser #ifndef TEST_HAS_NO_CHAR8_T
817dad0bd6SMarshall Clow {
827dad0bd6SMarshall Clow std::u8string_view sv = u8"12345678901234";
837dad0bd6SMarshall Clow std::basic_string s1{sv, min_allocator<char8_t>{}};
847dad0bd6SMarshall Clow using S = decltype(s1); // what type did we get?
857dad0bd6SMarshall Clow static_assert(std::is_same_v<S::value_type, char8_t>, "");
867dad0bd6SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");
877dad0bd6SMarshall Clow static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");
887dad0bd6SMarshall Clow assert(s1.size() == sv.size());
897dad0bd6SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
907dad0bd6SMarshall Clow }
917dad0bd6SMarshall Clow #endif
9276b26852SMarshall Clow {
9376b26852SMarshall Clow std::u16string_view sv = u"12345678901234";
9476b26852SMarshall Clow std::basic_string s1{sv, min_allocator<char16_t>{}};
9576b26852SMarshall Clow using S = decltype(s1); // what type did we get?
9676b26852SMarshall Clow static_assert(std::is_same_v<S::value_type, char16_t>, "");
9776b26852SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
9876b26852SMarshall Clow static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
9976b26852SMarshall Clow assert(s1.size() == sv.size());
10076b26852SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
10176b26852SMarshall Clow }
10276b26852SMarshall Clow {
10376b26852SMarshall Clow std::u32string_view sv = U"12345678901234";
10476b26852SMarshall Clow std::basic_string s1{sv, explicit_allocator<char32_t>{}};
10576b26852SMarshall Clow using S = decltype(s1); // what type did we get?
10676b26852SMarshall Clow static_assert(std::is_same_v<S::value_type, char32_t>, "");
10776b26852SMarshall Clow static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
10876b26852SMarshall Clow static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
10976b26852SMarshall Clow assert(s1.size() == sv.size());
11076b26852SMarshall Clow assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
11176b26852SMarshall Clow }
1122df59c50SJF Bastien
113e85018b7SNikolas Klauser return true;
114e85018b7SNikolas Klauser }
115e85018b7SNikolas Klauser
main(int,char **)116e85018b7SNikolas Klauser int main(int, char**)
117e85018b7SNikolas Klauser {
118e85018b7SNikolas Klauser test();
119e85018b7SNikolas Klauser #if TEST_STD_VER > 17
120*425620ccSNikolas Klauser static_assert(test());
121e85018b7SNikolas Klauser #endif
122e85018b7SNikolas Klauser
1232df59c50SJF Bastien return 0;
12476b26852SMarshall Clow }
125