generate.pass.cpp 1.5 KB

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