| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 |
|
| 10 |
// <mdspan> |
| 11 |
|
| 12 |
// Test construction from span: |
| 13 |
// |
| 14 |
// template<class OtherIndexType, size_t N> |
| 15 |
// constexpr explicit(N != rank_dynamic()) extents(span<OtherIndexType, N> exts) noexcept; |
| 16 |
// |
| 17 |
// Constraints: |
| 18 |
// * is_convertible_v<const OtherIndexType&, index_type> is true, |
| 19 |
// * is_nothrow_constructible_v<index_type, const OtherIndexType&> is true, and |
| 20 |
// * N == rank_dynamic() || N == rank() is true. |
| 21 |
// |
| 22 |
// Preconditions: |
| 23 |
// * If N != rank_dynamic() is true, exts[r] equals Er for each r for which |
| 24 |
// Er is a static extent, and |
| 25 |
// * either |
| 26 |
// - N is zero, or |
| 27 |
// - exts[r] is nonnegative and is representable as a value of type index_type |
| 28 |
// for every rank index r. |
| 29 |
// |
| 30 |
|
| 31 |
#include <mdspan> |
| 32 |
#include <cassert> |
| 33 |
#include <array> |
| 34 |
#include <span> |
| 35 |
#include <type_traits> |
| 36 |
|
| 37 |
#include "../ConvertibleToIntegral.h" |
| 38 |
#include "CtorTestCombinations.h" |
| 39 |
#include "test_macros.h" |
| 40 |
|
| 41 |
struct SpanCtorTest { |
| 42 |
template <class E, class T, size_t N, class Extents, size_t... Indices> |
| 43 |
static constexpr void test_construction(std::array<T, N> all_ext, Extents ext, std::index_sequence<Indices...>) { |
| 44 |
ASSERT_NOEXCEPT(E(ext)); |
| 45 |
if constexpr (N == E::rank_dynamic()) { |
| 46 |
test_implicit_construction_call<E>(std::span(ext), all_ext); |
| 47 |
} |
| 48 |
test_runtime_observers(E(std::span(ext)), all_ext); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
template <class E> |
| 53 |
struct implicit_construction { |
| 54 |
bool value; |
| 55 |
implicit_construction(E) : value(true) {} |
| 56 |
template <class T> |
| 57 |
implicit_construction(T) : value(false) {} |
| 58 |
}; |
| 59 |
|
| 60 |
int main(int, char**) { |
| 61 |
test_index_type_combo<SpanCtorTest>(); |
| 62 |
static_assert(test_index_type_combo<SpanCtorTest>()); |
| 63 |
|
| 64 |
constexpr size_t D = std::dynamic_extent; |
| 65 |
using E = std::extents<int, 1, D, 3, D>; |
| 66 |
|
| 67 |
// check can't construct from too few arguments |
| 68 |
static_assert(!std::is_constructible_v<E, std::span<int, 1>>, "extents constructible from illegal arguments"); |
| 69 |
// check can't construct from rank_dynamic < #args < rank |
| 70 |
static_assert(!std::is_constructible_v<E, std::span<int, 3>>, "extents constructible from illegal arguments"); |
| 71 |
// check can't construct from too many arguments |
| 72 |
static_assert(!std::is_constructible_v<E, std::span<int, 5>>, "extents constructible from illegal arguments"); |
| 73 |
|
| 74 |
// test implicit construction fails from span and array if all extents are given |
| 75 |
std::array a5{3, 4, 5, 6, 7}; |
| 76 |
std::span<int, 5> s5(a5.data(), 5); |
| 77 |
// check that explicit construction works, i.e. no error |
| 78 |
static_assert(std::is_constructible_v< std::extents<int, D, D, 5, D, D>, decltype(s5)>, |
| 79 |
"extents unexpectedly not constructible"); |
| 80 |
// check that implicit construction doesn't work |
| 81 |
assert((implicit_construction<std::extents<int, D, D, 5, D, D>>(s5).value == false)); |
| 82 |
|
| 83 |
// test construction fails from types not convertible to index_type but convertible to other integer types |
| 84 |
static_assert(std::is_convertible_v<IntType, int>, "Test helper IntType unexpectedly not convertible to int"); |
| 85 |
static_assert(!std::is_constructible_v< std::extents<unsigned long, D>, std::span<IntType, 1>>, |
| 86 |
"extents constructible from illegal arguments"); |
| 87 |
|
| 88 |
// index_type is not nothrow constructible |
| 89 |
static_assert(std::is_convertible_v<IntType, unsigned char>); |
| 90 |
static_assert(std::is_convertible_v<const IntType&, unsigned char>); |
| 91 |
static_assert(!std::is_nothrow_constructible_v<unsigned char, const IntType&>); |
| 92 |
static_assert(!std::is_constructible_v<std::dextents<unsigned char, 2>, std::span<IntType, 2>>); |
| 93 |
|
| 94 |
// convertible from non-const to index_type but not from const |
| 95 |
static_assert(std::is_convertible_v<IntTypeNC, int>); |
| 96 |
static_assert(!std::is_convertible_v<const IntTypeNC&, int>); |
| 97 |
static_assert(std::is_nothrow_constructible_v<int, IntTypeNC>); |
| 98 |
static_assert(!std::is_constructible_v<std::dextents<int, 2>, std::span<IntTypeNC, 2>>); |
| 99 |
return 0; |
| 100 |
} |