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"
24 #include "asan_testing.h"
26 template <class Alloc
, class It
>
27 TEST_CONSTEXPR_CXX20
void test(It first
, It last
) {
28 typedef typename
std::iterator_traits
<It
>::value_type charT
;
29 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
31 LIBCPP_ASSERT(s2
.__invariants());
32 assert(s2
.size() == static_cast<std::size_t>(std::distance(first
, last
)));
34 for (It it
= first
; it
!= last
;) {
39 assert(s2
.get_allocator() == Alloc());
40 assert(s2
.capacity() >= s2
.size());
41 LIBCPP_ASSERT(is_string_asan_correct(s2
));
44 template <class Alloc
, class It
>
45 TEST_CONSTEXPR_CXX20
void test(It first
, It last
, const Alloc
& a
) {
46 typedef typename
std::iterator_traits
<It
>::value_type charT
;
47 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
49 LIBCPP_ASSERT(s2
.__invariants());
50 assert(s2
.size() == static_cast<std::size_t>(std::distance(first
, last
)));
52 for (It it
= first
; it
!= last
;) {
57 assert(s2
.get_allocator() == a
);
58 assert(s2
.capacity() >= s2
.size());
59 LIBCPP_ASSERT(is_string_asan_correct(s2
));
62 template <class Alloc
>
63 TEST_CONSTEXPR_CXX20
void test_string(const Alloc
& a
) {
64 const char* s
= "12345678901234567890123456789012345678901234567890";
67 test
<Alloc
>(s
, s
, Alloc(a
));
69 test
<Alloc
>(s
, s
+ 1);
70 test
<Alloc
>(s
, s
+ 1, Alloc(a
));
72 test
<Alloc
>(s
, s
+ 10);
73 test
<Alloc
>(s
, s
+ 10, Alloc(a
));
75 test
<Alloc
>(s
, s
+ 50);
76 test
<Alloc
>(s
, s
+ 50, Alloc(a
));
78 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
));
79 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
), Alloc(a
));
81 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1));
82 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1), Alloc(a
));
84 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 10));
85 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 10), Alloc(a
));
87 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 50));
88 test
<Alloc
>(cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 50), Alloc(a
));
91 TEST_CONSTEXPR_CXX20
bool test() {
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>());
98 static_assert((!std::is_constructible
<std::string
, std::string
, std::string
>::value
), "");
99 static_assert((!std::is_constructible
<std::string
, std::string
, std::string
, std::allocator
<char> >::value
), "");
105 int main(int, char**) {
107 #if TEST_STD_VER > 17
108 static_assert(test());