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(const charT* s, size_type n, const Allocator& a = Allocator()); // constexpr since C++20
18 #include "test_macros.h"
19 #include "test_allocator.h"
20 #include "min_allocator.h"
22 template <class Alloc
, class CharT
>
23 TEST_CONSTEXPR_CXX20
void test(const CharT
* s
, unsigned n
) {
24 typedef std::basic_string
<CharT
, std::char_traits
<CharT
>, Alloc
> S
;
25 typedef typename
S::traits_type T
;
27 LIBCPP_ASSERT(s2
.__invariants());
28 assert(s2
.size() == n
);
29 assert(T::compare(s2
.data(), s
, n
) == 0);
30 assert(s2
.get_allocator() == Alloc());
31 assert(s2
.capacity() >= s2
.size());
34 template <class Alloc
, class CharT
>
35 TEST_CONSTEXPR_CXX20
void test(const CharT
* s
, unsigned n
, const Alloc
& a
) {
36 typedef std::basic_string
<CharT
, std::char_traits
<CharT
>, Alloc
> S
;
37 typedef typename
S::traits_type T
;
39 LIBCPP_ASSERT(s2
.__invariants());
40 assert(s2
.size() == n
);
41 assert(T::compare(s2
.data(), s
, n
) == 0);
42 assert(s2
.get_allocator() == a
);
43 assert(s2
.capacity() >= s2
.size());
46 template <class Alloc
>
47 TEST_CONSTEXPR_CXX20
void test(const Alloc
& a
) {
49 test
<Alloc
>("", 0, Alloc(a
));
52 test
<Alloc
>("1", 1, Alloc(a
));
54 test
<Alloc
>("1234567980", 10);
55 test
<Alloc
>("1234567980", 10, Alloc(a
));
57 test
<Alloc
>("123456798012345679801234567980123456798012345679801234567980", 60);
58 test
<Alloc
>("123456798012345679801234567980123456798012345679801234567980", 60, Alloc(a
));
61 TEST_CONSTEXPR_CXX20
bool test() {
62 test(std::allocator
<char>());
63 test(test_allocator
<char>());
64 test(test_allocator
<char>(2));
65 #if TEST_STD_VER >= 11
66 test(min_allocator
<char>());
69 #if TEST_STD_VER >= 11
71 std::string
s({"abc", 1});
72 assert(s
.size() == 1);
80 int main(int, char**) {
83 static_assert(test());