Commit | Line | Data |
---|---|---|
d4081a38 PA |
1 | /* Self tests for general utility routines for GDB, the GNU debugger. |
2 | ||
42a4f53d | 3 | Copyright (C) 2016-2019 Free Software Foundation, Inc. |
d4081a38 PA |
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 | ||
268a13a5 TT |
20 | #include "gdbsupport/common-defs.h" |
21 | #include "gdbsupport/selftest.h" | |
d4081a38 | 22 | |
d4081a38 PA |
23 | namespace selftests { |
24 | ||
4a250334 PA |
25 | /* Type of both 'string_printf' and the 'format' function below. Used |
26 | to run the same tests against both string_printf and | |
27 | string_vprintf. */ | |
28 | typedef std::string (format_func) (const char *fmt, ...) | |
29 | ATTRIBUTE_PRINTF (1, 2); | |
30 | ||
d4081a38 | 31 | static void |
4a250334 | 32 | test_format_func (format_func *func) |
d4081a38 | 33 | { |
4a250334 PA |
34 | /* Basic smoke tests. */ |
35 | SELF_CHECK (func ("%s", "") == ""); | |
36 | SELF_CHECK (func ("%s", "test") == "test"); | |
37 | SELF_CHECK (func ("%d", 23) == "23"); | |
38 | SELF_CHECK (func ("%s %d %s", "test", 23, "done") == "test 23 done"); | |
39 | SELF_CHECK (func ("nothing") == "nothing"); | |
40 | SELF_CHECK (func ("%d comes before 2", 1) == "1 comes before 2"); | |
41 | SELF_CHECK (func ("hello %s", "world") == "hello world"); | |
d4081a38 | 42 | |
4a250334 PA |
43 | /* Check that we don't mishandle very large strings. (An earlier |
44 | non-public implementation of string_printf mishandled this). */ | |
d4081a38 PA |
45 | #define X10 "0123456789" |
46 | #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10 | |
47 | #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100 | |
48 | #define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 | |
49 | #define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 | |
4a250334 PA |
50 | SELF_CHECK (func ("%s", X10) == X10); |
51 | SELF_CHECK (func ("%s", X100) == X100); | |
52 | SELF_CHECK (func ("%s", X1000) == X1000); | |
53 | SELF_CHECK (func ("%s", X10000) == X10000); | |
54 | SELF_CHECK (func ("%s", X100000) == X100000); | |
55 | } | |
56 | ||
57 | static void | |
58 | string_printf_tests () | |
59 | { | |
60 | test_format_func (string_printf); | |
d4081a38 PA |
61 | } |
62 | ||
b5540b5f | 63 | static std::string ATTRIBUTE_PRINTF (1, 2) |
bd413795 TT |
64 | format (const char *fmt, ...) |
65 | { | |
66 | va_list vp; | |
67 | ||
68 | va_start (vp, fmt); | |
69 | std::string result = string_vprintf (fmt, vp); | |
70 | va_end (vp); | |
71 | return result; | |
72 | } | |
73 | ||
74 | static void | |
75 | string_vprintf_tests () | |
76 | { | |
4a250334 | 77 | test_format_func (format); |
bd413795 TT |
78 | } |
79 | ||
31b833b3 PA |
80 | /* Type of both 'string_appendf' and the 'string_vappendf_wrapper' |
81 | function below. Used to run the same tests against both | |
82 | string_appendf and string_vappendf. */ | |
83 | typedef void (string_appendf_func) (std::string &str, const char *fmt, ...) | |
84 | ATTRIBUTE_PRINTF (2, 3); | |
85 | ||
86 | static void | |
87 | test_appendf_func (string_appendf_func *func) | |
88 | { | |
89 | std::string str; | |
90 | ||
91 | func (str, "%s", ""); | |
92 | SELF_CHECK (str == ""); | |
93 | ||
94 | func (str, "%s", "test"); | |
95 | SELF_CHECK (str == "test"); | |
96 | ||
97 | func (str, "%d", 23); | |
98 | SELF_CHECK (str == "test23"); | |
99 | ||
100 | func (str, "%s %d %s", "foo", 45, "bar"); | |
101 | SELF_CHECK (str == "test23foo 45 bar"); | |
102 | } | |
103 | ||
104 | static void ATTRIBUTE_PRINTF (2, 3) | |
105 | string_vappendf_wrapper (std::string &str, const char *fmt, ...) | |
106 | { | |
107 | va_list vp; | |
108 | ||
109 | va_start (vp, fmt); | |
110 | string_vappendf (str, fmt, vp); | |
111 | va_end (vp); | |
112 | } | |
113 | ||
114 | static void | |
115 | string_appendf_tests () | |
116 | { | |
117 | test_appendf_func (string_appendf); | |
118 | } | |
119 | ||
120 | static void | |
121 | string_vappendf_tests () | |
122 | { | |
123 | test_appendf_func (string_vappendf_wrapper); | |
124 | } | |
125 | ||
d4081a38 PA |
126 | } /* namespace selftests */ |
127 | ||
d4081a38 | 128 | void |
b2f8eb7a | 129 | _initialize_common_utils_selftests () |
d4081a38 | 130 | { |
b2f8eb7a | 131 | selftests::register_test ("string_printf", selftests::string_printf_tests); |
bd413795 | 132 | selftests::register_test ("string_vprintf", selftests::string_vprintf_tests); |
31b833b3 PA |
133 | selftests::register_test ("string_appendf", selftests::string_appendf_tests); |
134 | selftests::register_test ("string_vappendf", | |
135 | selftests::string_vappendf_tests); | |
d4081a38 | 136 | } |