clamp.pass.cpp 2.9 KB

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