fill.pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, class T>
  10. // requires OutputIterator<Iter, const T&>
  11. // constexpr void // constexpr after C++17
  12. // fill(Iter first, Iter last, const T& value);
  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[] = {0, 1, 2, 3, 4};
  20. std::fill(std::begin(ia), std::end(ia), 5);
  21. return std::all_of(std::begin(ia), std::end(ia), [](int a) {return a == 5; })
  22. ;
  23. }
  24. #endif
  25. template <class Iter>
  26. void
  27. test_char()
  28. {
  29. const unsigned n = 4;
  30. char ca[n] = {0};
  31. std::fill(Iter(ca), Iter(ca+n), char(1));
  32. assert(ca[0] == 1);
  33. assert(ca[1] == 1);
  34. assert(ca[2] == 1);
  35. assert(ca[3] == 1);
  36. }
  37. template <class Iter>
  38. void
  39. test_int()
  40. {
  41. const unsigned n = 4;
  42. int ia[n] = {0};
  43. std::fill(Iter(ia), Iter(ia+n), 1);
  44. assert(ia[0] == 1);
  45. assert(ia[1] == 1);
  46. assert(ia[2] == 1);
  47. assert(ia[3] == 1);
  48. }
  49. int main(int, char**)
  50. {
  51. test_char<forward_iterator<char*> >();
  52. test_char<bidirectional_iterator<char*> >();
  53. test_char<random_access_iterator<char*> >();
  54. test_char<char*>();
  55. test_int<forward_iterator<int*> >();
  56. test_int<bidirectional_iterator<int*> >();
  57. test_int<random_access_iterator<int*> >();
  58. test_int<int*>();
  59. #if TEST_STD_VER > 17
  60. static_assert(test_constexpr());
  61. #endif
  62. return 0;
  63. }