path.compare.pass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. struct PathCompareTest {
  33. const char* LHS;
  34. const char* RHS;
  35. int expect;
  36. };
  37. #define LONGA "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  38. #define LONGB "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
  39. #define LONGC "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  40. #define LONGD "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
  41. const PathCompareTest CompareTestCases[] =
  42. {
  43. {"", "", 0},
  44. {"a", "", 1},
  45. {"", "a", -1},
  46. {"a/b/c", "a/b/c", 0},
  47. {"b/a/c", "a/b/c", 1},
  48. {"a/b/c", "b/a/c", -1},
  49. {"a/b", "a/b/c", -1},
  50. {"a/b/c", "a/b", 1},
  51. {"a/b/", "a/b/.", 0},
  52. {"a/b//////", "a/b/////.", 0},
  53. {"a/.././b", "a///..//.////b", 0},
  54. {"//foo//bar///baz////", "//foo/bar/baz/", 0}, // duplicate separators
  55. {"///foo/bar", "/foo/bar", 0}, // "///" is not a root directory
  56. {"/foo/bar/", "/foo/bar", 1}, // trailing separator
  57. {"//" LONGA "////" LONGB "/" LONGC "///" LONGD, "//" LONGA "/" LONGB "/" LONGC "/" LONGD, 0},
  58. { LONGA "/" LONGB "/" LONGC, LONGA "/" LONGB "/" LONGB, 1}
  59. };
  60. #undef LONGA
  61. #undef LONGB
  62. #undef LONGC
  63. #undef LONGD
  64. static inline int normalize_ret(int ret)
  65. {
  66. return ret < 0 ? -1 : (ret > 0 ? 1 : 0);
  67. }
  68. int main()
  69. {
  70. using namespace fs;
  71. for (auto const & TC : CompareTestCases) {
  72. const path p1(TC.LHS);
  73. const path p2(TC.RHS);
  74. const std::string R(TC.RHS);
  75. const std::string_view RV(TC.RHS);
  76. const int E = TC.expect;
  77. { // compare(...) functions
  78. DisableAllocationGuard g; // none of these operations should allocate
  79. // check runtime results
  80. int ret1 = normalize_ret(p1.compare(p2));
  81. int ret2 = normalize_ret(p1.compare(R));
  82. int ret3 = normalize_ret(p1.compare(TC.RHS));
  83. int ret4 = normalize_ret(p1.compare(RV));
  84. assert(ret1 == ret2 && ret1 == ret3 && ret1 == ret4);
  85. assert(ret1 == E);
  86. // check signatures
  87. ASSERT_NOEXCEPT(p1.compare(p2));
  88. }
  89. { // comparison operators
  90. DisableAllocationGuard g; // none of these operations should allocate
  91. // Check runtime result
  92. assert((p1 == p2) == (E == 0));
  93. assert((p1 != p2) == (E != 0));
  94. assert((p1 < p2) == (E < 0));
  95. assert((p1 <= p2) == (E <= 0));
  96. assert((p1 > p2) == (E > 0));
  97. assert((p1 >= p2) == (E >= 0));
  98. // Check signatures
  99. ASSERT_NOEXCEPT(p1 == p2);
  100. ASSERT_NOEXCEPT(p1 != p2);
  101. ASSERT_NOEXCEPT(p1 < p2);
  102. ASSERT_NOEXCEPT(p1 <= p2);
  103. ASSERT_NOEXCEPT(p1 > p2);
  104. ASSERT_NOEXCEPT(p1 >= p2);
  105. }
  106. { // check hash values
  107. auto h1 = hash_value(p1);
  108. auto h2 = hash_value(p2);
  109. assert((h1 == h2) == (p1 == p2));
  110. // check signature
  111. ASSERT_SAME_TYPE(size_t, decltype(hash_value(p1)));
  112. ASSERT_NOEXCEPT(hash_value(p1));
  113. }
  114. }
  115. }