clamp.pass.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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>
  12. // const T&
  13. // clamp(const T& v, const T& lo, const T& hi);
  14. #include <algorithm>
  15. #include <cassert>
  16. template <class T>
  17. void
  18. test(const T& a, const T& lo, const T& hi, const T& x)
  19. {
  20. assert(&std::clamp(a, lo, hi) == &x);
  21. }
  22. int main()
  23. {
  24. {
  25. int x = 0;
  26. int y = 0;
  27. int z = 0;
  28. test(x, y, z, x);
  29. test(y, x, z, y);
  30. }
  31. {
  32. int x = 0;
  33. int y = 1;
  34. int z = 2;
  35. test(x, y, z, y);
  36. test(y, x, z, y);
  37. }
  38. {
  39. int x = 1;
  40. int y = 0;
  41. int z = 1;
  42. test(x, y, z, x);
  43. test(y, x, z, x);
  44. }
  45. #if _LIBCPP_STD_VER > 11
  46. {
  47. typedef int T;
  48. constexpr T x = 1;
  49. constexpr T y = 0;
  50. constexpr T z = 1;
  51. static_assert(std::clamp(x, y, z) == x, "" );
  52. static_assert(std::clamp(y, x, z) == x, "" );
  53. }
  54. #endif
  55. }