1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
16 #include <string_view>
18 #include <type_traits>
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "test_macros.h"
24 static_assert(!std::is_convertible
<std::string_view
, std::string
const&>::value
, "");
25 static_assert(!std::is_convertible
<std::string_view
, std::string
>::value
, "");
27 template <class Alloc
, class CharT
>
28 TEST_CONSTEXPR_CXX20
void test(std::basic_string_view
<CharT
> sv
) {
29 typedef std::basic_string
<CharT
, std::char_traits
<CharT
>, Alloc
> S
;
30 typedef typename
S::traits_type T
;
33 LIBCPP_ASSERT(s2
.__invariants());
34 assert(s2
.size() == sv
.size());
35 assert(T::compare(s2
.data(), sv
.data(), sv
.size()) == 0);
36 assert(s2
.get_allocator() == Alloc());
37 assert(s2
.capacity() >= s2
.size());
42 LIBCPP_ASSERT(s2
.__invariants());
43 assert(s2
.size() == sv
.size());
44 assert(T::compare(s2
.data(), sv
.data(), sv
.size()) == 0);
45 assert(s2
.get_allocator() == Alloc());
46 assert(s2
.capacity() >= s2
.size());
50 template <class Alloc
, class CharT
>
51 TEST_CONSTEXPR_CXX20
void test(std::basic_string_view
<CharT
> sv
, const Alloc
& a
) {
52 typedef std::basic_string
<CharT
, std::char_traits
<CharT
>, Alloc
> S
;
53 typedef typename
S::traits_type T
;
56 LIBCPP_ASSERT(s2
.__invariants());
57 assert(s2
.size() == sv
.size());
58 assert(T::compare(s2
.data(), sv
.data(), sv
.size()) == 0);
59 assert(s2
.get_allocator() == a
);
60 assert(s2
.capacity() >= s2
.size());
65 LIBCPP_ASSERT(s2
.__invariants());
66 assert(s2
.size() == sv
.size());
67 assert(T::compare(s2
.data(), sv
.data(), sv
.size()) == 0);
68 assert(s2
.get_allocator() == a
);
69 assert(s2
.capacity() >= s2
.size());
73 template <class Alloc
>
74 TEST_CONSTEXPR_CXX20
void test_string(const Alloc
& a
) {
75 typedef std::basic_string_view
<char, std::char_traits
<char> > SV
;
78 test
<Alloc
>(SV(""), Alloc(a
));
81 test
<Alloc
>(SV("1"), Alloc(a
));
83 test
<Alloc
>(SV("1234567980"));
84 test
<Alloc
>(SV("1234567980"), Alloc(a
));
86 test
<Alloc
>(SV("123456798012345679801234567980123456798012345679801234567980"));
87 test
<Alloc
>(SV("123456798012345679801234567980123456798012345679801234567980"), Alloc(a
));
90 TEST_CONSTEXPR_CXX20
bool test() {
91 test_string(std::allocator
<char>());
92 test_string(test_allocator
<char>());
93 test_string(test_allocator
<char>(2));
94 #if TEST_STD_VER >= 11
95 test_string(min_allocator
<char>());
101 int main(int, char**) {
103 #if TEST_STD_VER > 17
104 static_assert(test());