| 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 | // Test default construction: |
| 14 | // |
| 15 | // constexpr mapping() noexcept; |
| 16 | // |
| 17 | // |
| 18 | // Preconditions: layout_right::mapping<extents_type>().required_span_size() is representable as a value of type index_type ([basic.fundamental]). |
| 19 | // |
| 20 | // Effects: Direct-non-list-initializes extents_ with extents_type(), and for all d in the range [0, rank_), |
| 21 | // direct-non-list-initializes strides_[d] with layout_right::mapping<extents_type>().stride(d). |
| 22 | |
| 23 | #include <cassert> |
| 24 | #include <cstddef> |
| 25 | #include <cstdint> |
| 26 | #include <mdspan> |
| 27 | |
| 28 | #include "test_macros.h" |
| 29 | |
| 30 | template <class E> |
| 31 | constexpr void test_construction() { |
| 32 | using M = std::layout_stride::mapping<E>; |
| 33 | ASSERT_NOEXCEPT(M{}); |
| 34 | M m; |
| 35 | E e; |
| 36 | |
| 37 | // check correct extents are returned |
| 38 | ASSERT_NOEXCEPT(m.extents()); |
| 39 | assert(m.extents() == e); |
| 40 | |
| 41 | // check required_span_size() |
| 42 | typename E::index_type expected_size = 1; |
| 43 | for (typename E::rank_type r = 0; r < E::rank(); r++) |
| 44 | expected_size *= e.extent(r); |
| 45 | assert(m.required_span_size() == expected_size); |
| 46 | |
| 47 | // check strides: node stride function is constrained on rank>0, e.extent(r) is not |
| 48 | auto strides = m.strides(); |
| 49 | ASSERT_NOEXCEPT(m.strides()); |
| 50 | if constexpr (E::rank() > 0) { |
| 51 | std::layout_right::mapping<E> m_right; |
| 52 | for (typename E::rank_type r = 0; r < E::rank(); r++) { |
| 53 | assert(m.stride(r) == m_right.stride(r)); |
| 54 | assert(strides[r] == m.stride(r)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | constexpr bool test() { |
| 60 | constexpr size_t D = std::dynamic_extent; |
| 61 | test_construction<std::extents<int>>(); |
| 62 | test_construction<std::extents<unsigned, D>>(); |
| 63 | test_construction<std::extents<unsigned, 7>>(); |
| 64 | test_construction<std::extents<unsigned, 0>>(); |
| 65 | test_construction<std::extents<unsigned, 7, 8>>(); |
| 66 | test_construction<std::extents<int64_t, D, 8, D, D>>(); |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | int main(int, char**) { |
| 71 | test(); |
| 72 | static_assert(test()); |
| 73 | return 0; |
| 74 | } |
| 75 | |