Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.cons / string_view.pass.cpp
blob74fa76a013a6926c956398741948725cb734163a
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 // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
13 #include <algorithm>
14 #include <cassert>
15 #include <stdexcept>
16 #include <string_view>
17 #include <string>
18 #include <type_traits>
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "test_macros.h"
24 static_assert(!std::is_convertible<std::string_view, std::string const&>::value, "");
25 static_assert(!std::is_convertible<std::string_view, std::string>::value, "");
27 template <class Alloc, class CharT>
28 TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv) {
29 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
30 typedef typename S::traits_type T;
32 S s2(sv);
33 LIBCPP_ASSERT(s2.__invariants());
34 assert(s2.size() == sv.size());
35 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
36 assert(s2.get_allocator() == Alloc());
37 assert(s2.capacity() >= s2.size());
40 S s2;
41 s2 = sv;
42 LIBCPP_ASSERT(s2.__invariants());
43 assert(s2.size() == sv.size());
44 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
45 assert(s2.get_allocator() == Alloc());
46 assert(s2.capacity() >= s2.size());
50 template <class Alloc, class CharT>
51 TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv, const Alloc& a) {
52 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
53 typedef typename S::traits_type T;
55 S s2(sv, a);
56 LIBCPP_ASSERT(s2.__invariants());
57 assert(s2.size() == sv.size());
58 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
59 assert(s2.get_allocator() == a);
60 assert(s2.capacity() >= s2.size());
63 S s2(a);
64 s2 = sv;
65 LIBCPP_ASSERT(s2.__invariants());
66 assert(s2.size() == sv.size());
67 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
68 assert(s2.get_allocator() == a);
69 assert(s2.capacity() >= s2.size());
73 template <class Alloc>
74 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
75 typedef std::basic_string_view<char, std::char_traits<char> > SV;
77 test<Alloc>(SV(""));
78 test<Alloc>(SV(""), Alloc(a));
80 test<Alloc>(SV("1"));
81 test<Alloc>(SV("1"), Alloc(a));
83 test<Alloc>(SV("1234567980"));
84 test<Alloc>(SV("1234567980"), Alloc(a));
86 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"));
87 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"), Alloc(a));
90 TEST_CONSTEXPR_CXX20 bool test() {
91 test_string(std::allocator<char>());
92 test_string(test_allocator<char>());
93 test_string(test_allocator<char>(2));
94 #if TEST_STD_VER >= 11
95 test_string(min_allocator<char>());
96 #endif
98 return true;
101 int main(int, char**) {
102 test();
103 #if TEST_STD_VER > 17
104 static_assert(test());
105 #endif
107 return 0;