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