Don't create empty literal pieces
[deliverable/binutils-gdb.git] / gdb / unittests / format_pieces-selftests.c
1 /* Self tests for format_pieces for GDB, the GNU debugger.
2
3 Copyright (C) 2018-2019 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 {
32 ::format_pieces pieces (&str);
33
34 SELF_CHECK ((pieces.end () - pieces.begin ()) == expected_pieces.size ());
35 SELF_CHECK (std::equal (pieces.begin (), pieces.end (),
36 expected_pieces.begin ()));
37 }
38
39 static void
40 test_escape_sequences ()
41 {
42 check ("This is an escape sequence: \\e",
43 {
44 format_piece ("This is an escape sequence: \e", literal_piece),
45 });
46 }
47
48 static void
49 test_format_specifier ()
50 {
51 /* The format string here ends with a % sequence, to ensure we don't
52 see a trailing empty literal piece. */
53 check ("Hello %d%llx%%d%d", /* ARI: %ll */
54 {
55 format_piece ("Hello ", literal_piece),
56 format_piece ("%d", int_arg),
57 format_piece ("%llx", long_long_arg), /* ARI: %ll */
58 format_piece ("%%d", literal_piece),
59 format_piece ("%d", int_arg),
60 });
61 }
62
63 static void
64 run_tests ()
65 {
66 test_escape_sequences ();
67 test_format_specifier ();
68 }
69
70 } /* namespace format_pieces */
71 } /* namespace selftests */
72
73 void
74 _initialize_format_pieces_selftests ()
75 {
76 selftests::register_test ("format_pieces",
77 selftests::format_pieces::run_tests);
78 }
This page took 0.042993 seconds and 5 git commands to generate.