1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9// <string>
10
11// basic_string(size_type n, charT c, const Allocator& a = Allocator()); // constexpr since C++20
12
13#include <string>
14#include <stdexcept>
15#include <algorithm>
16#include <cassert>
17#include <cstddef>
18
19#include "test_macros.h"
20#include "test_allocator.h"
21#include "min_allocator.h"
22#include "asan_testing.h"
23
24template <class Alloc, class charT>
25TEST_CONSTEXPR_CXX20 void test(unsigned n, charT c) {
26 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
27 S s2(n, c);
28 LIBCPP_ASSERT(s2.__invariants());
29 assert(s2.size() == n);
30 for (unsigned i = 0; i < n; ++i)
31 assert(s2[i] == c);
32 assert(s2.get_allocator() == Alloc());
33 assert(s2.capacity() >= s2.size());
34 LIBCPP_ASSERT(is_string_asan_correct(s2));
35}
36
37template <class Alloc, class charT>
38TEST_CONSTEXPR_CXX20 void test(unsigned n, charT c, const Alloc& a) {
39 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
40 S s2(n, c, a);
41 LIBCPP_ASSERT(s2.__invariants());
42 assert(s2.size() == n);
43 for (unsigned i = 0; i < n; ++i)
44 assert(s2[i] == c);
45 assert(s2.get_allocator() == a);
46 assert(s2.capacity() >= s2.size());
47 LIBCPP_ASSERT(is_string_asan_correct(s2));
48}
49
50template <class Alloc, class Tp>
51TEST_CONSTEXPR_CXX20 void test(Tp n, Tp c) {
52 typedef char charT;
53 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
54 S s2(n, c);
55 LIBCPP_ASSERT(s2.__invariants());
56 assert(s2.size() == static_cast<std::size_t>(n));
57 for (int i = 0; i < n; ++i)
58 assert(s2[i] == c);
59 assert(s2.get_allocator() == Alloc());
60 assert(s2.capacity() >= s2.size());
61}
62
63template <class Alloc, class Tp>
64TEST_CONSTEXPR_CXX20 void test(Tp n, Tp c, const Alloc& a) {
65 typedef char charT;
66 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
67 S s2(n, c, a);
68 LIBCPP_ASSERT(s2.__invariants());
69 assert(s2.size() == static_cast<std::size_t>(n));
70 for (int i = 0; i < n; ++i)
71 assert(s2[i] == c);
72 assert(s2.get_allocator() == a);
73 assert(s2.capacity() >= s2.size());
74}
75
76template <class Alloc>
77TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
78 test<Alloc>(0, 'a');
79 test<Alloc>(0, 'a', Alloc(a));
80
81 test<Alloc>(1, 'a');
82 test<Alloc>(1, 'a', Alloc(a));
83
84 test<Alloc>(10, 'a');
85 test<Alloc>(10, 'a', Alloc(a));
86
87 test<Alloc>(100, 'a');
88 test<Alloc>(100, 'a', Alloc(a));
89
90 test<Alloc>(static_cast<char>(100), static_cast<char>(65));
91 test<Alloc>(static_cast<char>(100), static_cast<char>(65), a);
92}
93
94TEST_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>());
101#endif
102
103 return true;
104}
105
106int main(int, char**) {
107 test();
108#if TEST_STD_VER > 17
109 static_assert(test());
110#endif
111
112 return 0;
113}
114

source code of libcxx/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp