fill_n.pass.cpp 3.1 KB

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