brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 525737c Raw
98 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<class Iter, IntegralLike Size, Callable Generator>12//   requires OutputIterator<Iter, Generator::result_type>13//         && CopyConstructible<Generator>14//   constexpr void      // constexpr after c++1715//   generate_n(Iter first, Size n, Generator gen);16 17#include <algorithm>18#include <cassert>19#include <deque>20 21#include "test_iterators.h"22#include "test_macros.h"23#include "user_defined_integral.h"24 25TEST_MSVC_DIAGNOSTIC_IGNORED(4244) // conversion from 'const double' to 'int', possible loss of data26 27struct gen_test28{29    TEST_CONSTEXPR int operator()() const {return 2;}30};31 32 33#if TEST_STD_VER > 1734TEST_CONSTEXPR bool test_constexpr() {35    const std::size_t N = 5;36    int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N37 38    auto it = std::generate_n(std::begin(ib), N, gen_test());39 40    return it == (std::begin(ib) + N)41        && std::all_of(std::begin(ib), it, [](int x) { return x == 2; })42        && *it == 0 // don't overwrite the last value in the output array43        ;44    }45#endif46 47 48template <class Iter, class Size>49void50test2()51{52    const unsigned n = 4;53    int ia[n] = {0};54    assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));55    assert(ia[0] == 2);56    assert(ia[1] == 2);57    assert(ia[2] == 2);58    assert(ia[3] == 2);59}60 61template <class Iter>62void63test()64{65    test2<Iter, int>();66    test2<Iter, unsigned int>();67    test2<Iter, long>();68    test2<Iter, unsigned long>();69    test2<Iter, UserDefinedIntegral<unsigned> >();70    test2<Iter, float>();71    test2<Iter, double>();  // this is PR#3549872    test2<Iter, long double>();73}74 75void deque_test() {76  int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};77  for (const int size : sizes) {78    std::deque<int> d(size);79    std::generate_n(d.begin(), size, gen_test());80    assert(std::all_of(d.begin(), d.end(), [](int x) { return x == 2; }));81  }82}83 84int main(int, char**)85{86    test<forward_iterator<int*> >();87    test<bidirectional_iterator<int*> >();88    test<random_access_iterator<int*> >();89    test<int*>();90    deque_test();91 92#if TEST_STD_VER > 1793    static_assert(test_constexpr());94#endif95 96  return 0;97}98