Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / size_char_alloc.pass.cpp
blob3afbcdffa6bdfe370a5578d042e79d27d3e9882b
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // basic_string(size_type n, charT c, const Allocator& a = Allocator()); // constexpr since C++20
13 #include <string>
14 #include <stdexcept>
15 #include <algorithm>
16 #include <cassert>
17 #include <cstddef>
19 #include "test_macros.h"
20 #include "test_allocator.h"
21 #include "min_allocator.h"
23 template <class Alloc, class charT>
24 TEST_CONSTEXPR_CXX20 void test(unsigned n, charT c) {
25 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
26 S s2(n, c);
27 LIBCPP_ASSERT(s2.__invariants());
28 assert(s2.size() == n);
29 for (unsigned i = 0; i < n; ++i)
30 assert(s2[i] == c);
31 assert(s2.get_allocator() == Alloc());
32 assert(s2.capacity() >= s2.size());
35 template <class Alloc, class charT>
36 TEST_CONSTEXPR_CXX20 void test(unsigned n, charT c, const Alloc& a) {
37 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
38 S s2(n, c, a);
39 LIBCPP_ASSERT(s2.__invariants());
40 assert(s2.size() == n);
41 for (unsigned i = 0; i < n; ++i)
42 assert(s2[i] == c);
43 assert(s2.get_allocator() == a);
44 assert(s2.capacity() >= s2.size());
47 template <class Alloc, class Tp>
48 TEST_CONSTEXPR_CXX20 void test(Tp n, Tp c) {
49 typedef char charT;
50 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
51 S s2(n, c);
52 LIBCPP_ASSERT(s2.__invariants());
53 assert(s2.size() == static_cast<std::size_t>(n));
54 for (int i = 0; i < n; ++i)
55 assert(s2[i] == c);
56 assert(s2.get_allocator() == Alloc());
57 assert(s2.capacity() >= s2.size());
60 template <class Alloc, class Tp>
61 TEST_CONSTEXPR_CXX20 void test(Tp n, Tp c, const Alloc& a) {
62 typedef char charT;
63 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
64 S s2(n, c, a);
65 LIBCPP_ASSERT(s2.__invariants());
66 assert(s2.size() == static_cast<std::size_t>(n));
67 for (int i = 0; i < n; ++i)
68 assert(s2[i] == c);
69 assert(s2.get_allocator() == a);
70 assert(s2.capacity() >= s2.size());
73 template <class Alloc>
74 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
75 test<Alloc>(0, 'a');
76 test<Alloc>(0, 'a', Alloc(a));
78 test<Alloc>(1, 'a');
79 test<Alloc>(1, 'a', Alloc(a));
81 test<Alloc>(10, 'a');
82 test<Alloc>(10, 'a', Alloc(a));
84 test<Alloc>(100, 'a');
85 test<Alloc>(100, 'a', Alloc(a));
87 test<Alloc>(static_cast<char>(100), static_cast<char>(65));
88 test<Alloc>(static_cast<char>(100), static_cast<char>(65), a);
91 TEST_CONSTEXPR_CXX20 bool test() {
92 test_string(std::allocator<char>());
93 test_string(test_allocator<char>());
94 test_string(test_allocator<char>(2));
95 #if TEST_STD_VER >= 11
96 test_string(min_allocator<char>());
97 #endif
99 return true;
102 int main(int, char**) {
103 test();
104 #if TEST_STD_VER > 17
105 static_assert(test());
106 #endif
108 return 0;