]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string_view / cons / wchar_t / range_c++20.cc
CommitLineData
a945c346 1// Copyright (C) 2021-2024 Free Software Foundation, Inc.
7c100613
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
7c100613
JW
18// { dg-do run { target c++23 } }
19
20#include <string_view>
21#include <testsuite_hooks.h>
22
23void
24test01()
25{
26 struct R
27 {
28 const wchar_t* begin() const
29 { return str; }
30
31 const wchar_t* end() const
32 { return str + std::char_traits<wchar_t>::length(str); }
33
34 const wchar_t* str = L"Home on the range";
35 };
36
37 R r;
2678386d 38 std::wstring_view s{r};
7c100613
JW
39 VERIFY( s == r.str );
40 VERIFY( s.data() == std::ranges::data(r) );
41 VERIFY( s.size() == std::ranges::size(r) );
42
43 struct R2 : R
44 {
45 using R::begin;
46 using R::end;
47 operator std::wstring_view() const { return L"Out of range"; }
48 };
49 static_assert( std::ranges::contiguous_range<R2> );
50 static_assert( std::ranges::sized_range<R2> );
51 R2 r2;
2678386d 52 std::wstring_view s2(r2); // uses conversion to wstring_view
7c100613
JW
53 VERIFY( s2 == L"Out of range" );
54 VERIFY( std::wstring_view(const_cast<const R2&>(r2)) == s2 );
55
2678386d
JW
56 // And again using copy-initialization instead of direct-initialization.
57 std::wstring_view s2_implicit = r2; // uses conversion to wstring_view
58 VERIFY( s2_implicit == L"Out of range" );
59 VERIFY( std::wstring_view(const_cast<const R2&>(r2)) == s2_implicit );
60
7c100613
JW
61 struct R3 : R
62 {
63 using R::begin;
64 using R::end;
65 operator const wchar_t*() { return L"Orange"; }
66 };
67 static_assert( std::ranges::contiguous_range<R3> );
68 static_assert( std::ranges::sized_range<R3> );
69 R3 r3;
70 std::wstring_view s3(r3); // uses conversion to const wchar_t*
71 VERIFY( s3 == L"Orange" );
72 s3 = std::wstring_view(const_cast<const R3&>(r3)); // uses range constructor
73 VERIFY( s3 == L"Home on the range" );
74
75 struct R4 : R
76 {
77 using R::begin;
78 using R::end;
79 operator std::wstring_view() { return L"Strange"; }
80 };
81 static_assert( std::ranges::contiguous_range<R4> );
82 static_assert( std::ranges::sized_range<R4> );
83 R4 r4;
84 std::wstring_view s4 = r4; // Uses conversion to wstring_view
85 VERIFY( s4 == L"Strange" );
86 // Cannot construct from const R4 because of non-const conversion op:
87 static_assert( ! std::is_constructible_v<std::wstring_view, const R4&> );
88
89 struct R5 : R
90 {
91 using R::begin;
92 using R::end;
93 operator std::wstring_view() && { return L"Stranger"; }
94 };
95 static_assert( std::ranges::contiguous_range<R5> );
96 static_assert( std::ranges::sized_range<R5> );
97 R5 r5;
2678386d 98 std::wstring_view s5(r5); // Uses range constructor
7c100613
JW
99 VERIFY( s5 == r5.str );
100 s5 = std::wstring_view(std::move(r5)); // In C++20 this used conversion op.
2678386d 101 VERIFY( s5 == r5.str ); // In C++23 it uses range constructor.
7c100613
JW
102
103 wchar_t arr[] = L"arrangement\0with\0nulls";
104 std::wstring_view sa = arr; // Does not use range constructor
105 VERIFY( sa.data() == arr );
106 VERIFY( sa == L"arrangement" );
107 VERIFY( std::end(sa) != std::end(arr) );
108}
109
110void
111test02()
112{
113 using V1 = std::basic_string_view<char>;
114 // range_value_t<V1> is not the right type
115 static_assert( ! std::is_constructible_v<std::wstring_view, V1> );
116
117 using V2 = std::basic_string_view<wchar_t, __gnu_cxx::char_traits<wchar_t>>;
118 // V2::traits_type is not the right type
119 static_assert( ! std::is_constructible_v<std::wstring_view, V2> );
120
121 struct V3 : V2
122 {
123 private:
124 using V2::traits_type;
125 };
126 // V3::traits_type is not a valid (accessible) type
127 static_assert( std::is_constructible_v<std::wstring_view, V3> );
128
129 struct V4 : V2
130 {
131 using traits_type = std::wstring_view::traits_type;
132 };
133 // V4::traits_type is the right type
134 static_assert( std::is_constructible_v<std::wstring_view, V4> );
135}
136
137void
138test03()
139{
140 struct R
141 {
142 wchar_t* begin() { return nullptr; }
143 const wchar_t* begin() const noexcept { return nullptr; }
144
145 wchar_t* end() { return nullptr; }
146 const wchar_t* end() const noexcept { return nullptr; }
147 };
148
149 static_assert( ! noexcept(std::wstring_view(std::declval<R&>())) );
150 static_assert( noexcept(std::wstring_view(std::declval<const R&>())) );
151}
152
153void
154test04()
155{
156 struct R
157 {
158 const wchar_t* begin() const { return nullptr; }
159 const wchar_t* end() const { return nullptr; }
160 };
161
162 R r;
2678386d 163 std::basic_string_view s(r); // Use deduction guide.
7c100613
JW
164
165 static_assert( std::is_same_v<decltype(s), std::wstring_view> );
166}
167
2678386d
JW
168void
169test05()
170{
171 struct R
172 {
173 const wchar_t* begin() const { return nullptr; }
174 const wchar_t* end() const { return nullptr; }
175 };
176
177 // P2499R0 string_view range constructor should be explicit
178 // P2516R0 string_view is implicitly convertible from what?
179 static_assert( ! std::is_convertible_v<R, std::wstring_view> );
180}
181
7c100613
JW
182int main()
183{
184 test01();
185 test02();
186 test03();
187 test04();
2678386d 188 test05();
7c100613 189}