Home | History | Annotate | Download | only in alg.generate
      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, Callable Generator>
     13 //   requires OutputIterator<Iter, Generator::result_type>
     14 //         && CopyConstructible<Generator>
     15 //   constexpr void      // constexpr after c++17
     16 //   generate(Iter first, Iter last, Generator gen);
     17 
     18 #include <algorithm>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 #include "test_iterators.h"
     23 
     24 struct gen_test
     25 {
     26     TEST_CONSTEXPR int operator()() const {return 1;}
     27 };
     28 
     29 
     30 #if TEST_STD_VER > 17
     31 TEST_CONSTEXPR bool test_constexpr() {
     32     int ia[] = {0, 1, 2, 3, 4};
     33 
     34     std::generate(std::begin(ia), std::end(ia), gen_test());
     35 
     36     return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })
     37         ;
     38     }
     39 #endif
     40 
     41 
     42 template <class Iter>
     43 void
     44 test()
     45 {
     46     const unsigned n = 4;
     47     int ia[n] = {0};
     48     std::generate(Iter(ia), Iter(ia+n), gen_test());
     49     assert(ia[0] == 1);
     50     assert(ia[1] == 1);
     51     assert(ia[2] == 1);
     52     assert(ia[3] == 1);
     53 }
     54 
     55 int main()
     56 {
     57     test<forward_iterator<int*> >();
     58     test<bidirectional_iterator<int*> >();
     59     test<random_access_iterator<int*> >();
     60     test<int*>();
     61 
     62 #if TEST_STD_VER > 17
     63     static_assert(test_constexpr());
     64 #endif
     65 }
     66