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