xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/generate.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template<ForwardIterator Iter, Callable Generator>
125a83710eSEric Fiselier //   requires OutputIterator<Iter, Generator::result_type>
135a83710eSEric Fiselier //         && CopyConstructible<Generator>
144bfb9313SMarshall Clow //   constexpr void      // constexpr after c++17
155a83710eSEric Fiselier //   generate(Iter first, Iter last, Generator gen);
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <algorithm>
185a83710eSEric Fiselier #include <cassert>
195a83710eSEric Fiselier 
204bfb9313SMarshall Clow #include "test_macros.h"
215a83710eSEric Fiselier #include "test_iterators.h"
225a83710eSEric Fiselier 
235a83710eSEric Fiselier struct gen_test
245a83710eSEric Fiselier {
operator ()gen_test254bfb9313SMarshall Clow     TEST_CONSTEXPR int operator()() const {return 1;}
265a83710eSEric Fiselier };
275a83710eSEric Fiselier 
284bfb9313SMarshall Clow 
294bfb9313SMarshall Clow #if TEST_STD_VER > 17
test_constexpr()304bfb9313SMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
314bfb9313SMarshall Clow     int ia[] = {0, 1, 2, 3, 4};
324bfb9313SMarshall Clow 
334bfb9313SMarshall Clow     std::generate(std::begin(ia), std::end(ia), gen_test());
344bfb9313SMarshall Clow 
354bfb9313SMarshall Clow     return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })
364bfb9313SMarshall Clow         ;
374bfb9313SMarshall Clow     }
384bfb9313SMarshall Clow #endif
394bfb9313SMarshall Clow 
404bfb9313SMarshall Clow 
415a83710eSEric Fiselier template <class Iter>
425a83710eSEric Fiselier void
test()435a83710eSEric Fiselier test()
445a83710eSEric Fiselier {
455a83710eSEric Fiselier     const unsigned n = 4;
465a83710eSEric Fiselier     int ia[n] = {0};
475a83710eSEric Fiselier     std::generate(Iter(ia), Iter(ia+n), gen_test());
485a83710eSEric Fiselier     assert(ia[0] == 1);
495a83710eSEric Fiselier     assert(ia[1] == 1);
505a83710eSEric Fiselier     assert(ia[2] == 1);
515a83710eSEric Fiselier     assert(ia[3] == 1);
525a83710eSEric Fiselier }
535a83710eSEric Fiselier 
main(int,char **)54*2df59c50SJF Bastien int main(int, char**)
555a83710eSEric Fiselier {
565a83710eSEric Fiselier     test<forward_iterator<int*> >();
575a83710eSEric Fiselier     test<bidirectional_iterator<int*> >();
585a83710eSEric Fiselier     test<random_access_iterator<int*> >();
595a83710eSEric Fiselier     test<int*>();
604bfb9313SMarshall Clow 
614bfb9313SMarshall Clow #if TEST_STD_VER > 17
624bfb9313SMarshall Clow     static_assert(test_constexpr());
634bfb9313SMarshall Clow #endif
64*2df59c50SJF Bastien 
65*2df59c50SJF Bastien   return 0;
665a83710eSEric Fiselier }
67