libcxx

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

end.pass.cpp (4068B)


      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 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
     10 
     11 // <span>
     12 
     13 // constexpr       iterator  end() const noexcept;
     14 // constexpr const_iterator cend() const noexcept;
     15 
     16 #include <span>
     17 #include <cassert>
     18 #include <string>
     19 
     20 #include "test_macros.h"
     21 
     22 template <class Span>
     23 constexpr bool testConstexprSpan(Span s)
     24 {
     25     bool ret = true;
     26     typename Span::iterator e        = s. end();
     27     typename Span::const_iterator ce = s.cend();
     28     if (s.empty())
     29     {
     30         ret = ret &&  ( e ==  s.begin());
     31         ret = ret &&  (ce == s.cbegin());
     32     }
     33     else
     34     {
     35     	typename Span::const_pointer last = &*(s.cbegin() + s.size() - 1);
     36         ret = ret &&  ( e !=  s.begin());
     37         ret = ret &&  (ce != s.cbegin());
     38         ret = ret &&  (&*( e-1) == last);
     39         ret = ret &&  (&*(ce-1) == last);
     40     }
     41 
     42     ret = ret &&  (( e -  s.begin()) == s.size());
     43     ret = ret &&  ((ce - s.cbegin()) == s.size());
     44 
     45     ret = ret &&  (e == ce);
     46     return ret;
     47 }
     48 
     49 template <class Span>
     50 void testRuntimeSpan(Span s)
     51 {
     52     typename Span::iterator e        = s. end();
     53     typename Span::const_iterator ce = s.cend();
     54     if (s.empty())
     55     {
     56         assert( e ==  s.begin());
     57         assert(ce == s.cbegin());
     58     }
     59     else
     60     {
     61     	typename Span::const_pointer last = &*(s.cbegin() + s.size() - 1);
     62         assert( e !=  s.begin());
     63         assert(ce != s.cbegin());
     64         assert( &*( e-1) == last);
     65         assert( &*(ce-1) == last);
     66     }
     67 
     68     assert(( e -  s.begin()) == s.size());
     69     assert((ce - s.cbegin()) == s.size());
     70 
     71     assert(e == ce);
     72 }
     73 
     74 
     75 struct A{};
     76 bool operator==(A, A) {return true;}
     77 
     78 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
     79           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
     80 
     81 
     82 int main()
     83 {
     84     static_assert(testConstexprSpan(std::span<int>()),            "");
     85     static_assert(testConstexprSpan(std::span<long>()),           "");
     86     static_assert(testConstexprSpan(std::span<double>()),         "");
     87     static_assert(testConstexprSpan(std::span<A>()),              "");
     88     static_assert(testConstexprSpan(std::span<std::string>()),    "");
     89 
     90     static_assert(testConstexprSpan(std::span<int, 0>()),         "");
     91     static_assert(testConstexprSpan(std::span<long, 0>()),        "");
     92     static_assert(testConstexprSpan(std::span<double, 0>()),      "");
     93     static_assert(testConstexprSpan(std::span<A, 0>()),           "");
     94     static_assert(testConstexprSpan(std::span<std::string, 0>()), "");
     95 
     96     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)),    "");
     97     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)),    "");
     98     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)),    "");
     99     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)),    "");
    100     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)),    "");
    101 
    102 
    103     testRuntimeSpan(std::span<int>        ());
    104     testRuntimeSpan(std::span<long>       ());
    105     testRuntimeSpan(std::span<double>     ());
    106     testRuntimeSpan(std::span<A>          ());
    107     testRuntimeSpan(std::span<std::string>());
    108 
    109     testRuntimeSpan(std::span<int, 0>        ());
    110     testRuntimeSpan(std::span<long, 0>       ());
    111     testRuntimeSpan(std::span<double, 0>     ());
    112     testRuntimeSpan(std::span<A, 0>          ());
    113     testRuntimeSpan(std::span<std::string, 0>());
    114 
    115     testRuntimeSpan(std::span<int>(iArr2, 1));
    116     testRuntimeSpan(std::span<int>(iArr2, 2));
    117     testRuntimeSpan(std::span<int>(iArr2, 3));
    118     testRuntimeSpan(std::span<int>(iArr2, 4));
    119     testRuntimeSpan(std::span<int>(iArr2, 5));
    120 
    121     std::string s;
    122     testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0));
    123     testRuntimeSpan(std::span<std::string>(&s, 1));
    124 }