Home | History | Annotate | Download | only in insert.iter.op=
      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 // UNSUPPORTED: c++98, c++03
     11 
     12 // <iterator>
     13 
     14 // insert_iterator
     15 
     16 // requires CopyConstructible<Cont::value_type>
     17 //   insert_iterator<Cont>&
     18 //   operator=(const Cont::value_type& value);
     19 
     20 #include <iterator>
     21 
     22 #include <utility>
     23 #include <vector>
     24 #include <memory>
     25 #include <cassert>
     26 
     27 template <class C>
     28 void
     29 test(C c1, typename C::difference_type j,
     30      typename C::value_type x1, typename C::value_type x2,
     31      typename C::value_type x3, const C& c2)
     32 {
     33     std::insert_iterator<C> q(c1, c1.begin() + j);
     34     q = std::move(x1);
     35     q = std::move(x2);
     36     q = std::move(x3);
     37     assert(c1 == c2);
     38 }
     39 
     40 template <class C>
     41 void
     42 insert3at(C& c, typename C::iterator i,
     43      typename C::value_type x1, typename C::value_type x2,
     44      typename C::value_type x3)
     45 {
     46     i = c.insert(i, std::move(x1));
     47     i = c.insert(++i, std::move(x2));
     48     c.insert(++i, std::move(x3));
     49 }
     50 
     51 struct do_nothing
     52 {
     53     void operator()(void*) const {}
     54 };
     55 
     56 int main()
     57 {
     58     {
     59     typedef std::unique_ptr<int, do_nothing> Ptr;
     60     typedef std::vector<Ptr> C;
     61     C c1;
     62     int x[6] = {0};
     63     for (int i = 0; i < 3; ++i)
     64         c1.push_back(Ptr(x+i));
     65     C c2;
     66     for (int i = 0; i < 3; ++i)
     67         c2.push_back(Ptr(x+i));
     68     insert3at(c2, c2.begin(), Ptr(x+3), Ptr(x+4), Ptr(x+5));
     69     test(std::move(c1), 0, Ptr(x+3), Ptr(x+4), Ptr(x+5), c2);
     70     c1.clear();
     71     for (int i = 0; i < 3; ++i)
     72         c1.push_back(Ptr(x+i));
     73     c2.clear();
     74     for (int i = 0; i < 3; ++i)
     75         c2.push_back(Ptr(x+i));
     76     insert3at(c2, c2.begin()+1, Ptr(x+3), Ptr(x+4), Ptr(x+5));
     77     test(std::move(c1), 1, Ptr(x+3), Ptr(x+4), Ptr(x+5), c2);
     78     c1.clear();
     79     for (int i = 0; i < 3; ++i)
     80         c1.push_back(Ptr(x+i));
     81     c2.clear();
     82     for (int i = 0; i < 3; ++i)
     83         c2.push_back(Ptr(x+i));
     84     insert3at(c2, c2.begin()+2, Ptr(x+3), Ptr(x+4), Ptr(x+5));
     85     test(std::move(c1), 2, Ptr(x+3), Ptr(x+4), Ptr(x+5), c2);
     86     c1.clear();
     87     for (int i = 0; i < 3; ++i)
     88         c1.push_back(Ptr(x+i));
     89     c2.clear();
     90     for (int i = 0; i < 3; ++i)
     91         c2.push_back(Ptr(x+i));
     92     insert3at(c2, c2.begin()+3, Ptr(x+3), Ptr(x+4), Ptr(x+5));
     93     test(std::move(c1), 3, Ptr(x+3), Ptr(x+4), Ptr(x+5), c2);
     94     }
     95 }
     96