xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <algorithm>
10 
11 // template<ForwardIterator Iter, Callable Generator>
12 //   requires OutputIterator<Iter, Generator::result_type>
13 //         && CopyConstructible<Generator>
14 //   constexpr void      // constexpr after c++17
15 //   generate(Iter first, Iter last, Generator gen);
16 
17 #include <algorithm>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "test_iterators.h"
22 
23 struct gen_test
24 {
operator ()gen_test25     TEST_CONSTEXPR int operator()() const {return 1;}
26 };
27 
28 
29 #if TEST_STD_VER > 17
test_constexpr()30 TEST_CONSTEXPR bool test_constexpr() {
31     int ia[] = {0, 1, 2, 3, 4};
32 
33     std::generate(std::begin(ia), std::end(ia), gen_test());
34 
35     return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })
36         ;
37     }
38 #endif
39 
40 
41 template <class Iter>
42 void
test()43 test()
44 {
45     const unsigned n = 4;
46     int ia[n] = {0};
47     std::generate(Iter(ia), Iter(ia+n), gen_test());
48     assert(ia[0] == 1);
49     assert(ia[1] == 1);
50     assert(ia[2] == 1);
51     assert(ia[3] == 1);
52 }
53 
main(int,char **)54 int main(int, char**)
55 {
56     test<forward_iterator<int*> >();
57     test<bidirectional_iterator<int*> >();
58     test<random_access_iterator<int*> >();
59     test<int*>();
60 
61 #if TEST_STD_VER > 17
62     static_assert(test_constexpr());
63 #endif
64 
65   return 0;
66 }
67