minmax.pass.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // template<LessThanComparable T>
  11. // pair<const T&, const T&>
  12. // minmax(const T& a, const T& b);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. template <class T>
  17. void
  18. test(const T& a, const T& b, const T& x, const T& y)
  19. {
  20. std::pair<const T&, const T&> p = std::minmax(a, b);
  21. assert(&p.first == &x);
  22. assert(&p.second == &y);
  23. }
  24. int main()
  25. {
  26. {
  27. int x = 0;
  28. int y = 0;
  29. test(x, y, x, y);
  30. test(y, x, y, x);
  31. }
  32. {
  33. int x = 0;
  34. int y = 1;
  35. test(x, y, x, y);
  36. test(y, x, x, y);
  37. }
  38. {
  39. int x = 1;
  40. int y = 0;
  41. test(x, y, y, x);
  42. test(y, x, y, x);
  43. }
  44. #if TEST_STD_VER >= 14
  45. {
  46. // Note that you can't take a reference to a local var, since
  47. // its address is not a compile-time constant.
  48. constexpr static int x = 1;
  49. constexpr static int y = 0;
  50. constexpr auto p1 = std::minmax (x, y);
  51. static_assert(p1.first == y, "");
  52. static_assert(p1.second == x, "");
  53. constexpr auto p2 = std::minmax (y, x);
  54. static_assert(p2.first == y, "");
  55. static_assert(p2.second == x, "");
  56. }
  57. #endif
  58. }