path.compare.pass.cpp 4.3 KB

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