• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //   constexpr void      // constexpr after c++17
16 //   generate_n(Iter first, Size n, Generator gen);
17 
18 #include "test_macros.h"
19 
20 #ifdef TEST_COMPILER_C1XX
21 #pragma warning(disable: 4244) // conversion from 'const double' to 'int', possible loss of data
22 #endif
23 
24 #include <algorithm>
25 #include <cassert>
26 
27 #include "test_iterators.h"
28 #include "user_defined_integral.hpp"
29 
30 struct gen_test
31 {
operator ()gen_test32     TEST_CONSTEXPR int operator()() const {return 2;}
33 };
34 
35 
36 #if TEST_STD_VER > 17
test_constexpr()37 TEST_CONSTEXPR bool test_constexpr() {
38     const size_t N = 5;
39     int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
40 
41     auto it = std::generate_n(std::begin(ib), N, gen_test());
42 
43     return it == (std::begin(ib) + N)
44         && std::all_of(std::begin(ib), it, [](int x) { return x == 2; })
45         && *it == 0 // don't overwrite the last value in the output array
46         ;
47     }
48 #endif
49 
50 
51 template <class Iter, class Size>
52 void
test2()53 test2()
54 {
55     const unsigned n = 4;
56     int ia[n] = {0};
57     assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
58     assert(ia[0] == 2);
59     assert(ia[1] == 2);
60     assert(ia[2] == 2);
61     assert(ia[3] == 2);
62 }
63 
64 template <class Iter>
65 void
test()66 test()
67 {
68     test2<Iter, int>();
69     test2<Iter, unsigned int>();
70     test2<Iter, long>();
71     test2<Iter, unsigned long>();
72     test2<Iter, UserDefinedIntegral<unsigned> >();
73     test2<Iter, float>();
74     test2<Iter, double>();  // this is PR#35498
75     test2<Iter, long double>();
76 }
77 
main()78 int main()
79 {
80     test<forward_iterator<int*> >();
81     test<bidirectional_iterator<int*> >();
82     test<random_access_iterator<int*> >();
83     test<int*>();
84 
85 #if TEST_STD_VER > 17
86     static_assert(test_constexpr());
87 #endif
88 }
89