GDB (xrefs)
Loading...
Searching...
No Matches
format_pieces-selftests.c
Go to the documentation of this file.
1/* Self tests for format_pieces for GDB, the GNU debugger.
2
3 Copyright (C) 2018-2023 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#if USE_PRINTF_I64
25#define LL "I64"
26#else
27#define LL "ll"
28#endif
29
30namespace selftests {
31namespace format_pieces {
32
33/* Verify that parsing STR gives pieces equal to EXPECTED_PIECES. */
34
35static void
36check (const char *str, const std::vector<format_piece> &expected_pieces,
37 bool gdb_format = false)
38{
39 ::format_pieces pieces (&str, gdb_format);
40
41 SELF_CHECK ((pieces.end () - pieces.begin ()) == expected_pieces.size ());
42 SELF_CHECK (std::equal (pieces.begin (), pieces.end (),
43 expected_pieces.begin ()));
44}
45
46static void
48{
49 check ("This is an escape sequence: \\e",
50 {
51 format_piece ("This is an escape sequence: \e", literal_piece, 0),
52 });
53}
54
55static void
57{
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 */
61 {
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 });
68}
69
70static void
72{
73 check ("Hello\\t \"%p[%pF%ps%*.*d%p]\"",
74 {
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);
83}
84
85/* Test the different size modifiers that can be applied to an integer
86 argument. Test with different integer format specifiers too. */
87
88static void
90{
91 check ("Hello\\t %hu%lu%llu%zu", /* ARI: %ll */
92 {
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 });
99
100 check ("Hello\\t %hx%lx%llx%zx", /* ARI: %ll */
101 {
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)
107 });
108
109 check ("Hello\\t %ho%lo%llo%zo", /* ARI: %ll */
110 {
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)
116 });
117
118 check ("Hello\\t %hd%ld%lld%zd", /* ARI: %ll */
119 {
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)
125 });
126}
127
128static void
130{
131 check ("rc%I64d",
132 {
133 format_piece ("rc", literal_piece, 0),
134 format_piece ("%I64d", long_long_arg, 0),
135 });
136}
137
138static void
147
148} /* namespace format_pieces */
149} /* namespace selftests */
150
152void
154{
155 selftests::register_test ("format_pieces",
157}
#define LL
void _initialize_format_pieces_selftests()
static void check(const char *str, const std::vector< format_piece > &expected_pieces, bool gdb_format=false)