1 /*
2
3 Copyright (c) 2015, Arvid Norberg
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30
31 */
32
33 #include "test.hpp"
34 #include "libtorrent/heterogeneous_queue.hpp"
35
36 namespace {
37
38 struct A
39 {
40 int a;
A__anon282223800111::A41 explicit A(int a_) : a(a_) {}
42 A(A&&) noexcept = default;
43 virtual int type() = 0;
44 virtual ~A() = default;
45 };
46
47 struct B : A
48 {
49 int b;
B__anon282223800111::B50 explicit B(int a_, int b_) : A(a_), b(b_) {}
51 B(B&&) noexcept = default;
type__anon282223800111::B52 int type() override { return 1; }
53 };
54
55 struct C : A
56 {
57 char c[100];
C__anon282223800111::C58 explicit C(int a_, int c_) : A(a_)
59 {
60 memset(c, c_, sizeof(c));
61 }
62 C(C&&) noexcept = default;
type__anon282223800111::C63 int type() override { return 2; }
64 };
65
66 struct D
67 {
68 static int instances;
D__anon282223800111::D69 D() { ++instances; }
D__anon282223800111::D70 D(D const&) { ++instances; }
D__anon282223800111::D71 D(D&&) noexcept { ++instances; }
72
~D__anon282223800111::D73 ~D() { --instances; }
74 };
75
76 struct E
77 {
E__anon282223800111::E78 explicit E(char const* msg) : string_member(msg) {}
79 E(E&&) noexcept = default;
80 std::string string_member;
81 };
82
83 int D::instances = 0;
84
85 struct F
86 {
F__anon282223800111::F87 explicit F(int f_)
88 : self(this)
89 , f(f_)
90 , constructed(true)
91 , destructed(false)
92 , gutted(false)
93 {}
94
F__anon282223800111::F95 F(F const& f_)
96 : self(this), f(f_.f)
97 , constructed(f_.constructed)
98 , destructed(f_.destructed)
99 , gutted(false)
100 {
101 TEST_EQUAL(f_.constructed, true);
102 TEST_EQUAL(f_.destructed, false);
103 TEST_EQUAL(f_.gutted, false);
104 }
105
F__anon282223800111::F106 F(F&& f_) noexcept
107 : self(this)
108 , f(f_.f)
109 , constructed(f_.constructed)
110 , destructed(f_.destructed)
111 , gutted(f_.gutted)
112 {
113 TEST_EQUAL(f_.constructed, true);
114 TEST_EQUAL(f_.destructed, false);
115 TEST_EQUAL(f_.gutted, false);
116 f_.gutted = true;
117 }
118
~F__anon282223800111::F119 ~F()
120 {
121 TEST_EQUAL(constructed, true);
122 TEST_EQUAL(destructed, false);
123 TEST_EQUAL(self, this);
124 destructed = true;
125 constructed = false;
126 }
127
check_invariant__anon282223800111::F128 void check_invariant()
129 {
130 TEST_EQUAL(constructed, true);
131 TEST_EQUAL(destructed, false);
132 TEST_EQUAL(gutted, false);
133 TEST_EQUAL(self, this);
134 }
135
136 F* self;
137 int f;
138 bool constructed;
139 bool destructed;
140 bool gutted;
141 private:
142 // non-copyable
143 F& operator=(F const& f);
144 };
145
146 struct G : A
147 {
G__anon282223800111::G148 G(int base, int v) : A(base), g(v) {}
149 G(G&&) noexcept = default;
type__anon282223800111::G150 int type() override { return 3; }
151 std::int64_t g;
152 };
153
154 } // anonymous namespace
155
156 // test emplace_back of heterogeneous types
157 // and retrieval of their pointers
TORRENT_TEST(emplace_back)158 TORRENT_TEST(emplace_back)
159 {
160 using namespace lt;
161
162 heterogeneous_queue<A> q;
163 q.emplace_back<B>(0, 1);
164 TEST_EQUAL(q.size(), 1);
165 q.emplace_back<B>(2, 3);
166 TEST_EQUAL(q.size(), 2);
167 q.emplace_back<B>(4, 5);
168 TEST_EQUAL(q.size(), 3);
169 q.emplace_back<C>(6, 7);
170 TEST_EQUAL(q.size(), 4);
171 q.emplace_back<C>(8, 9);
172 TEST_EQUAL(q.size(), 5);
173 q.emplace_back<C>(10, 11);
174 TEST_EQUAL(q.size(), 6);
175
176 std::vector<A*> ptrs;
177 q.get_pointers(ptrs);
178
179 TEST_EQUAL(int(ptrs.size()), q.size());
180 TEST_EQUAL(ptrs[0]->type(), 1);
181 TEST_EQUAL(ptrs[1]->type(), 1);
182 TEST_EQUAL(ptrs[2]->type(), 1);
183 TEST_EQUAL(ptrs[3]->type(), 2);
184 TEST_EQUAL(ptrs[4]->type(), 2);
185 TEST_EQUAL(ptrs[5]->type(), 2);
186
187 TEST_EQUAL(static_cast<B*>(ptrs[0])->a, 0);
188 TEST_EQUAL(static_cast<B*>(ptrs[0])->b, 1);
189
190 TEST_EQUAL(static_cast<B*>(ptrs[1])->a, 2);
191 TEST_EQUAL(static_cast<B*>(ptrs[1])->b, 3);
192
193 TEST_EQUAL(static_cast<B*>(ptrs[2])->a, 4);
194 TEST_EQUAL(static_cast<B*>(ptrs[2])->b, 5);
195
196 TEST_EQUAL(static_cast<C*>(ptrs[3])->a, 6);
197 TEST_EQUAL(static_cast<C*>(ptrs[3])->c[0], 7);
198
199 TEST_EQUAL(static_cast<C*>(ptrs[4])->a, 8);
200 TEST_EQUAL(static_cast<C*>(ptrs[4])->c[0], 9);
201
202 TEST_EQUAL(static_cast<C*>(ptrs[5])->a, 10);
203 TEST_EQUAL(static_cast<C*>(ptrs[5])->c[0], 11);
204 }
205
TORRENT_TEST(emplace_back_over_aligned)206 TORRENT_TEST(emplace_back_over_aligned)
207 {
208 using namespace lt;
209
210 heterogeneous_queue<A> q;
211 q.emplace_back<G>(1, 2);
212 q.emplace_back<G>(3, 4);
213 q.emplace_back<B>(5, 6);
214
215 std::vector<A*> ptrs;
216 q.get_pointers(ptrs);
217
218 TEST_EQUAL(int(ptrs.size()), q.size());
219 TEST_EQUAL(ptrs.size(), 3);
220 TEST_EQUAL(ptrs[0]->type(), 3);
221 TEST_EQUAL(static_cast<G*>(ptrs[0])->a, 1);
222 TEST_EQUAL(static_cast<G*>(ptrs[0])->g, 2);
223 TEST_EQUAL(ptrs[1]->type(), 3);
224 TEST_EQUAL(static_cast<G*>(ptrs[1])->a, 3);
225 TEST_EQUAL(static_cast<G*>(ptrs[1])->g, 4);
226 TEST_EQUAL(ptrs[2]->type(), 1);
227 TEST_EQUAL(static_cast<B*>(ptrs[2])->a, 5);
228 TEST_EQUAL(static_cast<B*>(ptrs[2])->b, 6);
229 }
230
231 // test swap
TORRENT_TEST(swap)232 TORRENT_TEST(swap)
233 {
234 using namespace lt;
235
236 heterogeneous_queue<A> q1;
237 heterogeneous_queue<A> q2;
238
239 q1.emplace_back<B>(0, 1);
240 q1.emplace_back<B>(2, 3);
241 q1.emplace_back<B>(4, 5);
242 TEST_EQUAL(q1.size(), 3);
243
244 q2.emplace_back<C>(6, 7);
245 q2.emplace_back<C>(8, 9);
246 TEST_EQUAL(q2.size(), 2);
247
248 std::vector<A*> ptrs;
249 q1.get_pointers(ptrs);
250 TEST_EQUAL(int(ptrs.size()), q1.size());
251
252 TEST_EQUAL(ptrs[0]->type(), 1);
253 TEST_EQUAL(ptrs[1]->type(), 1);
254 TEST_EQUAL(ptrs[2]->type(), 1);
255
256 q2.get_pointers(ptrs);
257 TEST_EQUAL(int(ptrs.size()), q2.size());
258
259 TEST_EQUAL(ptrs[0]->type(), 2);
260 TEST_EQUAL(ptrs[1]->type(), 2);
261
262 q1.swap(q2);
263
264 q1.get_pointers(ptrs);
265 TEST_EQUAL(q1.size(), 2);
266 TEST_EQUAL(int(ptrs.size()), q1.size());
267
268 TEST_EQUAL(ptrs[0]->type(), 2);
269 TEST_EQUAL(ptrs[1]->type(), 2);
270
271 q2.get_pointers(ptrs);
272 TEST_EQUAL(q2.size(), 3);
273 TEST_EQUAL(int(ptrs.size()), q2.size());
274
275 TEST_EQUAL(ptrs[0]->type(), 1);
276 TEST_EQUAL(ptrs[1]->type(), 1);
277 TEST_EQUAL(ptrs[2]->type(), 1);
278 }
279
280 // test destruction
TORRENT_TEST(destruction)281 TORRENT_TEST(destruction)
282 {
283 using namespace lt;
284
285 heterogeneous_queue<D> q;
286 TEST_EQUAL(D::instances, 0);
287
288 q.emplace_back<D>();
289 TEST_EQUAL(D::instances, 1);
290 q.emplace_back<D>();
291 TEST_EQUAL(D::instances, 2);
292 q.emplace_back<D>();
293 TEST_EQUAL(D::instances, 3);
294 q.emplace_back<D>();
295 TEST_EQUAL(D::instances, 4);
296
297 q.clear();
298
299 TEST_EQUAL(D::instances, 0);
300 }
301
302 // test copy/move
TORRENT_TEST(copy_move)303 TORRENT_TEST(copy_move)
304 {
305 using namespace lt;
306
307 heterogeneous_queue<F> q;
308
309 // make sure the queue has to grow at some point, to exercise its
310 // copy/move of elements
311 for (int i = 0; i < 1000; ++i)
312 q.emplace_back<F>(i);
313
314 std::vector<F*> ptrs;
315 q.get_pointers(ptrs);
316
317 TEST_EQUAL(int(ptrs.size()), 1000);
318
319 for (std::size_t i = 0; i < ptrs.size(); ++i)
320 {
321 ptrs[i]->check_invariant();
322 TEST_EQUAL(ptrs[i]->f, int(i));
323 }
324
325 // destroy all objects, asserting that their invariant still holds
326 q.clear();
327 }
328
TORRENT_TEST(nontrivial)329 TORRENT_TEST(nontrivial)
330 {
331 using namespace lt;
332
333 heterogeneous_queue<E> q;
334 for (int i = 0; i < 10000; ++i)
335 {
336 q.emplace_back<E>("testing to allocate non-trivial objects");
337 }
338 }
339