| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 | |
| 10 | // <array> |
| 11 | |
| 12 | // template<class T, size_t N> |
| 13 | // constexpr synth-three-way-result<T> |
| 14 | // operator<=>(const array<T, N>& x, const array<T, N>& y); |
| 15 | |
| 16 | #include <array> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_comparisons.h" |
| 20 | |
| 21 | // SFINAE |
| 22 | |
| 23 | constexpr std::size_t N{1}; |
| 24 | |
| 25 | // The container should fulfill `std::three_way_comparable` |
| 26 | static_assert(std::three_way_comparable<std::array<int, N>>); |
| 27 | |
| 28 | // Thanks to SFINAE, the following is not a compiler error but returns `false` |
| 29 | struct NonComparable {}; |
| 30 | static_assert(!std::three_way_comparable<std::array<NonComparable, N>>); |
| 31 | |
| 32 | // Implementation detail of `test_sequence_container_array_spaceship` |
| 33 | template <typename Elem, typename Order> |
| 34 | constexpr void test_sequence_container_array_spaceship_with_type() { |
| 35 | // Empty containers |
| 36 | { |
| 37 | std::array<Elem, 0> l1 = {}; |
| 38 | std::array<Elem, 0> l2 = {}; |
| 39 | assert(testOrder(l1, l2, Order::equivalent)); |
| 40 | } |
| 41 | // Identical contents |
| 42 | { |
| 43 | std::array l1{Elem{1}, Elem{1}}; |
| 44 | std::array l2{Elem{1}, Elem{1}}; |
| 45 | assert(testOrder(l1, l2, Order::equivalent)); |
| 46 | } |
| 47 | // Less, due to contained values |
| 48 | { |
| 49 | std::array l1{Elem{1}, Elem{1}}; |
| 50 | std::array l2{Elem{1}, Elem{2}}; |
| 51 | assert(testOrder(l1, l2, Order::less)); |
| 52 | } |
| 53 | // Greater, due to contained values |
| 54 | { |
| 55 | std::array l1{Elem{1}, Elem{3}}; |
| 56 | std::array l2{Elem{1}, Elem{2}}; |
| 57 | assert(testOrder(l1, l2, Order::greater)); |
| 58 | } |
| 59 | // Shorter list - unsupported - containers must be of equal lengths |
| 60 | // Longer list - unsupported - containers must be of equal lengths |
| 61 | // Unordered |
| 62 | if constexpr (std::is_same_v<Elem, PartialOrder>) { |
| 63 | std::array l1{Elem{1}, Elem{std::numeric_limits<int>::min()}}; |
| 64 | std::array l2{Elem{1}, Elem{2}}; |
| 65 | assert(testOrder(l1, l2, Order::unordered)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Tests the `operator<=>` on sequence containers `array` |
| 70 | constexpr bool test_sequence_container_array_spaceship() { |
| 71 | // Test different comparison categories |
| 72 | test_sequence_container_array_spaceship_with_type<int, std::strong_ordering>(); |
| 73 | test_sequence_container_array_spaceship_with_type<StrongOrder, std::strong_ordering>(); |
| 74 | test_sequence_container_array_spaceship_with_type<WeakOrder, std::weak_ordering>(); |
| 75 | test_sequence_container_array_spaceship_with_type<PartialOrder, std::partial_ordering>(); |
| 76 | |
| 77 | // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<` |
| 78 | test_sequence_container_array_spaceship_with_type<LessAndEqComp, std::weak_ordering>(); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | int main(int, char**) { |
| 84 | assert(test_sequence_container_array_spaceship()); |
| 85 | static_assert(test_sequence_container_array_spaceship()); |
| 86 | return 0; |
| 87 | } |
| 88 | |