xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/unittests/path-join-selftests.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Self tests for path_join for GDB, the GNU debugger.
2 
3    Copyright (C) 2022-2023 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 "gdbsupport/pathstuff.h"
22 #include "gdbsupport/selftest.h"
23 
24 namespace selftests {
25 namespace path_join {
26 
27 template <typename ...Args>
28 static void
29 test_one (const char *expected, Args... paths)
30 {
31   std::string actual = ::path_join (paths...);
32 
33   SELF_CHECK (actual == expected);
34 }
35 
36 /* Test path_join.  */
37 
38 static void
39 test ()
40 {
41   test_one ("/foo/bar", "/foo", "bar");
42   test_one ("/bar", "/", "bar");
43   test_one ("foo/bar/", "foo", "bar", "");
44   test_one ("foo", "", "foo");
45   test_one ("foo/bar", "foo", "", "bar");
46   test_one ("foo/", "foo", "");
47   test_one ("foo/", "foo/", "");
48 
49   test_one ("D:/foo/bar", "D:/foo", "bar");
50   test_one ("D:/foo/bar", "D:/foo/", "bar");
51 
52 #if defined(_WIN32)
53   /* The current implementation doesn't recognize backslashes as directory
54      separators on Unix-like systems, so only run these tests on Windows.  If
55      we ever switch our implementation to use std::filesystem::path, they
56      should work anywhere, in theory.  */
57   test_one ("D:\\foo/bar", "D:\\foo", "bar");
58   test_one ("D:\\foo\\bar", "D:\\foo\\", "bar");
59   test_one ("\\\\server\\dir\\file", "\\\\server\\dir\\", "file");
60   test_one ("\\\\server\\dir/file", "\\\\server\\dir", "file");
61 #endif /* _WIN32 */
62 }
63 
64 }
65 }
66 
67 void _initialize_path_join_selftests ();
68 void
69 _initialize_path_join_selftests ()
70 {
71   selftests::register_test ("path_join",
72 			    selftests::path_join::test);
73 }
74