File: properties.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (116 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <mdspan>

// namespace std {
//   template<class Extents>
//   class layout_stride::mapping {
//
//     ...
//     static constexpr bool is_always_unique() noexcept { return true; }
//     static constexpr bool is_always_exhaustive() noexcept { return false; }
//     static constexpr bool is_always_strided() noexcept { return true; }
//
//     static constexpr bool is_unique() noexcept { return true; }
//     static constexpr bool is_exhaustive() noexcept;
//     static constexpr bool is_strided() noexcept { return true; }
//     ...
//   };
// }
//
//
// layout_stride::mapping<E> is a trivially copyable type that models regular for each E.
//
// constexpr bool is_exhaustive() const noexcept;
//
// Returns:
//   - true if rank_ is 0.
//   - Otherwise, true if there is a permutation P of the integers in the range [0, rank_) such that
//     stride(p0) equals 1, and stride(pi) equals stride(pi_1) * extents().extent(pi_1) for i in the
//     range [1, rank_), where pi is the ith element of P.
//   - Otherwise, false.

#include <mdspan>
#include <type_traits>
#include <concepts>
#include <cassert>

#include "test_macros.h"

template <class E>
constexpr void
test_layout_mapping_stride(E ext, std::array<typename E::index_type, E::rank()> strides, bool exhaustive) {
  using M = std::layout_stride::mapping<E>;
  M m(ext, strides);
  const M c_m = m;
  assert(m.strides() == strides);
  assert(c_m.strides() == strides);
  assert(m.extents() == ext);
  assert(c_m.extents() == ext);
  assert(M::is_unique() == true);
  assert(m.is_exhaustive() == exhaustive);
  assert(c_m.is_exhaustive() == exhaustive);
  assert(M::is_strided() == true);
  assert(M::is_always_unique() == true);
  assert(M::is_always_exhaustive() == false);
  assert(M::is_always_strided() == true);

  ASSERT_NOEXCEPT(m.strides());
  ASSERT_NOEXCEPT(c_m.strides());
  ASSERT_NOEXCEPT(m.extents());
  ASSERT_NOEXCEPT(c_m.extents());
  ASSERT_NOEXCEPT(M::is_unique());
  ASSERT_NOEXCEPT(m.is_exhaustive());
  ASSERT_NOEXCEPT(c_m.is_exhaustive());
  ASSERT_NOEXCEPT(M::is_strided());
  ASSERT_NOEXCEPT(M::is_always_unique());
  ASSERT_NOEXCEPT(M::is_always_exhaustive());
  ASSERT_NOEXCEPT(M::is_always_strided());

  for (typename E::rank_type r = 0; r < E::rank(); r++) {
    assert(m.stride(r) == strides[r]);
    assert(c_m.stride(r) == strides[r]);
    ASSERT_NOEXCEPT(m.stride(r));
    ASSERT_NOEXCEPT(c_m.stride(r));
  }

  typename E::index_type expected_size = 1;
  for (typename E::rank_type r = 0; r < E::rank(); r++) {
    if (ext.extent(r) == 0) {
      expected_size = 0;
      break;
    }
    expected_size += (ext.extent(r) - 1) * static_cast<typename E::index_type>(strides[r]);
  }
  assert(m.required_span_size() == expected_size);
  assert(c_m.required_span_size() == expected_size);
  ASSERT_NOEXCEPT(m.required_span_size());
  ASSERT_NOEXCEPT(c_m.required_span_size());

  static_assert(std::is_trivially_copyable_v<M>);
  static_assert(std::regular<M>);
}

constexpr bool test() {
  constexpr size_t D = std::dynamic_extent;
  test_layout_mapping_stride(std::extents<int>(), std::array<int, 0>{}, true);
  test_layout_mapping_stride(std::extents<signed char, 4, 5>(), std::array<signed char, 2>{1, 4}, true);
  test_layout_mapping_stride(std::extents<signed char, 4, 5>(), std::array<signed char, 2>{1, 5}, false);
  test_layout_mapping_stride(std::extents<unsigned, D, 4>(7), std::array<unsigned, 2>{20, 2}, false);
  test_layout_mapping_stride(std::extents<size_t, D, D, D, D>(3, 3, 3, 3), std::array<size_t, 4>{3, 1, 9, 27}, true);
  return true;
}

int main(int, char**) {
  test();
  static_assert(test());
  return 0;
}