summaryrefslogtreecommitdiff
path: root/gdb/unittests/string_view-selftests.c
blob: 910f6588d7b4543cff9a9bf65e9a853e9c1bfa0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Self tests for string_view for GDB, the GNU debugger.

   Copyright (C) 2018-2019 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* No need to test string_view if we're using C++17, since we're going to use
   the "real" version.  */
#if __cplusplus < 201703L

#define GNULIB_NAMESPACE gnulib

#include "defs.h"
#include "gdbsupport/selftest.h"
#include "gdbsupport/gdb_string_view.h"

/* Used by the included .cc files below.  Included here because the
   included test files are wrapped in a namespace.  */
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>

/* libstdc++'s testsuite uses VERIFY.  */
#define VERIFY SELF_CHECK

/* Used to disable testing features not supported by
   gdb::string_view.  */
#define GDB_STRING_VIEW

namespace selftests {
namespace string_view {

/* The actual tests live in separate files, which were originally
   copied over from libstdc++'s testsuite.  To preserve the structure
   and help with comparison with the original tests, the file names
   have been preserved, and only minimal modification was done to have
   them compile against gdb::string_view instead of std::string_view:

     - std::string_view->gdb::string_view, etc.
     - ATTRIBUTE_UNUSED in a few places
     - wrap each file in a namespace so they can all be compiled as a
       single unit.
     - libstdc++'s license and formatting style was preserved.
*/

#include "basic_string_view/capacity/1.cc"
#include "basic_string_view/cons/char/1.cc"
#include "basic_string_view/cons/char/2.cc"
#include "basic_string_view/cons/char/3.cc"
#include "basic_string_view/element_access/char/1.cc"
#include "basic_string_view/element_access/char/empty.cc"
#include "basic_string_view/element_access/char/front_back.cc"
#include "basic_string_view/inserters/char/2.cc"
#include "basic_string_view/modifiers/remove_prefix/char/1.cc"
#include "basic_string_view/modifiers/remove_suffix/char/1.cc"
#include "basic_string_view/modifiers/swap/char/1.cc"
#include "basic_string_view/operations/compare/char/1.cc"
#include "basic_string_view/operations/compare/char/13650.cc"
#include "basic_string_view/operations/copy/char/1.cc"
#include "basic_string_view/operations/data/char/1.cc"
#include "basic_string_view/operations/find/char/1.cc"
#include "basic_string_view/operations/find/char/2.cc"
#include "basic_string_view/operations/find/char/3.cc"
#include "basic_string_view/operations/find/char/4.cc"
#include "basic_string_view/operations/rfind/char/1.cc"
#include "basic_string_view/operations/rfind/char/2.cc"
#include "basic_string_view/operations/rfind/char/3.cc"
#include "basic_string_view/operations/substr/char/1.cc"
#include "basic_string_view/operators/char/2.cc"

static void
run_tests ()
{
  capacity_1::main ();
  cons_1::main ();
  cons_2::main ();
  cons_3::main ();
  element_access_1::main ();
  element_access_empty::main ();
  element_access_front_back::main ();
  inserters_2::main ();
  modifiers_remove_prefix::main ();
  modifiers_remove_suffix::main ();
  modifiers_swap::test01 ();
  operations_compare_1::main ();
  operations_compare_13650::main ();
  operations_copy_1::main ();
  operations_data_1::main ();
  operations_find_1::main ();
  operations_find_2::main ();
  operations_find_3::main ();
  operations_find_4::main ();
  operations_rfind_1::main ();
  operations_rfind_2::main ();
  operations_rfind_3::main ();
  operations_substr_1::main ();
  operators_2::main ();

  constexpr gdb::string_view sv_empty;
  SELF_CHECK (sv_empty.empty ());

  std::string std_string = "fika";
  gdb::string_view sv1 (std_string);
  SELF_CHECK (sv1 == "fika");

  constexpr const char *fika = "fika";
  gdb::string_view sv2 (fika);
  SELF_CHECK (sv2 == "fika");

  constexpr gdb::string_view sv3 (fika, 3);
  SELF_CHECK (sv3 == "fik");

  constexpr gdb::string_view sv4 (sv3);
  SELF_CHECK (sv4 == "fik");

  constexpr gdb::string_view::iterator it_begin = sv4.begin ();
  static_assert (*it_begin == 'f', "");

  constexpr gdb::string_view::iterator it_end = sv4.end ();
  static_assert (*it_end == 'a', "");

  const gdb::string_view::reverse_iterator it_rbegin = sv4.rbegin ();
  SELF_CHECK (*it_rbegin == 'k');

  const gdb::string_view::reverse_iterator it_rend = sv4.rend ();
  SELF_CHECK (*(it_rend - 1) == 'f');

  constexpr gdb::string_view::size_type size = sv4.size ();
  static_assert (size == 3, "");

  constexpr gdb::string_view::size_type length = sv4.length ();
  static_assert (length == 3, "");

  constexpr gdb::string_view::size_type max_size = sv4.max_size ();
  static_assert (max_size > 0, "");

  constexpr bool empty = sv4.empty ();
  static_assert (!empty, "");

  constexpr char c1 = sv4[1];
  static_assert (c1 == 'i', "");

  constexpr char c2 = sv4.at (2);
  static_assert (c2 == 'k', "");

  constexpr char front = sv4.front ();
  static_assert (front == 'f', "");

  constexpr char back = sv4.back ();
  static_assert (back == 'k', "");

  constexpr const char *data = sv4.data ();
  static_assert (data == fika, "");
}

} /* namespace string_view */
} /* namespace selftests */

#endif /* __cplusplus < 201703L */

void
_initialize_string_view_selftests ()
{
#if defined(GDB_STRING_VIEW)
  selftests::register_test ("string_view", selftests::string_view::run_tests);
#endif
}