minmax_element.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template<ForwardIterator Iter>
  11. // requires LessThanComparable<Iter::value_type>
  12. // pair<Iter, Iter>
  13. // minmax_element(Iter first, Iter last);
  14. #include <algorithm>
  15. #include <cassert>
  16. #include "../../iterators.h"
  17. template <class Iter>
  18. void
  19. test(Iter first, Iter last)
  20. {
  21. std::pair<Iter, Iter> p = std::minmax_element(first, last);
  22. if (first != last)
  23. {
  24. for (Iter j = first; j != last; ++j)
  25. {
  26. assert(!(*j < *p.first));
  27. assert(!(*p.second < *j));
  28. }
  29. }
  30. else
  31. {
  32. assert(p.first == last);
  33. assert(p.second == last);
  34. }
  35. }
  36. template <class Iter>
  37. void
  38. test(unsigned N)
  39. {
  40. int* a = new int[N];
  41. for (int i = 0; i < N; ++i)
  42. a[i] = i;
  43. std::random_shuffle(a, a+N);
  44. test(Iter(a), Iter(a+N));
  45. delete [] a;
  46. }
  47. template <class Iter>
  48. void
  49. test()
  50. {
  51. test<Iter>(0);
  52. test<Iter>(1);
  53. test<Iter>(2);
  54. test<Iter>(3);
  55. test<Iter>(10);
  56. test<Iter>(1000);
  57. {
  58. const unsigned N = 100;
  59. int* a = new int[N];
  60. for (int i = 0; i < N; ++i)
  61. a[i] = 5;
  62. std::random_shuffle(a, a+N);
  63. std::pair<Iter, Iter> p = std::minmax_element(Iter(a), Iter(a+N));
  64. assert(base(p.first) == a);
  65. assert(base(p.second) == a+N-1);
  66. delete [] a;
  67. }
  68. }
  69. int main()
  70. {
  71. test<forward_iterator<const int*> >();
  72. test<bidirectional_iterator<const int*> >();
  73. test<random_access_iterator<const int*> >();
  74. test<const int*>();
  75. }