prev.pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // <iterator>
  10. // template <BidirectionalIterator Iter>
  11. // Iter prev(Iter x, Iter::difference_type n = 1);
  12. #include <iterator>
  13. #include <cassert>
  14. #include "test_iterators.h"
  15. template <class It>
  16. void
  17. test(It i, typename std::iterator_traits<It>::difference_type n, It x)
  18. {
  19. assert(std::prev(i, n) == x);
  20. }
  21. template <class It>
  22. void
  23. test(It i, It x)
  24. {
  25. assert(std::prev(i) == x);
  26. }
  27. #if TEST_STD_VER > 14
  28. template <class It>
  29. constexpr bool
  30. constexpr_test(It i, typename std::iterator_traits<It>::difference_type n, It x)
  31. {
  32. return std::prev(i, n) == x;
  33. }
  34. template <class It>
  35. constexpr bool
  36. constexpr_test(It i, It x)
  37. {
  38. return std::prev(i) == x;
  39. }
  40. #endif
  41. int main()
  42. {
  43. {
  44. const char* s = "1234567890";
  45. test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s));
  46. test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s));
  47. test(s+10, 10, s);
  48. test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s));
  49. test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s));
  50. test(s+1, s);
  51. }
  52. #if TEST_STD_VER > 14
  53. {
  54. constexpr const char* s = "1234567890";
  55. static_assert( constexpr_test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s)), "" );
  56. static_assert( constexpr_test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s)), "" );
  57. static_assert( constexpr_test(s+10, 10, s), "" );
  58. static_assert( constexpr_test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s)), "" );
  59. static_assert( constexpr_test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s)), "" );
  60. static_assert( constexpr_test(s+1, s), "" );
  61. }
  62. #endif
  63. }