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 _Compare> struct __debug_less
12
13// Make sure __debug_less asserts when the comparator is not consistent.
14
15// REQUIRES: has-unix-headers, libcpp-hardening-mode=debug
16// UNSUPPORTED: c++03
17
18#include <algorithm>
19#include <iterator>
20
21#include "check_assertion.h"
22
23template <int ID>
24struct MyType {
25 int value;
26 explicit MyType(int xvalue = 0) : value(xvalue) {}
27};
28
29template <int ID1, int ID2>
30bool operator<(MyType<ID1> const& LHS, MyType<ID2> const& RHS) {
31 return LHS.value < RHS.value;
32}
33
34template <class ValueType>
35struct BadComparator {
36 bool operator()(ValueType const&, ValueType const&) const {
37 return true;
38 }
39};
40
41int main(int, char**) {
42 typedef MyType<0> MT0;
43 MT0 one(1);
44 MT0 two(2);
45
46 BadComparator<MT0> c;
47 std::__debug_less<BadComparator<MT0>> d(c);
48
49 TEST_LIBCPP_ASSERT_FAILURE(d(one, two), "Comparator does not induce a strict weak ordering");
50
51 return 0;
52}
53

source code of libcxx/test/libcxx/algorithms/debug_less.inconsistent.pass.cpp