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