Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.generate / generate_n.pass.cpp
blob13fd1cbf1f33ceabdc31ca180882efad41512257
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
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);
17 #include <algorithm>
18 #include <cassert>
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
26 struct gen_test
28 TEST_CONSTEXPR int operator()() const {return 2;}
32 #if TEST_STD_VER > 17
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
44 #endif
47 template <class Iter, class Size>
48 void
49 test2()
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);
60 template <class Iter>
61 void
62 test()
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>();
74 int main(int, char**)
76 test<forward_iterator<int*> >();
77 test<bidirectional_iterator<int*> >();
78 test<random_access_iterator<int*> >();
79 test<int*>();
81 #if TEST_STD_VER > 17
82 static_assert(test_constexpr());
83 #endif
85 return 0;