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 // template<class InputIterator>
12 // basic_string(InputIterator begin, InputIterator end,
13 // const Allocator& a = Allocator()); // constexpr since C++20
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "test_iterators.h"
23 #include "min_allocator.h"
25 template <class Alloc
, class It
>
26 TEST_CONSTEXPR_CXX20
void test(It first
, It last
) {
27 typedef typename
std::iterator_traits
<It
>::value_type charT
;
28 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
30 LIBCPP_ASSERT(s2
.__invariants());
31 assert(s2
.size() == static_cast<std::size_t>(std::distance(first
, last
)));
33 for (It it
= first
; it
!= last
;) {
38 assert(s2
.get_allocator() == Alloc());
39 assert(s2
.capacity() >= s2
.size());
42 template <class Alloc
, class It
>
43 TEST_CONSTEXPR_CXX20
void test(It first
, It last
, const Alloc
& a
) {
44 typedef typename
std::iterator_traits
<It
>::value_type charT
;
45 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
47 LIBCPP_ASSERT(s2
.__invariants());
48 assert(s2
.size() == static_cast<std::size_t>(std::distance(first
, last
)));
50 for (It it
= first
; it
!= last
;) {
55 assert(s2
.get_allocator() == a
);
56 assert(s2
.capacity() >= s2
.size());
59 template <class Alloc
>
60 TEST_CONSTEXPR_CXX20
void test_string(const Alloc
& a
) {
61 const char* s
= "12345678901234567890123456789012345678901234567890";
64 test
<Alloc
>(s
, s
, Alloc(a
));
66 test
<Alloc
>(s
, s
+ 1);
67 test
<Alloc
>(s
, s
+ 1, Alloc(a
));
69 test
<Alloc
>(s
, s
+ 10);
70 test
<Alloc
>(s
, s
+ 10, Alloc(a
));
72 test
<Alloc
>(s
, s
+ 50);
73 test
<Alloc
>(s
, s
+ 50, Alloc(a
));
75 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
));
76 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
), Alloc(a
));
78 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1));
79 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1), Alloc(a
));
81 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 10));
82 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 10), Alloc(a
));
84 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 50));
85 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 50), Alloc(a
));
88 TEST_CONSTEXPR_CXX20
bool test() {
89 test_string(test_allocator
<char>());
90 test_string(test_allocator
<char>(2));
91 #if TEST_STD_VER >= 11
92 test_string(min_allocator
<char>());
95 static_assert((!std::is_constructible
<std::string
, std::string
, std::string
>::value
), "");
96 static_assert((!std::is_constructible
<std::string
, std::string
, std::string
, std::allocator
<char> >::value
), "");
102 int main(int, char**) {
104 #if TEST_STD_VER > 17
105 static_assert(test());