From: Simon Marchi Date: Mon, 9 Apr 2018 17:31:06 +0000 (-0400) Subject: Adapt and integrate string_view tests X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c9638d2669ced9348eac869dadc7be24df85a9a8;p=binutils-gdb.git Adapt and integrate string_view tests The previous patch copied the string_view tests from libstdc++. This patch adjusts them in a similar way that the libstdc++ optional tests are integrated in our unit test suite. Not all tests are used, some of them require language features not present in c++11. For example, we can't use a string_view constructor where the length is not explicit in a constexpr, because std::char_traits::length is not a constexpr itself (it is in c++17 though). Nevertheless, a good number of tests are integrated, which covers pretty well the string_view features. gdb/ChangeLog: * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add string_view-selftests.c. * unittests/basic_string_view/capacity/1.cc: Adapt to GDB testsuite. * unittests/basic_string_view/cons/char/1.cc: Likewise. * unittests/basic_string_view/cons/char/2.cc: Likewise. * unittests/basic_string_view/cons/char/3.cc: Likewise. * unittests/basic_string_view/element_access/char/1.cc: Likewise. * unittests/basic_string_view/element_access/char/empty.cc: Likewise. * unittests/basic_string_view/element_access/char/front_back.cc: Likewise. * unittests/basic_string_view/inserters/char/2.cc: Likewise. * unittests/basic_string_view/modifiers/remove_prefix/char/1.cc: Likewise. * unittests/basic_string_view/modifiers/remove_suffix/char/1.cc: Likewise. * unittests/basic_string_view/modifiers/swap/char/1.cc: Likewise. * unittests/basic_string_view/operations/compare/char/1.cc: Likewise. * unittests/basic_string_view/operations/compare/char/13650.cc: Likewise. * unittests/basic_string_view/operations/copy/char/1.cc: Likewise. * unittests/basic_string_view/operations/data/char/1.cc: Likewise. * unittests/basic_string_view/operations/find/char/1.cc: Likewise. * unittests/basic_string_view/operations/find/char/2.cc: Likewise. * unittests/basic_string_view/operations/find/char/3.cc: Likewise. * unittests/basic_string_view/operations/find/char/4.cc: Likewise. * unittests/basic_string_view/operations/rfind/char/1.cc: Likewise. * unittests/basic_string_view/operations/rfind/char/2.cc: Likewise. * unittests/basic_string_view/operations/rfind/char/3.cc: Likewise. * unittests/basic_string_view/operations/substr/char/1.cc: Likewise. * unittests/basic_string_view/operators/char/2.cc: Likewise. * unittests/string_view-selftests.c: New file. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 28520b65e68..45e893f1ed5 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,52 @@ +2018-04-09 Simon Marchi + + * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add + string_view-selftests.c. + * unittests/basic_string_view/capacity/1.cc: Adapt to GDB + testsuite. + * unittests/basic_string_view/cons/char/1.cc: Likewise. + * unittests/basic_string_view/cons/char/2.cc: Likewise. + * unittests/basic_string_view/cons/char/3.cc: Likewise. + * unittests/basic_string_view/element_access/char/1.cc: + Likewise. + * unittests/basic_string_view/element_access/char/empty.cc: + Likewise. + * unittests/basic_string_view/element_access/char/front_back.cc: + Likewise. + * unittests/basic_string_view/inserters/char/2.cc: Likewise. + * unittests/basic_string_view/modifiers/remove_prefix/char/1.cc: + Likewise. + * unittests/basic_string_view/modifiers/remove_suffix/char/1.cc: + Likewise. + * unittests/basic_string_view/modifiers/swap/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/compare/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/compare/char/13650.cc: + Likewise. + * unittests/basic_string_view/operations/copy/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/data/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/find/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/find/char/2.cc: + Likewise. + * unittests/basic_string_view/operations/find/char/3.cc: + Likewise. + * unittests/basic_string_view/operations/find/char/4.cc: + Likewise. + * unittests/basic_string_view/operations/rfind/char/1.cc: + Likewise. + * unittests/basic_string_view/operations/rfind/char/2.cc: + Likewise. + * unittests/basic_string_view/operations/rfind/char/3.cc: + Likewise. + * unittests/basic_string_view/operations/substr/char/1.cc: + Likewise. + * unittests/basic_string_view/operators/char/2.cc: Likewise. + * unittests/string_view-selftests.c: New file. + 2018-04-09 Simon Marchi * unittests/basic_string_view/capacity/1.cc: New file. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 0a07cabb438..e885dca7d2c 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -430,6 +430,7 @@ SUBDIR_UNITTESTS_SRCS = \ unittests/scoped_fd-selftests.c \ unittests/scoped_mmap-selftests.c \ unittests/scoped_restore-selftests.c \ + unittests/string_view-selftests.c \ unittests/tracepoint-selftests.c \ unittests/unpack-selftests.c \ unittests/utils-selftests.c \ diff --git a/gdb/unittests/basic_string_view/capacity/1.cc b/gdb/unittests/basic_string_view/capacity/1.cc index d49ecbd9dd5..f297853f0b7 100644 --- a/gdb/unittests/basic_string_view/capacity/1.cc +++ b/gdb/unittests/basic_string_view/capacity/1.cc @@ -19,9 +19,7 @@ // string_view size, length -#include -#include -#include +namespace capacity_1 { template struct A { }; @@ -36,13 +34,19 @@ template struct B { }; +} // namespace capacity_1 +} // namespace string_view +} // namespace selftests + // char_traits specialization namespace std { template<> - struct char_traits > + struct char_traits > { - typedef A char_type; + typedef selftests::string_view::capacity_1::A< + selftests::string_view::capacity_1::B> char_type; // Unsigned as wint_t in unsigned. typedef unsigned long int_type; typedef streampos pos_type; @@ -123,11 +127,15 @@ namespace std }; } // namespace std +namespace selftests { +namespace string_view { +namespace capacity_1 { + void test01() { - std::basic_string_view> str02; - typedef std::basic_string_view< A >::size_type size_type_o; + gdb::basic_string_view> str02; + typedef gdb::basic_string_view< A >::size_type size_type_o; size_type_o sz03; size_type_o sz04; @@ -160,3 +168,5 @@ main() return 0; } + +} // namespace capacity_1 diff --git a/gdb/unittests/basic_string_view/cons/char/1.cc b/gdb/unittests/basic_string_view/cons/char/1.cc index f80ae92f2f8..cfe9cce4b55 100644 --- a/gdb/unittests/basic_string_view/cons/char/1.cc +++ b/gdb/unittests/basic_string_view/cons/char/1.cc @@ -19,43 +19,40 @@ // basic_string_view constructors. -#include -#include -#include -#include +namespace cons_1 { void test01() { - typedef std::string_view::size_type csize_type; + typedef gdb::string_view::size_type csize_type; // basic_string_view() - const std::string_view str00{}; + const gdb::string_view str00{}; VERIFY( str00.length() == 0 ); VERIFY( str00.data() == nullptr ); // basic_string_view(const char*) const char str_lit01[] = "rodeo beach, marin"; - const std::string_view str01{str_lit01}; + const gdb::string_view str01{str_lit01}; VERIFY( str01.length() == 18 ); VERIFY( str01.data() == str_lit01 ); - const std::string_view str02{"baker beach, san francisco"}; + const gdb::string_view str02{"baker beach, san francisco"}; VERIFY( str02.length() == 26 ); // basic_string_view(const string_view&) - std::string_view str04{str01}; + gdb::string_view str04{str01}; VERIFY( str04.length() == str01.length() ); VERIFY( str04.data() == str01.data() ); // basic_string_view(const char* s) csize_type len_lit01 = strlen(str_lit01); - std::string_view str05{str_lit01, len_lit01}; + gdb::string_view str05{str_lit01, len_lit01}; VERIFY( str05.length() == len_lit01 ); VERIFY( str05.data() == str_lit01 ); // basic_string_view(basic_string& s) std::string istr07(10, 'z'); - std::string_view str07{istr07}; + gdb::string_view str07{istr07}; VERIFY( str07.length() == 10 ); } @@ -66,3 +63,5 @@ main() return 0; } + +} // namespace cons_1 diff --git a/gdb/unittests/basic_string_view/cons/char/2.cc b/gdb/unittests/basic_string_view/cons/char/2.cc index 8dbca7a621e..a1b45367e00 100644 --- a/gdb/unittests/basic_string_view/cons/char/2.cc +++ b/gdb/unittests/basic_string_view/cons/char/2.cc @@ -19,10 +19,7 @@ // basic_string_view constructors. -#include -#include -#include -#include +namespace cons_2 { void test03() @@ -32,9 +29,9 @@ test03() // These are tests to see how basic_string_view handles data with NUL // bytes. Obviously basic_string_view(char*) will halt at the first one, but // nothing else should. - std::string_view s1(with_nulls, 28); + gdb::string_view s1(with_nulls, 28); VERIFY( s1.size() == 28 ); - std::string_view s2(s1); + gdb::string_view s2(s1); VERIFY( s2.size() == 28 ); } @@ -45,3 +42,5 @@ main() return 0; } + +} // namespace cons_2 diff --git a/gdb/unittests/basic_string_view/cons/char/3.cc b/gdb/unittests/basic_string_view/cons/char/3.cc index c892cbc6272..358c118508e 100644 --- a/gdb/unittests/basic_string_view/cons/char/3.cc +++ b/gdb/unittests/basic_string_view/cons/char/3.cc @@ -19,15 +19,13 @@ // basic_string_view constructors. -#include -#include -#include +namespace cons_3 { void test05() { char const * s = 0; - std::string_view zero_length_built_with_NULL(s, 0); + gdb::string_view zero_length_built_with_NULL(s, 0); } int @@ -37,3 +35,5 @@ main() return 0; } + +} // namespace cons_3 diff --git a/gdb/unittests/basic_string_view/element_access/char/1.cc b/gdb/unittests/basic_string_view/element_access/char/1.cc index 03c588efb0d..7f8e79e6d00 100644 --- a/gdb/unittests/basic_string_view/element_access/char/1.cc +++ b/gdb/unittests/basic_string_view/element_access/char/1.cc @@ -19,21 +19,19 @@ // basic_string element access -#include -#include -#include +namespace element_access_1 { void test01() { - typedef std::string_view::size_type csize_type; - typedef std::string_view::const_reference cref; - typedef std::string_view::reference ref; + typedef gdb::string_view::size_type csize_type; + typedef gdb::string_view::const_reference cref; + typedef gdb::string_view::reference ref; csize_type csz01, csz02; - const std::string_view str01("tamarindo, costa rica"); - std::string_view str02("41st street beach, capitola, california"); - std::string_view str03; + const gdb::string_view str01("tamarindo, costa rica"); + gdb::string_view str02("41st street beach, capitola, california"); + gdb::string_view str03; // const_reference operator[] (size_type pos) const; csz01 = str01.size(); @@ -52,7 +50,7 @@ test01() str01.at(csz01); VERIFY( false ); // Should not get here, as exception thrown. } - catch (std::out_of_range& fail) + catch (gdb_exception& fail) { VERIFY( true ); } @@ -68,3 +66,5 @@ main() test01(); return 0; } + +} // namespace element_access_1 diff --git a/gdb/unittests/basic_string_view/element_access/char/empty.cc b/gdb/unittests/basic_string_view/element_access/char/empty.cc index fad5eb798ba..97e85d675f2 100644 --- a/gdb/unittests/basic_string_view/element_access/char/empty.cc +++ b/gdb/unittests/basic_string_view/element_access/char/empty.cc @@ -18,21 +18,22 @@ // . // -#include -#include +namespace element_access_empty { int main() { { - std::string_view empty; + gdb::string_view empty; VERIFY( empty.empty() ); } { - const std::string_view empty; + const gdb::string_view empty; VERIFY( empty.empty() ); } return 0; } + +} // namespace element_access_empty diff --git a/gdb/unittests/basic_string_view/element_access/char/front_back.cc b/gdb/unittests/basic_string_view/element_access/char/front_back.cc index efff787e365..450364726cf 100644 --- a/gdb/unittests/basic_string_view/element_access/char/front_back.cc +++ b/gdb/unittests/basic_string_view/element_access/char/front_back.cc @@ -18,14 +18,13 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace element_access_front_back { void test01() { - std::string_view str("ramifications"); - const std::string_view cstr("melodien"); + gdb::string_view str("ramifications"); + const gdb::string_view cstr("melodien"); VERIFY( str.front() == 'r' ); VERIFY( str.back() == 's' ); @@ -40,3 +39,5 @@ main() return 0; } + +} // namespace element_access_front_back diff --git a/gdb/unittests/basic_string_view/inserters/char/2.cc b/gdb/unittests/basic_string_view/inserters/char/2.cc index 6562d58615d..64b82de66a4 100644 --- a/gdb/unittests/basic_string_view/inserters/char/2.cc +++ b/gdb/unittests/basic_string_view/inserters/char/2.cc @@ -25,11 +25,7 @@ // { dg-options "-std=gnu++17" } // { dg-require-fileio "" } -#include -#include -#include -#include -#include +namespace inserters_2 { // testing basic_filebuf::xsputn via stress testing with large string_views // based on a bug report libstdc++ 9 @@ -37,13 +33,13 @@ void test05(std::size_t size) { - bool test = true; + bool test ATTRIBUTE_UNUSED = true; const char filename[] = "inserters_extractors-2.txt"; const char fillc = 'f'; std::ofstream ofs(filename); std::string str(size, fillc); - std::string_view strv{str}; + gdb::string_view strv{str}; // sanity checks VERIFY( str.size() == size ); @@ -91,3 +87,5 @@ main() return 0; } + +} // namespace inserters_2 diff --git a/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc b/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc index ba08a98eeae..f73fdc72012 100644 --- a/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc +++ b/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc @@ -17,13 +17,12 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace modifiers_remove_prefix { void test01() { - using std::string_view; + using gdb::string_view; string_view str0{"olympus mons"}; string_view::pointer p = str0.data(); @@ -33,6 +32,7 @@ test01() VERIFY( str0 == string_view{"pus mons"} ); } +#ifndef GDB_STRING_VIEW constexpr bool test02() { @@ -50,12 +50,17 @@ test02() return true; } +#endif int main() { test01(); +#ifndef GDB_STRING_VIEW static_assert( test02() ); +#endif return 0; } + +} // namespace modifiers_remove_prefix diff --git a/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc b/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc index 1b71ee936e8..0407f37d227 100644 --- a/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc +++ b/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc @@ -17,13 +17,12 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace modifiers_remove_suffix { void test01() { - using std::string_view; + using gdb::string_view; string_view str0{"olympus mons"}; string_view::pointer p = str0.data(); @@ -33,6 +32,7 @@ test01() VERIFY( str0 == string_view{"olympus mo"} ); } +#ifndef GDB_STRING_VIEW constexpr bool test02() { @@ -50,12 +50,17 @@ test02() return true; } +#endif int main() { test01(); +#ifndef GDB_STRING_VIEW static_assert( test02() ); +#endif return 0; } + +} // namespace modifiers_remove_suffix diff --git a/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc b/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc index 90d26692599..3bc7f01fa7a 100644 --- a/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc +++ b/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc @@ -18,18 +18,19 @@ // { dg-options "-std=gnu++17" } // { dg-do compile { target c++17 } } -#include +namespace modifiers_swap { -constexpr bool +void test01() { - using std::string_view; + using gdb::string_view; string_view s1{"last"}; string_view s2{"first"}; s1.swap(s2); - return s1 == "first" && s2 == "last"; + VERIFY( s1 == "first" ); + VERIFY( s2 == "last" ); } -static_assert( test01() ); +} // namespace modifiers_swap diff --git a/gdb/unittests/basic_string_view/operations/compare/char/1.cc b/gdb/unittests/basic_string_view/operations/compare/char/1.cc index 46691ebd52a..66d2613a25b 100644 --- a/gdb/unittests/basic_string_view/operations/compare/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/compare/char/1.cc @@ -29,9 +29,7 @@ // NB compare should be thought of as a lexographical compare, ie how // things would be sorted in a dictionary. -#include -#include -#include +namespace operations_compare_1 { enum want_value {lt=0, z=1, gt=2}; @@ -66,7 +64,7 @@ test_value(int result, want_value expected) int test01() { - using std::string_view; + using gdb::string_view; string_view str_0("costa rica"); string_view str_1("costa marbella"); @@ -130,3 +128,5 @@ main() return 0; } + +} // namespace operations_compare_1 diff --git a/gdb/unittests/basic_string_view/operations/compare/char/13650.cc b/gdb/unittests/basic_string_view/operations/compare/char/13650.cc index ef9df20e6ee..0cc3ee824cb 100644 --- a/gdb/unittests/basic_string_view/operations/compare/char/13650.cc +++ b/gdb/unittests/basic_string_view/operations/compare/char/13650.cc @@ -19,14 +19,13 @@ // basic_string_view::compare [lib.string_view::compare] -#include -#include +namespace operations_compare_13650 { // libstdc++/13650 void test01() { - using std::string_view; + using gdb::string_view; const char lit_01[]{ 'w', 'e', '\0', 'r', 'd' }; const char lit_02[]{ 'w', 'e', 'i', '\0', 'd' }; @@ -47,3 +46,5 @@ main() return 0; } + +} // namespace operations_compare_13650 diff --git a/gdb/unittests/basic_string_view/operations/copy/char/1.cc b/gdb/unittests/basic_string_view/operations/copy/char/1.cc index 6caf0e1507a..5b6dd346583 100644 --- a/gdb/unittests/basic_string_view/operations/copy/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/copy/char/1.cc @@ -19,16 +19,15 @@ // basic_string_view::copy -#include -#include +namespace operations_copy_1 { void test01() { - typedef std::string_view::size_type csize_type; + typedef gdb::string_view::size_type csize_type; const char str_lit01[] = "123456789A"; - const std::string_view str01(str_lit01); + const gdb::string_view str01(str_lit01); char buffer[4] = { 0 }; csize_type len = str01.copy(buffer, sizeof(buffer), 8); @@ -43,3 +42,5 @@ main() return 0; } + +} // namespace operations_copy_1 diff --git a/gdb/unittests/basic_string_view/operations/data/char/1.cc b/gdb/unittests/basic_string_view/operations/data/char/1.cc index 18fc9f5fe88..4149e16b22d 100644 --- a/gdb/unittests/basic_string_view/operations/data/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/data/char/1.cc @@ -19,16 +19,15 @@ // string_view operations -#include -#include +namespace operations_data_1 { int test01() { - std::string_view empty; + gdb::string_view empty; VERIFY( empty.size() == 0 ); - const std::string_view::value_type* p = empty.data(); + const gdb::string_view::value_type* p = empty.data(); VERIFY( p == nullptr ); return 0; @@ -41,3 +40,5 @@ main() return 0; } + +} // namespace operations_data_1 diff --git a/gdb/unittests/basic_string_view/operations/find/char/1.cc b/gdb/unittests/basic_string_view/operations/find/char/1.cc index d7d2f37706e..a68434a22a9 100644 --- a/gdb/unittests/basic_string_view/operations/find/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/find/char/1.cc @@ -19,23 +19,22 @@ // basic_string_view find -#include -#include +namespace operations_find_1 { void test01() { - typedef std::string_view::size_type csize_type; - typedef std::string_view::const_reference cref; - typedef std::string_view::reference ref; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + typedef gdb::string_view::const_reference cref; + typedef gdb::string_view::reference ref; + csize_type npos = gdb::string_view::npos; csize_type csz01, csz02; const char str_lit01[] = "mave"; - const std::string_view str01("mavericks, santa cruz"); - std::string_view str02(str_lit01); - std::string_view str03("s, s"); - std::string_view str04; + const gdb::string_view str01("mavericks, santa cruz"); + gdb::string_view str02(str_lit01); + gdb::string_view str03("s, s"); + gdb::string_view str04; // size_type find(const string_view&, size_type pos = 0) const; csz01 = str01.find(str01); @@ -85,6 +84,7 @@ test01() VERIFY( csz01 == npos ); } +#ifndef GDB_STRING_VIEW constexpr bool test02() { @@ -152,13 +152,17 @@ test02() return true; } - +#endif int main() { test01(); +#ifndef GDB_STRING_VIEW static_assert( test02() ); +#endif return 0; } + +} // namespace operations_find_1 diff --git a/gdb/unittests/basic_string_view/operations/find/char/2.cc b/gdb/unittests/basic_string_view/operations/find/char/2.cc index af8a526d12c..ce4579eaf95 100644 --- a/gdb/unittests/basic_string_view/operations/find/char/2.cc +++ b/gdb/unittests/basic_string_view/operations/find/char/2.cc @@ -19,24 +19,23 @@ // basic_string_view find_first_of -#include -#include +namespace operations_find_2 { void test02() { - typedef std::string_view::size_type csize_type; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + csize_type npos = gdb::string_view::npos; csize_type csz01, csz02; const char str_lit01[] = "mave"; - const std::string_view str01("mavericks, santa cruz"); - std::string_view str02(str_lit01); - std::string_view str03("s, s"); - std::string_view str04; + const gdb::string_view str01("mavericks, santa cruz"); + gdb::string_view str02(str_lit01); + gdb::string_view str03("s, s"); + gdb::string_view str04; // size_type find_first_of(const string_view&, size_type pos = 0) const; - std::string_view str05("xena rulez"); + gdb::string_view str05("xena rulez"); csz01 = str01.find_first_of(str01); VERIFY( csz01 == 0 ); csz01 = str01.find_first_of(str01, 4); @@ -84,6 +83,7 @@ test02() VERIFY( csz01 == csz02 ); } +#ifndef GDB_STRING_VIEW constexpr bool test03() { @@ -150,12 +150,17 @@ test03() return true; } +#endif int main() { test02(); +#ifndef GDB_STRING_VIEW static_assert( test03() ); +#endif return 0; } + +} // namespace operations_find_2 { diff --git a/gdb/unittests/basic_string_view/operations/find/char/3.cc b/gdb/unittests/basic_string_view/operations/find/char/3.cc index 5ec651ef3f9..71fd83b727c 100644 --- a/gdb/unittests/basic_string_view/operations/find/char/3.cc +++ b/gdb/unittests/basic_string_view/operations/find/char/3.cc @@ -19,21 +19,20 @@ // basic_string_view find_first_not_of -#include -#include +namespace operations_find_3 { void test03() { - typedef std::string_view::size_type csize_type; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + csize_type npos = gdb::string_view::npos; csize_type csz01; - const std::string_view str01("Bob Rock, per me"); + const gdb::string_view str01("Bob Rock, per me"); const char str_lit01[] = "Bob Rock"; - std::string_view str02("ovvero Trivi"); - std::string_view str03(str_lit01); - std::string_view str04; + gdb::string_view str02("ovvero Trivi"); + gdb::string_view str03(str_lit01); + gdb::string_view str04; // size_type find_first_not_of(const string_view&, size_type pos = 0) const; csz01 = str01.find_first_not_of(str01); @@ -84,6 +83,7 @@ test03() VERIFY( csz01 == npos ); } +#ifndef GDB_STRING_VIEW constexpr bool test04() { @@ -150,12 +150,17 @@ test04() return true; } +#endif int main() { test03(); +#ifndef GDB_STRING_VIEW static_assert( test04() ); +#endif return 0; } + +} // namespace operations_find_3 diff --git a/gdb/unittests/basic_string_view/operations/find/char/4.cc b/gdb/unittests/basic_string_view/operations/find/char/4.cc index ca4b1f17db4..23d15b29134 100644 --- a/gdb/unittests/basic_string_view/operations/find/char/4.cc +++ b/gdb/unittests/basic_string_view/operations/find/char/4.cc @@ -19,17 +19,16 @@ // basic_string_view find -#include -#include +namespace operations_find_4 { // libstdc++/31401 void test01() { - typedef std::string_view::size_type csize_type; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + csize_type npos = gdb::string_view::npos; - std::string_view use = "anu"; + gdb::string_view use = "anu"; csize_type pos1 = use.find("a", npos); VERIFY( pos1 == npos ); @@ -42,3 +41,5 @@ main() return 0; } + +} // namespace operations_find_4 diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/1.cc b/gdb/unittests/basic_string_view/operations/rfind/char/1.cc index ef26688fd76..8b0c49800d7 100644 --- a/gdb/unittests/basic_string_view/operations/rfind/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/rfind/char/1.cc @@ -17,25 +17,24 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace operations_rfind_1 { // basic_string_view rfind void test01() { - typedef std::string_view::size_type csize_type; - typedef std::string_view::const_reference cref; - typedef std::string_view::reference ref; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + typedef gdb::string_view::const_reference cref; + typedef gdb::string_view::reference ref; + csize_type npos = gdb::string_view::npos; csize_type csz01, csz02; const char str_lit01[] = "mave"; - const std::string_view str01("mavericks, santa cruz"); - std::string_view str02(str_lit01); - std::string_view str03("s, s"); - std::string_view str04; + const gdb::string_view str01("mavericks, santa cruz"); + gdb::string_view str02(str_lit01); + gdb::string_view str03("s, s"); + gdb::string_view str04; // size_type rfind(const string_view&, size_type pos = 0) const; csz01 = str01.rfind(str01); @@ -92,3 +91,5 @@ main() return 0; } + +} // namespace operations_rfind_1 diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/2.cc b/gdb/unittests/basic_string_view/operations/rfind/char/2.cc index df37f8c7745..e51022da3fa 100644 --- a/gdb/unittests/basic_string_view/operations/rfind/char/2.cc +++ b/gdb/unittests/basic_string_view/operations/rfind/char/2.cc @@ -17,16 +17,15 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace operations_rfind_2 { // basic_string_view::find_last_of void test02() { - std::string_view z("ab"); - std::string_view::size_type pos; + gdb::string_view z("ab"); + gdb::string_view::size_type pos; pos = z.find_last_of("ab"); VERIFY( pos == 1 ); pos = z.find_last_of("Xa"); @@ -34,13 +33,13 @@ test02() pos = z.find_last_of("Xb"); VERIFY( pos == 1 ); pos = z.find_last_of("XYZ"); - VERIFY( pos == std::string_view::npos ); + VERIFY( pos == gdb::string_view::npos ); pos = z.find_last_of('a'); VERIFY( pos == 0 ); pos = z.find_last_of('b'); VERIFY( pos == 1 ); pos = z.find_last_of('X'); - VERIFY( pos == std::string_view::npos ); + VERIFY( pos == gdb::string_view::npos ); } int @@ -50,3 +49,5 @@ main() return 0; } + +} // namespace operations_rfind_2 diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/3.cc b/gdb/unittests/basic_string_view/operations/rfind/char/3.cc index 37f01a28af7..02d6a732531 100644 --- a/gdb/unittests/basic_string_view/operations/rfind/char/3.cc +++ b/gdb/unittests/basic_string_view/operations/rfind/char/3.cc @@ -17,25 +17,24 @@ // with this library; see the file COPYING3. If not see // . -#include -#include +namespace operations_rfind_3 { // basic_string_view::find_last_not_of void test03() { - typedef std::string_view::size_type csize_type; - std::string_view::size_type pos; - csize_type npos = std::string_view::npos; + typedef gdb::string_view::size_type csize_type; + gdb::string_view::size_type pos; + csize_type npos = gdb::string_view::npos; - std::string_view x; + gdb::string_view x; pos = x.find_last_not_of('X'); VERIFY( pos == npos ); pos = x.find_last_not_of("XYZ"); VERIFY( pos == npos ); - std::string_view y("a"); + gdb::string_view y("a"); pos = y.find_last_not_of('X'); VERIFY( pos == 0 ); pos = y.find_last_not_of('a'); @@ -45,7 +44,7 @@ test03() pos = y.find_last_not_of("a"); VERIFY( pos == npos ); - std::string_view z("ab"); + gdb::string_view z("ab"); pos = z.find_last_not_of('X'); VERIFY( pos == 1 ); pos = z.find_last_not_of("XYZ"); @@ -64,3 +63,5 @@ main() return 0; } + +} // namespace operations_rfind_3 diff --git a/gdb/unittests/basic_string_view/operations/substr/char/1.cc b/gdb/unittests/basic_string_view/operations/substr/char/1.cc index ac65c23c54b..cbc916b33de 100644 --- a/gdb/unittests/basic_string_view/operations/substr/char/1.cc +++ b/gdb/unittests/basic_string_view/operations/substr/char/1.cc @@ -19,21 +19,19 @@ // basic_string_view::substr -#include -#include -#include +namespace operations_substr_1 { void test01() { - typedef std::string_view::size_type csize_type; - typedef std::string_view::const_reference cref; - typedef std::string_view::reference ref; + typedef gdb::string_view::size_type csize_type; + typedef gdb::string_view::const_reference cref; + typedef gdb::string_view::reference ref; csize_type csz01; const char str_lit01[] = "rockaway, pacifica"; - const std::string_view str01(str_lit01); - std::string_view str02; + const gdb::string_view str01(str_lit01); + gdb::string_view str02; // basic_string_view // substr(size_type pos = 0, size_type n = npos) const; @@ -48,7 +46,7 @@ test01() str02 = str01.substr(csz01 + 1); VERIFY( false ); } - catch(std::out_of_range& fail) + catch(gdb_exception& fail) { VERIFY( true ); } @@ -77,3 +75,5 @@ main() return 0; } + +} // namespace operations_substr_1 diff --git a/gdb/unittests/basic_string_view/operators/char/2.cc b/gdb/unittests/basic_string_view/operators/char/2.cc index 4be652daafb..963bc4cec86 100644 --- a/gdb/unittests/basic_string_view/operators/char/2.cc +++ b/gdb/unittests/basic_string_view/operators/char/2.cc @@ -109,17 +109,16 @@ template const basic_string& rhs); */ -#include -#include +namespace operators_2 { void test01() { - std::string_view str_0("costa rica"); - std::string_view str_1("costa marbella"); - std::string_view str_2("cost"); - std::string_view str_3("costa ricans"); - std::string_view str_4; + gdb::string_view str_0("costa rica"); + gdb::string_view str_1("costa marbella"); + gdb::string_view str_2("cost"); + gdb::string_view str_3("costa ricans"); + gdb::string_view str_4; str_4 = str_0; //comparisons between string objects @@ -233,6 +232,7 @@ test01() VERIFY( str_0 <= "costa rica" ); } +#ifndef GDB_STRING_VIEW constexpr bool test02() { @@ -358,10 +358,16 @@ test02() return true; } +#endif int main() { test01(); +#ifndef GDB_STRING_VIEW static_assert( test02() ); +#endif + return 0; } + +} // namespace operators_2 diff --git a/gdb/unittests/string_view-selftests.c b/gdb/unittests/string_view-selftests.c new file mode 100644 index 00000000000..182a5df9e4d --- /dev/null +++ b/gdb/unittests/string_view-selftests.c @@ -0,0 +1,177 @@ +/* Self tests for string_view for GDB, the GNU debugger. + + Copyright (C) 2018 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 . */ + +/* No need to test string_view if we're using C++17, since we're going to use + the "real" version. */ +#if __cplusplus < 201703L + +#include "defs.h" +#include "selftest.h" +#include "common/gdb_string_view.h" + +/* Used by the included .cc files below. Included here because the + included test files are wrapped in a namespace. */ +#include +#include +#include +#include + +/* 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 */ + +void +_initialize_string_view_selftests () +{ + selftests::register_test ("string_view", selftests::string_view::run_tests); +} + +#endif /* __cplusplus < 201703L */