iterator.pass.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "test_macros.h"
  21. #include "filesystem_test_helper.hpp"
  22. namespace fs = std::experimental::filesystem;
  23. template <class It>
  24. std::reverse_iterator<It> mkRev(It it) {
  25. return std::reverse_iterator<It>(it);
  26. }
  27. void checkIteratorConcepts() {
  28. using namespace fs;
  29. using It = path::iterator;
  30. using Traits = std::iterator_traits<It>;
  31. ASSERT_SAME_TYPE(Traits::iterator_category, std::bidirectional_iterator_tag);
  32. ASSERT_SAME_TYPE(Traits::value_type, path);
  33. ASSERT_SAME_TYPE(Traits::pointer, path const*);
  34. ASSERT_SAME_TYPE(Traits::reference, path const&);
  35. {
  36. It it;
  37. ASSERT_SAME_TYPE(It&, decltype(++it));
  38. ASSERT_SAME_TYPE(It, decltype(it++));
  39. ASSERT_SAME_TYPE(It&, decltype(--it));
  40. ASSERT_SAME_TYPE(It, decltype(it--));
  41. ASSERT_SAME_TYPE(Traits::reference, decltype(*it));
  42. ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->()));
  43. ASSERT_SAME_TYPE(std::string const&, decltype(it->native()));
  44. ASSERT_SAME_TYPE(bool, decltype(it == it));
  45. ASSERT_SAME_TYPE(bool, decltype(it != it));
  46. }
  47. {
  48. path const p;
  49. ASSERT_SAME_TYPE(It, decltype(p.begin()));
  50. ASSERT_SAME_TYPE(It, decltype(p.end()));
  51. assert(p.begin() == p.end());
  52. }
  53. }
  54. void checkBeginEndBasic() {
  55. using namespace fs;
  56. using It = path::iterator;
  57. {
  58. path const p;
  59. ASSERT_SAME_TYPE(It, decltype(p.begin()));
  60. ASSERT_SAME_TYPE(It, decltype(p.end()));
  61. assert(p.begin() == p.end());
  62. }
  63. {
  64. path const p("foo");
  65. It default_constructed;
  66. default_constructed = p.begin();
  67. assert(default_constructed == p.begin());
  68. assert(default_constructed != p.end());
  69. default_constructed = p.end();
  70. assert(default_constructed == p.end());
  71. assert(default_constructed != p.begin());
  72. }
  73. {
  74. path p("//root_name//first_dir////second_dir");
  75. const path expect[] = {"//root_name", "/", "first_dir", "second_dir"};
  76. assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
  77. assert(checkCollectionsEqual(mkRev(p.end()), mkRev(p.begin()), mkRev(std::end(expect)), mkRev(std::begin(expect))));
  78. }
  79. {
  80. path p("////foo/bar/baz///");
  81. const path expect[] = {"/", "foo", "bar", "baz", "."};
  82. assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
  83. assert(checkCollectionsEqual(mkRev(p.end()), mkRev(p.begin()), mkRev(std::end(expect)), mkRev(std::begin(expect))));
  84. }
  85. }
  86. int main() {
  87. using namespace fs;
  88. checkIteratorConcepts();
  89. checkBeginEndBasic(); // See path.decompose.pass.cpp for more tests.
  90. }