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 // class path
15
16 // int compare(path const&) const noexcept;
17 // int compare(string_type const&) const;
18 // int compare(value_type const*) const;
19 //
20 // bool operator==(path const&, path const&) noexcept;
21 // bool operator!=(path const&, path const&) noexcept;
22 // bool operator< (path const&, path const&) noexcept;
23 // bool operator<=(path const&, path const&) noexcept;
24 // bool operator> (path const&, path const&) noexcept;
25 // bool operator>=(path const&, path const&) noexcept;
26 //
27 // size_t hash_value(path const&) noexcept;
28
29 #include <experimental/filesystem>
30 #include <type_traits>
31 #include <vector>
32 #include <cassert>
33
34 #include "test_macros.h"
35 #include "test_iterators.h"
36 #include "count_new.hpp"
37 #include "filesystem_test_helper.hpp"
38
39 namespace fs = std::experimental::filesystem;
40
41 struct PathCompareTest {
42 const char* LHS;
43 const char* RHS;
44 int expect;
45 };
46
47 #define LONGA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
48 #define LONGB "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
49 #define LONGC "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
50 #define LONGD "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
51 const PathCompareTest CompareTestCases[] =
52 {
53 {"", "", 0},
54 {"a", "", 1},
55 {"", "a", -1},
56 {"a/b/c", "a/b/c", 0},
57 {"b/a/c", "a/b/c", 1},
58 {"a/b/c", "b/a/c", -1},
59 {"a/b", "a/b/c", -1},
60 {"a/b/c", "a/b", 1},
61 {"a/b/", "a/b/.", 0},
62 {"a/b//////", "a/b/////.", 0},
63 {"a/.././b", "a///..//.////b", 0},
64 {"//foo//bar///baz////", "//foo/bar/baz/", 0}, // duplicate separators
65 {"///foo/bar", "/foo/bar", 0}, // "///" is not a root directory
66 {"/foo/bar/", "/foo/bar", 1}, // trailing separator
67 {"//" LONGA "////" LONGB "/" LONGC "///" LONGD, "//" LONGA "/" LONGB "/" LONGC "/" LONGD, 0},
68 { LONGA "/" LONGB "/" LONGC, LONGA "/" LONGB "/" LONGB, 1}
69
70 };
71 #undef LONGA
72 #undef LONGB
73 #undef LONGC
74 #undef LONGD
75
normalize_ret(int ret)76 static inline int normalize_ret(int ret)
77 {
78 return ret < 0 ? -1 : (ret > 0 ? 1 : 0);
79 }
80
main()81 int main()
82 {
83 using namespace fs;
84 for (auto const & TC : CompareTestCases) {
85 const path p1(TC.LHS);
86 const path p2(TC.RHS);
87 const std::string R(TC.RHS);
88 const std::string_view RV(TC.RHS);
89 const int E = TC.expect;
90 { // compare(...) functions
91 DisableAllocationGuard g; // none of these operations should allocate
92
93 // check runtime results
94 int ret1 = normalize_ret(p1.compare(p2));
95 int ret2 = normalize_ret(p1.compare(R));
96 int ret3 = normalize_ret(p1.compare(TC.RHS));
97 int ret4 = normalize_ret(p1.compare(RV));
98 assert(ret1 == ret2 && ret1 == ret3 && ret1 == ret4);
99 assert(ret1 == E);
100
101 // check signatures
102 ASSERT_NOEXCEPT(p1.compare(p2));
103 }
104 { // comparison operators
105 DisableAllocationGuard g; // none of these operations should allocate
106
107 // Check runtime result
108 assert((p1 == p2) == (E == 0));
109 assert((p1 != p2) == (E != 0));
110 assert((p1 < p2) == (E < 0));
111 assert((p1 <= p2) == (E <= 0));
112 assert((p1 > p2) == (E > 0));
113 assert((p1 >= p2) == (E >= 0));
114
115 // Check signatures
116 ASSERT_NOEXCEPT(p1 == p2);
117 ASSERT_NOEXCEPT(p1 != p2);
118 ASSERT_NOEXCEPT(p1 < p2);
119 ASSERT_NOEXCEPT(p1 <= p2);
120 ASSERT_NOEXCEPT(p1 > p2);
121 ASSERT_NOEXCEPT(p1 >= p2);
122 }
123 { // check hash values
124 auto h1 = hash_value(p1);
125 auto h2 = hash_value(p2);
126 assert((h1 == h2) == (p1 == p2));
127 // check signature
128 ASSERT_SAME_TYPE(size_t, decltype(hash_value(p1)));
129 ASSERT_NOEXCEPT(hash_value(p1));
130 }
131 }
132 }
133