2015-04-03 22:15:48 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2015, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "libtorrent/heterogeneous_queue.hpp"
|
|
|
|
|
|
|
|
struct A
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
explicit A(int a_) : a(a_) {}
|
|
|
|
virtual int type() = 0;
|
2016-07-10 13:34:45 +02:00
|
|
|
virtual ~A() = default;
|
2015-04-03 22:15:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct B : A
|
|
|
|
{
|
|
|
|
int b;
|
|
|
|
explicit B(int a_, int b_) : A(a_), b(b_) {}
|
2016-07-10 02:10:38 +02:00
|
|
|
int type() override { return 1; }
|
2015-04-03 22:15:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct C : A
|
|
|
|
{
|
|
|
|
char c[100];
|
|
|
|
explicit C(int a_, int c_) : A(a_)
|
|
|
|
{
|
|
|
|
memset(c, c_, sizeof(c));
|
|
|
|
}
|
2016-07-10 02:10:38 +02:00
|
|
|
int type() override { return 2; }
|
2015-04-03 22:15:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct D
|
|
|
|
{
|
|
|
|
static int instances;
|
|
|
|
D() { ++instances; }
|
|
|
|
D(D const& d) { ++instances; }
|
|
|
|
|
|
|
|
~D() { --instances; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct E
|
|
|
|
{
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit E(char const* msg) : string_member(msg) {}
|
2015-04-03 22:15:48 +02:00
|
|
|
std::string string_member;
|
|
|
|
};
|
|
|
|
|
|
|
|
int D::instances = 0;
|
|
|
|
|
|
|
|
struct F
|
|
|
|
{
|
2016-07-10 20:27:42 +02:00
|
|
|
explicit F(int f_)
|
2015-04-03 22:15:48 +02:00
|
|
|
: self(this)
|
|
|
|
, f(f_)
|
|
|
|
, constructed(true)
|
|
|
|
, destructed(false)
|
|
|
|
, gutted(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
F(F const& f_)
|
|
|
|
: self(this), f(f_.f)
|
|
|
|
, constructed(f_.constructed)
|
|
|
|
, destructed(f_.destructed)
|
|
|
|
, gutted(false)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(f_.constructed, true);
|
|
|
|
TEST_EQUAL(f_.destructed, false);
|
|
|
|
TEST_EQUAL(f_.gutted, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
F(F&& f_)
|
|
|
|
: self(this)
|
|
|
|
, f(f_.f)
|
|
|
|
, constructed(f_.constructed)
|
|
|
|
, destructed(f_.destructed)
|
2015-06-02 05:06:48 +02:00
|
|
|
, gutted(f_.gutted)
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL(f_.constructed, true);
|
|
|
|
TEST_EQUAL(f_.destructed, false);
|
|
|
|
TEST_EQUAL(f_.gutted, false);
|
|
|
|
f_.gutted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~F()
|
|
|
|
{
|
|
|
|
TEST_EQUAL(constructed, true);
|
|
|
|
TEST_EQUAL(destructed, false);
|
|
|
|
TEST_EQUAL(self, this);
|
|
|
|
destructed = true;
|
|
|
|
constructed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_invariant()
|
|
|
|
{
|
|
|
|
TEST_EQUAL(constructed, true);
|
|
|
|
TEST_EQUAL(destructed, false);
|
|
|
|
TEST_EQUAL(gutted, false);
|
|
|
|
TEST_EQUAL(self, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
F* self;
|
|
|
|
int f;
|
|
|
|
bool constructed;
|
|
|
|
bool destructed;
|
|
|
|
bool gutted;
|
|
|
|
private:
|
|
|
|
// non-copyable
|
|
|
|
F& operator=(F const& f);
|
|
|
|
};
|
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
struct G : A
|
|
|
|
{
|
|
|
|
G(int base, int v) : A(base), g(v) {}
|
|
|
|
int type() override { return 3; }
|
|
|
|
std::int64_t g;
|
|
|
|
};
|
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
// test emplace_back of heterogeneous types
|
2015-05-30 20:42:04 +02:00
|
|
|
// and retrieval of their pointers
|
2016-04-28 16:30:29 +02:00
|
|
|
TORRENT_TEST(emplace_back)
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
heterogeneous_queue<A> q;
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<B>(0, 1);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 1);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<B>(2, 3);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 2);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<B>(4, 5);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 3);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<C>(6, 7);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 4);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<C>(8, 9);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 5);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<C>(10, 11);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q.size(), 6);
|
|
|
|
|
|
|
|
std::vector<A*> ptrs;
|
|
|
|
q.get_pointers(ptrs);
|
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), q.size());
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(ptrs[0]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[2]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[3]->type(), 2);
|
|
|
|
TEST_EQUAL(ptrs[4]->type(), 2);
|
|
|
|
TEST_EQUAL(ptrs[5]->type(), 2);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[0])->a, 0);
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[0])->b, 1);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[1])->a, 2);
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[1])->b, 3);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[2])->a, 4);
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[2])->b, 5);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[3])->a, 6);
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[3])->c[0], 7);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[4])->a, 8);
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[4])->c[0], 9);
|
|
|
|
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[5])->a, 10);
|
|
|
|
TEST_EQUAL(static_cast<C*>(ptrs[5])->c[0], 11);
|
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2017-03-20 22:41:00 +01:00
|
|
|
TORRENT_TEST(emplace_back_over_aligned)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
heterogeneous_queue<A> q;
|
|
|
|
q.emplace_back<G>(1, 2);
|
|
|
|
q.emplace_back<G>(3, 4);
|
|
|
|
q.emplace_back<B>(5, 6);
|
|
|
|
|
|
|
|
std::vector<A*> ptrs;
|
|
|
|
q.get_pointers(ptrs);
|
|
|
|
|
|
|
|
TEST_EQUAL(int(ptrs.size()), q.size());
|
|
|
|
TEST_EQUAL(ptrs.size(), 3);
|
|
|
|
TEST_EQUAL(ptrs[0]->type(), 3);
|
|
|
|
TEST_EQUAL(static_cast<G*>(ptrs[0])->a, 1);
|
|
|
|
TEST_EQUAL(static_cast<G*>(ptrs[0])->g, 2);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 3);
|
|
|
|
TEST_EQUAL(static_cast<G*>(ptrs[1])->a, 3);
|
|
|
|
TEST_EQUAL(static_cast<G*>(ptrs[1])->g, 4);
|
|
|
|
TEST_EQUAL(ptrs[2]->type(), 1);
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[2])->a, 5);
|
|
|
|
TEST_EQUAL(static_cast<B*>(ptrs[2])->b, 6);
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
// test swap
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(swap)
|
2015-05-30 20:42:04 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
heterogeneous_queue<A> q1;
|
|
|
|
heterogeneous_queue<A> q2;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
q1.emplace_back<B>(0, 1);
|
|
|
|
q1.emplace_back<B>(2, 3);
|
|
|
|
q1.emplace_back<B>(4, 5);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q1.size(), 3);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
q2.emplace_back<C>(6, 7);
|
|
|
|
q2.emplace_back<C>(8, 9);
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(q2.size(), 2);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
std::vector<A*> ptrs;
|
|
|
|
q1.get_pointers(ptrs);
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), q1.size());
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(ptrs[0]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[2]->type(), 1);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
q2.get_pointers(ptrs);
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), q2.size());
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(ptrs[0]->type(), 2);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 2);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
q1.swap(q2);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
q1.get_pointers(ptrs);
|
|
|
|
TEST_EQUAL(q1.size(), 2);
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), q1.size());
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(ptrs[0]->type(), 2);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 2);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
q2.get_pointers(ptrs);
|
|
|
|
TEST_EQUAL(q2.size(), 3);
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), q2.size());
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(ptrs[0]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[1]->type(), 1);
|
|
|
|
TEST_EQUAL(ptrs[2]->type(), 1);
|
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
// test destruction
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(destruction)
|
2015-05-30 20:42:04 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
heterogeneous_queue<D> q;
|
|
|
|
TEST_EQUAL(D::instances, 0);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<D>();
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(D::instances, 1);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<D>();
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(D::instances, 2);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<D>();
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(D::instances, 3);
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<D>();
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(D::instances, 4);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
q.clear();
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
TEST_EQUAL(D::instances, 0);
|
|
|
|
}
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
// test copy/move
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(copy_move)
|
2015-05-30 20:42:04 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
heterogeneous_queue<F> q;
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
// make sure the queue has to grow at some point, to exercise its
|
|
|
|
// copy/move of elements
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<F>(i);
|
2015-05-30 20:42:04 +02:00
|
|
|
|
|
|
|
std::vector<F*> ptrs;
|
|
|
|
q.get_pointers(ptrs);
|
2015-04-03 22:15:48 +02:00
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
TEST_EQUAL(int(ptrs.size()), 1000);
|
2015-05-30 20:42:04 +02:00
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
for (int i = 0; i < int(ptrs.size()); ++i)
|
2015-04-03 22:15:48 +02:00
|
|
|
{
|
2015-05-30 20:42:04 +02:00
|
|
|
ptrs[i]->check_invariant();
|
|
|
|
TEST_EQUAL(ptrs[i]->f, i);
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 20:42:04 +02:00
|
|
|
// destroy all objects, asserting that their invariant still holds
|
|
|
|
q.clear();
|
2015-04-03 22:15:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:16 +02:00
|
|
|
TORRENT_TEST(nontrivial)
|
2015-05-30 20:42:04 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
heterogeneous_queue<E> q;
|
|
|
|
for (int i = 0; i < 10000; ++i)
|
|
|
|
{
|
2016-04-28 16:30:29 +02:00
|
|
|
q.emplace_back<E>("testing to allocate non-trivial objects");
|
2015-05-30 20:42:04 +02:00
|
|
|
}
|
|
|
|
}
|