rv_value.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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// <iterator>
11
12// insert_iterator
13
14// requires CopyConstructible<Cont::value_type>
15//   insert_iterator<Cont>&
16//   operator=(const Cont::value_type& value);
17
18#include <iterator>
19
20#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
21#include <vector>
22#include <memory>
23#include <cassert>
24
25template <class C>
26void
27test(C c1, typename C::difference_type j,
28     typename C::value_type x1, typename C::value_type x2,
29     typename C::value_type x3, const C& c2)
30{
31    std::insert_iterator<C> q(c1, c1.begin() + j);
32    q = std::move(x1);
33    q = std::move(x2);
34    q = std::move(x3);
35    assert(c1 == c2);
36}
37
38template <class C>
39void
40insert3at(C& c, typename C::iterator i,
41     typename C::value_type x1, typename C::value_type x2,
42     typename C::value_type x3)
43{
44    i = c.insert(i, std::move(x1));
45    i = c.insert(++i, std::move(x2));
46    c.insert(++i, std::move(x3));
47}
48
49struct do_nothing
50{
51    void operator()(void*) const {}
52};
53
54#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
55
56int main()
57{
58#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
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#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
95}
96