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 10 // UNSUPPORTED: libcpp-no-concepts 11 12 // <compare> 13 // <functional> 14 15 // compare_three_way 16 17 #include <compare> 18 #include <cassert> 19 #include <limits> 20 #include <type_traits> 21 22 #include "pointer_comparison_test_helper.h" 23 24 template<class T, class U> 25 constexpr auto test_sfinae(T t, U u) 26 -> decltype(std::compare_three_way()(t, u), std::true_type{}) 27 { return std::true_type{}; } 28 29 constexpr auto test_sfinae(...) 30 { return std::false_type{}; } 31 32 struct NotThreeWayComparable { 33 std::strong_ordering operator<=>(const NotThreeWayComparable&) const; 34 }; 35 ASSERT_SAME_TYPE(std::compare_three_way_result_t<NotThreeWayComparable>, std::strong_ordering); 36 static_assert(!std::three_way_comparable<NotThreeWayComparable>); // it lacks operator== 37 38 struct WeaklyOrdered { 39 int i; 40 friend constexpr std::weak_ordering operator<=>(const WeaklyOrdered&, const WeaklyOrdered&) = default; 41 }; 42 43 constexpr bool test() 44 { 45 ASSERT_SAME_TYPE(decltype(std::compare_three_way()(1, 1)), std::strong_ordering); 46 assert(std::compare_three_way()(1, 2) == std::strong_ordering::less); 47 assert(std::compare_three_way()(1, 1) == std::strong_ordering::equal); 48 assert(std::compare_three_way()(2, 1) == std::strong_ordering::greater); 49 50 ASSERT_SAME_TYPE(decltype(std::compare_three_way()(WeaklyOrdered{1}, WeaklyOrdered{2})), std::weak_ordering); 51 assert(std::compare_three_way()(WeaklyOrdered{1}, WeaklyOrdered{2}) == std::weak_ordering::less); 52 assert(std::compare_three_way()(WeaklyOrdered{1}, WeaklyOrdered{1}) == std::weak_ordering::equivalent); 53 assert(std::compare_three_way()(WeaklyOrdered{2}, WeaklyOrdered{1}) == std::weak_ordering::greater); 54 55 ASSERT_SAME_TYPE(decltype(std::compare_three_way()(1.0, 1.0)), std::partial_ordering); 56 double nan = std::numeric_limits<double>::quiet_NaN(); 57 assert(std::compare_three_way()(1.0, 2.0) == std::partial_ordering::less); 58 assert(std::compare_three_way()(1.0, 1.0) == std::partial_ordering::equivalent); 59 assert(std::compare_three_way()(2.0, 1.0) == std::partial_ordering::greater); 60 assert(std::compare_three_way()(nan, nan) == std::partial_ordering::unordered); 61 62 // Try heterogeneous comparison. 63 ASSERT_SAME_TYPE(decltype(std::compare_three_way()(42.0, 42)), std::partial_ordering); 64 assert(std::compare_three_way()(42.0, 42) == std::partial_ordering::equivalent); 65 ASSERT_SAME_TYPE(decltype(std::compare_three_way()(42, 42.0)), std::partial_ordering); 66 assert(std::compare_three_way()(42, 42.0) == std::partial_ordering::equivalent); 67 68 return true; 69 } 70 71 int main(int, char**) 72 { 73 test(); 74 static_assert(test()); 75 76 do_pointer_comparison_test(std::compare_three_way()); 77 78 static_assert(test_sfinae(1, 2)); 79 static_assert(!test_sfinae(1, nullptr)); 80 static_assert(!test_sfinae(NotThreeWayComparable(), NotThreeWayComparable())); 81 82 return 0; 83 } 84