distance.pass.cpp 2.1 KB

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