generate_n.pass.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // <algorithm>
  9. // template<class Iter, IntegralLike Size, Callable Generator>
  10. // requires OutputIterator<Iter, Generator::result_type>
  11. // && CopyConstructible<Generator>
  12. // constexpr void // constexpr after c++17
  13. // generate_n(Iter first, Size n, Generator gen);
  14. #include "test_macros.h"
  15. #ifdef TEST_COMPILER_C1XX
  16. #pragma warning(disable: 4244) // conversion from 'const double' to 'int', possible loss of data
  17. #endif
  18. #include <algorithm>
  19. #include <cassert>
  20. #include "test_iterators.h"
  21. #include "user_defined_integral.h"
  22. struct gen_test
  23. {
  24. TEST_CONSTEXPR int operator()() const {return 2;}
  25. };
  26. #if TEST_STD_VER > 17
  27. TEST_CONSTEXPR bool test_constexpr() {
  28. const size_t N = 5;
  29. int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
  30. auto it = std::generate_n(std::begin(ib), N, gen_test());
  31. return it == (std::begin(ib) + N)
  32. && std::all_of(std::begin(ib), it, [](int x) { return x == 2; })
  33. && *it == 0 // don't overwrite the last value in the output array
  34. ;
  35. }
  36. #endif
  37. template <class Iter, class Size>
  38. void
  39. test2()
  40. {
  41. const unsigned n = 4;
  42. int ia[n] = {0};
  43. assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
  44. assert(ia[0] == 2);
  45. assert(ia[1] == 2);
  46. assert(ia[2] == 2);
  47. assert(ia[3] == 2);
  48. }
  49. template <class Iter>
  50. void
  51. test()
  52. {
  53. test2<Iter, int>();
  54. test2<Iter, unsigned int>();
  55. test2<Iter, long>();
  56. test2<Iter, unsigned long>();
  57. test2<Iter, UserDefinedIntegral<unsigned> >();
  58. test2<Iter, float>();
  59. test2<Iter, double>(); // this is PR#35498
  60. test2<Iter, long double>();
  61. }
  62. int main(int, char**)
  63. {
  64. test<forward_iterator<int*> >();
  65. test<bidirectional_iterator<int*> >();
  66. test<random_access_iterator<int*> >();
  67. test<int*>();
  68. #if TEST_STD_VER > 17
  69. static_assert(test_constexpr());
  70. #endif
  71. return 0;
  72. }