1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <experimental/filesystem>
13
14 // path absolute(const path& p, const path& base=current_path());
15
16 #include <experimental/filesystem>
17 #include <type_traits>
18 #include <cassert>
19
20 #include "test_macros.h"
21 #include "rapid-cxx-test.hpp"
22 #include "filesystem_test_helper.hpp"
23
24 using namespace std::experimental::filesystem;
25
26 TEST_SUITE(filesystem_absolute_path_test_suite)
27
TEST_CASE(absolute_signature_test)28 TEST_CASE(absolute_signature_test)
29 {
30 const path p; ((void)p);
31 ASSERT_NOT_NOEXCEPT(absolute(p));
32 ASSERT_NOT_NOEXCEPT(absolute(p, p));
33 }
34
35 // There are 4 cases is the proposal for absolute path.
36 // Each scope tests one of the cases.
TEST_CASE(absolute_path_test)37 TEST_CASE(absolute_path_test)
38 {
39 // has_root_name() && has_root_directory()
40 {
41 const path p("//net/foo");
42 const path base("//net/bar/baz");
43 TEST_REQUIRE(p.has_root_name());
44 TEST_REQUIRE(p.has_root_directory());
45 TEST_CHECK(p.is_absolute());
46 path ret = absolute(p, base);
47 TEST_CHECK(ret.is_absolute());
48 TEST_CHECK(ret == p);
49 }
50 // !has_root_name() && has_root_directory()
51 {
52 const path p("/foo");
53 const path base("//net/bar");
54 TEST_REQUIRE(not p.has_root_name());
55 TEST_REQUIRE(p.has_root_directory());
56 TEST_CHECK(p.is_absolute());
57 // ensure absolute(base) is not recursively called
58 TEST_REQUIRE(base.has_root_name());
59 TEST_REQUIRE(base.has_root_directory());
60
61 path ret = absolute(p, base);
62 TEST_CHECK(ret.is_absolute());
63 TEST_CHECK(ret.has_root_name());
64 TEST_CHECK(ret.root_name() == path("//net"));
65 TEST_CHECK(ret.has_root_directory());
66 TEST_CHECK(ret.root_directory() == path("/"));
67 TEST_CHECK(ret == path("//net/foo"));
68 }
69 // has_root_name() && !has_root_directory()
70 {
71 const path p("//net");
72 const path base("//net/foo/bar");
73 TEST_REQUIRE(p.has_root_name());
74 TEST_REQUIRE(not p.has_root_directory());
75 TEST_CHECK(not p.is_absolute());
76 // absolute is called recursively on base. The following conditions
77 // must be true for it to return base unmodified
78 TEST_REQUIRE(base.has_root_name());
79 TEST_REQUIRE(base.has_root_directory());
80 path ret = absolute(p, base);
81 const path expect("//net/foo/bar");
82 TEST_CHECK(ret.is_absolute());
83 TEST_CHECK(ret == path("//net/foo/bar"));
84 }
85 // !has_root_name() && !has_root_directory()
86 {
87 const path p("bar/baz");
88 const path base("//net/foo");
89 TEST_REQUIRE(not p.has_root_name());
90 TEST_REQUIRE(not p.has_root_directory());
91 TEST_REQUIRE(base.has_root_name());
92 TEST_REQUIRE(base.has_root_directory());
93
94 path ret = absolute(p, base);
95 TEST_CHECK(ret.is_absolute());
96 TEST_CHECK(ret == path("//net/foo/bar/baz"));
97 }
98 }
99
TEST_CASE(absolute_path_with_default_base)100 TEST_CASE(absolute_path_with_default_base)
101 {
102 const path testCases[] = {
103 "//net/foo", // has_root_name() && has_root_directory()
104 "/foo", // !has_root_name() && has_root_directory()
105 "//net", // has_root_name() && !has_root_directory()
106 "bar/baz" // !has_root_name() && !has_root_directory()
107 };
108 const path base = current_path();
109 for (auto& p : testCases) {
110 const path ret = absolute(p);
111 const path expect = absolute(p, base);
112 TEST_CHECK(ret.is_absolute());
113 TEST_CHECK(ret == expect);
114 }
115 }
116
117 TEST_SUITE_END()
118