generate.pass.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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<ForwardIterator Iter, Callable Generator>
  11. // requires OutputIterator<Iter, Generator::result_type>
  12. // && CopyConstructible<Generator>
  13. // constexpr void // constexpr after c++17
  14. // generate(Iter first, Iter last, Generator gen);
  15. #include <algorithm>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. struct gen_test
  20. {
  21. TEST_CONSTEXPR int operator()() const {return 1;}
  22. };
  23. #if TEST_STD_VER > 17
  24. TEST_CONSTEXPR bool test_constexpr() {
  25. int ia[] = {0, 1, 2, 3, 4};
  26. std::generate(std::begin(ia), std::end(ia), gen_test());
  27. return std::all_of(std::begin(ia), std::end(ia), [](int x) { return x == 1; })
  28. ;
  29. }
  30. #endif
  31. template <class Iter>
  32. void
  33. test()
  34. {
  35. const unsigned n = 4;
  36. int ia[n] = {0};
  37. std::generate(Iter(ia), Iter(ia+n), gen_test());
  38. assert(ia[0] == 1);
  39. assert(ia[1] == 1);
  40. assert(ia[2] == 1);
  41. assert(ia[3] == 1);
  42. }
  43. int main()
  44. {
  45. test<forward_iterator<int*> >();
  46. test<bidirectional_iterator<int*> >();
  47. test<random_access_iterator<int*> >();
  48. test<int*>();
  49. #if TEST_STD_VER > 17
  50. static_assert(test_constexpr());
  51. #endif
  52. }