| 1 | /* Unit tests for the cli-utils.c file. |
| 2 | |
| 3 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
| 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" |
| 22 | #include "gdbsupport/selftest.h" |
| 23 | |
| 24 | namespace selftests { |
| 25 | namespace cli_utils { |
| 26 | |
| 27 | static void |
| 28 | test_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 ()); |
| 81 | try |
| 82 | { |
| 83 | minus_one.get_number (); |
| 84 | SELF_CHECK (false); |
| 85 | } |
| 86 | catch (const gdb_exception_error &ex) |
| 87 | { |
| 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); |
| 92 | } |
| 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 | |
| 104 | static void |
| 105 | test_cli_utils () |
| 106 | { |
| 107 | selftests::cli_utils::test_number_or_range_parser (); |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | _initialize_cli_utils_selftests () |
| 115 | { |
| 116 | selftests::register_test ("cli_utils", |
| 117 | selftests::cli_utils::test_cli_utils); |
| 118 | } |