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// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
12
13#include <algorithm>
14#include <cassert>
15#include <stdexcept>
16#include <string_view>
17#include <string>
18#include <type_traits>
19
20#include "min_allocator.h"
21#include "test_allocator.h"
22#include "test_macros.h"
23#include "asan_testing.h"
24
25static_assert(!std::is_convertible<std::string_view, std::string const&>::value, "");
26static_assert(!std::is_convertible<std::string_view, std::string>::value, "");
27
28template <class Alloc, class CharT>
29TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv) {
30 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
31 typedef typename S::traits_type T;
32 {
33 S s2(sv);
34 LIBCPP_ASSERT(s2.__invariants());
35 assert(s2.size() == sv.size());
36 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
37 assert(s2.get_allocator() == Alloc());
38 assert(s2.capacity() >= s2.size());
39 LIBCPP_ASSERT(is_string_asan_correct(s2));
40 }
41 {
42 S s2;
43 s2 = sv;
44 LIBCPP_ASSERT(s2.__invariants());
45 assert(s2.size() == sv.size());
46 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
47 assert(s2.get_allocator() == Alloc());
48 assert(s2.capacity() >= s2.size());
49 LIBCPP_ASSERT(is_string_asan_correct(s2));
50 }
51}
52
53template <class Alloc, class CharT>
54TEST_CONSTEXPR_CXX20 void test(std::basic_string_view<CharT> sv, const Alloc& a) {
55 typedef std::basic_string<CharT, std::char_traits<CharT>, Alloc> S;
56 typedef typename S::traits_type T;
57 {
58 S s2(sv, a);
59 LIBCPP_ASSERT(s2.__invariants());
60 assert(s2.size() == sv.size());
61 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
62 assert(s2.get_allocator() == a);
63 assert(s2.capacity() >= s2.size());
64 LIBCPP_ASSERT(is_string_asan_correct(s2));
65 }
66 {
67 S s2(a);
68 s2 = sv;
69 LIBCPP_ASSERT(s2.__invariants());
70 assert(s2.size() == sv.size());
71 assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
72 assert(s2.get_allocator() == a);
73 assert(s2.capacity() >= s2.size());
74 LIBCPP_ASSERT(is_string_asan_correct(s2));
75 }
76}
77
78template <class Alloc>
79TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
80 typedef std::basic_string_view<char, std::char_traits<char> > SV;
81
82 test<Alloc>(SV(""));
83 test<Alloc>(SV(""), Alloc(a));
84
85 test<Alloc>(SV("1"));
86 test<Alloc>(SV("1"), Alloc(a));
87
88 test<Alloc>(SV("1234567980"));
89 test<Alloc>(SV("1234567980"), Alloc(a));
90
91 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"));
92 test<Alloc>(SV("123456798012345679801234567980123456798012345679801234567980"), Alloc(a));
93}
94
95TEST_CONSTEXPR_CXX20 bool test() {
96 test_string(std::allocator<char>());
97 test_string(test_allocator<char>());
98 test_string(test_allocator<char>(2));
99#if TEST_STD_VER >= 11
100 test_string(min_allocator<char>());
101 test_string(safe_allocator<char>());
102#endif
103
104 return true;
105}
106
107int main(int, char**) {
108 test();
109#if TEST_STD_VER > 17
110 static_assert(test());
111#endif
112
113 return 0;
114}
115

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