diff options
Diffstat (limited to 'gdb/unittests')
-rw-r--r-- | gdb/unittests/command-def-selftests.c | 4 | ||||
-rw-r--r-- | gdb/unittests/filtered_iterator-selftests.c | 52 | ||||
-rw-r--r-- | gdb/unittests/format_pieces-selftests.c | 94 |
3 files changed, 110 insertions, 40 deletions
diff --git a/gdb/unittests/command-def-selftests.c b/gdb/unittests/command-def-selftests.c index 0a54d31..095b57f 100644 --- a/gdb/unittests/command-def-selftests.c +++ b/gdb/unittests/command-def-selftests.c @@ -72,7 +72,7 @@ check_doc (struct cmd_list_element *commandlist, const char *prefix) "first line is not terminated with a '.' character"); /* Checks the doc is not terminated with a new line. */ - if (isspace (c->doc[strlen (c->doc) - 1])) + if (c_isspace (c->doc[strlen (c->doc) - 1])) broken_doc_invariant (prefix, c->name, "has superfluous trailing whitespace"); @@ -87,7 +87,7 @@ check_doc (struct cmd_list_element *commandlist, const char *prefix) else { /* \n\n is ok, so we check that explicitly here. */ - if (isspace (nl[-1]) && nl[-1] != '\n') + if (c_isspace (nl[-1]) && nl[-1] != '\n') broken_doc_invariant (prefix, c->name, "has whitespace before a newline"); } diff --git a/gdb/unittests/filtered_iterator-selftests.c b/gdb/unittests/filtered_iterator-selftests.c index 49c95cb..c04cae4 100644 --- a/gdb/unittests/filtered_iterator-selftests.c +++ b/gdb/unittests/filtered_iterator-selftests.c @@ -125,6 +125,26 @@ test_filtered_iterator () SELF_CHECK (even_ints == expected_even_ints); } +/* Same as the above, but using pointers as the iterator base type. */ + +static void +test_filtered_iterator_ptr () +{ + int array[] = { 4, 4, 5, 6, 7, 8, 9 }; + std::vector<int> even_ints; + const std::vector<int> expected_even_ints { 4, 4, 6, 8 }; + + filtered_iterator<int *, even_numbers_only> iter + (array, array + ARRAY_SIZE (array)); + filtered_iterator<int *, even_numbers_only> end + (array + ARRAY_SIZE (array), array + ARRAY_SIZE (array)); + + for (; iter != end; ++iter) + even_ints.push_back (*iter); + + SELF_CHECK (even_ints == expected_even_ints); +} + /* Test operator== and operator!=. */ static void @@ -152,6 +172,34 @@ test_filtered_iterator_eq () SELF_CHECK (!(iter1 != iter2)); } + +/* Same as the above, but using pointers as the iterator base type. */ + +static void +test_filtered_iterator_eq_ptr () +{ + int array[] = { 4, 4, 5, 6, 7, 8, 9 }; + + filtered_iterator<int *, even_numbers_only> iter1 + (array, array + ARRAY_SIZE(array)); + filtered_iterator<int *, even_numbers_only> iter2 + (array, array + ARRAY_SIZE(array)); + + /* They start equal. */ + SELF_CHECK (iter1 == iter2); + SELF_CHECK (!(iter1 != iter2)); + + /* Advance 1, now they aren't equal (despite pointing to equal values). */ + ++iter1; + SELF_CHECK (!(iter1 == iter2)); + SELF_CHECK (iter1 != iter2); + + /* Advance 2, now they are equal again. */ + ++iter2; + SELF_CHECK (iter1 == iter2); + SELF_CHECK (!(iter1 != iter2)); +} + } /* namespace selftests */ INIT_GDB_FILE (filtered_iterator_selftests) @@ -160,4 +208,8 @@ INIT_GDB_FILE (filtered_iterator_selftests) selftests::test_filtered_iterator); selftests::register_test ("filtered_iterator_eq", selftests::test_filtered_iterator_eq); + selftests::register_test ("filtered_iterator_ptr", + selftests::test_filtered_iterator_ptr); + selftests::register_test ("filtered_iterator_eq_ptr", + selftests::test_filtered_iterator_eq_ptr); } diff --git a/gdb/unittests/format_pieces-selftests.c b/gdb/unittests/format_pieces-selftests.c index c7d8ff0..2deff0f 100644 --- a/gdb/unittests/format_pieces-selftests.c +++ b/gdb/unittests/format_pieces-selftests.c @@ -29,17 +29,35 @@ namespace selftests { namespace format_pieces { +/* Like format_piece, but with the expected string hardcoded instead of an + index. */ + +struct expected_format_piece +{ + std::string_view str; + enum argclass argclass; + int n_int_args; +}; + /* Verify that parsing STR gives pieces equal to EXPECTED_PIECES. */ static void -check (const char *str, const std::vector<format_piece> &expected_pieces, +check (const char *str, + const std::vector<expected_format_piece> &expected_pieces, bool gdb_format = false) { ::format_pieces pieces (&str, gdb_format); SELF_CHECK ((pieces.end () - pieces.begin ()) == expected_pieces.size ()); - SELF_CHECK (std::equal (pieces.begin (), pieces.end (), - expected_pieces.begin ())); + + for (auto it = pieces.begin (); it != pieces.end (); ++it) + { + auto &expected = expected_pieces[it - pieces.begin ()]; + + SELF_CHECK (expected.str == pieces.piece_str (*it)); + SELF_CHECK (expected.argclass == it->argclass); + SELF_CHECK (expected.n_int_args == it->n_int_args); + } } static void @@ -47,7 +65,7 @@ test_escape_sequences () { check ("This is an escape sequence: \\e", { - format_piece ("This is an escape sequence: \e", literal_piece, 0), + {"This is an escape sequence: \e", literal_piece, 0}, }); } @@ -58,11 +76,11 @@ test_format_specifier () see a trailing empty literal piece. */ check ("Hello\\t %d%llx%%d%d", /* ARI: %ll */ { - format_piece ("Hello\t ", literal_piece, 0), - format_piece ("%d", int_arg, 0), - format_piece ("%" LL "x", long_long_arg, 0), - format_piece ("%%d", literal_piece, 0), - format_piece ("%d", int_arg, 0), + {"Hello\t ", literal_piece, 0}, + {"%d", int_arg, 0}, + {"%" LL "x", long_long_arg, 0}, + {"%%d", literal_piece, 0}, + {"%d", int_arg, 0}, }); } @@ -71,13 +89,13 @@ test_gdb_formats () { check ("Hello\\t \"%p[%pF%ps%*.*d%p]\"", { - format_piece ("Hello\\t \"", literal_piece, 0), - format_piece ("%p[", ptr_arg, 0), - format_piece ("%pF", ptr_arg, 0), - format_piece ("%ps", ptr_arg, 0), - format_piece ("%*.*d", int_arg, 2), - format_piece ("%p]", ptr_arg, 0), - format_piece ("\"", literal_piece, 0), + {"Hello\\t \"", literal_piece, 0}, + {"%p[", ptr_arg, 0}, + {"%pF", ptr_arg, 0}, + {"%ps", ptr_arg, 0}, + {"%*.*d", int_arg, 2}, + {"%p]", ptr_arg, 0}, + {"\"", literal_piece, 0}, }, true); } @@ -89,38 +107,38 @@ test_format_int_sizes () { check ("Hello\\t %hu%lu%llu%zu", /* ARI: %ll */ { - format_piece ("Hello\t ", literal_piece, 0), - format_piece ("%hu", int_arg, 0), - format_piece ("%lu", long_arg, 0), - format_piece ("%" LL "u", long_long_arg, 0), - format_piece ("%zu", size_t_arg, 0) + {"Hello\t ", literal_piece, 0}, + {"%hu", int_arg, 0}, + {"%lu", long_arg, 0}, + {"%" LL "u", long_long_arg, 0}, + {"%zu", size_t_arg, 0}, }); check ("Hello\\t %hx%lx%llx%zx", /* ARI: %ll */ { - format_piece ("Hello\t ", literal_piece, 0), - format_piece ("%hx", int_arg, 0), - format_piece ("%lx", long_arg, 0), - format_piece ("%" LL "x", long_long_arg, 0), - format_piece ("%zx", size_t_arg, 0) + {"Hello\t ", literal_piece, 0}, + {"%hx", int_arg, 0}, + {"%lx", long_arg, 0}, + {"%" LL "x", long_long_arg, 0}, + {"%zx", size_t_arg, 0}, }); check ("Hello\\t %ho%lo%llo%zo", /* ARI: %ll */ { - format_piece ("Hello\t ", literal_piece, 0), - format_piece ("%ho", int_arg, 0), - format_piece ("%lo", long_arg, 0), - format_piece ("%" LL "o", long_long_arg, 0), - format_piece ("%zo", size_t_arg, 0) + {"Hello\t ", literal_piece, 0}, + {"%ho", int_arg, 0}, + {"%lo", long_arg, 0}, + {"%" LL "o", long_long_arg, 0}, + {"%zo", size_t_arg, 0}, }); check ("Hello\\t %hd%ld%lld%zd", /* ARI: %ll */ { - format_piece ("Hello\t ", literal_piece, 0), - format_piece ("%hd", int_arg, 0), - format_piece ("%ld", long_arg, 0), - format_piece ("%" LL "d", long_long_arg, 0), - format_piece ("%zd", size_t_arg, 0) + {"Hello\t ", literal_piece, 0}, + {"%hd", int_arg, 0}, + {"%ld", long_arg, 0}, + {"%" LL "d", long_long_arg, 0}, + {"%zd", size_t_arg, 0}, }); } @@ -129,8 +147,8 @@ test_windows_formats () { check ("rc%I64d", { - format_piece ("rc", literal_piece, 0), - format_piece ("%I64d", long_long_arg, 0), + {"rc", literal_piece, 0}, + {"%I64d", long_long_arg, 0}, }); } |