path-join-selftests.c revision 1.1 1 1.1 christos /* Self tests for path_join for GDB, the GNU debugger.
2 1.1 christos
3 1.1 christos Copyright (C) 2022-2023 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "gdbsupport/pathstuff.h"
22 1.1 christos #include "gdbsupport/selftest.h"
23 1.1 christos
24 1.1 christos namespace selftests {
25 1.1 christos namespace path_join {
26 1.1 christos
27 1.1 christos template <typename ...Args>
28 1.1 christos static void
29 1.1 christos test_one (const char *expected, Args... paths)
30 1.1 christos {
31 1.1 christos std::string actual = ::path_join (paths...);
32 1.1 christos
33 1.1 christos SELF_CHECK (actual == expected);
34 1.1 christos }
35 1.1 christos
36 1.1 christos /* Test path_join. */
37 1.1 christos
38 1.1 christos static void
39 1.1 christos test ()
40 1.1 christos {
41 1.1 christos test_one ("/foo/bar", "/foo", "bar");
42 1.1 christos test_one ("/bar", "/", "bar");
43 1.1 christos test_one ("foo/bar/", "foo", "bar", "");
44 1.1 christos test_one ("foo", "", "foo");
45 1.1 christos test_one ("foo/bar", "foo", "", "bar");
46 1.1 christos test_one ("foo/", "foo", "");
47 1.1 christos test_one ("foo/", "foo/", "");
48 1.1 christos
49 1.1 christos test_one ("D:/foo/bar", "D:/foo", "bar");
50 1.1 christos test_one ("D:/foo/bar", "D:/foo/", "bar");
51 1.1 christos
52 1.1 christos #if defined(_WIN32)
53 1.1 christos /* The current implementation doesn't recognize backslashes as directory
54 1.1 christos separators on Unix-like systems, so only run these tests on Windows. If
55 1.1 christos we ever switch our implementation to use std::filesystem::path, they
56 1.1 christos should work anywhere, in theory. */
57 1.1 christos test_one ("D:\\foo/bar", "D:\\foo", "bar");
58 1.1 christos test_one ("D:\\foo\\bar", "D:\\foo\\", "bar");
59 1.1 christos test_one ("\\\\server\\dir\\file", "\\\\server\\dir\\", "file");
60 1.1 christos test_one ("\\\\server\\dir/file", "\\\\server\\dir", "file");
61 1.1 christos #endif /* _WIN32 */
62 1.1 christos }
63 1.1 christos
64 1.1 christos }
65 1.1 christos }
66 1.1 christos
67 1.1 christos void _initialize_path_join_selftests ();
68 1.1 christos void
69 1.1 christos _initialize_path_join_selftests ()
70 1.1 christos {
71 1.1 christos selftests::register_test ("path_join",
72 1.1 christos selftests::path_join::test);
73 1.1 christos }
74