]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/unittests/cli-utils-selftests.c
Rename gdb exception types
[thirdparty/binutils-gdb.git] / gdb / unittests / cli-utils-selftests.c
CommitLineData
bc7b042b
PW
1/* Unit tests for the cli-utils.c file.
2
42a4f53d 3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
bc7b042b
PW
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 "cli/cli-utils.h"
0747795c 22#include "common/selftest.h"
bc7b042b
PW
23
24namespace selftests {
25namespace cli_utils {
26
27static void
28test_number_or_range_parser ()
29{
30 /* Test parsing a simple integer. */
31 {
32 number_or_range_parser one ("1");
33
34 SELF_CHECK (!one.finished ());
35 SELF_CHECK (one.get_number () == 1);
36 SELF_CHECK (one.finished ());
37 SELF_CHECK (strcmp (one.cur_tok (), "") == 0);
38 }
39
40 /* Test parsing an integer followed by a non integer. */
41 {
42 number_or_range_parser one_after ("1 after");
43
44 SELF_CHECK (!one_after.finished ());
45 SELF_CHECK (one_after.get_number () == 1);
46 SELF_CHECK (one_after.finished ());
47 SELF_CHECK (strcmp (one_after.cur_tok (), "after") == 0);
48 }
49
50 /* Test parsing a range. */
51 {
52 number_or_range_parser one_three ("1-3");
53
54 for (int i = 1; i < 4; i++)
55 {
56 SELF_CHECK (!one_three.finished ());
57 SELF_CHECK (one_three.get_number () == i);
58 }
59 SELF_CHECK (one_three.finished ());
60 SELF_CHECK (strcmp (one_three.cur_tok (), "") == 0);
61 }
62
63 /* Test parsing a range followed by a non-integer. */
64 {
65 number_or_range_parser one_three_after ("1-3 after");
66
67 for (int i = 1; i < 4; i++)
68 {
69 SELF_CHECK (!one_three_after.finished ());
70 SELF_CHECK (one_three_after.get_number () == i);
71 }
72 SELF_CHECK (one_three_after.finished ());
73 SELF_CHECK (strcmp (one_three_after.cur_tok (), "after") == 0);
74 }
75
76 /* Test a negative integer gives an error. */
77 {
78 number_or_range_parser minus_one ("-1");
79
80 SELF_CHECK (!minus_one.finished ());
a70b8144 81 try
bc7b042b
PW
82 {
83 minus_one.get_number ();
84 SELF_CHECK (false);
85 }
230d2906 86 catch (const gdb_exception_error &ex)
bc7b042b
PW
87 {
88 SELF_CHECK (ex.reason == RETURN_ERROR);
89 SELF_CHECK (ex.error == GENERIC_ERROR);
3d6e9d23 90 SELF_CHECK (strcmp (ex.what (), "negative value") == 0);
bc7b042b
PW
91 SELF_CHECK (strcmp (minus_one.cur_tok (), "-1") == 0);
92 }
bc7b042b
PW
93 }
94
95 /* Test that a - followed by not a number does not give an error. */
96 {
97 number_or_range_parser nan ("-whatever");
98
99 SELF_CHECK (nan.finished ());
100 SELF_CHECK (strcmp (nan.cur_tok (), "-whatever") == 0);
101 }
102}
103
104static void
105test_parse_flags ()
106{
107 const char *flags = "abc";
108 const char *non_flags_args = "non flags args";
109
110 /* Extract twice the same flag, separated by one space. */
111 {
112 const char *t1 = "-a -a non flags args";
113
114 SELF_CHECK (parse_flags (&t1, flags) == 1);
115 SELF_CHECK (parse_flags (&t1, flags) == 1);
116 SELF_CHECK (strcmp (t1, non_flags_args) == 0);
117 }
118
119 /* Extract some flags, separated by one or more spaces. */
120 {
121 const char *t2 = "-c -b -c -b -c non flags args";
122
123 SELF_CHECK (parse_flags (&t2, flags) == 3);
124 SELF_CHECK (parse_flags (&t2, flags) == 2);
125 SELF_CHECK (parse_flags (&t2, flags) == 3);
126 SELF_CHECK (parse_flags (&t2, flags) == 2);
127 SELF_CHECK (parse_flags (&t2, flags) == 3);
128 SELF_CHECK (strcmp (t2, non_flags_args) == 0);
129 }
130
131 /* Check behaviour where there is no flag to extract. */
132 {
133 const char *t3 = non_flags_args;
134
135 SELF_CHECK (parse_flags (&t3, flags) == 0);
136 SELF_CHECK (strcmp (t3, non_flags_args) == 0);
137 }
138
139 /* Extract 2 known flags in front of unknown flags. */
140 {
141 const char *t4 = "-c -b -x -y -z -c";
142
143 SELF_CHECK (parse_flags (&t4, flags) == 3);
144 SELF_CHECK (parse_flags (&t4, flags) == 2);
145 SELF_CHECK (strcmp (t4, "-x -y -z -c") == 0);
146 SELF_CHECK (parse_flags (&t4, flags) == 0);
147 SELF_CHECK (strcmp (t4, "-x -y -z -c") == 0);
148 }
149
150 /* Check combined flags are not recognised. */
151 {
152 const char *t5 = "-c -cb -c";
153
154 SELF_CHECK (parse_flags (&t5, flags) == 3);
155 SELF_CHECK (parse_flags (&t5, flags) == 0);
156 SELF_CHECK (strcmp (t5, "-cb -c") == 0);
157 }
158}
159
160static void
161test_parse_flags_qcs ()
162{
163 const char *non_flags_args = "non flags args";
164
165 /* Test parsing of 2 flags out of the known 3. */
166 {
167 const char *t1 = "-q -s non flags args";
168 qcs_flags flags;
169
170 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.q",
171 &t1,
172 &flags) == 1);
173 SELF_CHECK (flags.quiet && !flags.cont && !flags.silent);
174 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t1.s",
175 &t1,
176 &flags) == 1);
177 SELF_CHECK (flags.quiet && !flags.cont && flags.silent);
178 SELF_CHECK (strcmp (t1, non_flags_args) == 0);
179 }
180
181 /* Test parsing when there is no flag. */
182 {
183 const char *t2 = "non flags args";
184 qcs_flags flags;
185
186 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t2",
187 &t2,
188 &flags) == 0);
189 SELF_CHECK (!flags.quiet && !flags.cont && !flags.silent);
190 SELF_CHECK (strcmp (t2, non_flags_args) == 0);
191 }
192
193 /* Test parsing stops at a negative integer. */
194 {
195 const char *t3 = "-123 non flags args";
196 const char *orig_t3 = t3;
197 qcs_flags flags;
198
199 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t3",
200 &t3,
201 &flags) == 0);
202 SELF_CHECK (!flags.quiet && !flags.cont && !flags.silent);
203 SELF_CHECK (strcmp (t3, orig_t3) == 0);
204 }
205
206 /* Test mutual exclusion between -c and -s. */
207 {
208 const char *t4 = "-c -s non flags args";
209 qcs_flags flags;
210
a70b8144 211 try
bc7b042b
PW
212 {
213 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
214 &t4,
215 &flags) == 1);
216
217 (void) parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
218 &t4,
219 &flags);
220 SELF_CHECK (false);
221 }
230d2906 222 catch (const gdb_exception_error &ex)
bc7b042b
PW
223 {
224 SELF_CHECK (ex.reason == RETURN_ERROR);
225 SELF_CHECK (ex.error == GENERIC_ERROR);
226 SELF_CHECK
3d6e9d23 227 (strcmp (ex.what (),
bc7b042b
PW
228 "test_parse_flags_qcs.t4.cs: "
229 "-c and -s are mutually exclusive") == 0);
230 }
bc7b042b
PW
231 }
232
233}
234
235static void
236test_cli_utils ()
237{
238 selftests::cli_utils::test_number_or_range_parser ();
239 selftests::cli_utils::test_parse_flags ();
240 selftests::cli_utils::test_parse_flags_qcs ();
241}
242
243}
244}
245
246void
247_initialize_cli_utils_selftests ()
248{
249 selftests::register_test ("cli_utils",
250 selftests::cli_utils::test_cli_utils);
251}