1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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);
20 #include "test_iterators.h"
21 #include "test_macros.h"
22 #include "user_defined_integral.h"
24 TEST_MSVC_DIAGNOSTIC_IGNORED(4244) // conversion from 'const double' to 'int', possible loss of data
28 TEST_CONSTEXPR
int operator()() const {return 2;}
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
37 auto it
= std::generate_n(std::begin(ib
), N
, gen_test());
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
47 template <class Iter
, class Size
>
53 assert(std::generate_n(Iter(ia
), Size(n
), gen_test()) == Iter(ia
+n
));
65 test2
<Iter
, unsigned int>();
67 test2
<Iter
, unsigned long>();
68 test2
<Iter
, UserDefinedIntegral
<unsigned> >();
70 test2
<Iter
, double>(); // this is PR#35498
71 test2
<Iter
, long double>();
76 test
<forward_iterator
<int*> >();
77 test
<bidirectional_iterator
<int*> >();
78 test
<random_access_iterator
<int*> >();
82 static_assert(test_constexpr());