libcxx

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

op_idx.pass.cpp (4230B)


      1 // -*- C++ -*-
      2 //===------------------------------ span ---------------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      8 //
      9 //===---------------------------------------------------------------------===//
     10 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
     11 
     12 // <span>
     13 
     14 // constexpr reference operator[](index_type idx) const;
     15 // constexpr reference operator()(index_type idx) const;
     16 //
     17 
     18 
     19 #include <span>
     20 #include <cassert>
     21 #include <string>
     22 
     23 #include "test_macros.h"
     24 
     25 
     26 template <typename Span>
     27 constexpr bool testConstexprSpan(Span sp, ptrdiff_t idx)
     28 {
     29     _LIBCPP_ASSERT(noexcept(sp[idx]), "");
     30     _LIBCPP_ASSERT(noexcept(sp(idx)), "");
     31 
     32     typename Span::reference r1 = sp[idx];
     33     typename Span::reference r2 = sp(idx);
     34     typename Span::reference r3 = *(sp.data() + idx);
     35     return r1 == r2 && r2 == r3;
     36 }
     37 
     38 
     39 template <typename Span>
     40 void testRuntimeSpan(Span sp, ptrdiff_t idx)
     41 {
     42     _LIBCPP_ASSERT(noexcept(sp[idx]), "");
     43     _LIBCPP_ASSERT(noexcept(sp(idx)), "");
     44 
     45     typename Span::reference r1 = sp[idx];
     46     typename Span::reference r2 = sp(idx);
     47     typename Span::reference r3 = *(sp.data() + idx);
     48     assert(r1 == r2 && r2 == r3);
     49 }
     50 
     51 struct A{};
     52 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
     53           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
     54 
     55 int main ()
     56 {
     57     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 0), "");
     58 
     59     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 0), "");
     60     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 1), "");
     61 
     62     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 0), "");
     63     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 1), "");
     64     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 2), "");
     65 
     66     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 0), "");
     67     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 1), "");
     68     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 2), "");
     69     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 3), "");
     70 
     71 
     72     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1), 0), "");
     73 
     74     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 0), "");
     75     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2), 1), "");
     76 
     77     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 0), "");
     78     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 1), "");
     79     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3), 2), "");
     80 
     81     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 0), "");
     82     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 1), "");
     83     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 2), "");
     84     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4), 3), "");
     85 
     86 
     87     testRuntimeSpan(std::span<int>(iArr2, 1), 0);
     88 
     89     testRuntimeSpan(std::span<int>(iArr2, 2), 0);
     90     testRuntimeSpan(std::span<int>(iArr2, 2), 1);
     91 
     92     testRuntimeSpan(std::span<int>(iArr2, 3), 0);
     93     testRuntimeSpan(std::span<int>(iArr2, 3), 1);
     94     testRuntimeSpan(std::span<int>(iArr2, 3), 2);
     95 
     96     testRuntimeSpan(std::span<int>(iArr2, 4), 0);
     97     testRuntimeSpan(std::span<int>(iArr2, 4), 1);
     98     testRuntimeSpan(std::span<int>(iArr2, 4), 2);
     99     testRuntimeSpan(std::span<int>(iArr2, 4), 3);
    100 
    101 
    102     testRuntimeSpan(std::span<int, 1>(iArr2, 1), 0);
    103 
    104     testRuntimeSpan(std::span<int, 2>(iArr2, 2), 0);
    105     testRuntimeSpan(std::span<int, 2>(iArr2, 2), 1);
    106 
    107     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 0);
    108     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 1);
    109     testRuntimeSpan(std::span<int, 3>(iArr2, 3), 2);
    110 
    111     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 0);
    112     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 1);
    113     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 2);
    114     testRuntimeSpan(std::span<int, 4>(iArr2, 4), 3);
    115 
    116     std::string s;
    117     testRuntimeSpan(std::span<std::string>   (&s, 1), 0);
    118     testRuntimeSpan(std::span<std::string, 1>(&s, 1), 0);
    119 }