xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate_n.pass.cpp (revision 79e0733c20ea7dafd270aa468640c6cd5063b13c)
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 #ifdef _MSC_VER
19 #pragma warning(disable: 4244) // conversion from 'const double' to 'int', possible loss of data
20 #endif
21 
22 #include <algorithm>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 #include "user_defined_integral.hpp"
28 
29 struct gen_test
30 {
31     int operator()() const {return 2;}
32 };
33 
34 template <class Iter, class Size>
35 void
36 test2()
37 {
38     const unsigned n = 4;
39     int ia[n] = {0};
40     assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
41     assert(ia[0] == 2);
42     assert(ia[1] == 2);
43     assert(ia[2] == 2);
44     assert(ia[3] == 2);
45 }
46 
47 template <class Iter>
48 void
49 test()
50 {
51     test2<Iter, int>();
52     test2<Iter, unsigned int>();
53     test2<Iter, long>();
54     test2<Iter, unsigned long>();
55     test2<Iter, UserDefinedIntegral<unsigned> >();
56     test2<Iter, float>();
57     test2<Iter, double>();  // this is PR#35498
58     test2<Iter, long double>();
59 }
60 
61 int main()
62 {
63     test<forward_iterator<int*> >();
64     test<bidirectional_iterator<int*> >();
65     test<random_access_iterator<int*> >();
66     test<int*>();
67 }
68