78 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <algorithm>10 11// template<ForwardIterator Iter, Callable Generator>12// requires OutputIterator<Iter, Generator::result_type>13// && CopyConstructible<Generator>14// constexpr void // constexpr after c++1715// generate(Iter first, Iter last, Generator gen);16 17#include <algorithm>18#include <cassert>19#include <deque>20 21#include "test_macros.h"22#include "test_iterators.h"23 24struct gen_test25{26 TEST_CONSTEXPR int operator()() const {return 1;}27};28 29 30#if TEST_STD_VER > 1731TEST_CONSTEXPR bool test_constexpr() {32 int ia[] = {0, 1, 2, 3, 4};33 34 std::generate(std::begin(ia), std::end(ia), gen_test());35 36 return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })37 ;38 }39#endif40 41 42template <class Iter>43void44test()45{46 const unsigned n = 4;47 int ia[n] = {0};48 std::generate(Iter(ia), Iter(ia+n), gen_test());49 assert(ia[0] == 1);50 assert(ia[1] == 1);51 assert(ia[2] == 1);52 assert(ia[3] == 1);53}54 55void deque_test() {56 int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};57 for (const int size : sizes) {58 std::deque<int> d(size);59 std::generate(d.begin(), d.end(), gen_test());60 assert(std::all_of(d.begin(), d.end(), [](int x) { return x == 1; }));61 }62}63 64int main(int, char**)65{66 test<forward_iterator<int*> >();67 test<bidirectional_iterator<int*> >();68 test<random_access_iterator<int*> >();69 test<int*>();70 deque_test();71 72#if TEST_STD_VER > 1773 static_assert(test_constexpr());74#endif75 76 return 0;77}78