]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string_view/capacity/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string_view / capacity / 1.cc
CommitLineData
6458742a 1// { dg-do run { target c++17 } }
ca8f2cb1 2
a945c346 3// Copyright (C) 2013-2024 Free Software Foundation, Inc.
ca8f2cb1
VV
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20// string_view size, length
21
22#include <string_view>
23#include <cstring>
24#include <testsuite_hooks.h>
25
26template<typename T>
27 struct A { };
28
29template<typename T>
30 bool
31 operator==(const A<T>&, const A<T>&) { return true; }
32
33template<typename T>
34 bool
35 operator<(const A<T>&, const A<T>&) { return true; }
36
37struct B { };
38
39// char_traits specialization
40namespace std
41{
42 template<>
43 struct char_traits<A<B> >
44 {
45 typedef A<B> char_type;
46 // Unsigned as wint_t in unsigned.
47 typedef unsigned long int_type;
42d3f743 48#if __STDC_HOSTED__
ca8f2cb1
VV
49 typedef streampos pos_type;
50 typedef streamoff off_type;
51 typedef mbstate_t state_type;
42d3f743 52#endif // HOSTED
ca8f2cb1
VV
53
54 static void
55 assign(char_type& __c1, const char_type& __c2)
56 { __c1 = __c2; }
57
58 static bool
59 eq(const char_type& __c1, const char_type& __c2)
60 { return __c1 == __c2; }
61
62 static bool
63 lt(const char_type& __c1, const char_type& __c2)
64 { return __c1 < __c2; }
65
66 static int
67 compare(const char_type* __s1, const char_type* __s2, size_t __n)
68 {
69 for (size_t __i = 0; __i < __n; ++__i)
70 if (!eq(__s1[__i], __s2[__i]))
71 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
72 return 0;
73 }
74
75 static size_t
76 length(const char_type* __s)
77 {
78 const char_type* __p = __s;
79 while (__p)
80 ++__p;
81 return (__p - __s);
82 }
83
84 static const char_type*
85 find(const char_type* __s, size_t __n, const char_type& __a)
86 {
87 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
88 if (*__p == __a) return __p;
89 return 0;
90 }
91
92 static char_type*
93 move(char_type* __s1, const char_type* __s2, size_t __n)
94 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
95
96 static char_type*
97 copy(char_type* __s1, const char_type* __s2, size_t __n)
98 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
99
100 static char_type*
101 assign(char_type* __s, size_t __n, char_type __a)
102 {
103 for (char_type* __p = __s; __p < __s + __n; ++__p)
104 assign(*__p, __a);
105 return __s;
106 }
107
108 static char_type
109 to_char_type(const int_type&)
110 { return char_type(); }
111
112 static int_type
113 to_int_type(const char_type&) { return int_type(); }
114
115 static bool
116 eq_int_type(const int_type& __c1, const int_type& __c2)
117 { return __c1 == __c2; }
118
119 static int_type
120 eof() { return static_cast<int_type>(-1); }
121
122 static int_type
123 not_eof(const int_type& __c)
124 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
125 };
126} // namespace std
127
128void
129test01()
130{
ca8f2cb1
VV
131 std::basic_string_view<A<B>> str02;
132 typedef std::basic_string_view< A<B> >::size_type size_type_o;
133 size_type_o sz03;
134 size_type_o sz04;
135
136 // non-POD types: size, length, max_size, empty()
137 bool b01 = str02.empty();
138 VERIFY( b01 == true );
139 sz03 = str02.size();
140 sz04 = str02.length();
141 VERIFY( sz03 == sz04 );
b911ca42 142 (void) str02.data();
ca8f2cb1
VV
143 sz03 = str02.size();
144 sz04 = str02.length();
145 VERIFY( sz03 == sz04 );
146
147 sz03 = str02.max_size();
148 VERIFY( sz03 >= sz04 );
149
150 sz03 = str02.size();
151 str02 = {};
152 b01 = str02.empty();
153 VERIFY( b01 == true );
154 sz04 = str02.size();
155 VERIFY( sz03 >= sz04 );
156}
157
158int
159main()
160{
161 test01();
162
163 return 0;
164}