min_element.pass.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // Iter
  13. // min_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. Iter i = std::min_element(first, last);
  22. if (first != last)
  23. {
  24. for (Iter j = first; j != last; ++j)
  25. assert(!(*j < *i));
  26. }
  27. else
  28. assert(i == last);
  29. }
  30. template <class Iter>
  31. void
  32. test(unsigned N)
  33. {
  34. int* a = new int[N];
  35. for (int i = 0; i < N; ++i)
  36. a[i] = i;
  37. std::random_shuffle(a, a+N);
  38. test(Iter(a), Iter(a+N));
  39. delete [] a;
  40. }
  41. template <class Iter>
  42. void
  43. test()
  44. {
  45. test<Iter>(0);
  46. test<Iter>(1);
  47. test<Iter>(2);
  48. test<Iter>(3);
  49. test<Iter>(10);
  50. test<Iter>(1000);
  51. }
  52. int main()
  53. {
  54. test<forward_iterator<const int*> >();
  55. test<bidirectional_iterator<const int*> >();
  56. test<random_access_iterator<const int*> >();
  57. test<const int*>();
  58. }