]>
Commit | Line | Data |
---|---|---|
c9638d26 SM |
1 | /* Self tests for string_view for GDB, the GNU debugger. |
2 | ||
213516ef | 3 | Copyright (C) 2018-2023 Free Software Foundation, Inc. |
c9638d26 SM |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* No need to test string_view if we're using C++17, since we're going to use | |
21 | the "real" version. */ | |
22 | #if __cplusplus < 201703L | |
23 | ||
7402fbca SM |
24 | #define GNULIB_NAMESPACE gnulib |
25 | ||
a5b6e436 TO |
26 | #include "diagnostics.h" |
27 | ||
28 | /* Since this file uses GNULIB_NAMESPACE, some code defined in headers ends up | |
29 | using system functions rather than gnulib replacements. This is not really | |
30 | a problem for this test, but it generates some warnings with Clang, silence | |
31 | them. */ | |
32 | DIAGNOSTIC_PUSH | |
33 | DIAGNOSTIC_IGNORE_USER_DEFINED_WARNINGS | |
34 | ||
c9638d26 | 35 | #include "defs.h" |
268a13a5 TT |
36 | #include "gdbsupport/selftest.h" |
37 | #include "gdbsupport/gdb_string_view.h" | |
c9638d26 SM |
38 | |
39 | /* Used by the included .cc files below. Included here because the | |
40 | included test files are wrapped in a namespace. */ | |
41 | #include <string> | |
42 | #include <sstream> | |
43 | #include <fstream> | |
44 | #include <iostream> | |
45 | ||
a5b6e436 TO |
46 | DIAGNOSTIC_POP |
47 | ||
c9638d26 SM |
48 | /* libstdc++'s testsuite uses VERIFY. */ |
49 | #define VERIFY SELF_CHECK | |
50 | ||
51 | /* Used to disable testing features not supported by | |
52 | gdb::string_view. */ | |
53 | #define GDB_STRING_VIEW | |
54 | ||
55 | namespace selftests { | |
56 | namespace string_view { | |
57 | ||
58 | /* The actual tests live in separate files, which were originally | |
59 | copied over from libstdc++'s testsuite. To preserve the structure | |
60 | and help with comparison with the original tests, the file names | |
61 | have been preserved, and only minimal modification was done to have | |
62 | them compile against gdb::string_view instead of std::string_view: | |
63 | ||
64 | - std::string_view->gdb::string_view, etc. | |
65 | - ATTRIBUTE_UNUSED in a few places | |
66 | - wrap each file in a namespace so they can all be compiled as a | |
67 | single unit. | |
68 | - libstdc++'s license and formatting style was preserved. | |
69 | */ | |
70 | ||
71 | #include "basic_string_view/capacity/1.cc" | |
72 | #include "basic_string_view/cons/char/1.cc" | |
73 | #include "basic_string_view/cons/char/2.cc" | |
74 | #include "basic_string_view/cons/char/3.cc" | |
75 | #include "basic_string_view/element_access/char/1.cc" | |
76 | #include "basic_string_view/element_access/char/empty.cc" | |
77 | #include "basic_string_view/element_access/char/front_back.cc" | |
78 | #include "basic_string_view/inserters/char/2.cc" | |
79 | #include "basic_string_view/modifiers/remove_prefix/char/1.cc" | |
80 | #include "basic_string_view/modifiers/remove_suffix/char/1.cc" | |
81 | #include "basic_string_view/modifiers/swap/char/1.cc" | |
82 | #include "basic_string_view/operations/compare/char/1.cc" | |
83 | #include "basic_string_view/operations/compare/char/13650.cc" | |
84 | #include "basic_string_view/operations/copy/char/1.cc" | |
85 | #include "basic_string_view/operations/data/char/1.cc" | |
86 | #include "basic_string_view/operations/find/char/1.cc" | |
87 | #include "basic_string_view/operations/find/char/2.cc" | |
88 | #include "basic_string_view/operations/find/char/3.cc" | |
89 | #include "basic_string_view/operations/find/char/4.cc" | |
90 | #include "basic_string_view/operations/rfind/char/1.cc" | |
91 | #include "basic_string_view/operations/rfind/char/2.cc" | |
92 | #include "basic_string_view/operations/rfind/char/3.cc" | |
93 | #include "basic_string_view/operations/substr/char/1.cc" | |
94 | #include "basic_string_view/operators/char/2.cc" | |
95 | ||
96 | static void | |
97 | run_tests () | |
98 | { | |
99 | capacity_1::main (); | |
100 | cons_1::main (); | |
101 | cons_2::main (); | |
102 | cons_3::main (); | |
103 | element_access_1::main (); | |
104 | element_access_empty::main (); | |
105 | element_access_front_back::main (); | |
106 | inserters_2::main (); | |
107 | modifiers_remove_prefix::main (); | |
108 | modifiers_remove_suffix::main (); | |
109 | modifiers_swap::test01 (); | |
110 | operations_compare_1::main (); | |
111 | operations_compare_13650::main (); | |
112 | operations_copy_1::main (); | |
113 | operations_data_1::main (); | |
114 | operations_find_1::main (); | |
115 | operations_find_2::main (); | |
116 | operations_find_3::main (); | |
117 | operations_find_4::main (); | |
118 | operations_rfind_1::main (); | |
119 | operations_rfind_2::main (); | |
120 | operations_rfind_3::main (); | |
121 | operations_substr_1::main (); | |
122 | operators_2::main (); | |
123 | ||
124 | constexpr gdb::string_view sv_empty; | |
125 | SELF_CHECK (sv_empty.empty ()); | |
126 | ||
127 | std::string std_string = "fika"; | |
128 | gdb::string_view sv1 (std_string); | |
129 | SELF_CHECK (sv1 == "fika"); | |
130 | ||
131 | constexpr const char *fika = "fika"; | |
132 | gdb::string_view sv2 (fika); | |
133 | SELF_CHECK (sv2 == "fika"); | |
134 | ||
135 | constexpr gdb::string_view sv3 (fika, 3); | |
136 | SELF_CHECK (sv3 == "fik"); | |
137 | ||
138 | constexpr gdb::string_view sv4 (sv3); | |
139 | SELF_CHECK (sv4 == "fik"); | |
140 | ||
141 | constexpr gdb::string_view::iterator it_begin = sv4.begin (); | |
142 | static_assert (*it_begin == 'f', ""); | |
143 | ||
144 | constexpr gdb::string_view::iterator it_end = sv4.end (); | |
145 | static_assert (*it_end == 'a', ""); | |
146 | ||
147 | const gdb::string_view::reverse_iterator it_rbegin = sv4.rbegin (); | |
148 | SELF_CHECK (*it_rbegin == 'k'); | |
149 | ||
150 | const gdb::string_view::reverse_iterator it_rend = sv4.rend (); | |
151 | SELF_CHECK (*(it_rend - 1) == 'f'); | |
152 | ||
153 | constexpr gdb::string_view::size_type size = sv4.size (); | |
154 | static_assert (size == 3, ""); | |
155 | ||
156 | constexpr gdb::string_view::size_type length = sv4.length (); | |
157 | static_assert (length == 3, ""); | |
158 | ||
159 | constexpr gdb::string_view::size_type max_size = sv4.max_size (); | |
160 | static_assert (max_size > 0, ""); | |
161 | ||
162 | constexpr bool empty = sv4.empty (); | |
163 | static_assert (!empty, ""); | |
164 | ||
165 | constexpr char c1 = sv4[1]; | |
166 | static_assert (c1 == 'i', ""); | |
167 | ||
168 | constexpr char c2 = sv4.at (2); | |
169 | static_assert (c2 == 'k', ""); | |
170 | ||
171 | constexpr char front = sv4.front (); | |
172 | static_assert (front == 'f', ""); | |
173 | ||
174 | constexpr char back = sv4.back (); | |
175 | static_assert (back == 'k', ""); | |
176 | ||
177 | constexpr const char *data = sv4.data (); | |
178 | static_assert (data == fika, ""); | |
179 | } | |
180 | ||
181 | } /* namespace string_view */ | |
182 | } /* namespace selftests */ | |
183 | ||
58785d98 ST |
184 | #endif /* __cplusplus < 201703L */ |
185 | ||
6c265988 | 186 | void _initialize_string_view_selftests (); |
c9638d26 SM |
187 | void |
188 | _initialize_string_view_selftests () | |
189 | { | |
58785d98 | 190 | #if defined(GDB_STRING_VIEW) |
c9638d26 | 191 | selftests::register_test ("string_view", selftests::string_view::run_tests); |
58785d98 | 192 | #endif |
c9638d26 | 193 | } |