| 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 | // constexpr reference access(data_handle_type p, size_t i) const noexcept; |
| 14 | // |
| 15 | // Effects: Equivalent to: return assume_aligned<byte_alignment>(p)[i]; |
| 16 | |
| 17 | #include <mdspan> |
| 18 | #include <cassert> |
| 19 | #include <cstddef> |
| 20 | #include <concepts> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | // We are not using MinimalElementType.h because MinimalElementType is not |
| 26 | // default consructible and uninitialized storage does not work in constexpr. |
| 27 | |
| 28 | // Same as MinimalElementType but with a defaulted default constructor |
| 29 | struct MyMinimalElementType { |
| 30 | int val; |
| 31 | constexpr MyMinimalElementType() = default; |
| 32 | constexpr MyMinimalElementType(const MyMinimalElementType&) = delete; |
| 33 | constexpr explicit MyMinimalElementType(int v) noexcept : val(v) {} |
| 34 | constexpr MyMinimalElementType& operator=(const MyMinimalElementType&) = delete; |
| 35 | }; |
| 36 | |
| 37 | template <class T, std::size_t N> |
| 38 | constexpr void test_access() { |
| 39 | constexpr std::size_t Sz = 10; |
| 40 | alignas(N) T data[Sz]{}; |
| 41 | T* ptr = &data[0]; |
| 42 | std::aligned_accessor<T, N> acc; |
| 43 | for (std::size_t i = 0; i < Sz; ++i) { |
| 44 | std::same_as<typename std::aligned_accessor<T, N>::reference> decltype(auto) x = acc.access(ptr, i); |
| 45 | ASSERT_NOEXCEPT(acc.access(ptr, i)); |
| 46 | assert(&x == ptr + i); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | template <class T> |
| 51 | constexpr void test_it() { |
| 52 | constexpr std::size_t N = alignof(T); |
| 53 | test_access<T, N>(); |
| 54 | test_access<T, 2 * N>(); |
| 55 | test_access<T, 4 * N>(); |
| 56 | test_access<T, 8 * N>(); |
| 57 | test_access<T, 16 * N>(); |
| 58 | } |
| 59 | |
| 60 | constexpr bool test() { |
| 61 | test_it<int>(); |
| 62 | test_it<const int>(); |
| 63 | test_it<MyMinimalElementType>(); |
| 64 | test_it<const MyMinimalElementType>(); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | int main(int, char**) { |
| 69 | test(); |
| 70 | static_assert(test()); |
| 71 | return 0; |
| 72 | } |
| 73 | |