| 1 | /* Self tests for format_pieces for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 2018-2020 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 | #include "defs.h" |
| 21 | #include "gdbsupport/format.h" |
| 22 | #include "gdbsupport/selftest.h" |
| 23 | |
| 24 | namespace selftests { |
| 25 | namespace format_pieces { |
| 26 | |
| 27 | /* Verify that parsing STR gives pieces equal to EXPECTED_PIECES. */ |
| 28 | |
| 29 | static void |
| 30 | check (const char *str, const std::vector<format_piece> &expected_pieces, |
| 31 | bool gdb_format = false) |
| 32 | { |
| 33 | ::format_pieces pieces (&str, gdb_format); |
| 34 | |
| 35 | SELF_CHECK ((pieces.end () - pieces.begin ()) == expected_pieces.size ()); |
| 36 | SELF_CHECK (std::equal (pieces.begin (), pieces.end (), |
| 37 | expected_pieces.begin ())); |
| 38 | } |
| 39 | |
| 40 | static void |
| 41 | test_escape_sequences () |
| 42 | { |
| 43 | check ("This is an escape sequence: \\e", |
| 44 | { |
| 45 | format_piece ("This is an escape sequence: \e", literal_piece, 0), |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | test_format_specifier () |
| 51 | { |
| 52 | /* The format string here ends with a % sequence, to ensure we don't |
| 53 | see a trailing empty literal piece. */ |
| 54 | check ("Hello\\t %d%llx%%d%d", /* ARI: %ll */ |
| 55 | { |
| 56 | format_piece ("Hello\t ", literal_piece, 0), |
| 57 | format_piece ("%d", int_arg, 0), |
| 58 | format_piece ("%llx", long_long_arg, 0), /* ARI: %ll */ |
| 59 | format_piece ("%%d", literal_piece, 0), |
| 60 | format_piece ("%d", int_arg, 0), |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | test_gdb_formats () |
| 66 | { |
| 67 | check ("Hello\\t \"%p[%pF%ps%*.*d%p]\"", |
| 68 | { |
| 69 | format_piece ("Hello\\t \"", literal_piece, 0), |
| 70 | format_piece ("%p[", ptr_arg, 0), |
| 71 | format_piece ("%pF", ptr_arg, 0), |
| 72 | format_piece ("%ps", ptr_arg, 0), |
| 73 | format_piece ("%*.*d", int_arg, 2), |
| 74 | format_piece ("%p]", ptr_arg, 0), |
| 75 | format_piece ("\"", literal_piece, 0), |
| 76 | }, true); |
| 77 | } |
| 78 | |
| 79 | /* Test the different size modifiers that can be applied to an integer |
| 80 | argument. Test with different integer format specifiers too. */ |
| 81 | |
| 82 | static void |
| 83 | test_format_int_sizes () |
| 84 | { |
| 85 | check ("Hello\\t %hu%lu%llu%zu", /* ARI: %ll */ |
| 86 | { |
| 87 | format_piece ("Hello\t ", literal_piece, 0), |
| 88 | format_piece ("%hu", int_arg, 0), |
| 89 | format_piece ("%lu", long_arg, 0), |
| 90 | format_piece ("%llu", long_long_arg, 0), /* ARI: %ll */ |
| 91 | format_piece ("%zu", size_t_arg, 0) |
| 92 | }); |
| 93 | |
| 94 | check ("Hello\\t %hx%lx%llx%zx", /* ARI: %ll */ |
| 95 | { |
| 96 | format_piece ("Hello\t ", literal_piece, 0), |
| 97 | format_piece ("%hx", int_arg, 0), |
| 98 | format_piece ("%lx", long_arg, 0), |
| 99 | format_piece ("%llx", long_long_arg, 0), /* ARI: %ll */ |
| 100 | format_piece ("%zx", size_t_arg, 0) |
| 101 | }); |
| 102 | |
| 103 | check ("Hello\\t %ho%lo%llo%zo", /* ARI: %ll */ |
| 104 | { |
| 105 | format_piece ("Hello\t ", literal_piece, 0), |
| 106 | format_piece ("%ho", int_arg, 0), |
| 107 | format_piece ("%lo", long_arg, 0), |
| 108 | format_piece ("%llo", long_long_arg, 0), /* ARI: %ll */ |
| 109 | format_piece ("%zo", size_t_arg, 0) |
| 110 | }); |
| 111 | |
| 112 | check ("Hello\\t %hd%ld%lld%zd", /* ARI: %ll */ |
| 113 | { |
| 114 | format_piece ("Hello\t ", literal_piece, 0), |
| 115 | format_piece ("%hd", int_arg, 0), |
| 116 | format_piece ("%ld", long_arg, 0), |
| 117 | format_piece ("%lld", long_long_arg, 0), /* ARI: %ll */ |
| 118 | format_piece ("%zd", size_t_arg, 0) |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | test_windows_formats () |
| 124 | { |
| 125 | check ("rc%I64d", |
| 126 | { |
| 127 | format_piece ("rc", literal_piece, 0), |
| 128 | format_piece ("%I64d", long_long_arg, 0), |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | run_tests () |
| 134 | { |
| 135 | test_escape_sequences (); |
| 136 | test_format_specifier (); |
| 137 | test_gdb_formats (); |
| 138 | test_format_int_sizes (); |
| 139 | test_windows_formats (); |
| 140 | } |
| 141 | |
| 142 | } /* namespace format_pieces */ |
| 143 | } /* namespace selftests */ |
| 144 | |
| 145 | void _initialize_format_pieces_selftests (); |
| 146 | void |
| 147 | _initialize_format_pieces_selftests () |
| 148 | { |
| 149 | selftests::register_test ("format_pieces", |
| 150 | selftests::format_pieces::run_tests); |
| 151 | } |