[mlir][PDLL] Allow (and ignore) `-D` tablegen macros. (#124166)
[llvm-project.git] / libcxx / test / std / algorithms / alg.sorting / alg.clamp / clamp.comp.pass.cpp
blob379b3947542b20c74b7395ee01dd4a17895d47b4
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
10 // XFAIL: c++03, c++11, c++14
12 // template<class T, class Compare>
13 // const T&
14 // clamp(const T& v, const T& lo, const T& hi, Compare comp);
16 #include <algorithm>
17 #include <functional>
18 #include <cassert>
20 #include "test_macros.h"
22 struct Tag {
23 Tag() : val(0), tag("Default") {}
24 Tag(int a, const char *b) : val(a), tag(b) {}
25 ~Tag() {}
27 int val;
28 const char *tag;
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>
37 void
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);
43 int main(int, char**)
46 int x = 0;
47 int y = 0;
48 int z = 0;
49 test(x, y, z, std::greater<int>(), x);
50 test(y, x, z, std::greater<int>(), y);
53 int x = 0;
54 int y = 1;
55 int z = -1;
56 test(x, y, z, std::greater<int>(), x);
57 test(y, x, z, std::greater<int>(), x);
60 int x = 1;
61 int y = 0;
62 int z = 0;
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.
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));
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));
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));
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));
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));
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));
121 typedef int T;
122 constexpr T x = 1;
123 constexpr T y = 0;
124 constexpr T z = 0;
125 static_assert(std::clamp(x, y, z, std::greater<T>()) == y, "" );
126 static_assert(std::clamp(y, x, z, std::greater<T>()) == y, "" );
129 return 0;