Home | History | Annotate | Download | only in span.obs
      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 index_type size() const noexcept;
     15 //
     16 
     17 
     18 #include <span>
     19 #include <cassert>
     20 #include <string>
     21 
     22 #include "test_macros.h"
     23 
     24 
     25 template <typename Span>
     26 constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz)
     27 {
     28     ASSERT_NOEXCEPT(sp.size());
     29     return sp.size() == sz;
     30 }
     31 
     32 
     33 template <typename Span>
     34 void testRuntimeSpan(Span sp, ptrdiff_t sz)
     35 {
     36     ASSERT_NOEXCEPT(sp.size());
     37     assert(sp.size() == sz);
     38 }
     39 
     40 struct A{};
     41 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
     42           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
     43 
     44 int main ()
     45 {
     46     static_assert(testConstexprSpan(std::span<int>(), 0),            "");
     47     static_assert(testConstexprSpan(std::span<long>(), 0),           "");
     48     static_assert(testConstexprSpan(std::span<double>(), 0),         "");
     49     static_assert(testConstexprSpan(std::span<A>(), 0),              "");
     50     static_assert(testConstexprSpan(std::span<std::string>(), 0),    "");
     51 
     52     static_assert(testConstexprSpan(std::span<int, 0>(), 0),         "");
     53     static_assert(testConstexprSpan(std::span<long, 0>(), 0),        "");
     54     static_assert(testConstexprSpan(std::span<double, 0>(), 0),      "");
     55     static_assert(testConstexprSpan(std::span<A, 0>(), 0),           "");
     56     static_assert(testConstexprSpan(std::span<std::string, 0>(), 0), "");
     57 
     58     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 1),    "");
     59     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 2),    "");
     60     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 3),    "");
     61     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 4),    "");
     62     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5), 5),    "");
     63 
     64     testRuntimeSpan(std::span<int>        (), 0);
     65     testRuntimeSpan(std::span<long>       (), 0);
     66     testRuntimeSpan(std::span<double>     (), 0);
     67     testRuntimeSpan(std::span<A>          (), 0);
     68     testRuntimeSpan(std::span<std::string>(), 0);
     69 
     70     testRuntimeSpan(std::span<int, 0>        (), 0);
     71     testRuntimeSpan(std::span<long, 0>       (), 0);
     72     testRuntimeSpan(std::span<double, 0>     (), 0);
     73     testRuntimeSpan(std::span<A, 0>          (), 0);
     74     testRuntimeSpan(std::span<std::string, 0>(), 0);
     75 
     76     testRuntimeSpan(std::span<int>(iArr2, 1), 1);
     77     testRuntimeSpan(std::span<int>(iArr2, 2), 2);
     78     testRuntimeSpan(std::span<int>(iArr2, 3), 3);
     79     testRuntimeSpan(std::span<int>(iArr2, 4), 4);
     80     testRuntimeSpan(std::span<int>(iArr2, 5), 5);
     81 
     82     testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1), 1);
     83     testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2), 2);
     84     testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), 3);
     85     testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4), 4);
     86     testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5), 5);
     87 
     88     std::string s;
     89     testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0), 0);
     90     testRuntimeSpan(std::span<std::string>(&s, 1), 1);
     91 }
     92