| 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 |
|
| 9 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 |
|
| 11 |
// <mdspan> |
| 12 |
|
| 13 |
// template<class OtherExtents> |
| 14 |
// constexpr explicit(extents_type::rank() > 0) |
| 15 |
// mapping(const layout_stride::mapping<OtherExtents>& other); |
| 16 |
// |
| 17 |
// Constraints: is_constructible_v<extents_type, OtherExtents> is true. |
| 18 |
// |
| 19 |
// Preconditions: |
| 20 |
// - If extents_type::rank() > 0 is true, then for all r in the range [0, extents_type::rank()), |
| 21 |
// other.stride(r) equals other.extents().fwd-prod-of-extents(r), and |
| 22 |
// - other.required_span_size() is representable as a value of type index_type ([basic.fundamental]). |
| 23 |
// |
| 24 |
// Effects: Direct-non-list-initializes extents_ with other.extents(). |
| 25 |
|
| 26 |
#include <mdspan> |
| 27 |
#include <type_traits> |
| 28 |
#include <cassert> |
| 29 |
#include <limits> |
| 30 |
|
| 31 |
#include "test_macros.h" |
| 32 |
|
| 33 |
template <bool implicit, class ToE, class FromE> |
| 34 |
constexpr void test_conversion(FromE src_exts) { |
| 35 |
using To = std::layout_left::mapping<ToE>; |
| 36 |
using From = std::layout_stride::mapping<FromE>; |
| 37 |
std::array<typename FromE::index_type, FromE::rank()> strides; |
| 38 |
if constexpr (FromE::rank() > 0) { |
| 39 |
strides[0] = 1; |
| 40 |
for (size_t r = 1; r < FromE::rank(); r++) |
| 41 |
strides[r] = src_exts.extent(r - 1) * strides[r - 1]; |
| 42 |
} |
| 43 |
From src(src_exts, strides); |
| 44 |
|
| 45 |
ASSERT_NOEXCEPT(To(src)); |
| 46 |
To dest(src); |
| 47 |
assert(dest == src); |
| 48 |
|
| 49 |
if constexpr (implicit) { |
| 50 |
To dest_implicit = src; |
| 51 |
assert(dest_implicit == src); |
| 52 |
} else { |
| 53 |
assert((!std::is_convertible_v<From, To>)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
template <class T1, class T2> |
| 58 |
constexpr void test_conversion() { |
| 59 |
constexpr size_t D = std::dynamic_extent; |
| 60 |
|
| 61 |
// clang-format off |
| 62 |
test_conversion<true, std::extents<T1>>(std::extents<T2>()); |
| 63 |
test_conversion<false, std::extents<T1, D>>(std::extents<T2, D>(5)); |
| 64 |
test_conversion<false, std::extents<T1, 5>>(std::extents<T2, D>(5)); |
| 65 |
test_conversion<false, std::extents<T1, 5>>(std::extents<T2, 5>()); |
| 66 |
test_conversion<false, std::extents<T1, 5, D>>(std::extents<T2, D, D>(5, 5)); |
| 67 |
test_conversion<false, std::extents<T1, D, D>>(std::extents<T2, D, D>(5, 5)); |
| 68 |
test_conversion<false, std::extents<T1, D, D>>(std::extents<T2, D, 7>(5)); |
| 69 |
test_conversion<false, std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>()); |
| 70 |
test_conversion<false, std::extents<T1, 5, D, 8, D, D>>(std::extents<T2, D, D, 8, 9, 1>(5, 7)); |
| 71 |
test_conversion<false, std::extents<T1, D, D, D, D, D>>( |
| 72 |
std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1)); |
| 73 |
test_conversion<false, std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5)); |
| 74 |
test_conversion<false, std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>()); |
| 75 |
// clang-format on |
| 76 |
} |
| 77 |
|
| 78 |
template <class IdxT, size_t... Extents> |
| 79 |
using lr_mapping_t = std::layout_right::mapping<std::extents<IdxT, Extents...>>; |
| 80 |
template <class IdxT, size_t... Extents> |
| 81 |
using ls_mapping_t = std::layout_stride::mapping<std::extents<IdxT, Extents...>>; |
| 82 |
|
| 83 |
constexpr void test_rank_mismatch() { |
| 84 |
constexpr size_t D = std::dynamic_extent; |
| 85 |
|
| 86 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D>, ls_mapping_t<int>>); |
| 87 |
static_assert(!std::is_constructible_v<lr_mapping_t<int>, ls_mapping_t<int, D, D>>); |
| 88 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D>, ls_mapping_t<int, D, D>>); |
| 89 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D, D, D>, ls_mapping_t<int, D, D>>); |
| 90 |
} |
| 91 |
|
| 92 |
constexpr void test_static_extent_mismatch() { |
| 93 |
constexpr size_t D = std::dynamic_extent; |
| 94 |
|
| 95 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D, 5>, ls_mapping_t<int, D, 4>>); |
| 96 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, 5>, ls_mapping_t<int, 4>>); |
| 97 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, 5, D>, ls_mapping_t<int, 4, D>>); |
| 98 |
} |
| 99 |
|
| 100 |
constexpr bool test() { |
| 101 |
test_conversion<int, int>(); |
| 102 |
test_conversion<int, size_t>(); |
| 103 |
test_conversion<size_t, int>(); |
| 104 |
test_conversion<size_t, long>(); |
| 105 |
test_rank_mismatch(); |
| 106 |
test_static_extent_mismatch(); |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
int main(int, char**) { |
| 111 |
test(); |
| 112 |
static_assert(test()); |
| 113 |
return 0; |
| 114 |
} |