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/>. */
21 #include "cli/cli-utils.h"
22 #include "gdbsupport/selftest.h"
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 ());
83 minus_one.get_number ();
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);
107 selftests::cli_utils::test_number_or_range_parser ();
114 _initialize_cli_utils_selftests ()
116 selftests::register_test ("cli_utils",
117 selftests::cli_utils::test_cli_utils);