libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

test.pass.cpp (1687B)


      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 // reverse_iterator
     13 
     14 // template <RandomAccessIterator Iter1, RandomAccessIterator Iter2>
     15 //   requires HasMinus<Iter2, Iter1>
     16 //   constexpr auto operator-(const reverse_iterator<Iter1>& x, const reverse_iterator<Iter2>& y)
     17 //   -> decltype(y.base() - x.base());
     18 //
     19 // constexpr in C++17
     20 
     21 #include <iterator>
     22 #include <cstddef>
     23 #include <cassert>
     24 
     25 #include "test_macros.h"
     26 #include "test_iterators.h"
     27 
     28 template <class It1, class It2>
     29 void
     30 test(It1 l, It2 r, std::ptrdiff_t x)
     31 {
     32     const std::reverse_iterator<It1> r1(l);
     33     const std::reverse_iterator<It2> r2(r);
     34     assert((r1 - r2) == x);
     35 }
     36 
     37 int main()
     38 {
     39     char s[3] = {0};
     40     test(random_access_iterator<const char*>(s), random_access_iterator<char*>(s), 0);
     41     test(random_access_iterator<char*>(s), random_access_iterator<const char*>(s+1), 1);
     42     test(random_access_iterator<const char*>(s+1), random_access_iterator<char*>(s), -1);
     43     test(s, s, 0);
     44     test(s, s+1, 1);
     45     test(s+1, s, -1);
     46 
     47 #if TEST_STD_VER > 14
     48     {
     49         constexpr const char *p = "123456789";
     50         typedef std::reverse_iterator<const char *> RI;
     51         constexpr RI it1 = std::make_reverse_iterator(p);
     52         constexpr RI it2 = std::make_reverse_iterator(p+1);
     53         static_assert( it1 - it2 ==  1, "");
     54         static_assert( it2 - it1 == -1, "");
     55     }
     56 #endif
     57 }