Debian Codegraph @

libcxx/test/std/input.output/filesystems/class.path/path.member/path.compare.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: availability-filesystem-missing
11
12 // <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 // strong_ordering operator<=>(path const&, path const&) noexcept;
27 //
28 // size_t hash_value(path const&) noexcept;
29 // template<> struct hash<filesystem::path>;
30
31 #include <filesystem>
32 #include <cassert>
33 #include <string>
34 #include <type_traits>
35 #include <vector>
36
37 #include "assert_macros.h"
38 #include "count_new.h"
39 #include "test_comparisons.h"
40 #include "test_iterators.h"
41 #include "test_macros.h"
42 namespace fs = std::filesystem;
43
44 struct PathCompareTest {
45 const char* LHS;
46 const char* RHS;
47 int expect;
48 };
49
50 #define LONGA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
51 #define LONGB "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
52 #define LONGC "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
53 #define LONGD "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
54 const PathCompareTest CompareTestCases[] =
55 {
56 {"", "", 0},
57 {"a", "", 1},
58 {"", "a", -1},
59 {"a/b/c", "a/b/c", 0},
60 {"b/a/c", "a/b/c", 1},
61 {"a/b/c", "b/a/c", -1},
62 {"a/b", "a/b/c", -1},
63 {"a/b/c", "a/b", 1},
64 {"a/b/", "a/b/.", -1},
65 {"a/b/", "a/b", 1},
66 {"a/b//////", "a/b/////.", -1},
67 {"a/.././b", "a///..//.////b", 0},
68 {"//foo//bar///baz////", "//foo/bar/baz/", 0}, // duplicate separators
69 {"///foo/bar", "/foo/bar", 0}, // "///" is not a root directory
70 {"/foo/bar/", "/foo/bar", 1}, // trailing separator
71 {"foo", "/foo", -1}, // if !this->has_root_directory() and p.has_root_directory(), a value less than 0.
72 {"/foo", "foo", 1}, // if this->has_root_directory() and !p.has_root_directory(), a value greater than 0.
73 {("//" LONGA "////" LONGB "/" LONGC "///" LONGD), ("//" LONGA "/" LONGB "/" LONGC "/" LONGD), 0},
74 {(LONGA "/" LONGB "/" LONGC), (LONGA "/" LONGB "/" LONGB), 1}
75
76 };
77 #undef LONGA
78 #undef LONGB
79 #undef LONGC
80 #undef LONGD
81
82 static inline int normalize_ret(int ret)
83 {
84 return ret < 0 ? -1 : (ret > 0 ? 1 : 0);
85 }
86
87 void test_compare_basic()
88 {
89 using namespace fs;
90 for (auto const & TC : CompareTestCases) {
91 const path p1(TC.LHS);
92 const path p2(TC.RHS);
93 std::string RHS(TC.RHS);
94 const path::string_type R(RHS.begin(), RHS.end());
95 const std::basic_string_view<path::value_type> RV(R);
96 const path::value_type *Ptr = R.c_str();
97 const int E = TC.expect;
98 { // compare(...) functions
99 DisableAllocationGuard g; // none of these operations should allocate
100
101 // check runtime results
102 int ret1 = normalize_ret(p1.compare(p2));
103 int ret2 = normalize_ret(p1.compare(R));
104 int ret3 = normalize_ret(p1.compare(Ptr));
105 int ret4 = normalize_ret(p1.compare(RV));
106
107 g.release();
108 assert(ret1 == ret2);
109 assert(ret1 == ret3);
110 assert(ret1 == ret4);
111 assert(ret1 == E);
112
113 // check signatures
114 ASSERT_NOEXCEPT(p1.compare(p2));
115 }
116 { // comparison operators
117 DisableAllocationGuard g; // none of these operations should allocate
118
119 // check signatures
120 AssertComparisonsAreNoexcept<path>();
121 AssertComparisonsReturnBool<path>();
122 #if TEST_STD_VER > 17
123 AssertOrderAreNoexcept<path>();
124 AssertOrderReturn<std::strong_ordering, path>();
125 #endif
126
127 // check comarison results
128 assert(testComparisons(p1, p2, /*isEqual*/ E == 0, /*isLess*/ E < 0));
129 #if TEST_STD_VER > 17
130 assert(testOrder(p1, p2, E <=> 0));
131 #endif
132 }
133 { // check hash values
134 auto h1 = hash_value(p1);
135 auto h2 = hash_value(p2);
136 assert((h1 == h2) == (p1 == p2));
137 // check signature
138 ASSERT_SAME_TYPE(std::size_t, decltype(hash_value(p1)));
139 ASSERT_NOEXCEPT(hash_value(p1));
140 }
141 { // check std::hash
142 auto h1 = std::hash<fs::path>()(p1);
143 auto h2 = std::hash<fs::path>()(p2);
144 assert((h1 == h2) == (p1 == p2));
145 // check signature
146 ASSERT_SAME_TYPE(std::size_t, decltype(std::hash<fs::path>()(p1)));
147 ASSERT_NOEXCEPT(std::hash<fs::path>()(p1));
148 }
149 }
150 }
151
152 int CompareElements(std::vector<std::string> const& LHS, std::vector<std::string> const& RHS) {
153 bool IsLess = std::lexicographical_compare(LHS.begin(), LHS.end(), RHS.begin(), RHS.end());
154 if (IsLess)
155 return -1;
156
157 bool IsGreater = std::lexicographical_compare(RHS.begin(), RHS.end(), LHS.begin(), LHS.end());
158 if (IsGreater)
159 return 1;
160
161 return 0;
162 }
163
164 void test_compare_elements() {
165 struct {
166 std::vector<std::string> LHSElements;
167 std::vector<std::string> RHSElements;
168 int Expect;
169 } TestCases[] = {
170 {{"a"}, {"a"}, 0},
171 {{"a"}, {"b"}, -1},
172 {{"b"}, {"a"}, 1},
173 {{"a", "b", "c"}, {"a", "b", "c"}, 0},
174 {{"a", "b", "c"}, {"a", "b", "d"}, -1},
175 {{"a", "b", "d"}, {"a", "b", "c"}, 1},
176 {{"a", "b"}, {"a", "b", "c"}, -1},
177 {{"a", "b", "c"}, {"a", "b"}, 1},
178
179 };
180
181 auto BuildPath = [](std::vector<std::string> const& Elems) {
182 fs::path p;
183 for (auto &E : Elems)
184 p /= E;
185 return p;
186 };
187
188 for (auto &TC : TestCases) {
189 fs::path LHS = BuildPath(TC.LHSElements);
190 fs::path RHS = BuildPath(TC.RHSElements);
191 const int ExpectCmp = CompareElements(TC.LHSElements, TC.RHSElements);
192 assert(ExpectCmp == TC.Expect);
193 const int GotCmp = normalize_ret(LHS.compare(RHS));
194 assert(GotCmp == TC.Expect);
195 }
196 }
197
198 int main(int, char**) {
199 test_compare_basic();
200 test_compare_elements();
201
202 return 0;
203 }