fill_n.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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<class Iter, IntegralLike Size, class T>
  10. // requires OutputIterator<Iter, const T&>
  11. // constexpr OutputIterator // constexpr after C++17
  12. // fill_n(Iter first, Size n, const T& value);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. #include "user_defined_integral.h"
  18. #if TEST_STD_VER > 17
  19. TEST_CONSTEXPR bool test_constexpr() {
  20. const size_t N = 5;
  21. int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger than N
  22. auto it = std::fill_n(std::begin(ib), N, 5);
  23. return it == (std::begin(ib) + N)
  24. && std::all_of(std::begin(ib), it, [](int a) {return a == 5; })
  25. && *it == 0 // don't overwrite the last value in the output array
  26. ;
  27. }
  28. #endif
  29. typedef UserDefinedIntegral<unsigned> UDI;
  30. template <class Iter>
  31. void
  32. test_char()
  33. {
  34. const unsigned n = 4;
  35. char ca[n] = {0};
  36. assert(std::fill_n(Iter(ca), UDI(n), char(1)) == std::next(Iter(ca), n));
  37. assert(ca[0] == 1);
  38. assert(ca[1] == 1);
  39. assert(ca[2] == 1);
  40. assert(ca[3] == 1);
  41. }
  42. template <class Iter>
  43. void
  44. test_int()
  45. {
  46. const unsigned n = 4;
  47. int ia[n] = {0};
  48. assert(std::fill_n(Iter(ia), UDI(n), 1) == std::next(Iter(ia), n));
  49. assert(ia[0] == 1);
  50. assert(ia[1] == 1);
  51. assert(ia[2] == 1);
  52. assert(ia[3] == 1);
  53. }
  54. void
  55. test_int_array()
  56. {
  57. const unsigned n = 4;
  58. int ia[n] = {0};
  59. assert(std::fill_n(ia, UDI(n), static_cast<char>(1)) == std::next(ia, n));
  60. assert(ia[0] == 1);
  61. assert(ia[1] == 1);
  62. assert(ia[2] == 1);
  63. assert(ia[3] == 1);
  64. }
  65. struct source {
  66. source() : i(0) { }
  67. operator int() const { return i++; }
  68. mutable int i;
  69. };
  70. void
  71. test_int_array_struct_source()
  72. {
  73. const unsigned n = 4;
  74. int ia[n] = {0};
  75. assert(std::fill_n(ia, UDI(n), source()) == std::next(ia, n));
  76. assert(ia[0] == 0);
  77. assert(ia[1] == 1);
  78. assert(ia[2] == 2);
  79. assert(ia[3] == 3);
  80. }
  81. struct test1 {
  82. test1() : c(0) { }
  83. test1(char xc) : c(xc + 1) { }
  84. char c;
  85. };
  86. void
  87. test_struct_array()
  88. {
  89. const unsigned n = 4;
  90. test1 test1a[n] = {0};
  91. assert(std::fill_n(test1a, UDI(n), static_cast<char>(10)) == std::next(test1a, n));
  92. assert(test1a[0].c == 11);
  93. assert(test1a[1].c == 11);
  94. assert(test1a[2].c == 11);
  95. assert(test1a[3].c == 11);
  96. }
  97. class A
  98. {
  99. char a_;
  100. public:
  101. A() {}
  102. explicit A(char a) : a_(a) {}
  103. operator unsigned char() const {return 'b';}
  104. friend bool operator==(const A& x, const A& y)
  105. {return x.a_ == y.a_;}
  106. };
  107. void
  108. test5()
  109. {
  110. A a[3];
  111. assert(std::fill_n(&a[0], UDI(3), A('a')) == a+3);
  112. assert(a[0] == A('a'));
  113. assert(a[1] == A('a'));
  114. assert(a[2] == A('a'));
  115. }
  116. struct Storage
  117. {
  118. union
  119. {
  120. unsigned char a;
  121. unsigned char b;
  122. };
  123. };
  124. void test6()
  125. {
  126. Storage foo[5];
  127. std::fill_n(&foo[0], UDI(5), Storage());
  128. }
  129. int main(int, char**)
  130. {
  131. test_char<forward_iterator<char*> >();
  132. test_char<bidirectional_iterator<char*> >();
  133. test_char<random_access_iterator<char*> >();
  134. test_char<char*>();
  135. test_int<forward_iterator<int*> >();
  136. test_int<bidirectional_iterator<int*> >();
  137. test_int<random_access_iterator<int*> >();
  138. test_int<int*>();
  139. test_int_array();
  140. test_int_array_struct_source();
  141. test_struct_array();
  142. test5();
  143. test6();
  144. #if TEST_STD_VER > 17
  145. static_assert(test_constexpr());
  146. #endif
  147. return 0;
  148. }