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