fill.pass.cpp 1.7 KB

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