test.pass.cpp 1.5 KB

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