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 // basic_string(size_type n, charT c, const Allocator& a = Allocator()); // constexpr since C++20
19 #include "test_macros.h"
20 #include "test_allocator.h"
21 #include "min_allocator.h"
22 #include "asan_testing.h"
24 template <class Alloc
, class charT
>
25 TEST_CONSTEXPR_CXX20
void test(unsigned n
, charT c
) {
26 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
28 LIBCPP_ASSERT(s2
.__invariants());
29 assert(s2
.size() == n
);
30 for (unsigned i
= 0; i
< n
; ++i
)
32 assert(s2
.get_allocator() == Alloc());
33 assert(s2
.capacity() >= s2
.size());
34 LIBCPP_ASSERT(is_string_asan_correct(s2
));
37 template <class Alloc
, class charT
>
38 TEST_CONSTEXPR_CXX20
void test(unsigned n
, charT c
, const Alloc
& a
) {
39 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
41 LIBCPP_ASSERT(s2
.__invariants());
42 assert(s2
.size() == n
);
43 for (unsigned i
= 0; i
< n
; ++i
)
45 assert(s2
.get_allocator() == a
);
46 assert(s2
.capacity() >= s2
.size());
47 LIBCPP_ASSERT(is_string_asan_correct(s2
));
50 template <class Alloc
, class Tp
>
51 TEST_CONSTEXPR_CXX20
void test(Tp n
, Tp c
) {
53 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
55 LIBCPP_ASSERT(s2
.__invariants());
56 assert(s2
.size() == static_cast<std::size_t>(n
));
57 for (int i
= 0; i
< n
; ++i
)
59 assert(s2
.get_allocator() == Alloc());
60 assert(s2
.capacity() >= s2
.size());
63 template <class Alloc
, class Tp
>
64 TEST_CONSTEXPR_CXX20
void test(Tp n
, Tp c
, const Alloc
& a
) {
66 typedef std::basic_string
<charT
, std::char_traits
<charT
>, Alloc
> S
;
68 LIBCPP_ASSERT(s2
.__invariants());
69 assert(s2
.size() == static_cast<std::size_t>(n
));
70 for (int i
= 0; i
< n
; ++i
)
72 assert(s2
.get_allocator() == a
);
73 assert(s2
.capacity() >= s2
.size());
76 template <class Alloc
>
77 TEST_CONSTEXPR_CXX20
void test_string(const Alloc
& a
) {
79 test
<Alloc
>(0, 'a', Alloc(a
));
82 test
<Alloc
>(1, 'a', Alloc(a
));
85 test
<Alloc
>(10, 'a', Alloc(a
));
87 test
<Alloc
>(100, 'a');
88 test
<Alloc
>(100, 'a', Alloc(a
));
90 test
<Alloc
>(static_cast<char>(100), static_cast<char>(65));
91 test
<Alloc
>(static_cast<char>(100), static_cast<char>(65), a
);
94 TEST_CONSTEXPR_CXX20
bool test() {
95 test_string(std::allocator
<char>());
96 test_string(test_allocator
<char>());
97 test_string(test_allocator
<char>(2));
98 #if TEST_STD_VER >= 11
99 test_string(min_allocator
<char>());
100 test_string(safe_allocator
<char>());
106 int main(int, char**) {
108 #if TEST_STD_VER > 17
109 static_assert(test());