minmax.pass.cpp 1.4 KB

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