1 // filesystem relative_test.cpp ---------------------------------------------------- // 2 3 // Copyright Beman Dawes 2015 4 5 // Distributed under the Boost Software License, Version 1.0. 6 // See http://www.boost.org/LICENSE_1_0.txt 7 8 // Library home page: http://www.boost.org/libs/filesystem 9 10 // ---------------------------------------------------------------------------------- // 11 // 12 // At least initially, development is easier if these tests are in a separate file. 13 // 14 // ---------------------------------------------------------------------------------- // 15 16 #include <boost/config/warning_disable.hpp> 17 #include <boost/filesystem/path.hpp> 18 #include <boost/detail/lightweight_test_report.hpp> 19 #include <iostream> 20 21 using boost::filesystem::path; 22 using std::cout; 23 using std::endl; 24 25 namespace 26 { lexically_relative_test()27 void lexically_relative_test() 28 { 29 cout << "lexically_relative_test..." << endl; 30 31 BOOST_TEST(path("").lexically_relative("") == ""); 32 BOOST_TEST(path("").lexically_relative("/foo") == ""); 33 BOOST_TEST(path("/foo").lexically_relative("") == ""); 34 BOOST_TEST(path("/foo").lexically_relative("/foo") == "."); 35 BOOST_TEST(path("").lexically_relative("foo") == ""); 36 BOOST_TEST(path("foo").lexically_relative("") == ""); 37 BOOST_TEST(path("foo").lexically_relative("foo") == "."); 38 39 BOOST_TEST(path("a/b/c").lexically_relative("a") == "b/c"); 40 BOOST_TEST(path("a//b//c").lexically_relative("a") == "b/c"); 41 BOOST_TEST(path("a/b/c").lexically_relative("a/b") == "c"); 42 BOOST_TEST(path("a///b//c").lexically_relative("a//b") == "c"); 43 BOOST_TEST(path("a/b/c").lexically_relative("a/b/c") == "."); 44 BOOST_TEST(path("a/b/c").lexically_relative("a/b/c/x") == ".."); 45 BOOST_TEST(path("a/b/c").lexically_relative("a/b/c/x/y") == "../.."); 46 BOOST_TEST(path("a/b/c").lexically_relative("a/x") == "../b/c"); 47 BOOST_TEST(path("a/b/c").lexically_relative("a/b/x") == "../c"); 48 BOOST_TEST(path("a/b/c").lexically_relative("a/x/y") == "../../b/c"); 49 BOOST_TEST(path("a/b/c").lexically_relative("a/b/x/y") == "../../c"); 50 BOOST_TEST(path("a/b/c").lexically_relative("a/b/c/x/y/z") == "../../.."); 51 BOOST_TEST(path("a/b/c").lexically_relative("a/") == "b/c"); 52 BOOST_TEST(path("a/b/c").lexically_relative("a/.") == "b/c"); 53 BOOST_TEST(path("a/b/c").lexically_relative("a/./") == "b/c"); 54 BOOST_TEST(path("a/b/c").lexically_relative("a/b/..") == ""); 55 BOOST_TEST(path("a/b/c").lexically_relative("a/b/../") == ""); 56 BOOST_TEST(path("a/b/c").lexically_relative("a/b/d/..") == "c"); 57 BOOST_TEST(path("a/b/c").lexically_relative("a/b/d/../") == "c"); 58 59 // paths unrelated except first element, and first element is root directory 60 BOOST_TEST(path("/a/b/c").lexically_relative("/x") == "../a/b/c"); 61 BOOST_TEST(path("/a/b/c").lexically_relative("/x/y") == "../../a/b/c"); 62 BOOST_TEST(path("/a/b/c").lexically_relative("/x/y/z") == "../../../a/b/c"); 63 64 // paths unrelated 65 BOOST_TEST(path("a/b/c").lexically_relative("x") == ""); 66 BOOST_TEST(path("a/b/c").lexically_relative("x/y") == ""); 67 BOOST_TEST(path("a/b/c").lexically_relative("x/y/z") == ""); 68 BOOST_TEST(path("a/b/c").lexically_relative("/x") == ""); 69 BOOST_TEST(path("a/b/c").lexically_relative("/x/y") == ""); 70 BOOST_TEST(path("a/b/c").lexically_relative("/x/y/z") == ""); 71 BOOST_TEST(path("a/b/c").lexically_relative("/a/b/c") == ""); 72 73 // TODO: add some Windows-only test cases that probe presence or absence of 74 // drive specifier-and root-directory 75 76 // Some tests from Jamie Allsop's paper 77 BOOST_TEST(path("/a/d").lexically_relative("/a/b/c") == "../../d"); 78 BOOST_TEST(path("/a/b/c").lexically_relative("/a/d") == "../b/c"); 79 #ifdef BOOST_WINDOWS_API 80 BOOST_TEST(path("c:\\y").lexically_relative("c:\\x") == "../y"); 81 #else 82 BOOST_TEST(path("c:\\y").lexically_relative("c:\\x") == ""); 83 #endif 84 BOOST_TEST(path("d:\\y").lexically_relative("c:\\x") == ""); 85 86 // From issue #1976 87 BOOST_TEST(path("/foo/new").lexically_relative("/foo/bar") == "../new"); 88 } 89 lexically_proximate_test()90 void lexically_proximate_test() 91 { 92 cout << "lexically_proximate_test..." << endl; 93 // paths unrelated 94 BOOST_TEST(path("a/b/c").lexically_proximate("x") == "a/b/c"); 95 } 96 } // unnamed namespace 97 98 //--------------------------------------------------------------------------------------// 99 // // 100 // main // 101 // // 102 //--------------------------------------------------------------------------------------// 103 test_main(int,char * [])104int test_main(int, char*[]) 105 { 106 // document state of critical macros 107 #ifdef BOOST_POSIX_API 108 cout << "BOOST_POSIX_API" << endl; 109 #endif 110 #ifdef BOOST_WINDOWS_API 111 cout << "BOOST_WINDOWS_API" << endl; 112 #endif 113 114 lexically_relative_test(); 115 lexically_proximate_test(); 116 117 return ::boost::report_errors(); 118 } 119