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
10 
11 // <filesystem>
12 
13 // class directory_entry
14 
15 // bool operator==(directory_entry const&) const noexcept;
16 // bool operator!=(directory_entry const&) const noexcept;
17 // bool operator< (directory_entry const&) const noexcept;
18 // bool operator<=(directory_entry const&) const noexcept;
19 // bool operator> (directory_entry const&) const noexcept;
20 // bool operator>=(directory_entry const&) const noexcept;
21 
22 #include "filesystem_include.h"
23 #include <cassert>
24 #include <type_traits>
25 #include <utility>
26 
27 #include "test_macros.h"
28 
29 
30 #define CHECK_OP(Op) \
31   static_assert(std::is_same<decltype(ce Op ce), bool>::value, ""); \
32   static_assert(noexcept(ce Op ce), "Operation must be noexcept" )
33 
test_comparison_signatures()34 void test_comparison_signatures() {
35   using namespace fs;
36   path const p("foo/bar/baz");
37   // Check that the operators are valid, yield bool, and are not
38   // potentially-throwing.
39   {
40     directory_entry const ce(p);
41     CHECK_OP(==);
42     CHECK_OP(!=);
43     CHECK_OP(< );
44     CHECK_OP(<=);
45     CHECK_OP(> );
46     CHECK_OP(>=);
47   }
48 }
49 #undef CHECK_OP
50 
51 // The actual semantics of the comparisons are testing via paths operators.
test_comparisons_simple()52 void test_comparisons_simple() {
53   using namespace fs;
54   typedef std::pair<path, path> TestType;
55   TestType TestCases[] =
56   {
57       {"", ""},
58       {"", "a"},
59       {"a", "a"},
60       {"a", "b"},
61       {"foo/bar/baz", "foo/bar/baz/"}
62   };
63   auto TestFn = [](path const& LHS, const directory_entry& LHSE,
64                    path const& RHS, const directory_entry& RHSE) {
65     assert((LHS == RHS) == (LHSE == RHSE));
66     assert((LHS != RHS) == (LHSE != RHSE));
67     assert((LHS < RHS) ==  (LHSE < RHSE));
68     assert((LHS <= RHS) == (LHSE <= RHSE));
69     assert((LHS > RHS) ==  (LHSE > RHSE));
70     assert((LHS >= RHS) == (LHSE >= RHSE));
71   };
72   for (auto const& TC : TestCases) {
73     const directory_entry L(TC.first);
74     const directory_entry R(TC.second);
75     TestFn(TC.first,  L, TC.second, R);
76     TestFn(TC.second, R, TC.first, L);
77   }
78 }
79 
main(int,char **)80 int main(int, char**) {
81   test_comparison_signatures();
82   test_comparisons_simple();
83 
84   return 0;
85 }
86