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

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