xref: /llvm-project/libcxx/test/std/containers/views/mdspan/layout_left/comparison.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1b4ff8938SChristian Trott //===----------------------------------------------------------------------===//
2b4ff8938SChristian Trott //
3b4ff8938SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b4ff8938SChristian Trott // See https://llvm.org/LICENSE.txt for license information.
5b4ff8938SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b4ff8938SChristian Trott //
7b4ff8938SChristian Trott //===----------------------------------------------------------------------===//
8b4ff8938SChristian Trott 
9b4ff8938SChristian Trott // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10b4ff8938SChristian Trott 
11b4ff8938SChristian Trott // <mdspan>
12b4ff8938SChristian Trott 
13b4ff8938SChristian Trott // template<class OtherExtents>
14b4ff8938SChristian Trott //   friend constexpr bool operator==(const mapping& x, const mapping<OtherExtents>& y) noexcept;
15b4ff8938SChristian Trott //                                      `
16b4ff8938SChristian Trott // Constraints: extents_type::rank() == OtherExtents::rank() is true.
17b4ff8938SChristian Trott 
18b4ff8938SChristian Trott #include <cassert>
19*e99c4906SNikolas Klauser #include <cstddef>
20*e99c4906SNikolas Klauser #include <mdspan>
215e19fd17SLouis Dionne #include <span> // dynamic_extent
22b4ff8938SChristian Trott 
23b4ff8938SChristian Trott #include "test_macros.h"
24b4ff8938SChristian Trott 
25b4ff8938SChristian Trott template <class To, class From>
26b4ff8938SChristian Trott constexpr void test_comparison(bool equal, To dest_exts, From src_exts) {
27b4ff8938SChristian Trott   std::layout_left::mapping<To> dest(dest_exts);
28b4ff8938SChristian Trott   std::layout_left::mapping<From> src(src_exts);
29b4ff8938SChristian Trott   ASSERT_NOEXCEPT(dest == src);
30b4ff8938SChristian Trott   assert((dest == src) == equal);
31b4ff8938SChristian Trott   assert((dest != src) == !equal);
32b4ff8938SChristian Trott }
33b4ff8938SChristian Trott 
34b4ff8938SChristian Trott struct X {
35b4ff8938SChristian Trott   constexpr bool does_not_match() { return true; }
36b4ff8938SChristian Trott };
37b4ff8938SChristian Trott 
38b4ff8938SChristian Trott constexpr X compare_layout_mappings(...) { return {}; }
39b4ff8938SChristian Trott 
40b4ff8938SChristian Trott template <class E1, class E2>
41b4ff8938SChristian Trott constexpr auto compare_layout_mappings(E1 e1, E2 e2)
42b4ff8938SChristian Trott     -> decltype(std::layout_left::mapping<E1>(e1) == std::layout_left::mapping<E2>(e2)) {
43b4ff8938SChristian Trott   return true;
44b4ff8938SChristian Trott }
45b4ff8938SChristian Trott 
46b4ff8938SChristian Trott template <class T1, class T2>
47b4ff8938SChristian Trott constexpr void test_comparison_different_rank() {
48b4ff8938SChristian Trott   constexpr size_t D = std::dynamic_extent;
49b4ff8938SChristian Trott 
50b4ff8938SChristian Trott   // sanity check same rank
51b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D>(5)));
52b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, D>(5)));
53b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, 5>()));
54b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5>()));
55b4ff8938SChristian Trott 
56b4ff8938SChristian Trott   // not equality comparable when rank is not the same
57b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, D>(1)).does_not_match());
58b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1>(), std::extents<T2, 1>()).does_not_match());
59b4ff8938SChristian Trott 
60b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, D>(1), std::extents<T2>()).does_not_match());
61b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 1>(), std::extents<T2>()).does_not_match());
62b4ff8938SChristian Trott 
63b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, D>(5), std::extents<T2, D, D>(5, 5)).does_not_match());
64b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, D>(5)).does_not_match());
65b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5>(), std::extents<T2, 5, 1>()).does_not_match());
66b4ff8938SChristian Trott 
67b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, D, D>(5, 5), std::extents<T2, D>(5)).does_not_match());
68b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5, D>(5), std::extents<T2, D>(5)).does_not_match());
69b4ff8938SChristian Trott   static_assert(compare_layout_mappings(std::extents<T1, 5, 5>(), std::extents<T2, 5>()).does_not_match());
70b4ff8938SChristian Trott }
71b4ff8938SChristian Trott 
72b4ff8938SChristian Trott template <class T1, class T2>
73b4ff8938SChristian Trott constexpr void test_comparison_same_rank() {
74b4ff8938SChristian Trott   constexpr size_t D = std::dynamic_extent;
75b4ff8938SChristian Trott 
76b4ff8938SChristian Trott   test_comparison(true, std::extents<T1>(), std::extents<T2>());
77b4ff8938SChristian Trott 
78b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, D>(5), std::extents<T2, D>(5));
79b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, 5>(), std::extents<T2, D>(5));
80b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, D>(5), std::extents<T2, 5>());
81b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, 5>(), std::extents< T2, 5>());
82b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, D>(5), std::extents<T2, D>(7));
83b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, 5>(), std::extents<T2, D>(7));
84b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, D>(5), std::extents<T2, 7>());
85b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, 5>(), std::extents<T2, 7>());
86b4ff8938SChristian Trott 
87b4ff8938SChristian Trott   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));
88b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 8, 9>(6, 7));
89b4ff8938SChristian Trott   test_comparison(true, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 8, 9>());
90b4ff8938SChristian Trott   test_comparison(
91b4ff8938SChristian Trott       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));
92b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, D, 6, D, 8, D>(5, 7, 9), std::extents<T2, 5, D, D, 3, 9>(6, 7));
93b4ff8938SChristian Trott   test_comparison(false, std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), std::extents<T2, 5, 6, 7, 3, 9>());
94b4ff8938SChristian Trott }
95b4ff8938SChristian Trott 
96b4ff8938SChristian Trott template <class T1, class T2>
97b4ff8938SChristian Trott constexpr void test_comparison() {
98b4ff8938SChristian Trott   test_comparison_same_rank<T1, T2>();
99b4ff8938SChristian Trott   test_comparison_different_rank<T1, T2>();
100b4ff8938SChristian Trott }
101b4ff8938SChristian Trott 
102b4ff8938SChristian Trott constexpr bool test() {
103b4ff8938SChristian Trott   test_comparison<int, int>();
104b4ff8938SChristian Trott   test_comparison<int, size_t>();
105b4ff8938SChristian Trott   test_comparison<size_t, int>();
106b4ff8938SChristian Trott   test_comparison<size_t, long>();
107b4ff8938SChristian Trott   return true;
108b4ff8938SChristian Trott }
109b4ff8938SChristian Trott 
110b4ff8938SChristian Trott int main(int, char**) {
111b4ff8938SChristian Trott   test();
112b4ff8938SChristian Trott   static_assert(test());
113b4ff8938SChristian Trott   return 0;
114b4ff8938SChristian Trott }
115