[PATCH 7/57][Arm][GAS] Add support for MVE instructions: vstr/vldr
[binutils-gdb.git] / gdb / unittests / cli-utils-selftests.c
blob99b98bf8cd1b6007a256bcae1e0e3a050adb3a53
1 /* Unit tests for the cli-utils.c file.
3 Copyright (C) 2018-2019 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 "defs.h"
21 #include "cli/cli-utils.h"
22 #include "common/selftest.h"
24 namespace selftests {
25 namespace cli_utils {
27 static void
28 test_number_or_range_parser ()
30 /* Test parsing a simple integer. */
32 number_or_range_parser one ("1");
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);
40 /* Test parsing an integer followed by a non integer. */
42 number_or_range_parser one_after ("1 after");
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);
50 /* Test parsing a range. */
52 number_or_range_parser one_three ("1-3");
54 for (int i = 1; i < 4; i++)
56 SELF_CHECK (!one_three.finished ());
57 SELF_CHECK (one_three.get_number () == i);
59 SELF_CHECK (one_three.finished ());
60 SELF_CHECK (strcmp (one_three.cur_tok (), "") == 0);
63 /* Test parsing a range followed by a non-integer. */
65 number_or_range_parser one_three_after ("1-3 after");
67 for (int i = 1; i < 4; i++)
69 SELF_CHECK (!one_three_after.finished ());
70 SELF_CHECK (one_three_after.get_number () == i);
72 SELF_CHECK (one_three_after.finished ());
73 SELF_CHECK (strcmp (one_three_after.cur_tok (), "after") == 0);
76 /* Test a negative integer gives an error. */
78 number_or_range_parser minus_one ("-1");
80 SELF_CHECK (!minus_one.finished ());
81 try
83 minus_one.get_number ();
84 SELF_CHECK (false);
86 catch (const gdb_exception_error &ex)
88 SELF_CHECK (ex.reason == RETURN_ERROR);
89 SELF_CHECK (ex.error == GENERIC_ERROR);
90 SELF_CHECK (strcmp (ex.what (), "negative value") == 0);
91 SELF_CHECK (strcmp (minus_one.cur_tok (), "-1") == 0);
95 /* Test that a - followed by not a number does not give an error. */
97 number_or_range_parser nan ("-whatever");
99 SELF_CHECK (nan.finished ());
100 SELF_CHECK (strcmp (nan.cur_tok (), "-whatever") == 0);
104 static void
105 test_parse_flags ()
107 const char *flags = "abc";
108 const char *non_flags_args = "non flags args";
110 /* Extract twice the same flag, separated by one space. */
112 const char *t1 = "-a -a non flags args";
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);
119 /* Extract some flags, separated by one or more spaces. */
121 const char *t2 = "-c -b -c -b -c non flags args";
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);
131 /* Check behaviour where there is no flag to extract. */
133 const char *t3 = non_flags_args;
135 SELF_CHECK (parse_flags (&t3, flags) == 0);
136 SELF_CHECK (strcmp (t3, non_flags_args) == 0);
139 /* Extract 2 known flags in front of unknown flags. */
141 const char *t4 = "-c -b -x -y -z -c";
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);
150 /* Check combined flags are not recognised. */
152 const char *t5 = "-c -cb -c";
154 SELF_CHECK (parse_flags (&t5, flags) == 3);
155 SELF_CHECK (parse_flags (&t5, flags) == 0);
156 SELF_CHECK (strcmp (t5, "-cb -c") == 0);
160 static void
161 test_parse_flags_qcs ()
163 const char *non_flags_args = "non flags args";
165 /* Test parsing of 2 flags out of the known 3. */
167 const char *t1 = "-q -s non flags args";
168 qcs_flags flags;
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);
181 /* Test parsing when there is no flag. */
183 const char *t2 = "non flags args";
184 qcs_flags flags;
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);
193 /* Test parsing stops at a negative integer. */
195 const char *t3 = "-123 non flags args";
196 const char *orig_t3 = t3;
197 qcs_flags flags;
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);
206 /* Test mutual exclusion between -c and -s. */
208 const char *t4 = "-c -s non flags args";
209 qcs_flags flags;
213 SELF_CHECK (parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
214 &t4,
215 &flags) == 1);
217 (void) parse_flags_qcs ("test_parse_flags_qcs.t4.cs",
218 &t4,
219 &flags);
220 SELF_CHECK (false);
222 catch (const gdb_exception_error &ex)
224 SELF_CHECK (ex.reason == RETURN_ERROR);
225 SELF_CHECK (ex.error == GENERIC_ERROR);
226 SELF_CHECK
227 (strcmp (ex.what (),
228 "test_parse_flags_qcs.t4.cs: "
229 "-c and -s are mutually exclusive") == 0);
235 static void
236 test_cli_utils ()
238 selftests::cli_utils::test_number_or_range_parser ();
239 selftests::cli_utils::test_parse_flags ();
240 selftests::cli_utils::test_parse_flags_qcs ();
246 void
247 _initialize_cli_utils_selftests ()
249 selftests::register_test ("cli_utils",
250 selftests::cli_utils::test_cli_utils);