libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

min_element.pass.cpp (1663B)


      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 //   Iter
     15 //   min_element(Iter first, Iter last);
     16 
     17 #include <algorithm>
     18 #include <random>
     19 #include <cassert>
     20 
     21 #include "test_iterators.h"
     22 
     23 std::mt19937 randomness;
     24 
     25 template <class Iter>
     26 void
     27 test(Iter first, Iter last)
     28 {
     29     Iter i = std::min_element(first, last);
     30     if (first != last)
     31     {
     32         for (Iter j = first; j != last; ++j)
     33             assert(!(*j < *i));
     34     }
     35     else
     36         assert(i == last);
     37 }
     38 
     39 template <class Iter>
     40 void
     41 test(int N)
     42 {
     43     int* a = new int[N];
     44     for (int i = 0; i < N; ++i)
     45         a[i] = i;
     46     std::shuffle(a, a+N, randomness);
     47     test(Iter(a), Iter(a+N));
     48     delete [] a;
     49 }
     50 
     51 template <class Iter>
     52 void
     53 test()
     54 {
     55     test<Iter>(0);
     56     test<Iter>(1);
     57     test<Iter>(2);
     58     test<Iter>(3);
     59     test<Iter>(10);
     60     test<Iter>(1000);
     61 }
     62 
     63 #if TEST_STD_VER >= 14
     64 constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 };
     65 #endif
     66 
     67 void constexpr_test()
     68 {
     69 #if TEST_STD_VER >= 14
     70     constexpr auto p = std::min_element(il, il+8);
     71     static_assert ( *p == 1, "" );
     72 #endif
     73 }
     74 
     75 int main()
     76 {
     77     test<forward_iterator<const int*> >();
     78     test<bidirectional_iterator<const int*> >();
     79     test<random_access_iterator<const int*> >();
     80     test<const int*>();
     81 
     82     constexpr_test();
     83 }