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 // <vector>
13 
14 // vector(vector&& c);
15 
16 #include <vector>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "MoveOnly.h"
21 #include "test_allocator.h"
22 #include "min_allocator.h"
23 #include "asan_testing.h"
24 
main()25 int main()
26 {
27     {
28         std::vector<MoveOnly, test_allocator<MoveOnly> > l(test_allocator<MoveOnly>(5));
29         std::vector<MoveOnly, test_allocator<MoveOnly> > lo(test_allocator<MoveOnly>(5));
30         assert(is_contiguous_container_asan_correct(l));
31         assert(is_contiguous_container_asan_correct(lo));
32         for (int i = 1; i <= 3; ++i)
33         {
34             l.push_back(i);
35             lo.push_back(i);
36         }
37         assert(is_contiguous_container_asan_correct(l));
38         assert(is_contiguous_container_asan_correct(lo));
39         std::vector<MoveOnly, test_allocator<MoveOnly> > l2 = std::move(l);
40         assert(l2 == lo);
41         assert(l.empty());
42         assert(l2.get_allocator() == lo.get_allocator());
43         assert(is_contiguous_container_asan_correct(l2));
44     }
45     {
46         std::vector<MoveOnly, other_allocator<MoveOnly> > l(other_allocator<MoveOnly>(5));
47         std::vector<MoveOnly, other_allocator<MoveOnly> > lo(other_allocator<MoveOnly>(5));
48         assert(is_contiguous_container_asan_correct(l));
49         assert(is_contiguous_container_asan_correct(lo));
50         for (int i = 1; i <= 3; ++i)
51         {
52             l.push_back(i);
53             lo.push_back(i);
54         }
55         assert(is_contiguous_container_asan_correct(l));
56         assert(is_contiguous_container_asan_correct(lo));
57         std::vector<MoveOnly, other_allocator<MoveOnly> > l2 = std::move(l);
58         assert(l2 == lo);
59         assert(l.empty());
60         assert(l2.get_allocator() == lo.get_allocator());
61         assert(is_contiguous_container_asan_correct(l2));
62     }
63     {
64         int a1[] = {1, 3, 7, 9, 10};
65         std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
66         assert(is_contiguous_container_asan_correct(c1));
67         std::vector<int>::const_iterator i = c1.begin();
68         std::vector<int> c2 = std::move(c1);
69         assert(is_contiguous_container_asan_correct(c2));
70         std::vector<int>::iterator j = c2.erase(i);
71         assert(*j == 3);
72         assert(is_contiguous_container_asan_correct(c2));
73     }
74     {
75         std::vector<MoveOnly, min_allocator<MoveOnly> > l(min_allocator<MoveOnly>{});
76         std::vector<MoveOnly, min_allocator<MoveOnly> > lo(min_allocator<MoveOnly>{});
77         assert(is_contiguous_container_asan_correct(l));
78         assert(is_contiguous_container_asan_correct(lo));
79         for (int i = 1; i <= 3; ++i)
80         {
81             l.push_back(i);
82             lo.push_back(i);
83         }
84         assert(is_contiguous_container_asan_correct(l));
85         assert(is_contiguous_container_asan_correct(lo));
86         std::vector<MoveOnly, min_allocator<MoveOnly> > l2 = std::move(l);
87         assert(l2 == lo);
88         assert(l.empty());
89         assert(l2.get_allocator() == lo.get_allocator());
90         assert(is_contiguous_container_asan_correct(l2));
91     }
92     {
93         int a1[] = {1, 3, 7, 9, 10};
94         std::vector<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
95         assert(is_contiguous_container_asan_correct(c1));
96         std::vector<int, min_allocator<int>>::const_iterator i = c1.begin();
97         std::vector<int, min_allocator<int>> c2 = std::move(c1);
98         assert(is_contiguous_container_asan_correct(c2));
99         std::vector<int, min_allocator<int>>::iterator j = c2.erase(i);
100         assert(*j == 3);
101         assert(is_contiguous_container_asan_correct(c2));
102     }
103     {
104       test_alloc_base::clear();
105       using Vect = std::vector<int, test_allocator<int> >;
106       Vect v(test_allocator<int>(42, 101));
107       assert(test_alloc_base::count == 1);
108       assert(test_alloc_base::copied == 1);
109       assert(test_alloc_base::moved == 0);
110       {
111         const test_allocator<int>& a = v.get_allocator();
112         assert(a.get_data() == 42);
113         assert(a.get_id() == 101);
114       }
115       assert(test_alloc_base::count == 1);
116       test_alloc_base::clear_ctor_counters();
117 
118       Vect v2 = std::move(v);
119       assert(test_alloc_base::count == 2);
120       assert(test_alloc_base::copied == 0);
121       assert(test_alloc_base::moved == 1);
122       {
123         const test_allocator<int>& a = v.get_allocator();
124         assert(a.get_id() == test_alloc_base::moved_value);
125         assert(a.get_data() == test_alloc_base::moved_value);
126       }
127       {
128         const test_allocator<int>& a = v2.get_allocator();
129         assert(a.get_id() == 101);
130         assert(a.get_data() == 42);
131       }
132     }
133 }
134