1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03 10 11 // <set> 12 13 // class set 14 15 // set(set&& s, const allocator_type& a); 16 17 #include <set> 18 #include <cassert> 19 #include <iterator> 20 21 #include "test_macros.h" 22 #include "MoveOnly.h" 23 #include "../../../test_compare.h" 24 #include "test_allocator.h" 25 #include "Counter.h" 26 27 int main(int, char**) 28 { 29 { 30 typedef MoveOnly V; 31 typedef test_less<MoveOnly> C; 32 typedef test_allocator<V> A; 33 typedef std::set<MoveOnly, C, A> M; 34 typedef std::move_iterator<V*> I; 35 V a1[] = 36 { 37 V(1), 38 V(1), 39 V(1), 40 V(2), 41 V(2), 42 V(2), 43 V(3), 44 V(3), 45 V(3) 46 }; 47 M m1(I(a1), I(a1+sizeof(a1)/sizeof(a1[0])), C(5), A(7)); 48 V a2[] = 49 { 50 V(1), 51 V(1), 52 V(1), 53 V(2), 54 V(2), 55 V(2), 56 V(3), 57 V(3), 58 V(3) 59 }; 60 M m2(I(a2), I(a2+sizeof(a2)/sizeof(a2[0])), C(5), A(7)); 61 M m3(std::move(m1), A(7)); 62 assert(m3 == m2); 63 assert(m3.get_allocator() == A(7)); 64 assert(m3.key_comp() == C(5)); 65 LIBCPP_ASSERT(m1.empty()); 66 } 67 { 68 typedef MoveOnly V; 69 typedef test_less<MoveOnly> C; 70 typedef test_allocator<V> A; 71 typedef std::set<MoveOnly, C, A> M; 72 typedef std::move_iterator<V*> I; 73 V a1[] = 74 { 75 V(1), 76 V(1), 77 V(1), 78 V(2), 79 V(2), 80 V(2), 81 V(3), 82 V(3), 83 V(3) 84 }; 85 M m1(I(a1), I(a1+sizeof(a1)/sizeof(a1[0])), C(5), A(7)); 86 V a2[] = 87 { 88 V(1), 89 V(1), 90 V(1), 91 V(2), 92 V(2), 93 V(2), 94 V(3), 95 V(3), 96 V(3) 97 }; 98 M m2(I(a2), I(a2+sizeof(a2)/sizeof(a2[0])), C(5), A(7)); 99 M m3(std::move(m1), A(5)); 100 assert(m3 == m2); 101 assert(m3.get_allocator() == A(5)); 102 assert(m3.key_comp() == C(5)); 103 LIBCPP_ASSERT(m1.empty()); 104 } 105 { 106 typedef MoveOnly V; 107 typedef test_less<MoveOnly> C; 108 typedef other_allocator<V> A; 109 typedef std::set<MoveOnly, C, A> M; 110 typedef std::move_iterator<V*> I; 111 V a1[] = 112 { 113 V(1), 114 V(1), 115 V(1), 116 V(2), 117 V(2), 118 V(2), 119 V(3), 120 V(3), 121 V(3) 122 }; 123 M m1(I(a1), I(a1+sizeof(a1)/sizeof(a1[0])), C(5), A(7)); 124 V a2[] = 125 { 126 V(1), 127 V(1), 128 V(1), 129 V(2), 130 V(2), 131 V(2), 132 V(3), 133 V(3), 134 V(3) 135 }; 136 M m2(I(a2), I(a2+sizeof(a2)/sizeof(a2[0])), C(5), A(7)); 137 M m3(std::move(m1), A(5)); 138 assert(m3 == m2); 139 assert(m3.get_allocator() == A(5)); 140 assert(m3.key_comp() == C(5)); 141 LIBCPP_ASSERT(m1.empty()); 142 } 143 { 144 typedef Counter<int> V; 145 typedef std::less<V> C; 146 typedef test_allocator<V> A; 147 typedef std::set<V, C, A> M; 148 typedef V* I; 149 Counter_base::gConstructed = 0; 150 { 151 V a1[] = 152 { 153 V(1), 154 V(1), 155 V(1), 156 V(2), 157 V(2), 158 V(2), 159 V(3), 160 V(3), 161 V(3) 162 }; 163 const size_t num = sizeof(a1)/sizeof(a1[0]); 164 assert(Counter_base::gConstructed == num); 165 166 M m1(I(a1), I(a1+num), C(), A()); 167 assert(Counter_base::gConstructed == 3+num); 168 169 M m2(m1); 170 assert(m2 == m1); 171 assert(Counter_base::gConstructed == 6+num); 172 173 M m3(std::move(m1), A()); 174 assert(m3 == m2); 175 LIBCPP_ASSERT(m1.empty()); 176 assert(Counter_base::gConstructed >= (int)(6+num)); 177 assert(Counter_base::gConstructed <= (int)(m1.size()+6+num)); 178 179 { 180 M m4(std::move(m2), A(5)); 181 assert(Counter_base::gConstructed >= (int)(6+num)); 182 assert(Counter_base::gConstructed <= (int)(m1.size()+m2.size()+6+num)); 183 assert(m4 == m3); 184 LIBCPP_ASSERT(m2.empty()); 185 } 186 assert(Counter_base::gConstructed >= (int)(3+num)); 187 assert(Counter_base::gConstructed <= (int)(m1.size()+m2.size()+3+num)); 188 } 189 assert(Counter_base::gConstructed == 0); 190 } 191 192 193 return 0; 194 } 195