| 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, c++23 |
| 10 | |
| 11 | // <mdspan> |
| 12 | // |
| 13 | // template<class OtherElementType, size_t OtherByteAlignment> |
| 14 | // constexpr aligned_accessor(aligned_accessor<OtherElementType, OtherByteAlignment>) noexcept {} |
| 15 | // |
| 16 | // Constraints: |
| 17 | // - is_convertible_v<OtherElementType(*)[], element_type(*)[]> is true. |
| 18 | // - OtherByteAlignment >= byte_alignment is true. |
| 19 | |
| 20 | #include <mdspan> |
| 21 | #include <cassert> |
| 22 | #include <cstddef> |
| 23 | #include <type_traits> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | #include "../MinimalElementType.h" |
| 28 | |
| 29 | struct Base {}; |
| 30 | struct Derived : public Base {}; |
| 31 | |
| 32 | template <class FromT, std::size_t FromN, class ToT, std::size_t ToN> |
| 33 | constexpr void test_conversion() { |
| 34 | std::aligned_accessor<FromT, FromN> acc_from; |
| 35 | ASSERT_NOEXCEPT(std::aligned_accessor<ToT, ToN>(acc_from)); |
| 36 | [[maybe_unused]] std::aligned_accessor<ToT, ToN> acc_to(acc_from); |
| 37 | } |
| 38 | |
| 39 | template <class From, class To> |
| 40 | constexpr void test_it() { |
| 41 | constexpr std::size_t N = alignof(From); |
| 42 | static_assert(N == alignof(To)); |
| 43 | |
| 44 | test_conversion<From, N, To, N>(); |
| 45 | test_conversion<From, 2 * N, To, N>(); |
| 46 | test_conversion<From, 4 * N, To, N>(); |
| 47 | test_conversion<From, 8 * N, To, N>(); |
| 48 | test_conversion<From, 16 * N, To, N>(); |
| 49 | test_conversion<From, 32 * N, To, N>(); |
| 50 | |
| 51 | test_conversion<From, 2 * N, To, 2 * N>(); |
| 52 | test_conversion<From, 4 * N, To, 2 * N>(); |
| 53 | test_conversion<From, 8 * N, To, 2 * N>(); |
| 54 | test_conversion<From, 16 * N, To, 2 * N>(); |
| 55 | test_conversion<From, 32 * N, To, 2 * N>(); |
| 56 | |
| 57 | test_conversion<From, 4 * N, To, 4 * N>(); |
| 58 | test_conversion<From, 8 * N, To, 4 * N>(); |
| 59 | test_conversion<From, 16 * N, To, 4 * N>(); |
| 60 | test_conversion<From, 32 * N, To, 4 * N>(); |
| 61 | |
| 62 | test_conversion<From, 8 * N, To, 8 * N>(); |
| 63 | test_conversion<From, 16 * N, To, 8 * N>(); |
| 64 | test_conversion<From, 32 * N, To, 8 * N>(); |
| 65 | |
| 66 | test_conversion<From, 16 * N, To, 16 * N>(); |
| 67 | test_conversion<From, 32 * N, To, 16 * N>(); |
| 68 | |
| 69 | test_conversion<From, 32 * N, To, 32 * N>(); |
| 70 | } |
| 71 | |
| 72 | constexpr bool test() { |
| 73 | test_it<int, int>(); |
| 74 | test_it<int, const int>(); |
| 75 | test_it<const int, const int>(); |
| 76 | test_it<MinimalElementType, MinimalElementType>(); |
| 77 | test_it<MinimalElementType, const MinimalElementType>(); |
| 78 | test_it<const MinimalElementType, const MinimalElementType>(); |
| 79 | |
| 80 | // char is convertible to int, but accessors are not |
| 81 | static_assert(!std::is_constructible_v<std::aligned_accessor<int, 4>, std::aligned_accessor<char, 4>>); |
| 82 | // don't allow conversion from const elements to non-const |
| 83 | static_assert(!std::is_constructible_v<std::aligned_accessor<int, 4>, std::aligned_accessor<const int, 4>>); |
| 84 | // don't allow conversion from less to more alignment |
| 85 | static_assert(!std::is_constructible_v<std::aligned_accessor<int, 8>, std::aligned_accessor<int, 4>>); |
| 86 | static_assert(!std::is_constructible_v<std::aligned_accessor<const int, 8>, std::aligned_accessor<const int, 4>>); |
| 87 | // MinimalElementType is constructible from int, but accessors should not be convertible |
| 88 | static_assert(!std::is_constructible_v<std::aligned_accessor<MinimalElementType, 8>, std::aligned_accessor<int, 8>>); |
| 89 | // don't allow conversion from const elements to non-const |
| 90 | static_assert(!std::is_constructible_v<std::aligned_accessor<MinimalElementType, 16>, |
| 91 | std::aligned_accessor<const MinimalElementType, 16>>); |
| 92 | // don't allow conversion from Base to Derived |
| 93 | static_assert(!std::is_constructible_v<std::aligned_accessor<Derived, 1>, std::aligned_accessor<Base, 1>>); |
| 94 | // don't allow conversion from Derived to Base |
| 95 | static_assert(!std::is_constructible_v<std::aligned_accessor<Base, 1>, std::aligned_accessor<Derived, 1>>); |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | int main(int, char**) { |
| 101 | test(); |
| 102 | static_assert(test()); |
| 103 | return 0; |
| 104 | } |
| 105 | |