Fix nullptr in with_command_1
[external/binutils.git] / gdb / unittests / utils-selftests.c
1 /* Unit tests for the 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 "utils.h"
22 #include "gdbsupport/selftest.h"
23
24 namespace selftests {
25 namespace utils {
26
27 static void
28 test_substitute_path_component ()
29 {
30   auto test = [] (std::string s, const char *from, const char *to,
31                   const char *expected)
32     {
33       char *temp = xstrdup (s.c_str ());
34       substitute_path_component (&temp, from, to);
35       SELF_CHECK (strcmp (temp, expected) == 0);
36       xfree (temp);
37     };
38
39   test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g");
40   test ("abc/$def/g", "abc", "xyz", "xyz/$def/g");
41   test ("/abc/$def/g", "$def", "xyz", "/abc/xyz/g");
42   test ("/abc/$def/g", "g", "xyz", "/abc/$def/xyz");
43   test ("/abc/$def/g", "ab", "xyz", "/abc/$def/g");
44   test ("/abc/$def/g", "def", "xyz", "/abc/$def/g");
45   test ("/abc/$def/g", "abc", "abc", "/abc/$def/g");
46   test ("/abc/$def/g", "abc", "", "//$def/g");
47   test ("/abc/$def/g", "abc/$def", "xyz", "/xyz/g");
48   test ("/abc/$def/abc", "abc", "xyz", "/xyz/$def/xyz");
49 }
50
51 }
52 }
53
54 void
55 _initialize_utils_selftests ()
56 {
57   selftests::register_test ("substitute_path_component",
58                             selftests::utils::test_substitute_path_component);
59 }