Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / pointer_alloc.pass.cpp
blob84a29df5b57bf1626f5ad5ec19ec323fe9ddab62
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(const charT* s, 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(const charT* s) {
25 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
26 typedef typename S::traits_type T;
27 std::size_t n = T::length(s);
28 S s2(s);
29 LIBCPP_ASSERT(s2.__invariants());
30 assert(s2.size() == n);
31 assert(T::compare(s2.data(), s, n) == 0);
32 assert(s2.get_allocator() == Alloc());
33 assert(s2.capacity() >= s2.size());
36 template <class Alloc, class charT>
37 TEST_CONSTEXPR_CXX20 void test(const charT* s, const Alloc& a) {
38 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;
39 typedef typename S::traits_type T;
40 std::size_t n = T::length(s);
41 S s2(s, a);
42 LIBCPP_ASSERT(s2.__invariants());
43 assert(s2.size() == n);
44 assert(T::compare(s2.data(), s, n) == 0);
45 assert(s2.get_allocator() == a);
46 assert(s2.capacity() >= s2.size());
49 template <class Alloc>
50 TEST_CONSTEXPR_CXX20 void test(const Alloc& a) {
51 test<Alloc>("");
52 test<Alloc>("", Alloc(a));
54 test<Alloc>("1");
55 test<Alloc>("1", Alloc(a));
57 test<Alloc>("1234567980");
58 test<Alloc>("1234567980", Alloc(a));
60 test<Alloc>("123456798012345679801234567980123456798012345679801234567980");
61 test<Alloc>("123456798012345679801234567980123456798012345679801234567980", Alloc(a));
64 TEST_CONSTEXPR_CXX20 bool test() {
65 test(std::allocator<char>());
66 test(test_allocator<char>());
67 test(test_allocator<char>(2));
68 #if TEST_STD_VER >= 11
69 test(min_allocator<char>());
70 #endif
72 return true;
75 int main(int, char**) {
76 test();
77 #if TEST_STD_VER > 17
78 static_assert(test());
79 #endif
81 return 0;