Automatic date update in version.in
[binutils-gdb.git] / gdb / unittests / format_pieces-selftests.c
blob336a81456e397d2aa5065cf6a5c158843bb1174f
1 /* Self tests for format_pieces for GDB, the GNU debugger.
3 Copyright (C) 2018-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "gdbsupport/format.h"
22 #include "gdbsupport/selftest.h"
24 #if USE_PRINTF_I64
25 #define LL "I64"
26 #else
27 #define LL "ll"
28 #endif
30 namespace selftests {
31 namespace format_pieces {
33 /* Verify that parsing STR gives pieces equal to EXPECTED_PIECES. */
35 static void
36 check (const char *str, const std::vector<format_piece> &expected_pieces,
37 bool gdb_format = false)
39 ::format_pieces pieces (&str, gdb_format);
41 SELF_CHECK ((pieces.end () - pieces.begin ()) == expected_pieces.size ());
42 SELF_CHECK (std::equal (pieces.begin (), pieces.end (),
43 expected_pieces.begin ()));
46 static void
47 test_escape_sequences ()
49 check ("This is an escape sequence: \\e",
51 format_piece ("This is an escape sequence: \e", literal_piece, 0),
52 });
55 static void
56 test_format_specifier ()
58 /* The format string here ends with a % sequence, to ensure we don't
59 see a trailing empty literal piece. */
60 check ("Hello\\t %d%llx%%d%d", /* ARI: %ll */
62 format_piece ("Hello\t ", literal_piece, 0),
63 format_piece ("%d", int_arg, 0),
64 format_piece ("%" LL "x", long_long_arg, 0),
65 format_piece ("%%d", literal_piece, 0),
66 format_piece ("%d", int_arg, 0),
67 });
70 static void
71 test_gdb_formats ()
73 check ("Hello\\t \"%p[%pF%ps%*.*d%p]\"",
75 format_piece ("Hello\\t \"", literal_piece, 0),
76 format_piece ("%p[", ptr_arg, 0),
77 format_piece ("%pF", ptr_arg, 0),
78 format_piece ("%ps", ptr_arg, 0),
79 format_piece ("%*.*d", int_arg, 2),
80 format_piece ("%p]", ptr_arg, 0),
81 format_piece ("\"", literal_piece, 0),
82 }, true);
85 /* Test the different size modifiers that can be applied to an integer
86 argument. Test with different integer format specifiers too. */
88 static void
89 test_format_int_sizes ()
91 check ("Hello\\t %hu%lu%llu%zu", /* ARI: %ll */
93 format_piece ("Hello\t ", literal_piece, 0),
94 format_piece ("%hu", int_arg, 0),
95 format_piece ("%lu", long_arg, 0),
96 format_piece ("%" LL "u", long_long_arg, 0),
97 format_piece ("%zu", size_t_arg, 0)
98 });
100 check ("Hello\\t %hx%lx%llx%zx", /* ARI: %ll */
102 format_piece ("Hello\t ", literal_piece, 0),
103 format_piece ("%hx", int_arg, 0),
104 format_piece ("%lx", long_arg, 0),
105 format_piece ("%" LL "x", long_long_arg, 0),
106 format_piece ("%zx", size_t_arg, 0)
109 check ("Hello\\t %ho%lo%llo%zo", /* ARI: %ll */
111 format_piece ("Hello\t ", literal_piece, 0),
112 format_piece ("%ho", int_arg, 0),
113 format_piece ("%lo", long_arg, 0),
114 format_piece ("%" LL "o", long_long_arg, 0),
115 format_piece ("%zo", size_t_arg, 0)
118 check ("Hello\\t %hd%ld%lld%zd", /* ARI: %ll */
120 format_piece ("Hello\t ", literal_piece, 0),
121 format_piece ("%hd", int_arg, 0),
122 format_piece ("%ld", long_arg, 0),
123 format_piece ("%" LL "d", long_long_arg, 0),
124 format_piece ("%zd", size_t_arg, 0)
128 static void
129 test_windows_formats ()
131 check ("rc%I64d",
133 format_piece ("rc", literal_piece, 0),
134 format_piece ("%I64d", long_long_arg, 0),
138 static void
139 run_tests ()
141 test_escape_sequences ();
142 test_format_specifier ();
143 test_gdb_formats ();
144 test_format_int_sizes ();
145 test_windows_formats ();
148 } /* namespace format_pieces */
149 } /* namespace selftests */
151 void _initialize_format_pieces_selftests ();
152 void
153 _initialize_format_pieces_selftests ()
155 selftests::register_test ("format_pieces",
156 selftests::format_pieces::run_tests);