Home | History | Annotate | Download | only in alg.fill
      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 #include "user_defined_integral.hpp"
     22 
     23 typedef UserDefinedIntegral<unsigned> UDI;
     24 
     25 template <class Iter>
     26 void
     27 test_char()
     28 {
     29     const unsigned n = 4;
     30     char ca[n] = {0};
     31     assert(std::fill_n(Iter(ca), UDI(n), char(1)) == std::next(Iter(ca), n));
     32     assert(ca[0] == 1);
     33     assert(ca[1] == 1);
     34     assert(ca[2] == 1);
     35     assert(ca[3] == 1);
     36 }
     37 
     38 template <class Iter>
     39 void
     40 test_int()
     41 {
     42     const unsigned n = 4;
     43     int ia[n] = {0};
     44     assert(std::fill_n(Iter(ia), UDI(n), 1) == std::next(Iter(ia), n));
     45     assert(ia[0] == 1);
     46     assert(ia[1] == 1);
     47     assert(ia[2] == 1);
     48     assert(ia[3] == 1);
     49 }
     50 
     51 void
     52 test_int_array()
     53 {
     54     const unsigned n = 4;
     55     int ia[n] = {0};
     56     assert(std::fill_n(ia, UDI(n), static_cast<char>(1)) == std::next(ia, n));
     57     assert(ia[0] == 1);
     58     assert(ia[1] == 1);
     59     assert(ia[2] == 1);
     60     assert(ia[3] == 1);
     61 }
     62 
     63 struct source {
     64     source() : i(0) { }
     65 
     66     operator int() const { return i++; }
     67     mutable int i;
     68 };
     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 
     82 struct test1 {
     83     test1() : c(0) { }
     84     test1(char xc) : c(xc + 1) { }
     85     char c;
     86 };
     87 
     88 void
     89 test_struct_array()
     90 {
     91     const unsigned n = 4;
     92     test1 test1a[n] = {0};
     93     assert(std::fill_n(test1a, UDI(n), static_cast<char>(10)) == std::next(test1a, n));
     94     assert(test1a[0].c == 11);
     95     assert(test1a[1].c == 11);
     96     assert(test1a[2].c == 11);
     97     assert(test1a[3].c == 11);
     98 }
     99 
    100 class A
    101 {
    102     char a_;
    103 public:
    104     A() {}
    105     explicit A(char a) : a_(a) {}
    106     operator unsigned char() const {return 'b';}
    107 
    108     friend bool operator==(const A& x, const A& y)
    109         {return x.a_ == y.a_;}
    110 };
    111 
    112 void
    113 test5()
    114 {
    115     A a[3];
    116     assert(std::fill_n(&a[0], UDI(3), A('a')) == a+3);
    117     assert(a[0] == A('a'));
    118     assert(a[1] == A('a'));
    119     assert(a[2] == A('a'));
    120 }
    121 
    122 struct Storage
    123 {
    124   union
    125   {
    126     unsigned char a;
    127     unsigned char b;
    128   };
    129 };
    130 
    131 void test6()
    132 {
    133   Storage foo[5];
    134   std::fill_n(&foo[0], UDI(5), Storage());
    135 }
    136 
    137 
    138 int main()
    139 {
    140     test_char<forward_iterator<char*> >();
    141     test_char<bidirectional_iterator<char*> >();
    142     test_char<random_access_iterator<char*> >();
    143     test_char<char*>();
    144 
    145     test_int<forward_iterator<int*> >();
    146     test_int<bidirectional_iterator<int*> >();
    147     test_int<random_access_iterator<int*> >();
    148     test_int<int*>();
    149 
    150     test_int_array();
    151     test_int_array_struct_source();
    152     test_struct_array();
    153 
    154     test5();
    155     test6();
    156 }
    157