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