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
14 // clamp(const T& v, const T& lo, const T& hi);
19 #include "test_macros.h"
22 Tag() : val(0), tag("Default") {}
23 Tag(int a
, const char *b
) : val(a
), tag(b
) {}
30 bool eq(const Tag
& rhs
, const Tag
& lhs
) { return rhs
.val
== lhs
.val
&& rhs
.tag
== lhs
.tag
; }
31 // bool operator==(const Tag& rhs, const Tag& lhs) { return rhs.val == lhs.val; }
32 bool operator< (const Tag
& rhs
, const Tag
& lhs
) { return rhs
.val
< lhs
.val
; }
36 test(const T
& a
, const T
& lo
, const T
& hi
, const T
& x
)
38 assert(&std::clamp(a
, lo
, hi
) == &x
);
66 // If they're all the same, we should get the value back.
70 assert(eq(std::clamp(x
, y
, z
), x
));
71 assert(eq(std::clamp(y
, x
, z
), y
));
75 // If it's the same as the lower bound, we get the value back.
79 assert(eq(std::clamp(x
, y
, z
), x
));
80 assert(eq(std::clamp(y
, x
, z
), y
));
84 // If it's the same as the upper bound, we get the value back.
88 assert(eq(std::clamp(x
, y
, z
), x
));
89 assert(eq(std::clamp(z
, y
, x
), z
));
93 // If the value is between, we should get the value back
97 assert(eq(std::clamp(x
, y
, z
), x
));
98 assert(eq(std::clamp(y
, x
, z
), x
));
102 // If the value is less than the 'lo', we should get the lo back.
106 assert(eq(std::clamp(x
, y
, z
), y
));
107 assert(eq(std::clamp(y
, x
, z
), y
));
110 // If the value is greater than 'hi', we should get hi back.
114 assert(eq(std::clamp(x
, y
, z
), z
));
115 assert(eq(std::clamp(y
, z
, x
), z
));
123 static_assert(std::clamp(x
, y
, z
) == x
, "" );
124 static_assert(std::clamp(y
, x
, z
) == x
, "" );