Home | History | Annotate | Download | only in iterator.operations
      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 //   All of these became constexpr in C++17
     13 //
     14 // template <InputIterator Iter>
     15 //   constexpr void advance(Iter& i, Iter::difference_type n);
     16 //
     17 // template <BidirectionalIterator Iter>
     18 //   constexpr void advance(Iter& i, Iter::difference_type n);
     19 //
     20 // template <RandomAccessIterator Iter>
     21 //   constexpr void advance(Iter& i, Iter::difference_type n);
     22 
     23 #include <iterator>
     24 #include <cassert>
     25 
     26 #include "test_iterators.h"
     27 
     28 template <class It>
     29 void
     30 test(It i, typename std::iterator_traits<It>::difference_type n, It x)
     31 {
     32     std::advance(i, n);
     33     assert(i == x);
     34 }
     35 
     36 #if TEST_STD_VER > 14
     37 template <class It>
     38 constexpr bool
     39 constepxr_test(It i, typename std::iterator_traits<It>::difference_type n, It x)
     40 {
     41     std::advance(i, n);
     42     return i == x;
     43 }
     44 #endif
     45 
     46 int main()
     47 {
     48     {
     49     const char* s = "1234567890";
     50     test(input_iterator<const char*>(s), 10, input_iterator<const char*>(s+10));
     51     test(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10));
     52     test(bidirectional_iterator<const char*>(s+5), 5, bidirectional_iterator<const char*>(s+10));
     53     test(bidirectional_iterator<const char*>(s+5), -5, bidirectional_iterator<const char*>(s));
     54     test(random_access_iterator<const char*>(s+5), 5, random_access_iterator<const char*>(s+10));
     55     test(random_access_iterator<const char*>(s+5), -5, random_access_iterator<const char*>(s));
     56     test(s+5, 5, s+10);
     57     test(s+5, -5, s);
     58     }
     59 #if TEST_STD_VER > 14
     60     {
     61     constexpr const char* s = "1234567890";
     62     static_assert( constepxr_test(input_iterator<const char*>(s), 10, input_iterator<const char*>(s+10)), "" );
     63     static_assert( constepxr_test(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10)), "" );
     64     static_assert( constepxr_test(bidirectional_iterator<const char*>(s+5), 5, bidirectional_iterator<const char*>(s+10)), "" );
     65     static_assert( constepxr_test(bidirectional_iterator<const char*>(s+5), -5, bidirectional_iterator<const char*>(s)), "" );
     66     static_assert( constepxr_test(random_access_iterator<const char*>(s+5), 5, random_access_iterator<const char*>(s+10)), "" );
     67     static_assert( constepxr_test(random_access_iterator<const char*>(s+5), -5, random_access_iterator<const char*>(s)), "" );
     68     static_assert( constepxr_test(s+5, 5, s+10), "" );
     69     static_assert( constepxr_test(s+5, -5, s), "" );
     70     }
     71 #endif
     72 }
     73