1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 // XFAIL: c++03, c++11, c++14
12 // template<class T, class Compare>
14 // clamp(const T& v, const T& lo, const T& hi, Compare comp);
20 #include "test_macros.h"
23 Tag() : val(0), tag("Default") {}
24 Tag(int a
, const char *b
) : val(a
), tag(b
) {}
31 bool eq(const Tag
& rhs
, const Tag
& lhs
) { return rhs
.val
== lhs
.val
&& rhs
.tag
== lhs
.tag
; }
32 // bool operator==(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val; }
33 bool comp (const Tag
& rhs
, const Tag
& lhs
) { return rhs
.val
< lhs
.val
; }
36 template <class T
, class C
>
38 test(const T
& v
, const T
& lo
, const T
& hi
, C c
, const T
& x
)
40 assert(&std::clamp(v
, lo
, hi
, c
) == &x
);
49 test(x
, y
, z
, std::greater
<int>(), x
);
50 test(y
, x
, z
, std::greater
<int>(), y
);
56 test(x
, y
, z
, std::greater
<int>(), x
);
57 test(y
, x
, z
, std::greater
<int>(), x
);
63 test(x
, y
, z
, std::greater
<int>(), y
);
64 test(y
, x
, z
, std::greater
<int>(), y
);
68 // If they're all the same, we should get the value back.
72 assert(eq(std::clamp(x
, y
, z
, comp
), x
));
73 assert(eq(std::clamp(y
, x
, z
, comp
), y
));
77 // If it's the same as the lower bound, we get the value back.
81 assert(eq(std::clamp(x
, y
, z
, comp
), x
));
82 assert(eq(std::clamp(y
, x
, z
, comp
), y
));
86 // If it's the same as the upper bound, we get the value back.
90 assert(eq(std::clamp(x
, y
, z
, comp
), x
));
91 assert(eq(std::clamp(z
, y
, x
, comp
), z
));
95 // If the value is between, we should get the value back
99 assert(eq(std::clamp(x
, y
, z
, comp
), x
));
100 assert(eq(std::clamp(y
, x
, z
, comp
), x
));
104 // If the value is less than the 'lo', we should get the lo back.
108 assert(eq(std::clamp(x
, y
, z
, comp
), y
));
109 assert(eq(std::clamp(y
, x
, z
, comp
), y
));
112 // If the value is greater than 'hi', we should get hi back.
116 assert(eq(std::clamp(x
, y
, z
, comp
), z
));
117 assert(eq(std::clamp(y
, z
, x
, comp
), z
));
125 static_assert(std::clamp(x
, y
, z
, std::greater
<T
>()) == y
, "" );
126 static_assert(std::clamp(y
, x
, z
, std::greater
<T
>()) == y
, "" );