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// template<class OtherExtents>
14// friend constexpr bool operator==(const mapping& x, const mapping<OtherExtents>& y) noexcept;
15// `
16// Constraints: extents_type::rank() == OtherExtents::rank() is true.
17
18#include <cassert>
19#include <cstddef>
20#include <mdspan>
21
22#include "test_macros.h"
23
24template <class To, class From>
25constexpr void test_comparison(bool equal, To dest_exts, From src_exts) {
26 std::layout_right::mapping<To> dest(dest_exts);
27 std::layout_right::mapping<From> src(src_exts);
28 ASSERT_NOEXCEPT(dest == src);
29 assert((dest == src) == equal);
30 assert((dest != src) == !equal);
31}
32
33struct X {
34 constexpr bool does_not_match() { return true; }
35};
36
37constexpr X compare_layout_mappings(...) { return {}; }
38
39template <class E1, class E2>
40constexpr auto compare_layout_mappings(E1 e1, E2 e2)
41 -> decltype(std::layout_right::mapping<E1>(e1) == std::layout_right::mapping<E2>(e2)) {
42 return true;
43}
44
45template <class T1, class T2>
46constexpr void test_comparison_different_rank() {
47 constexpr size_t D = std::dynamic_extent;
48
49 // sanity check same rank
50 static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D>(5)));
51 static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, D>(5)));
52 static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, 5>()));
53 static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5>()));
54
55 // not equality comparable when rank is not the same
56 static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, D>(1)).does_not_match());
57 static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, 1>()).does_not_match());
58
59 static_assert(compare_layout_mappings(std::extents<T1, D>(1), std::extents<T2>()).does_not_match());
60 static_assert(compare_layout_mappings(std::extents<T1, 1>(), std::extents<T2>()).does_not_match());
61
62 static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D, D>(5, 5)).does_not_match());
63 static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, D>(5)).does_not_match());
64 static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, 1>()).does_not_match());
65
66 static_assert(compare_layout_mappings(std::extents<T1, D, D>(5, 5), std::extents<T2, D>(5)).does_not_match());
67 static_assert(compare_layout_mappings(std::extents<T1, 5, D>(5), std::extents<T2, D>(5)).does_not_match());
68 static_assert(compare_layout_mappings(std::extents<T1, 5, 5>(), std::extents<T2, 5>()).does_not_match());
69}
70
71template <class T1, class T2>
72constexpr void test_comparison_same_rank() {
73 constexpr size_t D = std::dynamic_extent;
74
75 test_comparison(true, std::extents<T1>(), std::extents<T2>());
76
77 test_comparison(true, std::extents<T1, D>(5), std::extents<T2, D>(5));
78 test_comparison(true, std::extents<T1, 5>(), std::extents<T2, D>(5));
79 test_comparison(true, std::extents<T1, D>(5), std::extents<T2, 5>());
80 test_comparison(true, std::extents<T1, 5>(), std::extents< T2, 5>());
81 test_comparison(false, std::extents<T1, D>(5), std::extents<T2, D>(7));
82 test_comparison(false, std::extents<T1, 5>(), std::extents<T2, D>(7));
83 test_comparison(false, std::extents<T1, D>(5), std::extents<T2, 7>());
84 test_comparison(false, std::extents<T1, 5>(), std::extents<T2, 7>());
85
86 test_comparison(true, std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), std::extents<T2, D, D, D, D, D>(5, 6, 7, 8, 9));
87 test_comparison(true, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 8, 9>(6, 7));
88 test_comparison(true, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 8, 9>());
89 test_comparison(
90 false, std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), std::extents<T2, D, D, D, D, D>(5, 6, 3, 8, 9));
91 test_comparison(false, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 3, 9>(6, 7));
92 test_comparison(false, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 3, 9>());
93}
94
95template <class T1, class T2>
96constexpr void test_comparison() {
97 test_comparison_same_rank<T1, T2>();
98 test_comparison_different_rank<T1, T2>();
99}
100
101constexpr bool test() {
102 test_comparison<int, int>();
103 test_comparison<int, size_t>();
104 test_comparison<size_t, int>();
105 test_comparison<size_t, long>();
106 return true;
107}
108
109int main(int, char**) {
110 test();
111 static_assert(test());
112 return 0;
113}
114

source code of libcxx/test/std/containers/views/mdspan/layout_right/comparison.pass.cpp