| 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(!is_convertible_v<OtherExtents, extents_type>) |
| 15 |
// mapping(const layout_right::mapping<OtherExtents>&) noexcept; |
| 16 |
|
| 17 |
// Constraints: |
| 18 |
// - extents_type::rank() <= 1 is true, and |
| 19 |
// - is_constructible_v<extents_type, OtherExtents> is true. |
| 20 |
// |
| 21 |
// Preconditions: other.required_span_size() is representable as a value of type index_type |
| 22 |
|
| 23 |
#include <mdspan> |
| 24 |
#include <type_traits> |
| 25 |
#include <cassert> |
| 26 |
#include <limits> |
| 27 |
|
| 28 |
#include "test_macros.h" |
| 29 |
|
| 30 |
template <class To, class From> |
| 31 |
constexpr void test_implicit_conversion(To dest, From src) { |
| 32 |
assert(dest.extents() == src.extents()); |
| 33 |
} |
| 34 |
|
| 35 |
template <bool implicit, class ToE, class FromE> |
| 36 |
constexpr void test_conversion(FromE src_exts) { |
| 37 |
using To = std::layout_left::mapping<ToE>; |
| 38 |
using From = std::layout_right::mapping<FromE>; |
| 39 |
From src(src_exts); |
| 40 |
|
| 41 |
ASSERT_NOEXCEPT(To(src)); |
| 42 |
To dest(src); |
| 43 |
|
| 44 |
assert(dest.extents() == src.extents()); |
| 45 |
if constexpr (implicit) { |
| 46 |
dest = src; |
| 47 |
assert(dest.extents() == src.extents()); |
| 48 |
test_implicit_conversion<To, From>(src, src); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
template <class T1, class T2> |
| 53 |
constexpr void test_conversion() { |
| 54 |
constexpr size_t D = std::dynamic_extent; |
| 55 |
constexpr bool idx_convertible = |
| 56 |
static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max()); |
| 57 |
|
| 58 |
// clang-format off |
| 59 |
test_conversion<idx_convertible && true, std::extents<T1>>(std::extents<T2>()); |
| 60 |
test_conversion<idx_convertible && true, std::extents<T1, D>>(std::extents<T2, D>(5)); |
| 61 |
test_conversion<idx_convertible && false, std::extents<T1, 5>>(std::extents<T2, D>(5)); |
| 62 |
test_conversion<idx_convertible && true, std::extents<T1, 5>>(std::extents<T2, 5>()); |
| 63 |
// clang-format on |
| 64 |
} |
| 65 |
|
| 66 |
template <class IdxT, size_t... Extents> |
| 67 |
using lr_mapping_t = std::layout_right::mapping<std::extents<IdxT, Extents...>>; |
| 68 |
template <class IdxT, size_t... Extents> |
| 69 |
using ll_mapping_t = std::layout_left::mapping<std::extents<IdxT, Extents...>>; |
| 70 |
|
| 71 |
constexpr void test_no_implicit_conversion() { |
| 72 |
constexpr size_t D = std::dynamic_extent; |
| 73 |
|
| 74 |
// Sanity check that one static to dynamic conversion works |
| 75 |
static_assert(std::is_constructible_v<ll_mapping_t<int, D>, lr_mapping_t<int, 5>>); |
| 76 |
static_assert(std::is_convertible_v<lr_mapping_t<int, 5>, ll_mapping_t<int, D>>); |
| 77 |
|
| 78 |
// Check that dynamic to static conversion only works explicitly |
| 79 |
static_assert(std::is_constructible_v<ll_mapping_t<int, 5>, lr_mapping_t<int, D>>); |
| 80 |
static_assert(!std::is_convertible_v<lr_mapping_t<int, D>, ll_mapping_t<int, 5>>); |
| 81 |
|
| 82 |
// Sanity check that smaller index_type to larger index_type conversion works |
| 83 |
static_assert(std::is_constructible_v<ll_mapping_t<size_t, 5>, lr_mapping_t<int, 5>>); |
| 84 |
static_assert(std::is_convertible_v<lr_mapping_t<int, 5>, ll_mapping_t<size_t, 5>>); |
| 85 |
|
| 86 |
// Check that larger index_type to smaller index_type conversion works explicitly only |
| 87 |
static_assert(std::is_constructible_v<ll_mapping_t<int, 5>, lr_mapping_t<size_t, 5>>); |
| 88 |
static_assert(!std::is_convertible_v<lr_mapping_t<size_t, 5>, ll_mapping_t<int, 5>>); |
| 89 |
} |
| 90 |
|
| 91 |
constexpr void test_rank_mismatch() { |
| 92 |
constexpr size_t D = std::dynamic_extent; |
| 93 |
|
| 94 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D>, lr_mapping_t<int>>); |
| 95 |
static_assert(!std::is_constructible_v<lr_mapping_t<int>, lr_mapping_t<int, D, D>>); |
| 96 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D>, lr_mapping_t<int, D, D>>); |
| 97 |
static_assert(!std::is_constructible_v<lr_mapping_t<int, D, D, D>, lr_mapping_t<int, D, D>>); |
| 98 |
} |
| 99 |
|
| 100 |
constexpr void test_static_extent_mismatch() { |
| 101 |
static_assert(!std::is_constructible_v<ll_mapping_t<int, 5>, lr_mapping_t<int, 4>>); |
| 102 |
} |
| 103 |
|
| 104 |
constexpr void test_rank_greater_one() { |
| 105 |
constexpr size_t D = std::dynamic_extent; |
| 106 |
|
| 107 |
static_assert(!std::is_constructible_v<ll_mapping_t<int, D, D>, lr_mapping_t<int, D, D>>); |
| 108 |
static_assert(!std::is_constructible_v<ll_mapping_t<int, 1, 1>, lr_mapping_t<int, 1, 1>>); |
| 109 |
static_assert(!std::is_constructible_v<ll_mapping_t<int, D, D, D>, lr_mapping_t<int, D, D, D>>); |
| 110 |
} |
| 111 |
|
| 112 |
constexpr bool test() { |
| 113 |
test_conversion<int, int>(); |
| 114 |
test_conversion<int, size_t>(); |
| 115 |
test_conversion<size_t, int>(); |
| 116 |
test_conversion<size_t, long>(); |
| 117 |
test_no_implicit_conversion(); |
| 118 |
test_rank_mismatch(); |
| 119 |
test_static_extent_mismatch(); |
| 120 |
test_rank_greater_one(); |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
int main(int, char**) { |
| 125 |
test(); |
| 126 |
static_assert(test()); |
| 127 |
return 0; |
| 128 |
} |