path.compare.pass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <experimental/filesystem>
  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. namespace fs = std::experimental::filesystem;
  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/.", 0},
  53. {"a/b//////", "a/b/////.", 0},
  54. {"a/.././b", "a///..//.////b", 0},
  55. {"//foo//bar///baz////", "//foo/bar/baz/", 0}, // duplicate separators
  56. {"///foo/bar", "/foo/bar", 0}, // "///" is not a root directory
  57. {"/foo/bar/", "/foo/bar", 1}, // trailing separator
  58. {"//" LONGA "////" LONGB "/" LONGC "///" LONGD, "//" LONGA "/" LONGB "/" LONGC "/" LONGD, 0},
  59. { LONGA "/" LONGB "/" LONGC, LONGA "/" LONGB "/" LONGB, 1}
  60. };
  61. #undef LONGA
  62. #undef LONGB
  63. #undef LONGC
  64. #undef LONGD
  65. int main()
  66. {
  67. using namespace fs;
  68. for (auto const & TC : CompareTestCases) {
  69. const path p1(TC.LHS);
  70. const path p2(TC.RHS);
  71. const std::string R(TC.RHS);
  72. const int E = TC.expect;
  73. { // compare(...) functions
  74. DisableAllocationGuard g; // none of these operations should allocate
  75. // check runtime results
  76. int ret1 = p1.compare(p2);
  77. int ret2 = p1.compare(R);
  78. int ret3 = p1.compare(TC.RHS);
  79. assert(ret1 == ret2 && ret1 == ret3);
  80. int normalized_ret = ret1 < 0 ? -1 : (ret1 > 0 ? 1 : 0);
  81. assert(normalized_ret == E);
  82. // check signatures
  83. ASSERT_NOEXCEPT(p1.compare(p2));
  84. }
  85. { // comparison operators
  86. DisableAllocationGuard g; // none of these operations should allocate
  87. // Check runtime result
  88. assert((p1 == p2) == (E == 0));
  89. assert((p1 != p2) == (E != 0));
  90. assert((p1 < p2) == (E < 0));
  91. assert((p1 <= p2) == (E <= 0));
  92. assert((p1 > p2) == (E > 0));
  93. assert((p1 >= p2) == (E >= 0));
  94. // Check signatures
  95. ASSERT_NOEXCEPT(p1 == p2);
  96. ASSERT_NOEXCEPT(p1 != p2);
  97. ASSERT_NOEXCEPT(p1 < p2);
  98. ASSERT_NOEXCEPT(p1 <= p2);
  99. ASSERT_NOEXCEPT(p1 > p2);
  100. ASSERT_NOEXCEPT(p1 >= p2);
  101. }
  102. { // check hash values
  103. auto h1 = hash_value(p1);
  104. auto h2 = hash_value(p2);
  105. assert((h1 == h2) == (p1 == p2));
  106. // check signature
  107. ASSERT_SAME_TYPE(size_t, decltype(hash_value(p1)));
  108. ASSERT_NOEXCEPT(hash_value(p1));
  109. }
  110. }
  111. }