iterator.pass.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <iterator>
  9. // template<class Category, class T, class Distance = ptrdiff_t,
  10. // class Pointer = T*, class Reference = T&>
  11. // struct iterator
  12. // {
  13. // typedef T value_type;
  14. // typedef Distance difference_type;
  15. // typedef Pointer pointer;
  16. // typedef Reference reference;
  17. // typedef Category iterator_category;
  18. // };
  19. #include <iterator>
  20. #include <type_traits>
  21. #include "test_macros.h"
  22. struct A {};
  23. template <class T>
  24. void
  25. test2()
  26. {
  27. typedef std::iterator<std::forward_iterator_tag, T> It;
  28. static_assert((std::is_same<typename It::value_type, T>::value), "");
  29. static_assert((std::is_same<typename It::difference_type, std::ptrdiff_t>::value), "");
  30. static_assert((std::is_same<typename It::pointer, T*>::value), "");
  31. static_assert((std::is_same<typename It::reference, T&>::value), "");
  32. static_assert((std::is_same<typename It::iterator_category, std::forward_iterator_tag>::value), "");
  33. }
  34. template <class T>
  35. void
  36. test3()
  37. {
  38. typedef std::iterator<std::bidirectional_iterator_tag, T, short> It;
  39. static_assert((std::is_same<typename It::value_type, T>::value), "");
  40. static_assert((std::is_same<typename It::difference_type, short>::value), "");
  41. static_assert((std::is_same<typename It::pointer, T*>::value), "");
  42. static_assert((std::is_same<typename It::reference, T&>::value), "");
  43. static_assert((std::is_same<typename It::iterator_category, std::bidirectional_iterator_tag>::value), "");
  44. }
  45. template <class T>
  46. void
  47. test4()
  48. {
  49. typedef std::iterator<std::random_access_iterator_tag, T, int, const T*> It;
  50. static_assert((std::is_same<typename It::value_type, T>::value), "");
  51. static_assert((std::is_same<typename It::difference_type, int>::value), "");
  52. static_assert((std::is_same<typename It::pointer, const T*>::value), "");
  53. static_assert((std::is_same<typename It::reference, T&>::value), "");
  54. static_assert((std::is_same<typename It::iterator_category, std::random_access_iterator_tag>::value), "");
  55. }
  56. template <class T>
  57. void
  58. test5()
  59. {
  60. typedef std::iterator<std::input_iterator_tag, T, long, const T*, const T&> It;
  61. static_assert((std::is_same<typename It::value_type, T>::value), "");
  62. static_assert((std::is_same<typename It::difference_type, long>::value), "");
  63. static_assert((std::is_same<typename It::pointer, const T*>::value), "");
  64. static_assert((std::is_same<typename It::reference, const T&>::value), "");
  65. static_assert((std::is_same<typename It::iterator_category, std::input_iterator_tag>::value), "");
  66. }
  67. int main(int, char**)
  68. {
  69. test2<A>();
  70. test3<A>();
  71. test4<A>();
  72. test5<A>();
  73. return 0;
  74. }