xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate_n.pass.cpp (revision feb8343281cf723081c189d3e661209490f92ac8)
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<class Iter, IntegralLike Size, Callable Generator>
13 //   requires OutputIterator<Iter, Generator::result_type>
14 //         && CopyConstructible<Generator>
15 //   void
16 //   generate_n(Iter first, Size n, Generator gen);
17 
18 #include <algorithm>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 #include "user_defined_integral.hpp"
24 
25 struct gen_test
26 {
27     int operator()() const {return 2;}
28 };
29 
30 template <class Iter, class Size>
31 void
32 test2()
33 {
34     const unsigned n = 4;
35     int ia[n] = {0};
36     assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
37     assert(ia[0] == 2);
38     assert(ia[1] == 2);
39     assert(ia[2] == 2);
40     assert(ia[3] == 2);
41 }
42 
43 template <class Iter>
44 void
45 test()
46 {
47     test2<Iter, int>();
48     test2<Iter, unsigned int>();
49     test2<Iter, long>();
50     test2<Iter, unsigned long>();
51     test2<Iter, UserDefinedIntegral<unsigned> >();
52     test2<Iter, float>();
53     test2<Iter, double>();  // this is PR#35498
54     test2<Iter, long double>();
55 }
56 
57 int main()
58 {
59     test<forward_iterator<int*> >();
60     test<bidirectional_iterator<int*> >();
61     test<random_access_iterator<int*> >();
62     test<int*>();
63 }
64