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