fill_n.pass.cpp 3.6 KB

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