string_view-selftests.c 5.8 KB

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