1 /* Self tests for general utility routines for GDB, the GNU debugger.
3 Copyright (C) 2016-2024 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 "gdbsupport/selftest.h"
24 /* Type of both 'string_printf' and the 'format' function below. Used
25 to run the same tests against both string_printf and
27 typedef std::string (format_func
) (const char *fmt
, ...)
28 ATTRIBUTE_PRINTF (1, 2);
31 test_format_func (format_func
*func
)
33 /* Basic smoke tests. */
34 SELF_CHECK (func ("%s", "") == "");
35 SELF_CHECK (func ("%s", "test") == "test");
36 SELF_CHECK (func ("%d", 23) == "23");
37 SELF_CHECK (func ("%s %d %s", "test", 23, "done") == "test 23 done");
38 SELF_CHECK (func ("nothing") == "nothing");
39 SELF_CHECK (func ("%d comes before 2", 1) == "1 comes before 2");
40 SELF_CHECK (func ("hello %s", "world") == "hello world");
42 /* Check that we don't mishandle very large strings. (An earlier
43 non-public implementation of string_printf mishandled this). */
44 #define X10 "0123456789"
45 #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
46 #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
47 #define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000
48 #define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000
49 SELF_CHECK (func ("%s", X10
) == X10
);
50 SELF_CHECK (func ("%s", X100
) == X100
);
51 SELF_CHECK (func ("%s", X1000
) == X1000
);
52 SELF_CHECK (func ("%s", X10000
) == X10000
);
53 SELF_CHECK (func ("%s", X100000
) == X100000
);
57 string_printf_tests ()
59 test_format_func (string_printf
);
62 static std::string
ATTRIBUTE_PRINTF (1, 2)
63 format (const char *fmt
, ...)
68 std::string result
= string_vprintf (fmt
, vp
);
74 string_vprintf_tests ()
76 test_format_func (format
);
79 /* Type of both 'string_appendf' and the 'string_vappendf_wrapper'
80 function below. Used to run the same tests against both
81 string_appendf and string_vappendf. */
82 typedef std::string
&(string_appendf_func
) (std::string
&str
, const char *fmt
,
84 ATTRIBUTE_PRINTF (2, 3);
87 test_appendf_func (string_appendf_func
*func
)
92 SELF_CHECK (str
== "");
94 func (str
, "%s", "test");
95 SELF_CHECK (str
== "test");
98 SELF_CHECK (str
== "test23");
100 func (str
, "%s %d %s", "foo", 45, "bar");
101 SELF_CHECK (str
== "test23foo 45 bar");
104 static std::string
& ATTRIBUTE_PRINTF (2, 3)
105 string_vappendf_wrapper (std::string
&str
, const char *fmt
, ...)
110 string_vappendf (str
, fmt
, vp
);
117 string_appendf_tests ()
119 test_appendf_func (string_appendf
);
123 string_vappendf_tests ()
125 test_appendf_func (string_vappendf_wrapper
);
128 } /* namespace selftests */
130 void _initialize_common_utils_selftests ();
132 _initialize_common_utils_selftests ()
134 selftests::register_test ("string_printf", selftests::string_printf_tests
);
135 selftests::register_test ("string_vprintf", selftests::string_vprintf_tests
);
136 selftests::register_test ("string_appendf", selftests::string_appendf_tests
);
137 selftests::register_test ("string_vappendf",
138 selftests::string_vappendf_tests
);