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