fill_n.pass.cpp 3.0 KB

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