Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / pointer_size_alloc.pass.cpp
blobe0dab72b5c6327eadd684cc2d2820043c8ed9c8d
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, size_type n, const Allocator& a = Allocator()); // constexpr since C++20
13 #include <string>
14 #include <stdexcept>
15 #include <algorithm>
16 #include <cassert>
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;
26 S s2(s, n);
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;
38 S s2(s, n, a);
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) {
48 test<Alloc>("", 0);
49 test<Alloc>("", 0, Alloc(a));
51 test<Alloc>("1", 1);
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>());
67 #endif
69 #if TEST_STD_VER >= 11
70 { // LWG 2946
71 std::string s({"abc", 1});
72 assert(s.size() == 1);
73 assert(s == "a");
75 #endif
77 return true;
80 int main(int, char**) {
81 test();
82 #if TEST_STD_VER > 17
83 static_assert(test());
84 #endif
86 return 0;