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 | // XFAIL: c++03, c++11, c++14 |
11 | |
12 | // template<class T, class Compare> |
13 | // const T& |
14 | // clamp(const T& v, const T& lo, const T& hi, Compare comp); |
15 | |
16 | #include <algorithm> |
17 | #include <functional> |
18 | #include <cassert> |
19 | |
20 | #include "test_macros.h" |
21 | |
22 | struct Tag { |
23 | Tag() : val(0), tag("Default" ) {} |
24 | Tag(int a, const char *b) : val(a), tag(b) {} |
25 | ~Tag() {} |
26 | |
27 | int val; |
28 | const char *tag; |
29 | }; |
30 | |
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; } |
34 | |
35 | |
36 | template <class T, class C> |
37 | void |
38 | test(const T& v, const T& lo, const T& hi, C c, const T& x) |
39 | { |
40 | assert(&std::clamp(v, lo, hi, c) == &x); |
41 | } |
42 | |
43 | int main(int, char**) |
44 | { |
45 | { |
46 | int x = 0; |
47 | int y = 0; |
48 | int z = 0; |
49 | test(v: x, lo: y, hi: z, c: std::greater<int>(), x); |
50 | test(v: y, lo: x, hi: z, c: std::greater<int>(), x: y); |
51 | } |
52 | { |
53 | int x = 0; |
54 | int y = 1; |
55 | int z = -1; |
56 | test(v: x, lo: y, hi: z, c: std::greater<int>(), x); |
57 | test(v: y, lo: x, hi: z, c: std::greater<int>(), x); |
58 | } |
59 | { |
60 | int x = 1; |
61 | int y = 0; |
62 | int z = 0; |
63 | test(v: x, lo: y, hi: z, c: std::greater<int>(), x: y); |
64 | test(v: y, lo: x, hi: z, c: std::greater<int>(), x: y); |
65 | } |
66 | |
67 | { |
68 | // If they're all the same, we should get the value back. |
69 | Tag x{0, "Zero-x" }; |
70 | Tag y{0, "Zero-y" }; |
71 | Tag z{0, "Zero-z" }; |
72 | assert(eq(std::clamp(x, y, z, comp), x)); |
73 | assert(eq(std::clamp(y, x, z, comp), y)); |
74 | } |
75 | |
76 | { |
77 | // If it's the same as the lower bound, we get the value back. |
78 | Tag x{0, "Zero-x" }; |
79 | Tag y{0, "Zero-y" }; |
80 | Tag z{1, "One-z" }; |
81 | assert(eq(std::clamp(x, y, z, comp), x)); |
82 | assert(eq(std::clamp(y, x, z, comp), y)); |
83 | } |
84 | |
85 | { |
86 | // If it's the same as the upper bound, we get the value back. |
87 | Tag x{1, "One-x" }; |
88 | Tag y{0, "Zero-y" }; |
89 | Tag z{1, "One-z" }; |
90 | assert(eq(std::clamp(x, y, z, comp), x)); |
91 | assert(eq(std::clamp(z, y, x, comp), z)); |
92 | } |
93 | |
94 | { |
95 | // If the value is between, we should get the value back |
96 | Tag x{1, "One-x" }; |
97 | Tag y{0, "Zero-y" }; |
98 | Tag z{2, "Two-z" }; |
99 | assert(eq(std::clamp(x, y, z, comp), x)); |
100 | assert(eq(std::clamp(y, x, z, comp), x)); |
101 | } |
102 | |
103 | { |
104 | // If the value is less than the 'lo', we should get the lo back. |
105 | Tag x{0, "Zero-x" }; |
106 | Tag y{1, "One-y" }; |
107 | Tag z{2, "Two-z" }; |
108 | assert(eq(std::clamp(x, y, z, comp), y)); |
109 | assert(eq(std::clamp(y, x, z, comp), y)); |
110 | } |
111 | { |
112 | // If the value is greater than 'hi', we should get hi back. |
113 | Tag x{2, "Two-x" }; |
114 | Tag y{0, "Zero-y" }; |
115 | Tag z{1, "One-z" }; |
116 | assert(eq(std::clamp(x, y, z, comp), z)); |
117 | assert(eq(std::clamp(y, z, x, comp), z)); |
118 | } |
119 | |
120 | { |
121 | typedef int T; |
122 | constexpr T x = 1; |
123 | constexpr T y = 0; |
124 | constexpr T z = 0; |
125 | static_assert(std::clamp(val: x, lo: y, hi: z, comp: std::greater<T>()) == y, "" ); |
126 | static_assert(std::clamp(val: y, lo: x, hi: z, comp: std::greater<T>()) == y, "" ); |
127 | } |
128 | |
129 | return 0; |
130 | } |
131 | |