clamp.comp.pass.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // XFAIL: c++98, c++03, c++11, c++14
  11. // template<class T, class Compare>
  12. // const T&
  13. // clamp(const T& v, const T& lo, const T& hi, Compare comp);
  14. #include <algorithm>
  15. #include <functional>
  16. #include <cassert>
  17. template <class T, class C>
  18. void
  19. test(const T& v, const T& lo, const T& hi, C c, const T& x)
  20. {
  21. assert(&std::clamp(v, lo, hi, c) == &x);
  22. }
  23. int main()
  24. {
  25. {
  26. int x = 0;
  27. int y = 0;
  28. int z = 0;
  29. test(x, y, z, std::greater<int>(), x);
  30. test(y, x, z, std::greater<int>(), y);
  31. }
  32. {
  33. int x = 0;
  34. int y = 1;
  35. int z = -1;
  36. test(x, y, z, std::greater<int>(), x);
  37. test(y, x, z, std::greater<int>(), x);
  38. }
  39. {
  40. int x = 1;
  41. int y = 0;
  42. int z = 0;
  43. test(x, y, z, std::greater<int>(), y);
  44. test(y, x, z, std::greater<int>(), y);
  45. }
  46. #if _LIBCPP_STD_VER > 11
  47. {
  48. typedef int T;
  49. constexpr T x = 1;
  50. constexpr T y = 0;
  51. constexpr T z = 0;
  52. static_assert(std::clamp(x, y, z, std::greater<T>()) == y, "" );
  53. static_assert(std::clamp(y, x, z, std::greater<T>()) == y, "" );
  54. }
  55. #endif
  56. }