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 <array>
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <mdspan>
27
28#include "test_macros.h"
29
30template <class E>
31constexpr void test_required_span_size(E e, std::array<int, E::rank()> strides, typename E::index_type expected_size) {
32 using M = std::layout_stride::mapping<E>;
33 const M m(e, strides);
34
35 ASSERT_NOEXCEPT(m.required_span_size());
36 assert(m.required_span_size() == expected_size);
37}
38
39constexpr bool test() {
40 constexpr size_t D = std::dynamic_extent;
41 test_required_span_size(std::extents<int>(), std::array<int, 0>{}, 1);
42 test_required_span_size(std::extents<unsigned, D>(0), std::array<int, 1>{5}, 0);
43 test_required_span_size(std::extents<unsigned, D>(1), std::array<int, 1>{5}, 1);
44 test_required_span_size(std::extents<unsigned, D>(7), std::array<int, 1>{5}, 31);
45 test_required_span_size(std::extents<unsigned, 7>(), std::array<int, 1>{5}, 31);
46 test_required_span_size(std::extents<unsigned, 7, 8>(), std::array<int, 2>{20, 2}, 135);
47 test_required_span_size(
48 std::extents<int64_t, D, 8, D, D>(7, 9, 10), std::array<int, 4>{1, 7, 7 * 8, 7 * 8 * 9}, 5040);
49 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);
50 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);
51 return true;
52}
53
54int main(int, char**) {
55 test();
56 static_assert(test());
57 return 0;
58}
59

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