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// Let REQUIRED-SPAN-SIZE(e, strides) be:
14// - 1, if e.rank() == 0 is true,
15// - otherwise 0, if the size of the multidimensional index space e is 0,
16// - otherwise 1 plus the sum of products of (e.extent(r) - 1) and strides[r] for all r in the range [0, e.rank()).
17
18// constexpr index_type required_span_size() const noexcept;
19//
20// Returns: REQUIRED-SPAN-SIZE(extents(), strides_).
21
22#include <mdspan>
23#include <cassert>
24#include <cstdint>
25
26#include "test_macros.h"
27
28template <class E>
29constexpr void test_required_span_size(E e, std::array<int, E::rank()> strides, typename E::index_type expected_size) {
30 using M = std::layout_stride::mapping<E>;
31 const M m(e, strides);
32
33 ASSERT_NOEXCEPT(m.required_span_size());
34 assert(m.required_span_size() == expected_size);
35}
36
37constexpr bool test() {
38 constexpr size_t D = std::dynamic_extent;
39 test_required_span_size(std::extents<int>(), std::array<int, 0>{}, 1);
40 test_required_span_size(std::extents<unsigned, D>(0), std::array<int, 1>{5}, 0);
41 test_required_span_size(std::extents<unsigned, D>(1), std::array<int, 1>{5}, 1);
42 test_required_span_size(std::extents<unsigned, D>(7), std::array<int, 1>{5}, 31);
43 test_required_span_size(std::extents<unsigned, 7>(), std::array<int, 1>{5}, 31);
44 test_required_span_size(std::extents<unsigned, 7, 8>(), std::array<int, 2>{20, 2}, 135);
45 test_required_span_size(
46 std::extents<int64_t, D, 8, D, D>(7, 9, 10), std::array<int, 4>{1, 7, 7 * 8, 7 * 8 * 9}, 5040);
47 test_required_span_size(std::extents<int64_t, 1, 8, D, D>(9, 10), std::array<int, 4>{1, 7, 7 * 8, 7 * 8 * 9}, 5034);
48 test_required_span_size(std::extents<int64_t, 1, 0, D, D>(9, 10), std::array<int, 4>{1, 7, 7 * 8, 7 * 8 * 9}, 0);
49 return true;
50}
51
52int main(int, char**) {
53 test();
54 static_assert(test());
55 return 0;
56}
57

source code of libcxx/test/std/containers/views/mdspan/layout_stride/required_span_size.pass.cpp