iterator.pass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // template <class Source>
  13. // path(const Source& source);
  14. // template <class InputIterator>
  15. // path(InputIterator first, InputIterator last);
  16. #include <experimental/filesystem>
  17. #include <iterator>
  18. #include <type_traits>
  19. #include <cassert>
  20. #include <iostream>
  21. #include "test_macros.h"
  22. #include "filesystem_test_helper.hpp"
  23. namespace fs = std::experimental::filesystem;
  24. template <class It>
  25. std::reverse_iterator<It> mkRev(It it) {
  26. return std::reverse_iterator<It>(it);
  27. }
  28. template <class Iter1, class Iter2>
  29. bool checkCollectionsEqualVerbose(
  30. Iter1 start1, Iter1 const end1
  31. , Iter2 start2, Iter2 const end2
  32. )
  33. {
  34. while (start1 != end1 && start2 != end2) {
  35. std::cout << "Got start1 = " << *start1 << "\n"
  36. << "Got start2 = " << *start2 << "\n";
  37. if (*start1 != *start2) {
  38. return false;
  39. }
  40. ++start1; ++start2;
  41. }
  42. if (start1 != end1) {
  43. std::cout << "Got start1 = " << *start1 << " but expected end1\n";
  44. }
  45. if (start2 != end2) {
  46. std::cout << "Got start2 = " << *start2 << " but expected end2\n";
  47. }
  48. return (start1 == end1 && start2 == end2);
  49. }
  50. void checkIteratorConcepts() {
  51. using namespace fs;
  52. using It = path::iterator;
  53. using Traits = std::iterator_traits<It>;
  54. ASSERT_SAME_TYPE(Traits::iterator_category, std::bidirectional_iterator_tag);
  55. ASSERT_SAME_TYPE(Traits::value_type, path);
  56. ASSERT_SAME_TYPE(Traits::pointer, path const*);
  57. ASSERT_SAME_TYPE(Traits::reference, path const&);
  58. {
  59. It it;
  60. ASSERT_SAME_TYPE(It&, decltype(++it));
  61. ASSERT_SAME_TYPE(It, decltype(it++));
  62. ASSERT_SAME_TYPE(It&, decltype(--it));
  63. ASSERT_SAME_TYPE(It, decltype(it--));
  64. ASSERT_SAME_TYPE(Traits::reference, decltype(*it));
  65. ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->()));
  66. ASSERT_SAME_TYPE(std::string const&, decltype(it->native()));
  67. ASSERT_SAME_TYPE(bool, decltype(it == it));
  68. ASSERT_SAME_TYPE(bool, decltype(it != it));
  69. }
  70. {
  71. path const p;
  72. ASSERT_SAME_TYPE(It, decltype(p.begin()));
  73. ASSERT_SAME_TYPE(It, decltype(p.end()));
  74. assert(p.begin() == p.end());
  75. }
  76. }
  77. void checkBeginEndBasic() {
  78. using namespace fs;
  79. using It = path::iterator;
  80. {
  81. path const p;
  82. ASSERT_SAME_TYPE(It, decltype(p.begin()));
  83. ASSERT_SAME_TYPE(It, decltype(p.end()));
  84. assert(p.begin() == p.end());
  85. }
  86. {
  87. path const p("foo");
  88. It default_constructed;
  89. default_constructed = p.begin();
  90. assert(default_constructed == p.begin());
  91. assert(default_constructed != p.end());
  92. default_constructed = p.end();
  93. assert(default_constructed == p.end());
  94. assert(default_constructed != p.begin());
  95. }
  96. {
  97. path p("//root_name//first_dir////second_dir");
  98. const path expect[] = {"//root_name", "/", "first_dir", "second_dir"};
  99. assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
  100. assert(checkCollectionsEqualVerbose(mkRev(p.end()), mkRev(p.begin()),
  101. mkRev(std::end(expect)),
  102. mkRev(std::begin(expect))));
  103. }
  104. {
  105. path p("////foo/bar/baz///");
  106. const path expect[] = {"/", "foo", "bar", "baz", "."};
  107. assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
  108. assert(checkCollectionsEqual(mkRev(p.end()), mkRev(p.begin()),
  109. mkRev(std::end(expect)), mkRev(std::begin(expect))));
  110. }
  111. }
  112. int main() {
  113. using namespace fs;
  114. checkIteratorConcepts();
  115. checkBeginEndBasic(); // See path.decompose.pass.cpp for more tests.
  116. }