Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/1.cc

raw
1 // { dg-do run { target c++17 } }
2
3 // Copyright (C) 2013-2025 Free Software Foundation, Inc.
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 // basic_string_view constructors.
21
22 #include <string_view>
23 #include <cstring>
24 #include <testsuite_hooks.h>
25
26 #if __STDC_HOSTED__
27 # include <string>
28 #endif // HOSTED
29
30 void
31 test01()
32 {
33 typedef std::string_view::size_type csize_type;
34
35 // basic_string_view()
36 const std::string_view str00{};
37 VERIFY( str00.length() == 0 );
38 VERIFY( str00.data() == nullptr );
39
40 // basic_string_view(const char*)
41 const char str_lit01[] = "rodeo beach, marin";
42 const std::string_view str01{str_lit01};
43 VERIFY( str01.length() == 18 );
44 VERIFY( str01.data() == str_lit01 );
45 const std::string_view str02{"baker beach, san francisco"};
46 VERIFY( str02.length() == 26 );
47
48 // basic_string_view(const string_view&)
49 std::string_view str04{str01};
50 VERIFY( str04.length() == str01.length() );
51 VERIFY( str04.data() == str01.data() );
52
53 // basic_string_view(const char* s)
54 csize_type len_lit01 = strlen(str_lit01);
55 std::string_view str05{str_lit01, len_lit01};
56 VERIFY( str05.length() == len_lit01 );
57 VERIFY( str05.data() == str_lit01 );
58
59 #if __STDC_HOSTED__
60 // basic_string_view(basic_string& s)
61 std::string istr07(10, 'z');
62 std::string_view str07{istr07};
63 VERIFY( str07.length() == 10 );
64 #endif // HOSTED
65 }
66
67 int
68 main()
69 {
70 test01();
71
72 return 0;
73 }