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