Commit | Line | Data |
---|---|---|
c9638d26 SM |
1 | /* Self tests for string_view for GDB, the GNU debugger. |
2 | ||
3 | Copyright (C) 2018 Free Software Foundation, Inc. | |
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 | ||
24 | #include "defs.h" | |
25 | #include "selftest.h" | |
26 | #include "common/gdb_string_view.h" | |
27 | ||
28 | /* Used by the included .cc files below. Included here because the | |
29 | included test files are wrapped in a namespace. */ | |
30 | #include <string> | |
31 | #include <sstream> | |
32 | #include <fstream> | |
33 | #include <iostream> | |
34 | ||
35 | /* libstdc++'s testsuite uses VERIFY. */ | |
36 | #define VERIFY SELF_CHECK | |
37 | ||
38 | /* Used to disable testing features not supported by | |
39 | gdb::string_view. */ | |
40 | #define GDB_STRING_VIEW | |
41 | ||
42 | namespace selftests { | |
43 | namespace string_view { | |
44 | ||
45 | /* The actual tests live in separate files, which were originally | |
46 | copied over from libstdc++'s testsuite. To preserve the structure | |
47 | and help with comparison with the original tests, the file names | |
48 | have been preserved, and only minimal modification was done to have | |
49 | them compile against gdb::string_view instead of std::string_view: | |
50 | ||
51 | - std::string_view->gdb::string_view, etc. | |
52 | - ATTRIBUTE_UNUSED in a few places | |
53 | - wrap each file in a namespace so they can all be compiled as a | |
54 | single unit. | |
55 | - libstdc++'s license and formatting style was preserved. | |
56 | */ | |
57 | ||
58 | #include "basic_string_view/capacity/1.cc" | |
59 | #include "basic_string_view/cons/char/1.cc" | |
60 | #include "basic_string_view/cons/char/2.cc" | |
61 | #include "basic_string_view/cons/char/3.cc" | |
62 | #include "basic_string_view/element_access/char/1.cc" | |
63 | #include "basic_string_view/element_access/char/empty.cc" | |
64 | #include "basic_string_view/element_access/char/front_back.cc" | |
65 | #include "basic_string_view/inserters/char/2.cc" | |
66 | #include "basic_string_view/modifiers/remove_prefix/char/1.cc" | |
67 | #include "basic_string_view/modifiers/remove_suffix/char/1.cc" | |
68 | #include "basic_string_view/modifiers/swap/char/1.cc" | |
69 | #include "basic_string_view/operations/compare/char/1.cc" | |
70 | #include "basic_string_view/operations/compare/char/13650.cc" | |
71 | #include "basic_string_view/operations/copy/char/1.cc" | |
72 | #include "basic_string_view/operations/data/char/1.cc" | |
73 | #include "basic_string_view/operations/find/char/1.cc" | |
74 | #include "basic_string_view/operations/find/char/2.cc" | |
75 | #include "basic_string_view/operations/find/char/3.cc" | |
76 | #include "basic_string_view/operations/find/char/4.cc" | |
77 | #include "basic_string_view/operations/rfind/char/1.cc" | |
78 | #include "basic_string_view/operations/rfind/char/2.cc" | |
79 | #include "basic_string_view/operations/rfind/char/3.cc" | |
80 | #include "basic_string_view/operations/substr/char/1.cc" | |
81 | #include "basic_string_view/operators/char/2.cc" | |
82 | ||
83 | static void | |
84 | run_tests () | |
85 | { | |
86 | capacity_1::main (); | |
87 | cons_1::main (); | |
88 | cons_2::main (); | |
89 | cons_3::main (); | |
90 | element_access_1::main (); | |
91 | element_access_empty::main (); | |
92 | element_access_front_back::main (); | |
93 | inserters_2::main (); | |
94 | modifiers_remove_prefix::main (); | |
95 | modifiers_remove_suffix::main (); | |
96 | modifiers_swap::test01 (); | |
97 | operations_compare_1::main (); | |
98 | operations_compare_13650::main (); | |
99 | operations_copy_1::main (); | |
100 | operations_data_1::main (); | |
101 | operations_find_1::main (); | |
102 | operations_find_2::main (); | |
103 | operations_find_3::main (); | |
104 | operations_find_4::main (); | |
105 | operations_rfind_1::main (); | |
106 | operations_rfind_2::main (); | |
107 | operations_rfind_3::main (); | |
108 | operations_substr_1::main (); | |
109 | operators_2::main (); | |
110 | ||
111 | constexpr gdb::string_view sv_empty; | |
112 | SELF_CHECK (sv_empty.empty ()); | |
113 | ||
114 | std::string std_string = "fika"; | |
115 | gdb::string_view sv1 (std_string); | |
116 | SELF_CHECK (sv1 == "fika"); | |
117 | ||
118 | constexpr const char *fika = "fika"; | |
119 | gdb::string_view sv2 (fika); | |
120 | SELF_CHECK (sv2 == "fika"); | |
121 | ||
122 | constexpr gdb::string_view sv3 (fika, 3); | |
123 | SELF_CHECK (sv3 == "fik"); | |
124 | ||
125 | constexpr gdb::string_view sv4 (sv3); | |
126 | SELF_CHECK (sv4 == "fik"); | |
127 | ||
128 | constexpr gdb::string_view::iterator it_begin = sv4.begin (); | |
129 | static_assert (*it_begin == 'f', ""); | |
130 | ||
131 | constexpr gdb::string_view::iterator it_end = sv4.end (); | |
132 | static_assert (*it_end == 'a', ""); | |
133 | ||
134 | const gdb::string_view::reverse_iterator it_rbegin = sv4.rbegin (); | |
135 | SELF_CHECK (*it_rbegin == 'k'); | |
136 | ||
137 | const gdb::string_view::reverse_iterator it_rend = sv4.rend (); | |
138 | SELF_CHECK (*(it_rend - 1) == 'f'); | |
139 | ||
140 | constexpr gdb::string_view::size_type size = sv4.size (); | |
141 | static_assert (size == 3, ""); | |
142 | ||
143 | constexpr gdb::string_view::size_type length = sv4.length (); | |
144 | static_assert (length == 3, ""); | |
145 | ||
146 | constexpr gdb::string_view::size_type max_size = sv4.max_size (); | |
147 | static_assert (max_size > 0, ""); | |
148 | ||
149 | constexpr bool empty = sv4.empty (); | |
150 | static_assert (!empty, ""); | |
151 | ||
152 | constexpr char c1 = sv4[1]; | |
153 | static_assert (c1 == 'i', ""); | |
154 | ||
155 | constexpr char c2 = sv4.at (2); | |
156 | static_assert (c2 == 'k', ""); | |
157 | ||
158 | constexpr char front = sv4.front (); | |
159 | static_assert (front == 'f', ""); | |
160 | ||
161 | constexpr char back = sv4.back (); | |
162 | static_assert (back == 'k', ""); | |
163 | ||
164 | constexpr const char *data = sv4.data (); | |
165 | static_assert (data == fika, ""); | |
166 | } | |
167 | ||
168 | } /* namespace string_view */ | |
169 | } /* namespace selftests */ | |
170 | ||
171 | void | |
172 | _initialize_string_view_selftests () | |
173 | { | |
174 | selftests::register_test ("string_view", selftests::string_view::run_tests); | |
175 | } | |
176 | ||
177 | #endif /* __cplusplus < 201703L */ |