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