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 | // <algorithm> |
10 | |
11 | // template <class _Comp> struct __debug_three_way_comp |
12 | |
13 | // Make sure __debug_three_way_comp asserts when the comparator is not consistent. |
14 | |
15 | // REQUIRES: libcpp-hardening-mode=debug |
16 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
17 | |
18 | #include <algorithm> |
19 | #include <iterator> |
20 | |
21 | #include "check_assertion.h" |
22 | |
23 | struct AlwaysLess { |
24 | std::strong_ordering operator()(int, int) const { return std::strong_ordering::less; } |
25 | }; |
26 | |
27 | struct AlwaysGreater { |
28 | std::strong_ordering operator()(int, int) const { return std::strong_ordering::greater; } |
29 | }; |
30 | |
31 | struct InconsistentEquals { |
32 | std::strong_ordering operator()(int a, int) const { |
33 | if (a == 0) |
34 | return std::strong_ordering::equal; |
35 | return std::strong_ordering::greater; |
36 | } |
37 | }; |
38 | |
39 | int main(int, char**) { |
40 | int zero = 0; |
41 | int one = 1; |
42 | |
43 | AlwaysLess alwaysLess; |
44 | std::__debug_three_way_comp<AlwaysLess> debugAlwaysLess(alwaysLess); |
45 | TEST_LIBCPP_ASSERT_FAILURE(debugAlwaysLess(zero, one), "Comparator does not induce a strict weak ordering" ); |
46 | |
47 | AlwaysGreater alwaysGreater; |
48 | std::__debug_three_way_comp<AlwaysGreater> debugAlwaysGreater(alwaysGreater); |
49 | TEST_LIBCPP_ASSERT_FAILURE(debugAlwaysGreater(zero, one), "Comparator does not induce a strict weak ordering" ); |
50 | |
51 | InconsistentEquals inconsistentEquals; |
52 | std::__debug_three_way_comp<InconsistentEquals> debugInconsistentEquals(inconsistentEquals); |
53 | TEST_LIBCPP_ASSERT_FAILURE(debugInconsistentEquals(zero, one), "Comparator does not induce a strict weak ordering" ); |
54 | |
55 | return 0; |
56 | } |
57 | |