Home | History | Annotate | Download | only in alg.min.max
      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 
     10 // <algorithm>
     11 
     12 // template<ForwardIterator Iter>
     13 //   requires LessThanComparable<Iter::value_type>
     14 //   pair<Iter, Iter>
     15 //   minmax_element(Iter first, Iter last);
     16 
     17 #include <algorithm>
     18 #include <cassert>
     19 
     20 #include "test_iterators.h"
     21 
     22 template <class Iter>
     23 void
     24 test(Iter first, Iter last)
     25 {
     26     std::pair<Iter, Iter> p = std::minmax_element(first, last);
     27     if (first != last)
     28     {
     29         for (Iter j = first; j != last; ++j)
     30         {
     31             assert(!(*j < *p.first));
     32             assert(!(*p.second < *j));
     33         }
     34     }
     35     else
     36     {
     37         assert(p.first == last);
     38         assert(p.second == last);
     39     }
     40 }
     41 
     42 template <class Iter>
     43 void
     44 test(unsigned N)
     45 {
     46     int* a = new int[N];
     47     for (int i = 0; i < N; ++i)
     48         a[i] = i;
     49     std::random_shuffle(a, a+N);
     50     test(Iter(a), Iter(a+N));
     51     delete [] a;
     52 }
     53 
     54 template <class Iter>
     55 void
     56 test()
     57 {
     58     test<Iter>(0);
     59     test<Iter>(1);
     60     test<Iter>(2);
     61     test<Iter>(3);
     62     test<Iter>(10);
     63     test<Iter>(1000);
     64     {
     65     const unsigned N = 100;
     66     int* a = new int[N];
     67     for (int i = 0; i < N; ++i)
     68         a[i] = 5;
     69     std::random_shuffle(a, a+N);
     70     std::pair<Iter, Iter> p = std::minmax_element(Iter(a), Iter(a+N));
     71     assert(base(p.first) == a);
     72     assert(base(p.second) == a+N-1);
     73     delete [] a;
     74     }
     75 }
     76 
     77 int main()
     78 {
     79     test<forward_iterator<const int*> >();
     80     test<bidirectional_iterator<const int*> >();
     81     test<random_access_iterator<const int*> >();
     82     test<const int*>();
     83 }
     84