test.pass.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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<InputIterator Iter, Callable<auto, Iter::reference> Function>
  10. // requires CopyConstructible<Function>
  11. // constexpr Function // constexpr after C++17
  12. // for_each(Iter first, Iter last, Function f);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. #if TEST_STD_VER > 17
  18. TEST_CONSTEXPR bool test_constexpr() {
  19. int ia[] = {1, 3, 6, 7};
  20. int expected[] = {3, 5, 8, 9};
  21. std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; });
  22. return std::equal(std::begin(ia), std::end(ia), std::begin(expected))
  23. ;
  24. }
  25. #endif
  26. struct for_each_test
  27. {
  28. for_each_test(int c) : count(c) {}
  29. int count;
  30. void operator()(int& i) {++i; ++count;}
  31. };
  32. int main(int, char**)
  33. {
  34. int ia[] = {0, 1, 2, 3, 4, 5};
  35. const unsigned s = sizeof(ia)/sizeof(ia[0]);
  36. for_each_test f = std::for_each(input_iterator<int*>(ia),
  37. input_iterator<int*>(ia+s),
  38. for_each_test(0));
  39. assert(f.count == s);
  40. for (unsigned i = 0; i < s; ++i)
  41. assert(ia[i] == static_cast<int>(i+1));
  42. #if TEST_STD_VER > 17
  43. static_assert(test_constexpr());
  44. #endif
  45. return 0;
  46. }