generate_n.pass.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template<class Iter, IntegralLike Size, Callable Generator>
  11. // requires OutputIterator<Iter, Generator::result_type>
  12. // && CopyConstructible<Generator>
  13. // constexpr void // constexpr after c++17
  14. // generate_n(Iter first, Size n, Generator gen);
  15. #ifdef _MSC_VER
  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_macros.h"
  21. #include "test_iterators.h"
  22. #include "user_defined_integral.hpp"
  23. struct gen_test
  24. {
  25. TEST_CONSTEXPR int operator()() const {return 2;}
  26. };
  27. #if TEST_STD_VER > 17
  28. TEST_CONSTEXPR bool test_constexpr() {
  29. const size_t N = 5;
  30. int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
  31. auto it = std::generate_n(std::begin(ib), N, gen_test());
  32. return it == (std::begin(ib) + N)
  33. && std::all_of(std::begin(ib), it, [](int x) { return x == 2; })
  34. && *it == 0 // don't overwrite the last value in the output array
  35. ;
  36. }
  37. #endif
  38. template <class Iter, class Size>
  39. void
  40. test2()
  41. {
  42. const unsigned n = 4;
  43. int ia[n] = {0};
  44. assert(std::generate_n(Iter(ia), Size(n), gen_test()) == Iter(ia+n));
  45. assert(ia[0] == 2);
  46. assert(ia[1] == 2);
  47. assert(ia[2] == 2);
  48. assert(ia[3] == 2);
  49. }
  50. template <class Iter>
  51. void
  52. test()
  53. {
  54. test2<Iter, int>();
  55. test2<Iter, unsigned int>();
  56. test2<Iter, long>();
  57. test2<Iter, unsigned long>();
  58. test2<Iter, UserDefinedIntegral<unsigned> >();
  59. test2<Iter, float>();
  60. test2<Iter, double>(); // this is PR#35498
  61. test2<Iter, long double>();
  62. }
  63. int main()
  64. {
  65. test<forward_iterator<int*> >();
  66. test<bidirectional_iterator<int*> >();
  67. test<random_access_iterator<int*> >();
  68. test<int*>();
  69. #if TEST_STD_VER > 17
  70. static_assert(test_constexpr());
  71. #endif
  72. }