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